Repository navigation
Push all branches of a rebased branch stack at once - #6045
Conversation
|
@ashishsinghbora Please stop posting AI generated code reviews in this repo, this is not appreciated, and not helpful. |
8284d79 to
99c5edf
Compare
To push a branch other than the checked-out one, lazygit has to name the remote and the remote branch in the push command; a bare `git push` only works for the current branch. The upstream isn't always the right destination. In a triangular workflow the branch is pushed to a different remote than it pulls from, and with push.default set to "current" it goes to a branch of the same name whatever the upstream is called. Read the destination from git instead of working it out from the config. The %(push) field of for-each-ref names the remote-tracking ref that a push would update, taking push.default, remote.pushDefault and branch.<name>.pushRemote into account. It is also the ref that the push:track counts are computed against, so the force-push detection and the destination agree. The field is available since git 2.5, well before the oldest version lazygit supports. Nothing uses the new fields yet. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
PushOpts can only describe a push of the current branch. It takes the branch's name and the upstream to push it to, and builds a single refspec from them. Pushing several branches in one command needs one refspec per branch, so let callers pass the refspecs themselves. The sync controller, the only caller so far, builds the same single refspec that PushCmdObj built before. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
lazygit will offer to push these along with the current branch, so that a rebased stack can be pushed in one go. The commits panel already marks the tips of stacked branches, and this uses the same criterion. A branch is below the given one if its tip is one of the loaded commits of that branch that isn't merged into a main branch. This needs no git command. In whole-graph mode the commit list also contains commits of other branches, but those are marked as merged, so they don't count. Not used yet. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ushing Pushing the current branch has three cases: it has an upstream, it has none but push.default is "current", or the user is prompted for one. Each case ends in a push, and the first one also checks whether a force push is needed. Let the three cases call a common callback instead, and put the check and the push there. This makes it possible to run the same three cases with a different callback, so that the branches stacked below the current one can be pushed along with it. The check returns false for a branch without an upstream, so the other two cases behave as before. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
After rebasing a stack of branches, every branch of the stack has to be force-pushed, and so far the only way to do that in lazygit was to check out each branch in turn and push it. For a stack of a dozen branches that is a lot of work for a routine task. When the current branch is pushed and there are branches below it in the stack that have commits to push, show a menu that offers to push them along with it. The menu lists the branches and how far each of them has diverged from its remote branch, so that the user can see what is about to happen. It doesn't show where each branch goes; the branches panel doesn't show that for a normal push either. Pushing only the current branch is the second entry. If any of the branches has diverged from its remote branch, a single confirmation covers force-pushing all of them. A branch is offered if its tip is a commit of the current branch that isn't merged yet, it has an upstream that is stored locally, that upstream is not gone, and it is ahead of its push destination. Branches without an upstream are left out because a branch that was never pushed can't be told apart from one that is meant to stay local. A branch that is only behind is left out because pushing it would move the remote branch back to an older commit; the lease doesn't catch that when the remote-tracking branch is up to date. Each branch is pushed to where `git push` would push it if it were checked out, using the destination git reports in the %(push) field. This honors push.default, remote.pushDefault and branch.<name>.pushRemote without lazygit having to interpret them. Branches going to the same remote are pushed in one command, with --force-with-lease when the user confirmed force-pushing; the lease checks each ref against its remote-tracking branch, so a coworker's unseen push is still rejected. The current branch joins that command when its remote-tracking branch is stored locally. Otherwise it needs a plain push first, for example with --set-upstream, and that option would apply to every refspec of a combined command; so in that case it is pushed on its own as before, and the other branches follow in a second command. Pushing the current branch on its own still runs a bare `git push`, so users who rely on push.default or remote.<name>.push for it see no change. The push is non-atomic, as git defaults to; if one branch's lease fails, the others still go through and the error names the one to look at. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
WrapViewLinesToWidth counted the characters of ANSI escape sequences as if they were visible, so colored text wrapped earlier than the view does, and the number of lines it reported was too high. In a narrow terminal the divergence in the menu that offers to push a stack of branches, colored yellow, ended up on a line of its own even though it fitted. The tooltip of a disabled menu item, whose prefix is red, gets its height from the same function. Skip escape sequences when measuring the width, and never break a line inside one. gocui wraps parsed cells and never sees escape sequences, so this brings the two in line; the test's parity check now compares the wrapped lines with the escape sequences stripped. Only CSI sequences are recognized. Those are the color and style codes that lazygit puts into view content; hyperlinks (OSC 8) only occur in the main view, and gocui wraps that one itself. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
99c5edf to
149c5f8
Compare
|
I ran into this recently after upgrading my lazygit. Is there some way to configure or disable this feature? Suppose I stacked branch B on top of branch A. Suppose in the meantime I've pushed some new changes to A from some other machine. Now whenever I want to push to B, lazygit helpfully offers to push A as well. If I haven't updated/pulled A since it was changed upstream, this would unhelpfully overwrite the new changes on remote. It doesn't look like there's an easy way to opt-out of this. There's no keybindings mapped to the Push window to quickly select the desired option. The default selection is the potentially destructuve "Push all these branches" |
Not if you have set the git config I don't see how this is related to pushing a stack of branches though; the same could happen if you push a single branch that someone else pushed commits to.
That's true, I could have added those; personally I'm happy pressing |
|
Thanks for getting back to me so fast! Would you be open to a small PR adding configuration and keybindings? #6173 |
No, I don't accept PRs here, see CONTRIBUTING.md. I'll consider adding the menu shortcuts, but I'm reluctant to add a user config unless more people ask for it. See also #6144 for related discussion. |
After rebasing a stack of branches, every branch of the stack has to be force-pushed, and so far the only way to do that in lazygit was to check out each branch in turn and push it. For a stack of a dozen branches that is a lot of work for a routine task.
When the current branch is pushed and there are branches below it in the stack that have commits to push, show a menu that offers to push them along with it. The menu lists the branches and how far each of them has diverged from its remote branch, so that the user can see what is about to happen. It doesn't show where each branch goes (i.e. what its upstream branch's name is or on which remote it is stored); the branches panel doesn't show that for a normal push either. If any of the branches has diverged from its remote branch, a single confirmation covers force-pushing all of them (listing exactly the ones that need force-pushing).
This only covers branches that already have an upstream configured, because a branch that was never pushed yet
can't be told apart from one that is meant to stay local. This means that the very first time you want to push a newly created stack, you still need to do it manually the old way. We can see if we want to improve this somehow in the future, but repeatedly pushing a rebased stack is the more frequent operation, and that's what we improve here.
Pushing the current branch on its own still runs a bare
git push, so users who rely onpush.defaultorremote.<name>.pushfor it see no change. The push is non-atomic, as git defaults to; if one branch's lease fails, the others still go through and the error names the one to look at.Some repositories are configured to reject pushes of more than x branches at once; for those, the command fails with an error. We could be smarter about this, detect the error, and fall back to pushing each branch one by one, but I first want to see how many reports we get about this before investing in the extra logic.