Skip to content

Commit c42b07e

Browse files
jim80netclaude
andcommitted
fix: normalize git remote URLs for consistent cross-machine project tags
SSH and HTTPS URLs for the same repo (e.g. git@github.com:user/repo.git vs https://github.com/user/repo) produced different hashes, silently breaking cross-machine memory sync. Normalize URLs to a canonical form before hashing. Also restores trailing newline. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3d87d79 commit c42b07e

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/services/tags.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ export function getGitEmail(): string | null {
1515
}
1616
}
1717

18+
/**
19+
* Normalize a git remote URL to a canonical form so that SSH, HTTPS,
20+
* and with/without `.git` suffix all produce the same identifier.
21+
*
22+
* Examples:
23+
* git@github.com:user/repo.git → github.com/user/repo
24+
* https://github.com/user/repo → github.com/user/repo
25+
* git@gitlab.com:org/sub/repo.git → gitlab.com/org/sub/repo
26+
*/
27+
export function normalizeGitUrl(url: string): string {
28+
return url
29+
.replace(/^[a-z+]+:\/\//, "") // strip protocol (https://, git://, ssh://)
30+
.replace(/^[^@]+@/, "") // strip user@ prefix (git@, user@)
31+
.replace(/:(\d+)\//, "/$1/") // preserve port numbers (e.g. :8080/)
32+
.replace(":", "/") // SSH colon to slash (github.com:user → github.com/user)
33+
.replace(/\.git$/, "") // strip trailing .git
34+
.replace(/\/+$/, ""); // strip trailing slashes
35+
}
36+
1837
/**
1938
* Get the git remote URL for the given directory.
2039
* This provides a stable, cross-machine identifier for projects.
@@ -58,7 +77,7 @@ export function getProjectTag(directory: string): string {
5877
// This allows the same project on different machines to share memories
5978
const remoteUrl = getGitRemoteUrl(directory);
6079
if (remoteUrl) {
61-
return `${CONFIG.containerTagPrefix}_project_${sha256(remoteUrl)}`;
80+
return `${CONFIG.containerTagPrefix}_project_${sha256(normalizeGitUrl(remoteUrl))}`;
6281
}
6382

6483
// Fall back to directory path hash (machine-specific)
@@ -70,4 +89,4 @@ export function getTags(directory: string): { user: string; project: string } {
7089
user: getUserTag(),
7190
project: getProjectTag(directory),
7291
};
73-
}
92+
}

0 commit comments

Comments
 (0)