Skip to content

Commit 49305e6

Browse files
fix(reconcile): made cross repo more robust (#47)
* fix: made cross repo more robust * fix: fixed command tooltip * fix: fixed linting error * fix: fixed linting error * fix: fixed linting error * fix: updated tooltip --------- Co-authored-by: Reitwiec Shandilya <reitwiec.shandilya@sap.com>
1 parent 2b161f9 commit 49305e6

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

cmd/coco/commands/reconcile.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,27 @@ func newReconcile() *cobra.Command {
7979
},
8080
}
8181

82-
c.PersistentFlags().StringVarP(&sourceBranch, "source", "s", "", "The souce branch to reconcile from.")
82+
c.PersistentFlags().StringVarP(&sourceBranch, "source", "s", "", "The source branch to reconcile from.")
8383
failOnError(c.MarkPersistentFlagRequired("source"), "reconcile")
8484

85-
c.PersistentFlags().StringVar(&sourceRemote, "source-remote", "origin", "The remote for the source branch.")
85+
c.PersistentFlags().StringVar(&sourceRemote, "source-remote", "origin", `The URL for the source branch.
86+
Can be left out incase the source and target branches are in the same repository.`)
8687

8788
c.PersistentFlags().StringVarP(&targetBranch, "target", "t", "", "The target branch to reconcile to.")
8889
failOnError(c.MarkPersistentFlagRequired("target"), "reconcile")
8990

90-
c.PersistentFlags().StringVar(&targetRemote, "target-remote", "origin", "The remote for the target branch.")
91+
c.PersistentFlags().StringVar(&targetRemote, "target-remote", "origin", `The URL for the target branch.
92+
Can be left out incase the source and target branches are in the same repository.`)
9193

92-
c.PersistentFlags().StringVar(&repositoryName, "repo", "", "The name of the gihtub repository.")
94+
c.PersistentFlags().StringVar(&repositoryName, "repo", "", "The name of the TARGET github repository.")
9395
failOnError(
9496
c.PersistentFlags().MarkDeprecated("repo", `please use "repository" flag instead.`),
9597
"reconcile",
9698
)
97-
c.PersistentFlags().StringVar(&repositoryName, "repository", "", "The name of the gihtub repository.")
99+
c.PersistentFlags().StringVar(&repositoryName, "repository", "", "The name of the TARGET github repository.")
98100
failOnError(c.MarkPersistentFlagRequired("repository"), "reconcile")
99101

100-
c.PersistentFlags().StringVar(&owner, "owner", "", "The account owner of the github repository.")
102+
c.PersistentFlags().StringVar(&owner, "owner", "", "The account owner of the TARGET github repository.")
101103
failOnError(c.MarkPersistentFlagRequired("owner"), "reconcile")
102104

103105
c.Flags().BoolVar(

cmd/coco/reconcile/reconcile.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func New(
6161
if err := differentRemotes(targetBranch, sourceBranch, token, logger); err != nil {
6262
return nil, err
6363
}
64+
reconcileBranchName = fmt.Sprintf("reconcile/remote-replica/%s-%s", sourceBranch.Name, targetBranch.Name)
6465
}
6566

6667
client, err := githubClient(ctx, token, owner, repo, githubBaseURL, isEnterprise)
@@ -80,7 +81,8 @@ func New(
8081

8182
func (r *Client) Reconcile(force bool) error {
8283
if r.target.Remote != r.source.Remote {
83-
return nil
84+
// change source.name to remote-replica/source.Name and continue
85+
r.source.Name = fmt.Sprintf("remote-replica/%s", r.source.Name)
8486
}
8587
return r.merge(force)
8688
}
@@ -140,7 +142,7 @@ func differentRemotes(targetBranch, sourceBranch BranchConfig, token string, log
140142
if err == git.NoErrAlreadyUpToDate {
141143
logger.Debugf("Target branch is already up to date")
142144
} else if err != nil {
143-
return err
145+
return fmt.Errorf("target pull failed: %w", err)
144146
}
145147

146148
// If the remotes are different, add the remote repository of the 'source' branch
@@ -171,6 +173,7 @@ func copyBranch(targetRepo *git.Repository, sourceBranch BranchConfig, remoteNam
171173
if err != nil {
172174
return err
173175
}
176+
174177
err = refs.ForEach(func(ref *plumbing.Reference) error {
175178
if ref.Name() == plumbing.ReferenceName(fmt.Sprintf("refs/remotes/%s/%s", remoteName, sourceBranch.Name)) {
176179
sourceRef = ref
@@ -181,15 +184,20 @@ func copyBranch(targetRepo *git.Repository, sourceBranch BranchConfig, remoteNam
181184
return err
182185
}
183186
ref := plumbing.NewHashReference(replicaBranchName, sourceRef.Hash())
184-
if err := targetRepo.Storer.SetReference(ref); err != nil {
187+
err = targetRepo.Storer.SetReference(ref)
188+
if err != nil {
185189
return err
186190
}
187-
return gitPush(targetRepo, &git.PushOptions{
191+
err = gitPush(targetRepo, &git.PushOptions{
188192
RemoteName: "origin",
189193
Auth: &githttp.BasicAuth{Username: notUsed, Password: token},
190194
Progress: os.Stdout,
191195
Force: true,
192196
})
197+
if err == git.NoErrAlreadyUpToDate {
198+
return nil
199+
}
200+
return err
193201
}
194202

195203
func (r *Client) merge(force bool) error {

0 commit comments

Comments
 (0)