-
Notifications
You must be signed in to change notification settings - Fork 12.3k
fix(core): make worktrees read the project id from local workspace #16795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+38
−27
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,12 @@ export namespace Project { | |
| } | ||
| } | ||
|
|
||
| function readCachedId(dir: string) { | ||
| return Filesystem.readText(path.join(dir, "opencode")) | ||
| .then((x) => x.trim()) | ||
| .catch(() => undefined) | ||
| } | ||
|
|
||
| export async function fromDirectory(directory: string) { | ||
| log.info("fromDirectory", { directory }) | ||
|
|
||
|
|
@@ -101,19 +107,43 @@ export namespace Project { | |
| const gitBinary = which("git") | ||
|
|
||
| // cached id calculation | ||
| let id = await Filesystem.readText(path.join(dotgit, "opencode")) | ||
| .then((x) => x.trim()) | ||
| .catch(() => undefined) | ||
| let id = await readCachedId(dotgit) | ||
|
|
||
| if (!gitBinary) { | ||
| return { | ||
| id: id ?? "global", | ||
| worktree: sandbox, | ||
| sandbox: sandbox, | ||
| sandbox, | ||
| vcs: Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS), | ||
| } | ||
| } | ||
|
|
||
| const worktree = await git(["rev-parse", "--git-common-dir"], { | ||
| cwd: sandbox, | ||
| }) | ||
| .then(async (result) => { | ||
| const common = gitpath(sandbox, await result.text()) | ||
| // Avoid going to parent of sandbox when git-common-dir is empty. | ||
| return common === sandbox ? sandbox : path.dirname(common) | ||
| }) | ||
| .catch(() => undefined) | ||
|
|
||
| if (!worktree) { | ||
| return { | ||
| id: id ?? "global", | ||
| worktree: sandbox, | ||
| sandbox, | ||
| vcs: Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS), | ||
| } | ||
| } | ||
|
|
||
| // In the case of a git worktree, it can't cache the id | ||
| // because `.git` is not a folder, but it always needs the | ||
| // same project id as the common dir, so we resolve it now | ||
| if (id == null) { | ||
| id = await readCachedId(path.join(worktree, ".git")) | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the main block of code I added |
||
|
|
||
| // generate id from root commit | ||
| if (!id) { | ||
| const roots = await git(["rev-list", "--max-parents=0", "--all"], { | ||
|
|
@@ -132,7 +162,7 @@ export namespace Project { | |
| return { | ||
| id: "global", | ||
| worktree: sandbox, | ||
| sandbox: sandbox, | ||
| sandbox, | ||
| vcs: Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS), | ||
| } | ||
| } | ||
|
|
@@ -147,7 +177,7 @@ export namespace Project { | |
| return { | ||
| id: "global", | ||
| worktree: sandbox, | ||
| sandbox: sandbox, | ||
| sandbox, | ||
| vcs: "git", | ||
| } | ||
| } | ||
|
|
@@ -161,33 +191,14 @@ export namespace Project { | |
| if (!top) { | ||
| return { | ||
| id, | ||
| sandbox, | ||
| worktree: sandbox, | ||
| sandbox, | ||
| vcs: Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS), | ||
| } | ||
| } | ||
|
|
||
| sandbox = top | ||
|
|
||
| const worktree = await git(["rev-parse", "--git-common-dir"], { | ||
| cwd: sandbox, | ||
| }) | ||
| .then(async (result) => { | ||
| const common = gitpath(sandbox, await result.text()) | ||
| // Avoid going to parent of sandbox when git-common-dir is empty. | ||
| return common === sandbox ? sandbox : path.dirname(common) | ||
| }) | ||
| .catch(() => undefined) | ||
|
|
||
| if (!worktree) { | ||
| return { | ||
| id, | ||
| sandbox, | ||
| worktree: sandbox, | ||
| vcs: Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS), | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| id, | ||
| sandbox, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look below, all I did was move this up. There shouldn't be much difference in the order of execution. Only difference will be what happens when these commands file: now we might not have an
id, but that's ok. now it'll just fallback toglobalwhich is fine