Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for remotes other than origin #340

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/av/commit_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/av/stack_adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/stack_reparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/av/stack_restack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/av/stack_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 5 additions & 6 deletions internal/actions/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var Av = struct {
GitHub GitHub
Aviator Aviator
AdditionalTrunkBranches []string
Remote string
}{
Aviator: Aviator{
APIHost: "https://api.aviator.co",
Expand All @@ -67,6 +68,7 @@ var Av = struct {
},
GitHub: GitHub{},
AdditionalTrunkBranches: []string{},
Remote: "",
}

// Load initializes the configuration values.
Expand Down
9 changes: 5 additions & 4 deletions internal/gh/ghui/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions internal/gh/ghui/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down
10 changes: 10 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,6 +110,14 @@ func (r *Repo) TrunkBranches() ([]string, error) {
return branches, nil
}

func (r *Repo) GetRemoteName() string {
if config.Av.Remote != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is better, setting it as the initial value (at here) or checking it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine.

return config.Av.Remote
}

return DEFAULT_REMOTE_NAME
}

func (r *Repo) Git(args ...string) (string, error) {
startTime := time.Now()
cmd := exec.Command("git", args...)
Expand Down
10 changes: 10 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")

}
2 changes: 1 addition & 1 deletion internal/git/gitui/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down