mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 20:58:00 +00:00
fix(gitshallow): mirror upstream with fetch+reset instead of pull --ff-only
The shallow clone is a read-only mirror, so a force-push on the upstream branch caused pull --ff-only to bail with "refusing to merge unrelated histories". Switch to git fetch + git reset --hard origin/<branch> so the local copy always tracks upstream, force-push or not. Auto-detects the branch from origin/HEAD when Branch is empty.
This commit is contained in:
parent
8f40bbf110
commit
631f32cf95
@ -115,7 +115,9 @@ func (r *Repo) runGit(args ...string) (string, error) {
|
|||||||
return strings.TrimSpace(string(output)), nil
|
return strings.TrimSpace(string(output)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull performs a shallow pull (--ff-only) and reports whether HEAD changed.
|
// Pull fetches from origin and hard-resets the working tree to the remote
|
||||||
|
// branch, reporting whether HEAD changed. This is a read-only mirror — we
|
||||||
|
// never try to merge, so force-pushes upstream are handled transparently.
|
||||||
func (r *Repo) Pull() (updated bool, err error) {
|
func (r *Repo) Pull() (updated bool, err error) {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
@ -127,19 +129,27 @@ func (r *Repo) pull() (updated bool, err error) {
|
|||||||
return false, fmt.Errorf("repository does not exist at %s", r.Path)
|
return false, fmt.Errorf("repository does not exist at %s", r.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
oldHead, err := r.runGit("rev-parse", "HEAD")
|
oldHead, _ := r.runGit("rev-parse", "HEAD")
|
||||||
if err != nil {
|
|
||||||
|
branch := r.Branch
|
||||||
|
if branch == "" {
|
||||||
|
out, err := r.runGit("symbolic-ref", "--short", "refs/remotes/origin/HEAD")
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
_, branch, _ = strings.Cut(out, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchArgs := []string{"fetch", "--no-tags"}
|
||||||
|
if depth := r.effectiveDepth(); depth >= 0 {
|
||||||
|
fetchArgs = append(fetchArgs, "--depth", fmt.Sprintf("%d", depth))
|
||||||
|
}
|
||||||
|
fetchArgs = append(fetchArgs, "origin", branch)
|
||||||
|
if _, err := r.runGit(fetchArgs...); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pullArgs := []string{"pull", "--ff-only", "--no-tags"}
|
if _, err := r.runGit("reset", "--hard", "origin/"+branch); err != nil {
|
||||||
if depth := r.effectiveDepth(); depth >= 0 {
|
|
||||||
pullArgs = append(pullArgs, "--depth", fmt.Sprintf("%d", depth))
|
|
||||||
}
|
|
||||||
if r.Branch != "" {
|
|
||||||
pullArgs = append(pullArgs, "origin", r.Branch)
|
|
||||||
}
|
|
||||||
if _, err = r.runGit(pullArgs...); err != nil {
|
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user