fix: deduplicate projects by git remote and detect worktree workspaces

Reuse existing project when a matching git remote is found instead of
creating duplicates. Detect git worktrees via --git-common-dir and set
workspace kind accordingly.
This commit is contained in:
Mohamed Boudra
2026-04-02 11:31:13 +07:00
parent 68c8efc5d3
commit b730d83943
2 changed files with 30 additions and 10 deletions
+25 -10
View File
@@ -5165,20 +5165,35 @@ export class Session {
const timestamp = new Date().toISOString(); const timestamp = new Date().toISOString();
const directoryName = normalizedCwd.split(/[\\/]/).filter(Boolean).at(-1) ?? normalizedCwd; const directoryName = normalizedCwd.split(/[\\/]/).filter(Boolean).at(-1) ?? normalizedCwd;
const gitMetadata = detectWorkspaceGitMetadata(normalizedCwd, directoryName); const gitMetadata = detectWorkspaceGitMetadata(normalizedCwd, directoryName);
const projectId = await this.projectRegistry.insert({
directory: normalizedCwd, let projectId: number | null = null;
displayName: gitMetadata.projectDisplayName, if (gitMetadata.gitRemote) {
kind: gitMetadata.projectKind, const existingProjects = await this.projectRegistry.list();
gitRemote: gitMetadata.gitRemote, const matchingProject = existingProjects.find(
createdAt: timestamp, (p) => p.gitRemote === gitMetadata.gitRemote && !p.archivedAt,
updatedAt: timestamp, );
archivedAt: null, if (matchingProject) {
}); projectId = matchingProject.id;
}
}
if (projectId === null) {
projectId = await this.projectRegistry.insert({
directory: normalizedCwd,
displayName: gitMetadata.projectDisplayName,
kind: gitMetadata.projectKind,
gitRemote: gitMetadata.gitRemote,
createdAt: timestamp,
updatedAt: timestamp,
archivedAt: null,
});
}
const workspaceId = await this.workspaceRegistry.insert({ const workspaceId = await this.workspaceRegistry.insert({
projectId, projectId,
directory: normalizedCwd, directory: normalizedCwd,
displayName: gitMetadata.workspaceDisplayName, displayName: gitMetadata.workspaceDisplayName,
kind: "checkout", kind: gitMetadata.isWorktree ? "worktree" : "checkout",
createdAt: timestamp, createdAt: timestamp,
updatedAt: timestamp, updatedAt: timestamp,
archivedAt: null, archivedAt: null,
@@ -6,6 +6,7 @@ export type WorkspaceGitMetadata = {
projectDisplayName: string; projectDisplayName: string;
workspaceDisplayName: string; workspaceDisplayName: string;
gitRemote: string | null; gitRemote: string | null;
isWorktree: boolean;
}; };
export function readGitCommand(cwd: string, command: string): string | null { export function readGitCommand(cwd: string, command: string): string | null {
@@ -66,17 +67,21 @@ export function detectWorkspaceGitMetadata(
projectDisplayName: directoryName, projectDisplayName: directoryName,
workspaceDisplayName: directoryName, workspaceDisplayName: directoryName,
gitRemote: null, gitRemote: null,
isWorktree: false,
}; };
} }
const gitRemote = readGitCommand(cwd, "git config --get remote.origin.url"); const gitRemote = readGitCommand(cwd, "git config --get remote.origin.url");
const githubRepo = gitRemote ? parseGitHubRepoFromRemote(gitRemote) : null; const githubRepo = gitRemote ? parseGitHubRepoFromRemote(gitRemote) : null;
const branchName = readGitCommand(cwd, "git symbolic-ref --short HEAD"); const branchName = readGitCommand(cwd, "git symbolic-ref --short HEAD");
const gitCommonDir = readGitCommand(cwd, "git rev-parse --git-common-dir");
const isWorktree = gitCommonDir !== null && gitDir !== gitCommonDir;
return { return {
projectKind: "git", projectKind: "git",
projectDisplayName: githubRepo ?? directoryName, projectDisplayName: githubRepo ?? directoryName,
workspaceDisplayName: branchName ?? directoryName, workspaceDisplayName: branchName ?? directoryName,
gitRemote, gitRemote,
isWorktree,
}; };
} }