-
Notifications
You must be signed in to change notification settings - Fork 37
feat(sync): add opt-in fast-forward of trunk branch after sync #686
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
Open
draftcode
wants to merge
1
commit into
master
Choose a base branch
from
ff_trunk_on_sync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package gitui | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/aviator-co/av/internal/git" | ||
| "github.com/aviator-co/av/internal/utils/colors" | ||
| tea "github.com/charmbracelet/bubbletea" | ||
| ) | ||
|
|
||
| // FastForwardTrunkModel is a Bubbletea model that fast-forward merges the local | ||
| // trunk branch (e.g., main/master) to match its remote tracking branch. | ||
| type FastForwardTrunkModel struct { | ||
| repo *git.Repo | ||
| onDone func() tea.Cmd | ||
|
|
||
| done bool | ||
| skipped bool | ||
| diverged bool | ||
| trunk string | ||
| } | ||
|
|
||
| type ffTrunkDone struct{} | ||
|
|
||
| func NewFastForwardTrunkModel( | ||
| repo *git.Repo, | ||
| onDone func() tea.Cmd, | ||
| ) *FastForwardTrunkModel { | ||
| return &FastForwardTrunkModel{ | ||
| repo: repo, | ||
| onDone: onDone, | ||
| } | ||
| } | ||
|
|
||
| func (m *FastForwardTrunkModel) Init() tea.Cmd { | ||
| return m.run | ||
| } | ||
|
|
||
| func (m *FastForwardTrunkModel) run() tea.Msg { | ||
| ctx := context.Background() | ||
| m.trunk = m.repo.DefaultBranch() | ||
| remote := m.repo.GetRemoteName() | ||
|
|
||
| // Check if the local trunk branch exists. | ||
| exists, err := m.repo.DoesBranchExist(ctx, m.trunk) | ||
| if err != nil || !exists { | ||
| m.skipped = true | ||
| return ffTrunkDone{} | ||
| } | ||
|
|
||
| // Check if the remote tracking branch exists. | ||
| remoteExists, err := m.repo.DoesRemoteBranchExist(ctx, m.trunk) | ||
| if err != nil || !remoteExists { | ||
| m.skipped = true | ||
| return ffTrunkDone{} | ||
| } | ||
|
|
||
| // Check if we are currently on the trunk branch. If so, we need to use | ||
| // "git merge --ff-only" directly. Otherwise, we can use update-ref to | ||
| // update the local ref without checking it out. | ||
| currentBranch, err := m.repo.CurrentBranchName() | ||
| if err != nil { | ||
| // Detached HEAD or other issue; just try the update-ref approach. | ||
| currentBranch = "" | ||
| } | ||
|
|
||
| remoteRef := fmt.Sprintf("%s/%s", remote, m.trunk) | ||
|
|
||
| if currentBranch == m.trunk { | ||
| // We're on the trunk branch, use merge --ff-only. | ||
| _, err := m.repo.Run(ctx, &git.RunOpts{ | ||
| Args: []string{"merge", "--ff-only", remoteRef}, | ||
| ExitError: true, | ||
| }) | ||
| if err != nil { | ||
| m.diverged = true | ||
| return ffTrunkDone{} | ||
| } | ||
| } else { | ||
| // Not on the trunk branch. Verify that the remote is a fast-forward | ||
| // of the local branch using merge-base --is-ancestor, then update the | ||
| // ref. | ||
| _, err := m.repo.Run(ctx, &git.RunOpts{ | ||
| Args: []string{"merge-base", "--is-ancestor", fmt.Sprintf("refs/heads/%s", m.trunk), fmt.Sprintf("refs/remotes/%s/%s", remote, m.trunk)}, | ||
| ExitError: true, | ||
| }) | ||
| if err != nil { | ||
| m.diverged = true | ||
| return ffTrunkDone{} | ||
| } | ||
| _, err = m.repo.Run(ctx, &git.RunOpts{ | ||
| Args: []string{"update-ref", fmt.Sprintf("refs/heads/%s", m.trunk), fmt.Sprintf("refs/remotes/%s/%s", remote, m.trunk)}, | ||
| ExitError: true, | ||
| }) | ||
| if err != nil { | ||
| m.diverged = true | ||
| return ffTrunkDone{} | ||
| } | ||
| } | ||
|
|
||
| m.done = true | ||
| return ffTrunkDone{} | ||
| } | ||
|
|
||
| func (m *FastForwardTrunkModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| switch msg.(type) { | ||
| case ffTrunkDone: | ||
| return m, m.onDone() | ||
| } | ||
| return m, nil | ||
| } | ||
|
|
||
| func (m *FastForwardTrunkModel) View() string { | ||
| if m.skipped { | ||
| return "" | ||
| } | ||
| if m.diverged { | ||
| return colors.ProgressStyle.Render(fmt.Sprintf(" Could not fast-forward %s (local branch has diverged from remote)", m.trunk)) + "\n" | ||
| } | ||
| if m.done { | ||
| return colors.SuccessStyle.Render(fmt.Sprintf("✓ Fast-forwarded %s to match remote", m.trunk)) + "\n" | ||
| } | ||
| return "" | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling in this model can be improved to provide more accurate feedback to the user. Currently:
DoesBranchExistandDoesRemoteBranchExistare silently treated as a 'skipped' case. This can hide underlying problems (e.g., file permission issues) from the user.git update-refcommand is categorized as a 'diverged' branch. However,update-refcan fail for reasons other than divergence (e.g., the ref is locked). This can lead to misleading error messages.I suggest introducing a new state to handle unexpected/internal errors, and updating the logic to distinguish between 'skipped', 'diverged', and 'error' states correctly. This will make the UI more robust and less confusing.