Skip to content

Commit

Permalink
use upstream as a base to create branches (#359)
Browse files Browse the repository at this point in the history
Co-authored-by: Pedro Tashima <pedro.tashima@suse.com>
  • Loading branch information
tashima42 and tashima42 committed Feb 8, 2024
1 parent 884d62b commit 09bc29c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/backport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
cmd.Flags().StringVarP(&backportCmdOpts.User, "user", "u", "", "user to assign new issues to (default: user assigned to the original issue)")
cmd.Flags().StringVarP(&backportCmdOpts.Owner, "owner", "o", "", "owner of the repository, e.g: k3s-io, rancher")
cmd.Flags().BoolVarP(&backportCmdOpts.DryRun, "dry-run", "n", false, "skip creating issues and pushing changes to remote")
cmd.Flags().BoolVarP(&backportCmdOpts.DryRun, "skip-create-issue", "s", false, "skip creating issues")
cmd.Flags().BoolVarP(&backportCmdOpts.SkipCreateIssue, "skip-create-issue", "s", false, "skip creating issues")

if err := cmd.MarkFlagRequired("repo"); err != nil {
logrus.Fatal(err)
Expand Down
33 changes: 23 additions & 10 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-github/v39/github"
"github.com/rancher/ecm-distro-tools/exec"
Expand Down Expand Up @@ -272,20 +273,32 @@ func PerformBackport(ctx context.Context, client *github.Client, pbo *PerformBac
if err != nil {
return nil, err
}
upstreamRemoteURL := "https://github.com/" + pbo.Owner + "/" + pbo.Repo + ".git"
fmt.Println("creating remote: 'upstream " + upstreamRemoteURL + "'")
if _, err := r.CreateRemote(&config.RemoteConfig{
Name: "upstream",
URLs: []string{upstreamRemoteURL},
}); err != nil {
if err != git.ErrRemoteExists {
return nil, err
}
}
fmt.Println("fetching remote: upstream")
if err := r.Fetch(&git.FetchOptions{
RemoteName: "upstream",
Progress: os.Stdout,
Tags: git.AllTags,
}); err != nil {
if err != git.NoErrAlreadyUpToDate {
return nil, err
}
}
}

for _, branch := range pbo.Branches {
if cherryPick {
logrus.Info("fetching branch from origin: " + branch + ":" + branch)
fetchOut, err := exec.RunCommand(cwd, "git", "fetch", "origin", branch+":"+branch)
if err != nil {
return nil, err
}
logrus.Info(fetchOut)
coo := git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/heads/" + branch),
}
logrus.Info("checking out on reference refs/heads/" + branch)
coo := git.CheckoutOptions{Branch: plumbing.ReferenceName("refs/remotes/upstream/" + branch)}
logrus.Info("checking out on reference refs/remotes/upstream/" + branch)
logrus.Infof("checkout options: %+v", coo)
if err := w.Checkout(&coo); err != nil {
return nil, errors.New("failed checkout: " + err.Error())
Expand Down

0 comments on commit 09bc29c

Please sign in to comment.