From 5bc351ffe811eb370773d3b128ecca07b4822306 Mon Sep 17 00:00:00 2001 From: Konboi Date: Fri, 14 Jun 2024 14:28:41 +0900 Subject: [PATCH 1/3] add a new config value 'Remote' to be able to set another remote --- internal/config/config.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index 04663b31..dc539b33 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -58,6 +58,7 @@ var Av = struct { GitHub GitHub Aviator Aviator AdditionalTrunkBranches []string + Remote string }{ Aviator: Aviator{ APIHost: "https://api.aviator.co", @@ -67,6 +68,7 @@ var Av = struct { }, GitHub: GitHub{}, AdditionalTrunkBranches: []string{}, + Remote: "", } // Load initializes the configuration values. From cb45212f57b8e3fc75568135cb1d688205d0aef6 Mon Sep 17 00:00:00 2001 From: Konboi Date: Fri, 14 Jun 2024 14:28:56 +0900 Subject: [PATCH 2/3] add GetRemoteName method --- internal/git/git.go | 10 ++++++++++ internal/git/git_test.go | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/internal/git/git.go b/internal/git/git.go index 46b16879..282f2234 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -20,6 +20,8 @@ import ( var ErrRemoteNotFound = errors.Sentinel("this repository doesn't have a remote origin") +const DEFAULT_REMOTE_NAME = "origin" + type Repo struct { repoDir string gitDir string @@ -108,6 +110,14 @@ func (r *Repo) TrunkBranches() ([]string, error) { return branches, nil } +func (r *Repo) GetRemoteName() string { + if config.Av.Remote != "" { + return config.Av.Remote + } + + return DEFAULT_REMOTE_NAME +} + func (r *Repo) Git(args ...string) (string, error) { startTime := time.Now() cmd := exec.Command("git", args...) diff --git a/internal/git/git_test.go b/internal/git/git_test.go index fa1864fc..819bb45e 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/aviator-co/av/internal/config" + "github.com/aviator-co/av/internal/git" "github.com/aviator-co/av/internal/git/gittest" "github.com/stretchr/testify/require" ) @@ -35,3 +36,12 @@ func TestTrunkBranches(t *testing.T) { require.NoError(t, err) require.Equal(t, branches, []string{"main", "develop", "staging"}) } + +func TestGetRemoteName(t *testing.T) { + repo := gittest.NewTempRepo(t) + require.Equal(t, repo.AsAvGitRepo().GetRemoteName(), git.DEFAULT_REMOTE_NAME) + + config.Av.Remote = "new-remote" + require.Equal(t, repo.AsAvGitRepo().GetRemoteName(), "new-remote") + +} From 922eacbd0dab5aa896eae20e69039b670028b305 Mon Sep 17 00:00:00 2001 From: Konboi Date: Fri, 14 Jun 2024 14:33:28 +0900 Subject: [PATCH 3/3] use repo.GetRemoteName instead of 'origin' --- cmd/av/commit_common.go | 2 +- cmd/av/stack_adopt.go | 2 +- cmd/av/stack_reparent.go | 2 +- cmd/av/stack_restack.go | 2 +- cmd/av/stack_sync.go | 2 +- internal/actions/pr.go | 11 +++++------ internal/gh/ghui/fetch.go | 9 +++++---- internal/gh/ghui/push.go | 8 ++++---- internal/git/gitui/prune.go | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cmd/av/commit_common.go b/cmd/av/commit_common.go index 27e09067..2207c36d 100644 --- a/cmd/av/commit_common.go +++ b/cmd/av/commit_common.go @@ -128,6 +128,6 @@ func (vm *postCommitRestackViewModel) createState() (*sequencerui.RestackState, if len(ops) == 0 { return nil, nothingToRestackError } - state.Seq = sequencer.NewSequencer("origin", vm.db, ops) + state.Seq = sequencer.NewSequencer(vm.repo.GetRemoteName(), vm.db, ops) return &state, nil } diff --git a/cmd/av/stack_adopt.go b/cmd/av/stack_adopt.go index f1d1f3c1..da305676 100644 --- a/cmd/av/stack_adopt.go +++ b/cmd/av/stack_adopt.go @@ -197,7 +197,7 @@ func (vm stackAdoptViewModel) initCmd() tea.Msg { for _, branch := range trunkBranches { refs = append(refs, plumbing.NewBranchReferenceName(branch)) } - allBranches, err := treedetector.DetectBranchTree(vm.repo.GoGitRepo(), "origin", refs) + allBranches, err := treedetector.DetectBranchTree(vm.repo.GoGitRepo(), vm.repo.GetRemoteName(), refs) if err != nil { return err } diff --git a/cmd/av/stack_reparent.go b/cmd/av/stack_reparent.go index 47a733ae..d8dfd18e 100644 --- a/cmd/av/stack_reparent.go +++ b/cmd/av/stack_reparent.go @@ -181,7 +181,7 @@ func (vm *stackReparentViewModel) createState() (*sequencerui.RestackState, erro if len(ops) == 0 { return nil, nothingToRestackError } - state.Seq = sequencer.NewSequencer("origin", vm.db, ops) + state.Seq = sequencer.NewSequencer(vm.repo.GetRemoteName(), vm.db, ops) return &state, nil } diff --git a/cmd/av/stack_restack.go b/cmd/av/stack_restack.go index d67ef2d5..65b16ab8 100644 --- a/cmd/av/stack_restack.go +++ b/cmd/av/stack_restack.go @@ -200,7 +200,7 @@ func (vm *stackRestackViewModel) createState() (*sequencerui.RestackState, error if err != nil { return nil, err } - state.Seq = sequencer.NewSequencer("origin", vm.db, ops) + state.Seq = sequencer.NewSequencer(vm.repo.GetRemoteName(), vm.db, ops) return &state, nil } diff --git a/cmd/av/stack_sync.go b/cmd/av/stack_sync.go index 691ecdf9..4da0255e 100644 --- a/cmd/av/stack_sync.go +++ b/cmd/av/stack_sync.go @@ -504,7 +504,7 @@ func (vm *stackSyncViewModel) createState() (*savedStackSyncState, error) { if err != nil { return nil, err } - state.RestackState.Seq = sequencer.NewSequencer("origin", vm.db, ops) + state.RestackState.Seq = sequencer.NewSequencer(vm.repo.GetRemoteName(), vm.db, ops) return &state, nil } diff --git a/internal/actions/pr.go b/internal/actions/pr.go index 8130c5c6..33610599 100644 --- a/internal/actions/pr.go +++ b/internal/actions/pr.go @@ -197,19 +197,18 @@ func CreatePullRequest( pushFlags = append(pushFlags, "--force-with-lease") } - // NOTE: This assumes that the user use the default push strategy (simple). It would - // be rare to use the upstream strategy. - pushFlags = append(pushFlags, "origin", opts.BranchName) + remote := repo.GetRemoteName() + pushFlags = append(pushFlags, remote, opts.BranchName) logrus.Debug("pushing latest changes") _, _ = fmt.Fprint(os.Stderr, - " - pushing to ", color.CyanString("origin/%s", opts.BranchName), + " - pushing to ", color.CyanString("%s/%s", remote, opts.BranchName), "\n", ) if _, err := repo.Git(pushFlags...); err != nil { return nil, errors.WrapIf(err, "failed to push") } - if err := repo.BranchSetConfig(opts.BranchName, "av-pushed-remote", "origin"); err != nil { + if err := repo.BranchSetConfig(opts.BranchName, "av-pushed-remote", remote); err != nil { return nil, err } if err := repo.BranchSetConfig(opts.BranchName, "av-pushed-ref", fmt.Sprintf("refs/heads/%s", opts.BranchName)); err != nil { @@ -255,7 +254,7 @@ func CreatePullRequest( } } else { logrus.WithField("base", parentState.Name).Debug("base branch is a trunk branch") - prCompareRef = "origin/" + parentState.Name + prCompareRef = fmt.Sprintf("%s/%s", repo.GetRemoteName(), parentState.Name) } commitsList, err := repo.Git("rev-list", "--reverse", fmt.Sprintf("%s..%s", prCompareRef, opts.BranchName)) diff --git a/internal/gh/ghui/fetch.go b/internal/gh/ghui/fetch.go index 662218ed..fc8ea616 100644 --- a/internal/gh/ghui/fetch.go +++ b/internal/gh/ghui/fetch.go @@ -162,8 +162,9 @@ func (vm *GitHubFetchModel) View() string { } func (vm *GitHubFetchModel) runGitFetch() tea.Msg { - if _, err := vm.repo.Git("fetch", "origin"); err != nil { - return errors.Errorf("failed to fetch from origin: %v", err) + remote := vm.repo.GetRemoteName() + if _, err := vm.repo.Git("fetch", remote); err != nil { + return errors.Errorf("failed to fetch from %s: %v", remote, err) } return &GitHubFetchProgress{gitFetchIsDone: true} } @@ -199,9 +200,9 @@ func (vm *GitHubFetchModel) updateMergeCommitsFromCommitMessage() tea.Msg { } repo := vm.repo.GoGitRepo() - remote, err := repo.Remote("origin") + remote, err := repo.Remote(vm.repo.GetRemoteName()) if err != nil { - return errors.Errorf("failed to get remote origin: %v", err) + return errors.Errorf("failed to get remote %s: %v", vm.repo.GetRemoteName(), err) } remoteConfig := remote.Config() diff --git a/internal/gh/ghui/push.go b/internal/gh/ghui/push.go index 052ef114..fb017555 100644 --- a/internal/gh/ghui/push.go +++ b/internal/gh/ghui/push.go @@ -259,7 +259,7 @@ func (vm *GitHubPushModel) runUpdate() (ret tea.Msg) { } func (vm *GitHubPushModel) runGitPush() error { - pushArgs := []string{"push", "origin", "--atomic"} + pushArgs := []string{"push", vm.repo.GetRemoteName(), "--atomic"} for _, branch := range vm.pushCandidates { // Do a compare-and-swap to be strict on what we show as a difference. pushArgs = append(pushArgs, @@ -283,7 +283,7 @@ func (vm *GitHubPushModel) runGitPush() error { } for _, branch := range vm.pushCandidates { - if err := vm.repo.BranchSetConfig(branch.branch.Short(), "av-pushed-remote", "origin"); err != nil { + if err := vm.repo.BranchSetConfig(branch.branch.Short(), "av-pushed-remote", vm.repo.GetRemoteName()); err != nil { return err } if err := vm.repo.BranchSetConfig(branch.branch.Short(), "av-pushed-ref", branch.branch.String()); err != nil { @@ -370,9 +370,9 @@ func (vm *GitHubPushModel) undraftPRs(ghPRs map[plumbing.ReferenceName]*gh.PullR func (vm *GitHubPushModel) calculateChangedBranches() tea.Msg { repo := vm.repo.GoGitRepo() - remote, err := repo.Remote("origin") + remote, err := repo.Remote(vm.repo.GetRemoteName()) if err != nil { - return errors.Errorf("failed to get remote origin: %v", err) + return errors.Errorf("failed to get remote %s: %v", vm.repo.GetRemoteName(), err) } remoteConfig := remote.Config() diff --git a/internal/git/gitui/prune.go b/internal/git/gitui/prune.go index 08b2c8c5..b982b524 100644 --- a/internal/git/gitui/prune.go +++ b/internal/git/gitui/prune.go @@ -240,7 +240,7 @@ func (vm *PruneBranchModel) runDelete() tea.Msg { } func (vm *PruneBranchModel) calculateMergedBranches() tea.Msg { - remoteBranches, err := vm.repo.LsRemote("origin") + remoteBranches, err := vm.repo.LsRemote(vm.repo.GetRemoteName()) if err != nil { return err }