@omercnet/paseo-fresh-worktrees 1.1.1-next.72.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +51 -0
- package/index.client.ts +162 -0
- package/index.server.ts +36 -0
- package/package.json +55 -0
- package/paseo-plugin.json +6 -0
- package/server/fresh-worktrees.ts +290 -0
- package/server/workspace-freshness.ts +76 -0
- package/shared/workspace-freshness.ts +15 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
* Fast-forward the clean, checked-out local base branch before creating a branch-off worktree.
|
|
8
|
+
* Warn and skip the local branch update when the source checkout is dirty instead of blocking workspace creation.
|
|
9
|
+
* Show a workspace-header indicator when a worktree is behind the source branch's remote-tracking ref.
|
|
10
|
+
|
|
11
|
+
## [1.1.1](https://github.com/omercnet/paseo-plugins/compare/fresh-worktrees-v1.1.0...fresh-worktrees-v1.1.1) (2026-09-13)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
* **fresh-worktrees:** prevent freshness RPC storms ([#28](https://github.com/omercnet/paseo-plugins/issues/28)) ([6244ebe](https://github.com/omercnet/paseo-plugins/commit/6244ebec4dee63694fa16a376877453510d9364f))
|
|
17
|
+
|
|
18
|
+
## [1.1.0](https://github.com/omercnet/paseo-plugins/compare/fresh-worktrees-v1.0.0...fresh-worktrees-v1.1.0) (2026-09-10)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
* **fresh-worktrees:** keep local base branch current ([#9](https://github.com/omercnet/paseo-plugins/issues/9)) ([2ebd062](https://github.com/omercnet/paseo-plugins/commit/2ebd0620d4356103c2a162b503103090b7289add))
|
|
24
|
+
|
|
25
|
+
## 1.0.0 (2026-09-10)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Bug Fixes
|
|
29
|
+
|
|
30
|
+
* upgrade plugins to Paseo 0.8.0 ([c6b4e40](https://github.com/omercnet/paseo-plugins/commit/c6b4e4081fc9527de0ca6183a3903ff5ec4f93c4))
|
|
31
|
+
* upgrade plugins to Paseo 0.8.0 ([d7ad454](https://github.com/omercnet/paseo-plugins/commit/d7ad4540d2e1875bdf4a4d21f76ded25cac2f3dc))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Omer Cohen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Fresh Worktrees
|
|
2
|
+
|
|
3
|
+
A Paseo plugin that fast-forwards the source checkout's clean local base branch before creating a
|
|
4
|
+
branch-off workspace.
|
|
5
|
+
|
|
6
|
+
The plugin runs the refresh inside the Paseo daemon and adds a workspace-header indicator when an
|
|
7
|
+
active worktree falls behind the source checkout's remote base.
|
|
8
|
+
|
|
9
|
+
## Behavior
|
|
10
|
+
|
|
11
|
+
Before Paseo creates a branch-off worktree, the plugin:
|
|
12
|
+
|
|
13
|
+
1. Resolves the source repository from the request path or Paseo project.
|
|
14
|
+
2. Fetches and prunes the relevant Git remote without interactive prompts.
|
|
15
|
+
3. For an implicit or local base such as `main`, verifies that branch is checked out and the source
|
|
16
|
+
checkout is clean, then fast-forwards it to its remote-tracking branch.
|
|
17
|
+
4. Leaves the workspace request unchanged, so Paseo forks from the local branch.
|
|
18
|
+
5. Rechecks active worktrees every five minutes and shows `Behind · N` in the workspace header when
|
|
19
|
+
their `HEAD` is behind the source branch's remote-tracking ref. Select the indicator to recheck it.
|
|
20
|
+
|
|
21
|
+
Explicit checkout and change-request workspaces are unchanged. Explicit remote bases are fetched
|
|
22
|
+
but do not mutate a local branch. Repositories without remotes are unchanged. A dirty source
|
|
23
|
+
checkout emits a warning and skips the local branch update, allowing workspace creation to continue
|
|
24
|
+
from the existing local base. Failed fetches and non-fast-forward updates still stop creation.
|
|
25
|
+
Concurrent requests for the same target share one refresh, and updates are serialized per repository.
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
Paseo plugins are trusted, unsandboxed code. Review the source before installing it on the daemon
|
|
30
|
+
host.
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
paseo plugin install npm:@omercnet/paseo-fresh-worktrees
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Or install from Git:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
paseo plugin add omercnet/paseo-plugins:fresh-worktrees
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The plugin requires Paseo `>=0.8.0`.
|
|
43
|
+
|
|
44
|
+
## Develop
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm ci
|
|
48
|
+
npm run typecheck
|
|
49
|
+
npm test
|
|
50
|
+
paseo plugin install "$PWD"
|
|
51
|
+
```
|
package/index.client.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PluginButton,
|
|
3
|
+
PluginButtonRegistration,
|
|
4
|
+
PluginClientContext,
|
|
5
|
+
} from "@getpaseo/plugin/client";
|
|
6
|
+
import { workspaceFreshness } from "./shared/workspace-freshness";
|
|
7
|
+
|
|
8
|
+
const REFRESH_INTERVAL_MS = 5 * 60_000;
|
|
9
|
+
|
|
10
|
+
interface WorkspaceEntry {
|
|
11
|
+
id: string;
|
|
12
|
+
projectId: string;
|
|
13
|
+
workspaceDirectory: string | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default function contribute(client: PluginClientContext) {
|
|
17
|
+
const buttons = new Map<string, PluginButtonRegistration>();
|
|
18
|
+
const checks = new Map<string, Promise<void>>();
|
|
19
|
+
const workspaceLocations = new Map<
|
|
20
|
+
string,
|
|
21
|
+
{ projectId: string; workspaceDirectory: string }
|
|
22
|
+
>();
|
|
23
|
+
const projectRoots = new Map<string, string>();
|
|
24
|
+
let stopped = false;
|
|
25
|
+
|
|
26
|
+
function removeIndicator(workspaceId: string) {
|
|
27
|
+
buttons.get(workspaceId)?.remove();
|
|
28
|
+
buttons.delete(workspaceId);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function checkFreshness(workspaceId: string) {
|
|
32
|
+
const location = workspaceLocations.get(workspaceId);
|
|
33
|
+
if (!location) return;
|
|
34
|
+
let projectRootPath = projectRoots.get(location.projectId);
|
|
35
|
+
if (!projectRootPath) {
|
|
36
|
+
const { projects } = await client.paseo.projects.list();
|
|
37
|
+
for (const project of projects) projectRoots.set(project.projectId, project.projectRootPath);
|
|
38
|
+
projectRootPath = projectRoots.get(location.projectId);
|
|
39
|
+
}
|
|
40
|
+
if (!projectRootPath) return;
|
|
41
|
+
|
|
42
|
+
const freshness = await client.rpc(workspaceFreshness, {
|
|
43
|
+
projectRootPath,
|
|
44
|
+
workspaceDirectory: location.workspaceDirectory,
|
|
45
|
+
});
|
|
46
|
+
if (stopped || workspaceLocations.get(workspaceId) !== location) return;
|
|
47
|
+
if (freshness.kind !== "behind") {
|
|
48
|
+
removeIndicator(workspaceId);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const commits = freshness.behindBy === 1 ? "commit" : "commits";
|
|
53
|
+
const button: PluginButton = {
|
|
54
|
+
title: `Worktree is ${freshness.behindBy} ${commits} behind ${freshness.remoteRef}. Select to recheck.`,
|
|
55
|
+
icon: "GitPullRequest",
|
|
56
|
+
label: `Behind · ${freshness.behindBy}`,
|
|
57
|
+
behavior: {
|
|
58
|
+
kind: "action",
|
|
59
|
+
onPress() {
|
|
60
|
+
return scheduleFreshnessCheck(workspaceId);
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
const existing = buttons.get(workspaceId);
|
|
65
|
+
if (existing) existing.update(button);
|
|
66
|
+
else {
|
|
67
|
+
buttons.set(
|
|
68
|
+
workspaceId,
|
|
69
|
+
client.addHeaderButton({ id: "remote-behind", workspaceId, button }),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function scheduleFreshnessCheck(workspaceId: string): Promise<void> {
|
|
75
|
+
const existing = checks.get(workspaceId);
|
|
76
|
+
if (existing) return existing;
|
|
77
|
+
|
|
78
|
+
const scheduled = checkFreshness(workspaceId);
|
|
79
|
+
checks.set(workspaceId, scheduled);
|
|
80
|
+
void scheduled.then(
|
|
81
|
+
() => {
|
|
82
|
+
if (checks.get(workspaceId) === scheduled) checks.delete(workspaceId);
|
|
83
|
+
},
|
|
84
|
+
() => {
|
|
85
|
+
if (checks.get(workspaceId) === scheduled) checks.delete(workspaceId);
|
|
86
|
+
},
|
|
87
|
+
);
|
|
88
|
+
return scheduled;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function trackWorkspace(workspace: WorkspaceEntry) {
|
|
92
|
+
if (!workspace.workspaceDirectory) return;
|
|
93
|
+
const previous = workspaceLocations.get(workspace.id);
|
|
94
|
+
if (
|
|
95
|
+
previous?.projectId === workspace.projectId &&
|
|
96
|
+
previous.workspaceDirectory === workspace.workspaceDirectory
|
|
97
|
+
) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const location = {
|
|
102
|
+
projectId: workspace.projectId,
|
|
103
|
+
workspaceDirectory: workspace.workspaceDirectory,
|
|
104
|
+
};
|
|
105
|
+
workspaceLocations.set(workspace.id, location);
|
|
106
|
+
|
|
107
|
+
const existing = checks.get(workspace.id);
|
|
108
|
+
if (existing) {
|
|
109
|
+
const recheck = () => {
|
|
110
|
+
if (stopped || workspaceLocations.get(workspace.id) !== location) return;
|
|
111
|
+
void scheduleFreshnessCheck(workspace.id).catch((error) => {
|
|
112
|
+
console.warn(`[fresh-worktrees] Could not inspect workspace ${workspace.id}`, error);
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
void existing.then(recheck, recheck);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
void scheduleFreshnessCheck(workspace.id).catch((error) => {
|
|
120
|
+
console.warn(`[fresh-worktrees] Could not inspect workspace ${workspace.id}`, error);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const unsubscribe = client.paseo.workspaces.subscribe((update) => {
|
|
125
|
+
if (update.kind === "upsert") trackWorkspace(update.workspace);
|
|
126
|
+
else {
|
|
127
|
+
workspaceLocations.delete(update.id);
|
|
128
|
+
removeIndicator(update.id);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
void Promise.all([
|
|
133
|
+
client.paseo.projects.list(),
|
|
134
|
+
client.paseo.workspaces.list({ subscribe: { subscriptionId: "fresh-worktrees-status" } }),
|
|
135
|
+
])
|
|
136
|
+
.then(([{ projects }, { entries }]) => {
|
|
137
|
+
if (stopped) return;
|
|
138
|
+
for (const project of projects) projectRoots.set(project.projectId, project.projectRootPath);
|
|
139
|
+
for (const workspace of entries) trackWorkspace(workspace);
|
|
140
|
+
})
|
|
141
|
+
.catch((error) => {
|
|
142
|
+
console.warn("[fresh-worktrees] Could not list workspaces", error);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const interval = setInterval(() => {
|
|
146
|
+
for (const workspaceId of workspaceLocations.keys()) {
|
|
147
|
+
void scheduleFreshnessCheck(workspaceId).catch((error) => {
|
|
148
|
+
console.warn(`[fresh-worktrees] Could not inspect workspace ${workspaceId}`, error);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}, REFRESH_INTERVAL_MS);
|
|
152
|
+
|
|
153
|
+
return () => {
|
|
154
|
+
stopped = true;
|
|
155
|
+
clearInterval(interval);
|
|
156
|
+
unsubscribe();
|
|
157
|
+
for (const button of buttons.values()) button.remove();
|
|
158
|
+
buttons.clear();
|
|
159
|
+
workspaceLocations.clear();
|
|
160
|
+
projectRoots.clear();
|
|
161
|
+
};
|
|
162
|
+
}
|
package/index.server.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PluginServerContext } from "@getpaseo/plugin/server";
|
|
2
|
+
import {
|
|
3
|
+
createRepositoryRefreshCoordinator,
|
|
4
|
+
refreshWorkspaceRequest,
|
|
5
|
+
} from "./server/fresh-worktrees";
|
|
6
|
+
import { inspectWorkspaceFreshness } from "./server/workspace-freshness";
|
|
7
|
+
import { workspaceFreshness } from "./shared/workspace-freshness";
|
|
8
|
+
|
|
9
|
+
export default function contribute(server: PluginServerContext) {
|
|
10
|
+
const refreshRepository = createRepositoryRefreshCoordinator();
|
|
11
|
+
|
|
12
|
+
server.handle(workspaceFreshness, ({ projectRootPath, workspaceDirectory }) =>
|
|
13
|
+
inspectWorkspaceFreshness(projectRootPath, workspaceDirectory, {
|
|
14
|
+
signal: new AbortController().signal,
|
|
15
|
+
refreshRepository,
|
|
16
|
+
}),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
server.before("workspace.create", async ({ request }, { paseo, signal }) => {
|
|
20
|
+
return refreshWorkspaceRequest(request, {
|
|
21
|
+
signal,
|
|
22
|
+
refreshRepository,
|
|
23
|
+
async listProjects() {
|
|
24
|
+
return (await paseo.projects.list()).projects;
|
|
25
|
+
},
|
|
26
|
+
log(message) {
|
|
27
|
+
console.log(`[fresh-worktrees] ${message}`);
|
|
28
|
+
},
|
|
29
|
+
warn(message) {
|
|
30
|
+
console.warn(`[fresh-worktrees] ${message}`);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return () => {};
|
|
36
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@omercnet/paseo-fresh-worktrees",
|
|
3
|
+
"description": "Fast-forwards clean local base branches before Paseo creates branch-off worktrees",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Omer Cohen",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/omercnet/paseo-plugins.git",
|
|
9
|
+
"directory": "fresh-worktrees"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/omercnet/paseo-plugins/tree/main/fresh-worktrees#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/omercnet/paseo-plugins/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"paseo",
|
|
20
|
+
"paseo-plugin",
|
|
21
|
+
"git",
|
|
22
|
+
"worktrees",
|
|
23
|
+
"coding-agents"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"CHANGELOG.md",
|
|
27
|
+
"LICENSE",
|
|
28
|
+
"README.md",
|
|
29
|
+
"index.client.ts",
|
|
30
|
+
"index.server.ts",
|
|
31
|
+
"server",
|
|
32
|
+
"shared",
|
|
33
|
+
"paseo-plugin.json"
|
|
34
|
+
],
|
|
35
|
+
"version": "1.1.1-next.72.1",
|
|
36
|
+
"type": "module",
|
|
37
|
+
"packageManager": "npm@11.19.1",
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=24"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"typecheck": "tsc --noEmit"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@getpaseo/client": "0.8.0",
|
|
47
|
+
"@getpaseo/plugin": "0.8.0",
|
|
48
|
+
"@types/node": "^24.5.2",
|
|
49
|
+
"@types/react": "~19.2.0",
|
|
50
|
+
"react": "19.1.0",
|
|
51
|
+
"typescript": "^7.0.0",
|
|
52
|
+
"vitest": "^5.0.0",
|
|
53
|
+
"zod": "^4.4.3"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import type { PluginBeforeRequests } from "@getpaseo/plugin/server";
|
|
4
|
+
|
|
5
|
+
export type WorkspaceCreateRequest = PluginBeforeRequests["workspace.create"];
|
|
6
|
+
type WorktreeSource = Extract<WorkspaceCreateRequest["source"], { kind: "worktree" }>;
|
|
7
|
+
|
|
8
|
+
export interface ProjectDescriptor {
|
|
9
|
+
projectId: string;
|
|
10
|
+
projectRootPath: string;
|
|
11
|
+
projectKind: "git" | "non_git" | "directory";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type GitRunner = (
|
|
15
|
+
cwd: string,
|
|
16
|
+
args: readonly string[],
|
|
17
|
+
signal: AbortSignal,
|
|
18
|
+
) => Promise<string>;
|
|
19
|
+
|
|
20
|
+
export type RepositoryRefreshResult =
|
|
21
|
+
| { kind: "updated"; freshBase: string }
|
|
22
|
+
| { kind: "unchanged" }
|
|
23
|
+
| { kind: "dirty" };
|
|
24
|
+
|
|
25
|
+
export type RepositoryRefresh = (
|
|
26
|
+
cwd: string,
|
|
27
|
+
remote: string,
|
|
28
|
+
localBranch: string | null,
|
|
29
|
+
upstream: string | null,
|
|
30
|
+
signal: AbortSignal,
|
|
31
|
+
) => Promise<RepositoryRefreshResult>;
|
|
32
|
+
|
|
33
|
+
export interface RefreshDependencies {
|
|
34
|
+
signal: AbortSignal;
|
|
35
|
+
listProjects(): Promise<readonly ProjectDescriptor[]>;
|
|
36
|
+
refreshRepository: RepositoryRefresh;
|
|
37
|
+
runGit?: GitRunner;
|
|
38
|
+
log?(message: string): void;
|
|
39
|
+
warn?(message: string): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const execFileAsync = promisify(execFile);
|
|
43
|
+
const GIT_TIMEOUT_MS = 10_000;
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
export async function executeGit(
|
|
47
|
+
cwd: string,
|
|
48
|
+
args: readonly string[],
|
|
49
|
+
signal: AbortSignal,
|
|
50
|
+
): Promise<string> {
|
|
51
|
+
const operationSignal = AbortSignal.any([signal, AbortSignal.timeout(GIT_TIMEOUT_MS)]);
|
|
52
|
+
try {
|
|
53
|
+
const { stdout } = await execFileAsync("git", ["-C", cwd, ...args], {
|
|
54
|
+
encoding: "utf8",
|
|
55
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
56
|
+
maxBuffer: 64 * 1024,
|
|
57
|
+
signal: operationSignal,
|
|
58
|
+
});
|
|
59
|
+
return stdout.trim();
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const stderr =
|
|
62
|
+
typeof error === "object" && error !== null && "stderr" in error
|
|
63
|
+
? String(error.stderr).trim()
|
|
64
|
+
: "";
|
|
65
|
+
const detail = stderr || (error instanceof Error ? error.message : String(error));
|
|
66
|
+
throw new Error(`git ${args.join(" ")} failed in ${cwd}: ${detail}`, { cause: error });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createRepositoryRefreshCoordinator(
|
|
71
|
+
runGit: GitRunner = executeGit,
|
|
72
|
+
): RepositoryRefresh {
|
|
73
|
+
const inFlight = new Map<string, Promise<RepositoryRefreshResult>>();
|
|
74
|
+
const repositoryTails = new Map<string, Promise<void>>();
|
|
75
|
+
|
|
76
|
+
return async (cwd, remote, localBranch, upstream, signal) => {
|
|
77
|
+
const targetKey = `${cwd}\0${remote}\0${localBranch ?? ""}\0${upstream ?? ""}`;
|
|
78
|
+
const existingRefresh = inFlight.get(targetKey);
|
|
79
|
+
if (existingRefresh) return existingRefresh;
|
|
80
|
+
|
|
81
|
+
const previousRefresh = repositoryTails.get(cwd) ?? Promise.resolve();
|
|
82
|
+
const refresh = previousRefresh.catch(() => {}).then(async (): Promise<RepositoryRefreshResult> => {
|
|
83
|
+
await runGit(cwd, ["fetch", "--prune", "--quiet", remote], signal);
|
|
84
|
+
if (!localBranch) return { kind: "unchanged" };
|
|
85
|
+
|
|
86
|
+
const matchingRemoteBranch = `${remote}/${localBranch}`;
|
|
87
|
+
const freshBase =
|
|
88
|
+
upstream && (await remoteRefExists(runGit, cwd, upstream, signal))
|
|
89
|
+
? upstream
|
|
90
|
+
: (await remoteRefExists(runGit, cwd, matchingRemoteBranch, signal))
|
|
91
|
+
? matchingRemoteBranch
|
|
92
|
+
: null;
|
|
93
|
+
if (!freshBase) return { kind: "unchanged" };
|
|
94
|
+
|
|
95
|
+
const currentBranch = await optionalGit(
|
|
96
|
+
runGit,
|
|
97
|
+
cwd,
|
|
98
|
+
["symbolic-ref", "--quiet", "--short", "HEAD"],
|
|
99
|
+
signal,
|
|
100
|
+
);
|
|
101
|
+
if (currentBranch !== localBranch) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Cannot refresh local branch ${localBranch} in ${cwd}: the source checkout is on ${currentBranch ?? "a detached HEAD"}`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const status = await runGit(
|
|
108
|
+
cwd,
|
|
109
|
+
["status", "--porcelain", "--untracked-files=normal"],
|
|
110
|
+
signal,
|
|
111
|
+
);
|
|
112
|
+
if (status) return { kind: "dirty" };
|
|
113
|
+
|
|
114
|
+
await runGit(cwd, ["merge", "--ff-only", "--quiet", freshBase], signal);
|
|
115
|
+
return { kind: "updated", freshBase };
|
|
116
|
+
});
|
|
117
|
+
const tail = refresh.then(
|
|
118
|
+
() => {},
|
|
119
|
+
() => {},
|
|
120
|
+
);
|
|
121
|
+
inFlight.set(targetKey, refresh);
|
|
122
|
+
repositoryTails.set(cwd, tail);
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
return await refresh;
|
|
126
|
+
} finally {
|
|
127
|
+
if (inFlight.get(targetKey) === refresh) inFlight.delete(targetKey);
|
|
128
|
+
if (repositoryTails.get(cwd) === tail) repositoryTails.delete(cwd);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function remoteForRef(ref: string | null, remotes: readonly string[]): string | null {
|
|
134
|
+
if (!ref) return null;
|
|
135
|
+
const shortRef = ref.startsWith("refs/remotes/") ? ref.slice("refs/remotes/".length) : ref;
|
|
136
|
+
return (
|
|
137
|
+
[...remotes]
|
|
138
|
+
.sort((left, right) => right.length - left.length)
|
|
139
|
+
.find((remote) => shortRef.startsWith(`${remote}/`)) ?? null
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function optionalGit(
|
|
144
|
+
runGit: GitRunner,
|
|
145
|
+
cwd: string,
|
|
146
|
+
args: readonly string[],
|
|
147
|
+
signal: AbortSignal,
|
|
148
|
+
): Promise<string | null> {
|
|
149
|
+
try {
|
|
150
|
+
return await runGit(cwd, args, signal);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (signal.aborted) throw error;
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function localBranchDetails(
|
|
158
|
+
runGit: GitRunner,
|
|
159
|
+
cwd: string,
|
|
160
|
+
ref: string,
|
|
161
|
+
signal: AbortSignal,
|
|
162
|
+
): Promise<{ branch: string; upstream: string | null } | null> {
|
|
163
|
+
const branch = ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
|
|
164
|
+
const exists = await optionalGit(
|
|
165
|
+
runGit,
|
|
166
|
+
cwd,
|
|
167
|
+
["show-ref", "--verify", "--quiet", `refs/heads/${branch}`],
|
|
168
|
+
signal,
|
|
169
|
+
);
|
|
170
|
+
if (exists === null) return null;
|
|
171
|
+
|
|
172
|
+
const upstream = await optionalGit(
|
|
173
|
+
runGit,
|
|
174
|
+
cwd,
|
|
175
|
+
["for-each-ref", "--format=%(upstream:short)", `refs/heads/${branch}`],
|
|
176
|
+
signal,
|
|
177
|
+
);
|
|
178
|
+
return { branch, upstream: upstream || null };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function remoteRefExists(
|
|
182
|
+
runGit: GitRunner,
|
|
183
|
+
cwd: string,
|
|
184
|
+
ref: string,
|
|
185
|
+
signal: AbortSignal,
|
|
186
|
+
): Promise<boolean> {
|
|
187
|
+
const fullRef = ref.startsWith("refs/remotes/") ? ref : `refs/remotes/${ref}`;
|
|
188
|
+
return (
|
|
189
|
+
(await optionalGit(runGit, cwd, ["show-ref", "--verify", "--quiet", fullRef], signal)) !==
|
|
190
|
+
null
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function resolveProjectRoot(
|
|
195
|
+
source: WorktreeSource,
|
|
196
|
+
listProjects: RefreshDependencies["listProjects"],
|
|
197
|
+
): Promise<string | null> {
|
|
198
|
+
if (source.cwd) return source.cwd;
|
|
199
|
+
if (!source.projectId) return null;
|
|
200
|
+
|
|
201
|
+
const project = (await listProjects()).find(({ projectId }) => projectId === source.projectId);
|
|
202
|
+
return project?.projectKind === "git" ? project.projectRootPath : null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export async function refreshWorkspaceRequest(
|
|
206
|
+
request: WorkspaceCreateRequest,
|
|
207
|
+
dependencies: RefreshDependencies,
|
|
208
|
+
): Promise<WorkspaceCreateRequest> {
|
|
209
|
+
const source = request.source;
|
|
210
|
+
if (
|
|
211
|
+
source.kind !== "worktree" ||
|
|
212
|
+
source.action === "checkout" ||
|
|
213
|
+
source.checkoutSource !== undefined ||
|
|
214
|
+
source.githubPrNumber !== undefined
|
|
215
|
+
) {
|
|
216
|
+
return request;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const cwd = await resolveProjectRoot(source, dependencies.listProjects);
|
|
220
|
+
if (!cwd) {
|
|
221
|
+
dependencies.log?.("Skipped refresh because the source project path could not be resolved");
|
|
222
|
+
return request;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const runGit = dependencies.runGit ?? executeGit;
|
|
226
|
+
const remotes = (await runGit(cwd, ["remote"], dependencies.signal))
|
|
227
|
+
.split(/\r?\n/)
|
|
228
|
+
.map((remote) => remote.trim())
|
|
229
|
+
.filter(Boolean);
|
|
230
|
+
if (remotes.length === 0) return request;
|
|
231
|
+
|
|
232
|
+
const requestedRef = source.refName?.trim() || null;
|
|
233
|
+
let localBranch = requestedRef
|
|
234
|
+
? await localBranchDetails(runGit, cwd, requestedRef, dependencies.signal)
|
|
235
|
+
: null;
|
|
236
|
+
|
|
237
|
+
if (!requestedRef) {
|
|
238
|
+
const currentBranch = await optionalGit(
|
|
239
|
+
runGit,
|
|
240
|
+
cwd,
|
|
241
|
+
["symbolic-ref", "--quiet", "--short", "HEAD"],
|
|
242
|
+
dependencies.signal,
|
|
243
|
+
);
|
|
244
|
+
if (currentBranch) {
|
|
245
|
+
localBranch = await localBranchDetails(
|
|
246
|
+
runGit,
|
|
247
|
+
cwd,
|
|
248
|
+
currentBranch,
|
|
249
|
+
dependencies.signal,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const remote =
|
|
255
|
+
remoteForRef(requestedRef, remotes) ??
|
|
256
|
+
remoteForRef(localBranch?.upstream ?? null, remotes) ??
|
|
257
|
+
(remotes.includes("origin") ? "origin" : remotes[0]);
|
|
258
|
+
|
|
259
|
+
const requestedRemote = remoteForRef(requestedRef, remotes);
|
|
260
|
+
let refreshResult: RepositoryRefreshResult;
|
|
261
|
+
try {
|
|
262
|
+
refreshResult = await dependencies.refreshRepository(
|
|
263
|
+
cwd,
|
|
264
|
+
remote,
|
|
265
|
+
requestedRemote ? null : (localBranch?.branch ?? null),
|
|
266
|
+
localBranch?.upstream ?? null,
|
|
267
|
+
dependencies.signal,
|
|
268
|
+
);
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (dependencies.signal.aborted) throw error;
|
|
271
|
+
dependencies.warn?.(
|
|
272
|
+
`Skipped refresh because fetching ${remote} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
273
|
+
);
|
|
274
|
+
return request;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (refreshResult.kind === "dirty") {
|
|
278
|
+
dependencies.warn?.(
|
|
279
|
+
`Skipped refreshing local branch ${localBranch?.branch} in ${cwd} because the source checkout is not clean`,
|
|
280
|
+
);
|
|
281
|
+
} else {
|
|
282
|
+
dependencies.log?.(
|
|
283
|
+
refreshResult.kind === "updated"
|
|
284
|
+
? `Fetched ${remote} for ${cwd}; fast-forwarded ${localBranch?.branch} to ${refreshResult.freshBase}`
|
|
285
|
+
: `Fetched ${remote} for ${cwd}; no local branch update was needed`,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return request;
|
|
290
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { RpcOutput } from "@getpaseo/plugin";
|
|
2
|
+
import type { GitRunner, RepositoryRefresh } from "./fresh-worktrees";
|
|
3
|
+
import { executeGit } from "./fresh-worktrees";
|
|
4
|
+
import { workspaceFreshness } from "../shared/workspace-freshness";
|
|
5
|
+
|
|
6
|
+
export interface WorkspaceFreshnessDependencies {
|
|
7
|
+
signal: AbortSignal;
|
|
8
|
+
refreshRepository: RepositoryRefresh;
|
|
9
|
+
runGit?: GitRunner;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function optionalGit(
|
|
13
|
+
runGit: GitRunner,
|
|
14
|
+
cwd: string,
|
|
15
|
+
args: readonly string[],
|
|
16
|
+
signal: AbortSignal,
|
|
17
|
+
): Promise<string | null> {
|
|
18
|
+
try {
|
|
19
|
+
return await runGit(cwd, args, signal);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
if (signal.aborted) throw error;
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function inspectWorkspaceFreshness(
|
|
27
|
+
projectRootPath: string,
|
|
28
|
+
workspaceDirectory: string,
|
|
29
|
+
dependencies: WorkspaceFreshnessDependencies,
|
|
30
|
+
): Promise<RpcOutput<typeof workspaceFreshness>> {
|
|
31
|
+
const runGit = dependencies.runGit ?? executeGit;
|
|
32
|
+
const branch = await optionalGit(
|
|
33
|
+
runGit,
|
|
34
|
+
projectRootPath,
|
|
35
|
+
["symbolic-ref", "--quiet", "--short", "HEAD"],
|
|
36
|
+
dependencies.signal,
|
|
37
|
+
);
|
|
38
|
+
if (!branch) return { kind: "unavailable" };
|
|
39
|
+
|
|
40
|
+
const remote = await optionalGit(
|
|
41
|
+
runGit,
|
|
42
|
+
projectRootPath,
|
|
43
|
+
["config", "--get", `branch.${branch}.remote`],
|
|
44
|
+
dependencies.signal,
|
|
45
|
+
);
|
|
46
|
+
const mergeRef = await optionalGit(
|
|
47
|
+
runGit,
|
|
48
|
+
projectRootPath,
|
|
49
|
+
["config", "--get", `branch.${branch}.merge`],
|
|
50
|
+
dependencies.signal,
|
|
51
|
+
);
|
|
52
|
+
if (!remote || remote === "." || !mergeRef?.startsWith("refs/heads/")) {
|
|
53
|
+
return { kind: "unavailable" };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
await dependencies.refreshRepository(projectRootPath, remote, null, null, dependencies.signal);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (dependencies.signal.aborted) throw error;
|
|
60
|
+
return { kind: "unavailable" };
|
|
61
|
+
}
|
|
62
|
+
const remoteRef = `${remote}/${mergeRef.slice("refs/heads/".length)}`;
|
|
63
|
+
const behindByText = await optionalGit(
|
|
64
|
+
runGit,
|
|
65
|
+
workspaceDirectory,
|
|
66
|
+
["rev-list", "--count", `HEAD..refs/remotes/${remoteRef}`],
|
|
67
|
+
dependencies.signal,
|
|
68
|
+
);
|
|
69
|
+
if (behindByText === null) return { kind: "unavailable" };
|
|
70
|
+
|
|
71
|
+
const behindBy = Number.parseInt(behindByText, 10);
|
|
72
|
+
if (!Number.isSafeInteger(behindBy) || behindBy <= 0) {
|
|
73
|
+
return { kind: "current", remoteRef };
|
|
74
|
+
}
|
|
75
|
+
return { kind: "behind", remoteRef, behindBy };
|
|
76
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineRpc } from "@getpaseo/plugin";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
export const workspaceFreshness = defineRpc({
|
|
5
|
+
name: "fresh-worktrees.workspace-freshness",
|
|
6
|
+
input: z.object({
|
|
7
|
+
projectRootPath: z.string().min(1),
|
|
8
|
+
workspaceDirectory: z.string().min(1),
|
|
9
|
+
}),
|
|
10
|
+
output: z.discriminatedUnion("kind", [
|
|
11
|
+
z.object({ kind: z.literal("behind"), remoteRef: z.string(), behindBy: z.number().int().positive() }),
|
|
12
|
+
z.object({ kind: z.literal("current"), remoteRef: z.string() }),
|
|
13
|
+
z.object({ kind: z.literal("unavailable") }),
|
|
14
|
+
]),
|
|
15
|
+
});
|