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.
This commit is contained in:
Willie
2026-09-25 02:22:35 +08:00
parent 51af1d30ba
commit bf32112032
19 changed files with 258 additions and 7 deletions
+68 -5
View File
@@ -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")
+37
View File
@@ -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",
+61
View File
@@ -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
}
+27
View File
@@ -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)
}
}
+3
View File
@@ -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.
+25
View File
@@ -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)
+2
View File
@@ -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;
+2
View File
@@ -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",
+2
View File
@@ -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",
+2
View File
@@ -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",
+2
View File
@@ -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",
+2
View File
@@ -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",
+2
View File
@@ -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",
+2
View File
@@ -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} 件",
+2
View File
@@ -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}개",
+2
View File
@@ -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",
+2
View File
@@ -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",
+2
View File
@@ -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",
+13 -2
View File
@@ -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() {
)}
<Button variant="secondary" onClick={() => pull()} loading={busy === 'pull'} disabled={writing || !status.hasRemote || status.isDirty} title={!status.hasRemote ? t('gitSync.noRemoteHint') : undefined}>
{busy !== 'pull' && <ArrowDownToLine size={16} />}
{t('gitSync.actions.pull')}
{status.behind > 0 ? t(status.behind === 1 ? 'gitSync.actions.pullCommits.one' : 'gitSync.actions.pullCommits.other', { count: status.behind }) : t('gitSync.actions.pull')}
</Button>
</span>
))}