Skip to content

Commit

Permalink
Push refs instead of creating local branches
Browse files Browse the repository at this point in the history
Fixes: #87

commit-id:5b5cbd54
  • Loading branch information
ejoffe committed Jul 17, 2021
1 parent 11dec28 commit cf322ce
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
13 changes: 4 additions & 9 deletions git/mockgit/mockgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,12 @@ func (m *mock) ExpectLogAndRespond(commits []*git.Commit) {
func (m *mock) ExpectPushCommits(commits []*git.Commit) {
m.expect("git status --porcelain --untracked-files=no").respond(nil)

var branchNames []string
var refNames []string
for _, c := range commits {
m.expect("git checkout %s", c.CommitHash)
m.expect("git switch -C pr/TestSPR/master/%s", c.CommitID)
m.expect("git switch master")
branchNames = append(branchNames, "pr/TestSPR/master/"+c.CommitID)
}
m.expect("git push --force --atomic origin " + strings.Join(branchNames, " "))
for _, c := range commits {
m.expect("git branch -D pr/TestSPR/master/%s", c.CommitID)
branchName := "pr/TestSPR/master/" + c.CommitID
refNames = append(refNames, c.CommitHash+":refs/heads/"+branchName)
}
m.expect("git push --force --atomic origin " + strings.Join(refNames, " "))
}

func (m *mock) expect(cmd string, args ...interface{}) *mock {
Expand Down
26 changes: 5 additions & 21 deletions spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,34 +399,18 @@ func (sd *stackediff) syncCommitStackToGitHub(ctx context.Context,
}
}

var branchNames []string
var refNames []string
for _, commit := range updatedCommits {
branch := sd.branchNameFromCommit(info, commit)
branchNames = append(branchNames, branch)
sd.mustgit("checkout "+commit.CommitHash, nil)
sd.mustgit("switch -C "+branch, nil)
sd.mustgit("switch "+info.LocalBranch, nil)
sd.profiletimer.Step("SyncCommitStack::CreateBranch::" + branch)
branchName := sd.branchNameFromCommit(info, commit)
refNames = append(refNames,
commit.CommitHash+":refs/heads/"+branchName)
}
if len(updatedCommits) > 0 {
sd.mustgit("push --force --atomic origin "+strings.Join(branchNames, " "), nil)
}
for _, commit := range updatedCommits {
branch := sd.branchNameFromCommit(info, commit)
sd.mustgit("branch -D "+branch, nil)
sd.mustgit("push --force --atomic origin "+strings.Join(refNames, " "), nil)
}
sd.profiletimer.Step("SyncCommitStack::PushBranches")
}

func (sd *stackediff) pushCommitToRemote(commit git.Commit, info *github.GitHubInfo) {
headRefName := sd.branchNameFromCommit(info, commit)
sd.mustgit("checkout "+commit.CommitHash, nil)
sd.mustgit("switch -C "+headRefName, nil)
sd.mustgit("push --force --set-upstream origin "+headRefName, nil)
sd.mustgit("switch "+info.LocalBranch, nil)
sd.mustgit("branch -D "+headRefName, nil)
}

func (sd *stackediff) branchNameFromCommit(info *github.GitHubInfo, commit git.Commit) string {
return "pr/" + info.UserName + "/" + info.LocalBranch + "/" + commit.CommitID
}
Expand Down

0 comments on commit cf322ce

Please sign in to comment.