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:
@@ -5165,20 +5165,35 @@ export class Session {
|
||||
const timestamp = new Date().toISOString();
|
||||
const directoryName = normalizedCwd.split(/[\\/]/).filter(Boolean).at(-1) ?? normalizedCwd;
|
||||
const gitMetadata = detectWorkspaceGitMetadata(normalizedCwd, directoryName);
|
||||
const projectId = await this.projectRegistry.insert({
|
||||
directory: normalizedCwd,
|
||||
displayName: gitMetadata.projectDisplayName,
|
||||
kind: gitMetadata.projectKind,
|
||||
gitRemote: gitMetadata.gitRemote,
|
||||
createdAt: timestamp,
|
||||
updatedAt: timestamp,
|
||||
archivedAt: null,
|
||||
});
|
||||
|
||||
let projectId: number | null = null;
|
||||
if (gitMetadata.gitRemote) {
|
||||
const existingProjects = await this.projectRegistry.list();
|
||||
const matchingProject = existingProjects.find(
|
||||
(p) => p.gitRemote === gitMetadata.gitRemote && !p.archivedAt,
|
||||
);
|
||||
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({
|
||||
projectId,
|
||||
directory: normalizedCwd,
|
||||
displayName: gitMetadata.workspaceDisplayName,
|
||||
kind: "checkout",
|
||||
kind: gitMetadata.isWorktree ? "worktree" : "checkout",
|
||||
createdAt: timestamp,
|
||||
updatedAt: timestamp,
|
||||
archivedAt: null,
|
||||
|
||||
@@ -6,6 +6,7 @@ export type WorkspaceGitMetadata = {
|
||||
projectDisplayName: string;
|
||||
workspaceDisplayName: string;
|
||||
gitRemote: string | null;
|
||||
isWorktree: boolean;
|
||||
};
|
||||
|
||||
export function readGitCommand(cwd: string, command: string): string | null {
|
||||
@@ -66,17 +67,21 @@ export function detectWorkspaceGitMetadata(
|
||||
projectDisplayName: directoryName,
|
||||
workspaceDisplayName: directoryName,
|
||||
gitRemote: null,
|
||||
isWorktree: false,
|
||||
};
|
||||
}
|
||||
|
||||
const gitRemote = readGitCommand(cwd, "git config --get remote.origin.url");
|
||||
const githubRepo = gitRemote ? parseGitHubRepoFromRemote(gitRemote) : null;
|
||||
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 {
|
||||
projectKind: "git",
|
||||
projectDisplayName: githubRepo ?? directoryName,
|
||||
workspaceDisplayName: branchName ?? directoryName,
|
||||
gitRemote,
|
||||
isWorktree,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user