From bf32112032dcc19a3a1894b22d85aafb56c61348 Mon Sep 17 00:00:00 2001 From: Willie Date: Fri, 25 Sep 2026 02:22:35 +0800 Subject: [PATCH] feat(git): show pending remote commits and auto-merge metadata conflicts Two machines sharing a source repo kept stopping at the same two places. The Git page had no way to know the remote moved until a push bounced, so it now fetches once on open and labels Pull with the commit count (status itself stays offline because the sidebar polls it). And both machines rewrite .metadata.json on every install or update, so nearly every diverged pull conflicted there; those conflicts are now merged per entry, keeping the later installed_at, while any other conflict still stops the pull and names the files. --- internal/git/info.go | 73 +++++++++++++++++++++++-- internal/git/info_test.go | 37 +++++++++++++ internal/install/metadata_merge.go | 61 +++++++++++++++++++++ internal/install/metadata_merge_test.go | 27 +++++++++ internal/server/handler_git.go | 3 + internal/server/handler_git_test.go | 25 +++++++++ ui/src/api/client.ts | 2 + ui/src/i18n/locales/de.json | 2 + ui/src/i18n/locales/en.json | 2 + ui/src/i18n/locales/es.json | 2 + ui/src/i18n/locales/fa.json | 2 + ui/src/i18n/locales/fr.json | 2 + ui/src/i18n/locales/id.json | 2 + ui/src/i18n/locales/ja.json | 2 + ui/src/i18n/locales/ko.json | 2 + ui/src/i18n/locales/pt-BR.json | 2 + ui/src/i18n/locales/zh-CN.json | 2 + ui/src/i18n/locales/zh-TW.json | 2 + ui/src/pages/GitSyncPage.tsx | 15 ++++- 19 files changed, 258 insertions(+), 7 deletions(-) create mode 100644 internal/install/metadata_merge.go create mode 100644 internal/install/metadata_merge_test.go diff --git a/internal/git/info.go b/internal/git/info.go index 2ac928d1f..a37c4bf46 100644 --- a/internal/git/info.go +++ b/internal/git/info.go @@ -9,6 +9,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "strconv" "strings" "time" @@ -255,11 +256,17 @@ func PullWithProgress(repoPath string, extraEnv []string, onProgress func(string args[len(args)-1] = "--progress" } if err := runGitWithProgress(repoPath, args, extraEnv, onProgress); err != nil { - // A conflicted merge would leave markers that a later commit-all picks up. - abort := exec.Command("git", "merge", "--abort") - abort.Dir = repoPath - abort.Run() // best-effort; fails harmlessly when no merge started - return nil, err + conflicts := conflictedFiles(repoPath) + if len(conflicts) == 0 || resolveMetadataConflicts(repoPath, conflicts) != nil { + // A conflicted merge would leave markers that a later commit-all picks up. + abort := exec.Command("git", "merge", "--abort") + abort.Dir = repoPath + abort.Run() // best-effort; fails harmlessly when no merge started + if len(conflicts) > 0 { + return nil, fmt.Errorf("pull stopped: this machine and the remote both changed %s; the merge was undone, resolve it with git in %s", strings.Join(conflicts, ", "), repoPath) + } + return nil, err + } } afterHash, err := GetCurrentFullHash(repoPath) @@ -282,6 +289,49 @@ func PullWithProgress(repoPath string, extraEnv []string, onProgress func(string return info, nil } +// conflictedFiles lists the paths an in-progress merge left unmerged. +func conflictedFiles(dir string) []string { + cmd := exec.Command("git", "diff", "--name-only", "--diff-filter=U") + cmd.Dir = dir + out, err := cmd.Output() + if err != nil { + return nil + } + return strings.Fields(string(out)) +} + +// resolveMetadataConflicts concludes a merge whose only conflicts are +// .metadata.json files, merging them per entry (see install.MergeMetadata). +// Both machines rewriting the shared file is the common case, not a real clash. +func resolveMetadataConflicts(dir string, conflicts []string) error { + stage := func(n int, path string) []byte { + cmd := exec.Command("git", "show", fmt.Sprintf(":%d:%s", n, path)) + cmd.Dir = dir + out, _ := cmd.Output() // a missing stage (file absent on that side) reads as empty + return out + } + for _, path := range conflicts { + if filepath.Base(path) != install.MetadataFileName { + return fmt.Errorf("%s is not metadata", path) + } + merged, err := install.MergeMetadata(stage(1, path), stage(2, path), stage(3, path)) + if err != nil { + return err + } + if err := os.WriteFile(filepath.Join(dir, path), merged, 0644); err != nil { + return err + } + add := exec.Command("git", "add", "--", path) + add.Dir = dir + if err := add.Run(); err != nil { + return err + } + } + commit := exec.Command("git", "commit", "--no-edit") + commit.Dir = dir + return commit.Run() +} + // PullWithEnv runs git pull and returns update info (quiet mode) with // additional environment variables. func PullWithEnv(repoPath string, extraEnv []string) (*UpdateInfo, error) { @@ -570,6 +620,19 @@ func AheadCount(dir string) int { return n } +// BehindCount returns how many upstream commits HEAD lacks, as of the last +// fetch. It returns 0 when unknown, e.g. without an upstream. +func BehindCount(dir string) int { + cmd := exec.Command("git", "rev-list", "--count", "HEAD..@{u}") + cmd.Dir = dir + out, err := cmd.Output() + if err != nil { + return 0 + } + n, _ := strconv.Atoi(strings.TrimSpace(string(out))) + return n +} + // ErrMergeFailed reports that merging origin's history during a first pull // failed, usually on a conflict. The merge has been aborted. var ErrMergeFailed = errors.New("merging remote history failed") diff --git a/internal/git/info_test.go b/internal/git/info_test.go index e60d3c6c2..b5f7400bf 100644 --- a/internal/git/info_test.go +++ b/internal/git/info_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "strings" "testing" + + "skillshare/internal/install" ) // initTestRepo creates a temporary git repo with one commit @@ -560,6 +562,41 @@ func TestPullWithProgress_ConflictLeavesRepoClean(t *testing.T) { } } +func TestPullWithProgress_ResolvesMetadataConflict(t *testing.T) { + meta := func(entries string) string { + return `{"version": 1, "entries": {` + entries + `}}` + "\n" + } + shared := func(at string) string { + return `"shared": {"source": "github.com/o/r/shared", "installed_at": "` + at + `"}` + } + remote := createBareRemoteWithBranch(t, "main", map[string]string{ + ".metadata.json": meta(shared("2026-01-01T00:00:00Z")), + }) + repo := cloneRepo(t, remote) + divergeFromRemote(t, remote, repo, + [2]string{".metadata.json", meta(shared("2026-03-01T00:00:00Z") + `, "remote-only": {"source": "r"}`)}, + [2]string{".metadata.json", meta(shared("2026-02-01T00:00:00Z") + `, "local-only": {"source": "l"}`)}) + + if _, err := PullWithProgress(repo, nil, nil); err != nil { + t.Fatalf("expected a metadata-only conflict to resolve, got: %v", err) + } + store, err := install.LoadMetadata(repo) + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"remote-only", "local-only"} { + if !store.Has(name) { + t.Errorf("expected entry %q after merge", name) + } + } + if got := store.Get("shared").InstalledAt.Format("2006-01-02"); got != "2026-03-01" { + t.Errorf("expected the later installed_at to win, got %s", got) + } + if status := runGit(t, repo, "status", "--porcelain"); status != "" { + t.Fatalf("expected the merge to be committed, got status:\n%s", status) + } +} + func TestForcePullWithProgress(t *testing.T) { remote := createBareRemoteWithBranch(t, "main", map[string]string{ "README.md": "# v1\n", diff --git a/internal/install/metadata_merge.go b/internal/install/metadata_merge.go new file mode 100644 index 000000000..fd5dad17f --- /dev/null +++ b/internal/install/metadata_merge.go @@ -0,0 +1,61 @@ +package install + +import ( + "encoding/json" + "fmt" + "reflect" +) + +// MergeMetadata three-way merges .metadata.json contents per entry, for a git +// merge where two machines both installed or updated skills. An entry changed +// on one side only takes that side; changed on both, the later installed_at +// wins (the remote on a tie), and an edit beats a delete. An empty base means +// the file did not exist in the common ancestor. +func MergeMetadata(base, ours, theirs []byte) ([]byte, error) { + parse := func(data []byte, side string) (*MetadataStore, error) { + store := NewMetadataStore() + if len(data) == 0 { + return store, nil + } + if err := json.Unmarshal(data, store); err != nil { + return nil, fmt.Errorf("parse %s metadata: %w", side, err) + } + if store.Entries == nil { + store.Entries = make(map[string]*MetadataEntry) + } + return store, nil + } + b, err := parse(base, "base") + if err != nil { + return nil, err + } + o, err := parse(ours, "local") + if err != nil { + return nil, err + } + th, err := parse(theirs, "remote") + if err != nil { + return nil, err + } + + merged := th + for name, mine := range o.Entries { + old, remote := b.Entries[name], th.Entries[name] + switch { + case reflect.DeepEqual(mine, old): // unchanged here: the remote side stands + case reflect.DeepEqual(remote, old) || remote == nil || mine.InstalledAt.After(remote.InstalledAt): + merged.Entries[name] = mine + } + } + for name := range th.Entries { + if _, kept := o.Entries[name]; !kept && b.Entries[name] != nil && reflect.DeepEqual(th.Entries[name], b.Entries[name]) { + delete(merged.Entries, name) // deleted here, untouched remotely + } + } + + data, err := json.MarshalIndent(merged, "", " ") + if err != nil { + return nil, fmt.Errorf("marshal metadata: %w", err) + } + return append(data, '\n'), nil +} diff --git a/internal/install/metadata_merge_test.go b/internal/install/metadata_merge_test.go new file mode 100644 index 000000000..f155d5e3b --- /dev/null +++ b/internal/install/metadata_merge_test.go @@ -0,0 +1,27 @@ +package install + +import ( + "encoding/json" + "testing" +) + +func TestMergeMetadata_DeletesOnlyUntouchedEntries(t *testing.T) { + base := []byte(`{"version":1,"entries":{"uninstalled-here":{"source":"a"},"deleted-remotely":{"source":"b"}}}`) + ours := []byte(`{"version":1,"entries":{"deleted-remotely":{"source":"b2"}}}`) + theirs := []byte(`{"version":1,"entries":{"uninstalled-here":{"source":"a"}}}`) + + out, err := MergeMetadata(base, ours, theirs) + if err != nil { + t.Fatal(err) + } + var got MetadataStore + if err := json.Unmarshal(out, &got); err != nil { + t.Fatal(err) + } + if _, ok := got.Entries["uninstalled-here"]; ok { + t.Error("expected a local uninstall of an entry the remote left alone to stick") + } + if e := got.Entries["deleted-remotely"]; e == nil || e.Source != "b2" { + t.Errorf("expected the local edit to beat the remote delete, got %+v", e) + } +} diff --git a/internal/server/handler_git.go b/internal/server/handler_git.go index da51c9515..b8f9674b8 100644 --- a/internal/server/handler_git.go +++ b/internal/server/handler_git.go @@ -30,6 +30,8 @@ type gitStatusResponse struct { TrackingBranch string `json:"trackingBranch,omitempty"` // Ahead counts commits no remote-tracking branch has yet, i.e. what a push uploads. Ahead int `json:"ahead"` + // Behind counts upstream commits HEAD lacks as of the last fetch, i.e. what a pull brings in. + Behind int `json:"behind"` // Root-scope hazards (populated only when scope == "root"): NestedRepos are // subdirectories with their own .git that commit as empty submodules; // ConfigTracked means config.yaml leaked into version control. @@ -108,6 +110,7 @@ func (s *Server) handleGitStatus(w http.ResponseWriter, r *http.Request) { if resp.HasRemote { resp.Ahead = git.AheadCount(src) + resp.Behind = git.BehindCount(src) } // Root-scope hazards: nested submodule traps and a leaked config.yaml. diff --git a/internal/server/handler_git_test.go b/internal/server/handler_git_test.go index 2073b8748..8eed8e20e 100644 --- a/internal/server/handler_git_test.go +++ b/internal/server/handler_git_test.go @@ -436,6 +436,31 @@ func postPull(s *Server, body string) *httptest.ResponseRecorder { return rr } +func TestHandleGitStatus_ReportsBehindAfterFetch(t *testing.T) { + s, src := newTestServer(t) + initServerGitRepo(t, src) + remote := filepath.Join(t.TempDir(), "remote.git") + testutil.RunGit(t, "", "init", "--bare", remote) + testutil.RunGit(t, src, "remote", "add", "origin", remote) + testutil.RunGit(t, src, "push", "-u", "origin", "HEAD") + pushRemoteFile(t, remote, "remote-skill/SKILL.md", "# remote\n") + testutil.RunGit(t, src, "fetch") + + req := httptest.NewRequest(http.MethodGet, "/api/git/status", nil) + rr := httptest.NewRecorder() + s.handler.ServeHTTP(rr, req) + + var resp struct { + Behind int `json:"behind"` + } + if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil { + t.Fatalf("decode: %v (%s)", err, rr.Body.String()) + } + if resp.Behind != 1 { + t.Fatalf("expected behind=1, got %d: %s", resp.Behind, rr.Body.String()) + } +} + func TestHandlePush_RemoteAheadReportsPushRejected(t *testing.T) { s, src := newTestServer(t) initServerGitRepo(t, src) diff --git a/ui/src/api/client.ts b/ui/src/api/client.ts index a68b92a2a..d462983a1 100644 --- a/ui/src/api/client.ts +++ b/ui/src/api/client.ts @@ -1205,6 +1205,8 @@ export interface GitStatus { trackingBranch?: string; /** Commits no remote-tracking branch has yet (what a push uploads); 0 without a remote. */ ahead: number; + /** Upstream commits HEAD lacks as of the last fetch (what a pull brings in); 0 without an upstream. */ + behind: number; // Root-scope hazards (populated only when scope === 'root'). nestedRepos: string[]; configTracked: boolean; diff --git a/ui/src/i18n/locales/de.json b/ui/src/i18n/locales/de.json index 65f9a6ec1..f76ba24c8 100644 --- a/ui/src/i18n/locales/de.json +++ b/ui/src/i18n/locales/de.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "Backups ansehen", "gitSync.actions.pushCommits.one": "{count} Commit pushen", "gitSync.actions.pushCommits.other": "{count} Commits pushen", + "gitSync.actions.pullCommits.one": "{count} Commit pullen", + "gitSync.actions.pullCommits.other": "{count} Commits pullen", "gitSync.toast.uploaded.one": "{count} Commit gepusht", "gitSync.toast.uploaded.other": "{count} Commits gepusht", "gitSync.repo.ahead.one": "{count} Commit zum Pushen", diff --git a/ui/src/i18n/locales/en.json b/ui/src/i18n/locales/en.json index f42a21c34..9f210c394 100644 --- a/ui/src/i18n/locales/en.json +++ b/ui/src/i18n/locales/en.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "View backups", "gitSync.actions.pushCommits.one": "Push {count} commit", "gitSync.actions.pushCommits.other": "Push {count} commits", + "gitSync.actions.pullCommits.one": "Pull {count} commit", + "gitSync.actions.pullCommits.other": "Pull {count} commits", "gitSync.toast.uploaded.one": "Pushed {count} commit", "gitSync.toast.uploaded.other": "Pushed {count} commits", "gitSync.repo.ahead.one": "{count} commit to push", diff --git a/ui/src/i18n/locales/es.json b/ui/src/i18n/locales/es.json index a23b49046..c78aceaf6 100644 --- a/ui/src/i18n/locales/es.json +++ b/ui/src/i18n/locales/es.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "Ver copias", "gitSync.actions.pushCommits.one": "Push de {count} commit", "gitSync.actions.pushCommits.other": "Push de {count} commits", + "gitSync.actions.pullCommits.one": "Obtener {count} commit", + "gitSync.actions.pullCommits.other": "Obtener {count} commits", "gitSync.toast.uploaded.one": "Se hizo push de {count} commit", "gitSync.toast.uploaded.other": "Se hizo push de {count} commits", "gitSync.repo.ahead.one": "{count} commit por enviar", diff --git a/ui/src/i18n/locales/fa.json b/ui/src/i18n/locales/fa.json index bad402668..bc847e09e 100644 --- a/ui/src/i18n/locales/fa.json +++ b/ui/src/i18n/locales/fa.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "مشاهدهٔ پشتیبان‌ها", "gitSync.actions.pushCommits.one": "Push کردن {count} commit", "gitSync.actions.pushCommits.other": "Push کردن {count} commit", + "gitSync.actions.pullCommits.one": "دریافت {count} commit", + "gitSync.actions.pullCommits.other": "دریافت {count} commit", "gitSync.toast.uploaded.one": "{count} commit Push شد", "gitSync.toast.uploaded.other": "{count} commit Push شد", "gitSync.repo.ahead.one": "{count} commit برای Push", diff --git a/ui/src/i18n/locales/fr.json b/ui/src/i18n/locales/fr.json index 32c732939..c669101dd 100644 --- a/ui/src/i18n/locales/fr.json +++ b/ui/src/i18n/locales/fr.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "Voir les sauvegardes", "gitSync.actions.pushCommits.one": "Push de {count} commit", "gitSync.actions.pushCommits.other": "Push de {count} commits", + "gitSync.actions.pullCommits.one": "Tirer {count} commit", + "gitSync.actions.pullCommits.other": "Tirer {count} commits", "gitSync.toast.uploaded.one": "{count} commit poussé", "gitSync.toast.uploaded.other": "{count} commits poussés", "gitSync.repo.ahead.one": "{count} commit à pousser", diff --git a/ui/src/i18n/locales/id.json b/ui/src/i18n/locales/id.json index aa848288a..4f28b1716 100644 --- a/ui/src/i18n/locales/id.json +++ b/ui/src/i18n/locales/id.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "Lihat cadangan", "gitSync.actions.pushCommits.one": "Push {count} commit", "gitSync.actions.pushCommits.other": "Push {count} commit", + "gitSync.actions.pullCommits.one": "Tarik {count} commit", + "gitSync.actions.pullCommits.other": "Tarik {count} commit", "gitSync.toast.uploaded.one": "{count} commit di-push", "gitSync.toast.uploaded.other": "{count} commit di-push", "gitSync.repo.ahead.one": "{count} commit belum di-push", diff --git a/ui/src/i18n/locales/ja.json b/ui/src/i18n/locales/ja.json index 0fee93cf5..9c22d35b7 100644 --- a/ui/src/i18n/locales/ja.json +++ b/ui/src/i18n/locales/ja.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "バックアップを見る", "gitSync.actions.pushCommits.one": "{count} 件の Commit を Push", "gitSync.actions.pushCommits.other": "{count} 件の Commit を Push", + "gitSync.actions.pullCommits.one": "{count} 件の Commit をプル", + "gitSync.actions.pullCommits.other": "{count} 件の Commit をプル", "gitSync.toast.uploaded.one": "{count} 件の Commit を Push しました", "gitSync.toast.uploaded.other": "{count} 件の Commit を Push しました", "gitSync.repo.ahead.one": "未 Push の Commit {count} 件", diff --git a/ui/src/i18n/locales/ko.json b/ui/src/i18n/locales/ko.json index f6075f409..ac71df741 100644 --- a/ui/src/i18n/locales/ko.json +++ b/ui/src/i18n/locales/ko.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "백업 보기", "gitSync.actions.pushCommits.one": "Commit {count}개 Push", "gitSync.actions.pushCommits.other": "Commit {count}개 Push", + "gitSync.actions.pullCommits.one": "Commit {count}개 가져오기", + "gitSync.actions.pullCommits.other": "Commit {count}개 가져오기", "gitSync.toast.uploaded.one": "Commit {count}개를 Push했습니다", "gitSync.toast.uploaded.other": "Commit {count}개를 Push했습니다", "gitSync.repo.ahead.one": "Push할 Commit {count}개", diff --git a/ui/src/i18n/locales/pt-BR.json b/ui/src/i18n/locales/pt-BR.json index 2a35bde91..9e239411d 100644 --- a/ui/src/i18n/locales/pt-BR.json +++ b/ui/src/i18n/locales/pt-BR.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "Ver backups", "gitSync.actions.pushCommits.one": "Push de {count} commit", "gitSync.actions.pushCommits.other": "Push de {count} commits", + "gitSync.actions.pullCommits.one": "Baixar {count} commit", + "gitSync.actions.pullCommits.other": "Baixar {count} commits", "gitSync.toast.uploaded.one": "Push de {count} commit concluído", "gitSync.toast.uploaded.other": "Push de {count} commits concluído", "gitSync.repo.ahead.one": "{count} commit para enviar", diff --git a/ui/src/i18n/locales/zh-CN.json b/ui/src/i18n/locales/zh-CN.json index a4302585e..d0a24357c 100644 --- a/ui/src/i18n/locales/zh-CN.json +++ b/ui/src/i18n/locales/zh-CN.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "查看备份", "gitSync.actions.pushCommits.one": "Push {count} 个 Commit", "gitSync.actions.pushCommits.other": "Push {count} 个 Commit", + "gitSync.actions.pullCommits.one": "拉取 {count} 个 Commit", + "gitSync.actions.pullCommits.other": "拉取 {count} 个 Commit", "gitSync.toast.uploaded.one": "已 Push {count} 个 Commit", "gitSync.toast.uploaded.other": "已 Push {count} 个 Commit", "gitSync.repo.ahead.one": "{count} 个 Commit 待 Push", diff --git a/ui/src/i18n/locales/zh-TW.json b/ui/src/i18n/locales/zh-TW.json index d9f53946f..55ca1a6b0 100644 --- a/ui/src/i18n/locales/zh-TW.json +++ b/ui/src/i18n/locales/zh-TW.json @@ -1326,6 +1326,8 @@ "sync.backupLink": "查看備份", "gitSync.actions.pushCommits.one": "Push {count} 個 Commit", "gitSync.actions.pushCommits.other": "Push {count} 個 Commit", + "gitSync.actions.pullCommits.one": "Pull {count} 個 Commit", + "gitSync.actions.pullCommits.other": "Pull {count} 個 Commit", "gitSync.toast.uploaded.one": "已 Push {count} 個 Commit", "gitSync.toast.uploaded.other": "已 Push {count} 個 Commit", "gitSync.repo.ahead.one": "{count} 個 Commit 待 Push", diff --git a/ui/src/pages/GitSyncPage.tsx b/ui/src/pages/GitSyncPage.tsx index d5e641179..1f89d173f 100644 --- a/ui/src/pages/GitSyncPage.tsx +++ b/ui/src/pages/GitSyncPage.tsx @@ -28,7 +28,18 @@ export default function GitSyncPage() { const queryClient = useQueryClient(); const { toast } = useToast(); const { data: status, isPending, error } = useQuery({ queryKey: queryKeys.gitStatus, queryFn: () => api.gitStatus(), staleTime: staleTimes.gitStatus, enabled: !isProjectMode }); - const branches = useQuery({ queryKey: queryKeys.gitBranches, queryFn: () => api.gitBranches(), staleTime: staleTimes.gitStatus, enabled: !isProjectMode && !!status?.isRepo }); + // Fetch once when the page opens so status knows what the remote has (status itself never fetches: the sidebar polls it). + const branches = useQuery({ + queryKey: queryKeys.gitBranches, + queryFn: async () => { + // An offline or unauthenticated fetch must not empty the branch list. + const res = await api.gitBranches({ fetch: !!status?.hasRemote }).catch(() => api.gitBranches()); + void queryClient.invalidateQueries({ queryKey: queryKeys.gitStatus }); + return res; + }, + staleTime: staleTimes.gitStatus, + enabled: !isProjectMode && !!status?.isRepo, + }); const [message, setMessage] = useState(''); const [dryRun, setDryRun] = useState(false); @@ -142,7 +153,7 @@ export default function GitSyncPage() { )} ))}