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

Base branch: Safely handle divergence #4899

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

estib-vega
Copy link
Contributor

@estib-vega estib-vega commented Sep 12, 2024

☕️ Reasoning

The application doesn't display any kind of information related to base branch divergence.
It should detect this, raise this to the user and present alternative actions to address this.

Scenario

  1. I open a repository on GitButler.
  2. A team-mate of mine makes some changes, and mistakenly pushes a commit with sensitive information.
  3. I merge the changes into the local base branch of GitButler. I can see the mistaken commit.
  4. They decide to force-push the commit away.
  5. I reload the application, but the state is the same as in step 3.

Expected behavior would be to have some information about this and the ability to restore the application to the latest version of the target upstream.

🧢 Changes

  1. Detect whether the base-branch has diverged with its upstream.
  2. Display the information to the user in a way that makes it clear if and which changes will be lost, if any action is taken

Visuals

Local is ahead of upstream

In this state, gitbutler/workspace is ahead of the target upstream.
The information is presented to the user as a heads-up only, as they might want to push the changes,
If they want to reset their local target branch to the current state of the target upstream, they will be prompted with a confirmation modal.

Screenshot 2024-09-13 at 16 57 45

Local has changes ahead and behind upstream

Similar to the previous state, there are some local commits that are not in the remote, but also some remote changes that are not in the local branch.
The information is displayed as a warning.

The options are to hard reset to the fork-point, or force-push.
Both buttons trigger confirmation dialogues.

Screenshot 2024-09-17 at 14 20 44

Confirmation dialogue when resetting to the fork-point

Screenshot 2024-09-17 at 14 21 01

Confirmation dialogue when force-pushing the local changes to the remote

Screenshot 2024-09-13 at 16 02 10

Copy link

vercel bot commented Sep 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
gitbutler-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 18, 2024 8:23am

@github-actions github-actions bot added rust Pull requests that update Rust code @gitbutler/desktop labels Sep 12, 2024
@estib-vega estib-vega force-pushed the base-branch-improvements branch 2 times, most recently from ba3329e to 901e225 Compare September 13, 2024 08:10
@estib-vega estib-vega changed the title base-branch-improvements Base branch: Safely handle divergence Sep 13, 2024
Determine whether the local target has diverged from the remote,
and return some information about that
If the local target has diverged from the remote target, display that to the user as a warning
In order to make it a bit clearer what the divergence state of the base branch is. reuse the line graphs to display it
If needed, the base branch can be pushed or force pushed
Only display the warnings and confirmation modals when actual changes can get lost (resting to local or remote).
Otherwise, display less scary notifications and let the user push if needed
@krlvi
Copy link
Member

krlvi commented Sep 16, 2024

Lets separate out the update of base branch (because we can in the future show exactly what is gonna happen to branches)

If the base branch has diverged from the upstream, reset the local base branch to the fork-point.
This clears the way for merging the upstream changes later on.
The two available options to resolve base branch divergence are now:
- Push (with force if needed) your local changes to the upstream
- Reset your local base branch to the fork-point

That way, the user is able to update their workspace as usual
Comment on lines +669 to +691
pub(crate) fn realign_base_branch(
ctx: &CommandContext,
perm: &mut WorktreeWritePermission,
) -> anyhow::Result<Vec<ReferenceName>> {
ctx.assure_resolved()?;

let target = default_target(&ctx.project().gb_dir())?;
let base = target_to_base_branch(ctx, &target)?;

if !base.diverged {
return Ok(Vec::new());
}

let target_commit = ctx
.repository()
.find_commit(
base.diverged_fork_point
.context("failed to find fork point")?,
)
.context("failed to find target commit")?;

set_base_branch_to_commit(ctx, target_commit, perm)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the difference between update_base_branch and realign_base_branch? Can they be made the same function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

update_base_branch:
This resets the local base branch to whatever the tip of its upstream is.

realign_base_branch:
This resets the local base branch to the fork-point it has to its upstream.
This drops any ahead changes in the local base branch.
So it's "aligned" in the sense that it no longer diverts from upstream.

Comment on lines +93 to +105
async realignBaseBranch(): Promise<string | undefined> {
this.loading.set(true);
try {
const stashedConflicting = await invoke<string[]>('realign_base_branch', {
projectId: this.projectId
});
await this.fetchFromRemotes();
return getStashedConflictingMessage(stashedConflicting);
} catch (err: any) {
showError('Failed to realign base branch', err);
console.error(err);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we even call this from the frontend? Doing a quick text search I'm not seeing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's a large diff that wasn't rendered. At least for me


async function realignBaseBranch() {
baseBranchIsUpdating = true;
const infoText = await baseBranchService.realignBaseBranch();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Caleb-T-Owens here :)

@estib-vega
Copy link
Contributor Author

FYI: This PR needs some rework around the way that we want the resolution to work.
We decided to use the new update with rebase/merge UI in order to deal with this kind of situations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@gitbutler/desktop @gitbutler/ui rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants