diff --git a/cmd/coco/commands/reconcile.go b/cmd/coco/commands/reconcile.go index eec1755..6af31e8 100644 --- a/cmd/coco/commands/reconcile.go +++ b/cmd/coco/commands/reconcile.go @@ -79,25 +79,27 @@ func newReconcile() *cobra.Command { }, } - c.PersistentFlags().StringVarP(&sourceBranch, "source", "s", "", "The souce branch to reconcile from.") + c.PersistentFlags().StringVarP(&sourceBranch, "source", "s", "", "The source branch to reconcile from.") failOnError(c.MarkPersistentFlagRequired("source"), "reconcile") - c.PersistentFlags().StringVar(&sourceRemote, "source-remote", "origin", "The remote for the source branch.") + c.PersistentFlags().StringVar(&sourceRemote, "source-remote", "origin", `The URL for the source branch. + Can be left out incase the source and target branches are in the same repository.`) c.PersistentFlags().StringVarP(&targetBranch, "target", "t", "", "The target branch to reconcile to.") failOnError(c.MarkPersistentFlagRequired("target"), "reconcile") - c.PersistentFlags().StringVar(&targetRemote, "target-remote", "origin", "The remote for the target branch.") + c.PersistentFlags().StringVar(&targetRemote, "target-remote", "origin", `The URL for the target branch. + Can be left out incase the source and target branches are in the same repository.`) - c.PersistentFlags().StringVar(&repositoryName, "repo", "", "The name of the gihtub repository.") + c.PersistentFlags().StringVar(&repositoryName, "repo", "", "The name of the TARGET github repository.") failOnError( c.PersistentFlags().MarkDeprecated("repo", `please use "repository" flag instead.`), "reconcile", ) - c.PersistentFlags().StringVar(&repositoryName, "repository", "", "The name of the gihtub repository.") + c.PersistentFlags().StringVar(&repositoryName, "repository", "", "The name of the TARGET github repository.") failOnError(c.MarkPersistentFlagRequired("repository"), "reconcile") - c.PersistentFlags().StringVar(&owner, "owner", "", "The account owner of the github repository.") + c.PersistentFlags().StringVar(&owner, "owner", "", "The account owner of the TARGET github repository.") failOnError(c.MarkPersistentFlagRequired("owner"), "reconcile") c.Flags().BoolVar( diff --git a/cmd/coco/reconcile/reconcile.go b/cmd/coco/reconcile/reconcile.go index 5337654..87c6500 100644 --- a/cmd/coco/reconcile/reconcile.go +++ b/cmd/coco/reconcile/reconcile.go @@ -61,6 +61,7 @@ func New( if err := differentRemotes(targetBranch, sourceBranch, token, logger); err != nil { return nil, err } + reconcileBranchName = fmt.Sprintf("reconcile/remote-replica/%s-%s", sourceBranch.Name, targetBranch.Name) } client, err := githubClient(ctx, token, owner, repo, githubBaseURL, isEnterprise) @@ -80,7 +81,8 @@ func New( func (r *Client) Reconcile(force bool) error { if r.target.Remote != r.source.Remote { - return nil + // change source.name to remote-replica/source.Name and continue + r.source.Name = fmt.Sprintf("remote-replica/%s", r.source.Name) } return r.merge(force) } @@ -140,7 +142,7 @@ func differentRemotes(targetBranch, sourceBranch BranchConfig, token string, log if err == git.NoErrAlreadyUpToDate { logger.Debugf("Target branch is already up to date") } else if err != nil { - return err + return fmt.Errorf("target pull failed: %w", err) } // 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 if err != nil { return err } + err = refs.ForEach(func(ref *plumbing.Reference) error { if ref.Name() == plumbing.ReferenceName(fmt.Sprintf("refs/remotes/%s/%s", remoteName, sourceBranch.Name)) { sourceRef = ref @@ -181,15 +184,20 @@ func copyBranch(targetRepo *git.Repository, sourceBranch BranchConfig, remoteNam return err } ref := plumbing.NewHashReference(replicaBranchName, sourceRef.Hash()) - if err := targetRepo.Storer.SetReference(ref); err != nil { + err = targetRepo.Storer.SetReference(ref) + if err != nil { return err } - return gitPush(targetRepo, &git.PushOptions{ + err = gitPush(targetRepo, &git.PushOptions{ RemoteName: "origin", Auth: &githttp.BasicAuth{Username: notUsed, Password: token}, Progress: os.Stdout, Force: true, }) + if err == git.NoErrAlreadyUpToDate { + return nil + } + return err } func (r *Client) merge(force bool) error {