diff options
| author | Shulhan <m.shulhan@gmail.com> | 2024-07-21 03:48:32 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2026-04-09 22:20:06 +0700 |
| commit | 31f678e5a420fecf567c30bc66e40b43ad85fd17 (patch) | |
| tree | e3b1a4b83fb4db9e2dde3d3846966240e3b5e618 /git-codereview/sync.go | |
| parent | 99962d5770bded6aa8fac22d4c74103b5f832d6d (diff) | |
| download | go-x-review-31f678e5a420fecf567c30bc66e40b43ad85fd17.tar.xz | |
For the git-codereview commands to works on any Go's repositories, the
remote name for "origin" must be pointed to the Go original repository URL
(in most cases the one with go.googlesource.com domain).
The problem is when the "origin" is from the fork, all of the
git-codereview commands then will not works.
For example, in computer X, I clone the this repository from
"https://go.googlesource.com/review" (the origin) and then push my
changes on "git@git.sr.ht:~shulhan/go-x-review" (the fork).
In another computer Y, I then clone from the fork to continue my works.
The fork become origin.
One of the solution is to rename the remote names manually each time we
clone the fork.
The only cons using this solution is every time new branch created we need
to remember to pass "--set-upstream" during git push.
This changes introduce "remote" configuration in codereview.cfg.
When one first working on fork of Go repository, they set the "remote"
key, commit it, and push it.
When they cloned the fork, they did not needs to changes anything except
adding new remote using the pre-defined name.
Change-Id: I335d08fd8b7efe17ba07b3c0a3794f9ccf59b339
Diffstat (limited to 'git-codereview/sync.go')
| -rw-r--r-- | git-codereview/sync.go | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/git-codereview/sync.go b/git-codereview/sync.go index 71fc312..6ff36a3 100644 --- a/git-codereview/sync.go +++ b/git-codereview/sync.go @@ -60,10 +60,11 @@ func cmdSync(args []string) { // hint: use --reapply-cherry-picks to include skipped commits // hint: Disable this message with "git config advice.skippedCherryPicks false" // + cfgRemote := config()["remote"] if *verbose > 1 { - run("git", "-c", "advice.skippedCherryPicks=false", "pull", "-q", "-r", "-v", "origin", strings.TrimPrefix(b.OriginBranch(), "origin/")) + run("git", "-c", "advice.skippedCherryPicks=false", "pull", "-q", "-r", "-v", cfgRemote, strings.TrimPrefix(b.OriginBranch(), cfgRemote+"/")) } else { - run("git", "-c", "advice.skippedCherryPicks=false", "pull", "-q", "-r", "origin", strings.TrimPrefix(b.OriginBranch(), "origin/")) + run("git", "-c", "advice.skippedCherryPicks=false", "pull", "-q", "-r", cfgRemote, strings.TrimPrefix(b.OriginBranch(), cfgRemote+"/")) } b = CurrentBranch() // discard any cached information @@ -188,30 +189,32 @@ func cmdSyncBranch(args []string) { // Note that this does a remote fetch of b.OriginBranch() (aka branch). cmdSync(nil) + cfgRemote := config()["remote"] + // Pull down parent commits too. quiet := "-q" if *verbose > 0 { quiet = "-v" } - run("git", "fetch", quiet, "origin", "refs/heads/"+parent+":refs/remotes/origin/"+parent) + run("git", "fetch", quiet, cfgRemote, "refs/heads/"+parent+":refs/remotes/"+cfgRemote+"/"+parent) // Write the status file to make sure we can, before starting a merge. status := &syncBranchStatus{ Local: b.Name, Parent: parent, - ParentHash: gitHash("origin/" + parent), + ParentHash: gitHash(cfgRemote + "/" + parent), Branch: branch, - BranchHash: gitHash("origin/" + branch), + BranchHash: gitHash(cfgRemote + "/" + branch), } writeSyncBranchStatus(status) - parentHash, err := cmdOutputErr("git", "rev-parse", "origin/"+parent) + parentHash, err := cmdOutputErr("git", "rev-parse", cfgRemote+"/"+parent) if err != nil { - dief("cannot sync-branch: cannot resolve origin/%s: %v\n%s", parent, err, parentHash) + dief("cannot sync-branch: cannot resolve %s/%s: %v\n%s", cfgRemote, parent, err, parentHash) } - branchHash, err := cmdOutputErr("git", "rev-parse", "origin/"+branch) + branchHash, err := cmdOutputErr("git", "rev-parse", cfgRemote+"/"+branch) if err != nil { - dief("cannot sync-branch: cannot resolve origin/%s: %v\n%s", branch, err, branchHash) + dief("cannot sync-branch: cannot resolve %s/%s: %v\n%s", cfgRemote, branch, err, branchHash) } parentHash = trim(parentHash) branchHash = trim(branchHash) @@ -221,7 +224,7 @@ func cmdSyncBranch(args []string) { // to be done, it should be done first on the dev branch, // not the parent branch. if mergeBackToParent { - other := cmdOutput("git", "log", "--format=format:+ %cd %h %s", "--date=short", "origin/"+branch+"..origin/"+parent) + other := cmdOutput("git", "log", "--format=format:+ %cd %h %s", "--date=short", cfgRemote+"/"+branch+".."+cfgRemote+"/"+parent) if other != "" { dief("cannot sync-branch --merge-back-to-parent: parent has new commits.\n"+ "\trun 'git codereview sync-branch' to bring them into this branch first:\n%s", @@ -237,10 +240,10 @@ func cmdSyncBranch(args []string) { // of the merge, the same as it would when we are doing it by hand // with a plain "git merge". This may help the display of the // merge graph in some tools more closely reflect what we did. - run("git", "reset", "--hard", "origin/"+parent) - _, err = cmdOutputErr("git", "merge", "--no-ff", "origin/"+branch) + run("git", "reset", "--hard", cfgRemote+"/"+parent) + _, err = cmdOutputErr("git", "merge", "--no-ff", cfgRemote+"/"+branch) } else { - _, err = cmdOutputErr("git", "merge", "--no-ff", "origin/"+parent) + _, err = cmdOutputErr("git", "merge", "--no-ff", cfgRemote+"/"+parent) } // Resolve codereview.cfg the right way - never take it from the merge. @@ -322,10 +325,11 @@ const ( ) func syncBranchContinue(flag string, b *Branch, status *syncBranchStatus) { - if h := gitHash("origin/" + status.Parent); h != status.ParentHash { + cfgRemote := config()["remote"] + if h := gitHash(cfgRemote + "/" + status.Parent); h != status.ParentHash { dief("cannot sync-branch%s: parent hash changed: %.7s -> %.7s", flag, status.ParentHash, h) } - if h := gitHash("origin/" + status.Branch); h != status.BranchHash { + if h := gitHash(cfgRemote + "/" + status.Branch); h != status.BranchHash { dief("cannot sync-branch%s: branch hash changed: %.7s -> %.7s", flag, status.BranchHash, h) } if b.Name != status.Local { @@ -364,11 +368,11 @@ func syncBranchContinue(flag string, b *Branch, status *syncBranchStatus) { } mergeHead = trim(mergeHead) if mergeHead != srcHash { - dief("cannot sync-branch%s: MERGE_HEAD is %.7s, but origin/%s is %.7s", flag, mergeHead, src, srcHash) + dief("cannot sync-branch%s: MERGE_HEAD is %.7s, but %s/%s is %.7s", flag, mergeHead, cfgRemote, src, srcHash) } head := gitHash("HEAD") if head != dstHash { - dief("cannot sync-branch%s: HEAD is %.7s, but origin/%s is %.7s", flag, head, dst, dstHash) + dief("cannot sync-branch%s: HEAD is %.7s, but %s/%s is %.7s", flag, head, cfgRemote, dst, dstHash) } if HasUnstagedChanges() { @@ -387,7 +391,7 @@ func syncBranchContinue(flag string, b *Branch, status *syncBranchStatus) { // Merge must never sync codereview.cfg, // because it contains the src and dst config. // Force the on-dst copy back while amending the commit. - cmdOutputDir(repoRoot(), "git", "checkout", "origin/"+dst, "--", "codereview.cfg") + cmdOutputDir(repoRoot(), "git", "checkout", cfgRemote+"/"+dst, "--", "codereview.cfg") conflictMsg := "" if len(status.Conflicts) > 0 { |
