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

stuffs #5836

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

stuffs #5836

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 apps/desktop/src/lib/baseBranch/baseBranchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export async function getRemoteBranches(
projectId: string | undefined
): Promise<RemoteBranchInfo[]> {
if (!projectId) return [];
return await invoke<Array<string>>('git_remote_branches', { projectId }).then((branches) =>
return await invoke<string[]>('git_remote_branches', { projectId }).then((branches) =>
branches
.map((name) => name.substring(13))
.sort((a, b) => a.localeCompare(b))
Expand Down
11 changes: 0 additions & 11 deletions crates/gitbutler-branch-actions/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,6 @@ pub fn save_and_unapply_virutal_branch(
result
}

pub fn push_virtual_branch(
project: &Project,
stack_id: StackId,
with_force: bool,
askpass: Option<Option<StackId>>,
) -> Result<vbranch::PushResult> {
let ctx = open_with_verify(project)?;
assure_open_workspace_mode(&ctx).context("Pushing a branch requires open workspace mode")?;
vbranch::push(&ctx, stack_id, with_force, askpass)
}

pub fn find_git_branches(project: Project, branch_name: &str) -> Result<Vec<RemoteBranchData>> {
let ctx = CommandContext::open(&project)?;
remote::find_git_branches(&ctx, branch_name)
Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-branch-actions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use actions::{
find_git_branches, get_base_branch_data, get_uncommited_files, get_uncommited_files_reusable,
insert_blank_commit, integrate_upstream, integrate_upstream_commits, list_commit_files,
list_virtual_branches, list_virtual_branches_cached, move_commit, move_commit_file,
push_base_branch, push_virtual_branch, reorder_stack, reset_files, reset_virtual_branch,
push_base_branch, reorder_stack, reset_files, reset_virtual_branch,
resolve_upstream_integration, save_and_unapply_virutal_branch, set_base_branch,
set_target_push_remote, squash, unapply_lines, unapply_ownership,
unapply_without_saving_virtual_branch, undo_commit, update_branch_order, update_commit_message,
Expand Down
91 changes: 9 additions & 82 deletions crates/gitbutler-branch-actions/src/virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use crate::{
file::VirtualBranchFile,
hunk::VirtualBranchHunk,
integration::get_workspace_head,
remote::branch_to_remote_branch,
stack::stack_series,
status::{get_applied_status, get_applied_status_cached},
Get, RemoteBranchData, VirtualBranchHunkRange, VirtualBranchHunkRangeMap, VirtualBranchesExt,
Get, VirtualBranchHunkRange, VirtualBranchHunkRangeMap, VirtualBranchesExt,
};
use anyhow::{anyhow, bail, Context, Result};
use bstr::{BString, ByteSlice};
use git2_hooks::HookResult;
use gitbutler_branch::dedup;
use gitbutler_branch::BranchUpdateRequest;
use gitbutler_branch::{dedup, dedup_fmt};
use gitbutler_cherry_pick::RepositoryExt as _;
use gitbutler_command_context::CommandContext;
use gitbutler_commit::{commit_ext::CommitExt, commit_headers::HasCommitHeaders};
Expand Down Expand Up @@ -62,8 +61,6 @@ pub struct VirtualBranch {
pub requires_force: bool, // does this branch require a force push to the upstream?
pub conflicted: bool, // is this branch currently in a conflicted state (only for the workspace)
pub order: usize, // the order in which this branch should be displayed in the UI
pub upstream: Option<RemoteBranchData>, // the upstream branch where this branch pushes to, if any
pub upstream_name: Option<String>, // the upstream branch where this branch will push to on next push
pub base_current: bool, // is this vbranch based on the current base branch? if false, this needs to be manually merged with conflicts
/// The hunks (as `[(file, [hunks])]`) which are uncommitted but assigned to this branch.
/// This makes them committable.
Expand Down Expand Up @@ -350,11 +347,6 @@ pub fn list_virtual_branches_cached(
for (mut branch, mut files) in status.branches {
update_conflict_markers(ctx, files.clone())?;

let upstream_branch = match &branch.upstream {
Some(upstream) => repo.maybe_find_branch_by_refname(&Refname::from(upstream))?,
None => None,
};

// find all commits on head that are not on target.sha
let commits = repo.log(branch.head(), LogUntil::Commit(default_target.sha), false)?;
let mut check_commit =
Expand All @@ -370,12 +362,6 @@ pub fn list_virtual_branches_cached(
let merge_base = gix_to_git2_oid(merge_base);
let base_current = true;

let raw_remotes = repo.remotes()?;
let remotes: Vec<_> = raw_remotes.into_iter().flatten().collect();
let upstream = upstream_branch
.map(|upstream_branch| branch_to_remote_branch(ctx, &upstream_branch, &remotes))
.transpose()?;

let path_claim_positions: HashMap<&PathBuf, usize> = branch
.ownership
.claims
Expand Down Expand Up @@ -429,10 +415,6 @@ pub fn list_virtual_branches_cached(
files,
order: branch.order,
requires_force,
upstream,
upstream_name: branch
.upstream
.and_then(|r| Refname::from(r).branch().map(Into::into)),
conflicted: conflicts::is_resolving(ctx),
base_current,
ownership: branch.ownership,
Expand Down Expand Up @@ -859,64 +841,6 @@ pub fn commit(
Ok(commit_oid)
}

pub(crate) fn push(
ctx: &CommandContext,
stack_id: StackId,
with_force: bool,
askpass: Option<Option<StackId>>,
) -> Result<PushResult> {
let vb_state = ctx.project().virtual_branches();

let default_target = vb_state.get_default_target()?;
let upstream_remote = match default_target.push_remote_name {
Some(remote) => remote.clone(),
None => default_target.branch.remote().to_owned(),
};

let mut stack = vb_state.get_stack_in_workspace(stack_id)?;
let remote_branch = if let Some(upstream_branch) = &stack.upstream {
upstream_branch.clone()
} else {
let remote_branch = format!(
"refs/remotes/{}/{}",
upstream_remote,
normalize_branch_name(&stack.name)?
)
.parse::<RemoteRefname>()
.context("failed to parse remote branch name")?;

let remote_branches = ctx.repo().remote_branches()?;
let existing_branches = remote_branches
.iter()
.map(RemoteRefname::branch)
.map(str::to_lowercase) // git is weird about case sensitivity here, assume not case sensitive
.collect::<Vec<_>>();

remote_branch.with_branch(&dedup_fmt(
&existing_branches
.iter()
.map(String::as_str)
.collect::<Vec<_>>(),
remote_branch.branch(),
"-",
))
};

ctx.push(stack.head(), &remote_branch, with_force, None, askpass)?;

stack.upstream = Some(remote_branch.clone());
stack.upstream_head = Some(stack.head());
vb_state
.set_stack(stack.clone())
.context("failed to write target branch after push")?;
ctx.fetch(remote_branch.remote(), askpass.map(|_| "modal".to_string()))?;

Ok(PushResult {
remote: upstream_remote,
refname: gitbutler_reference::Refname::Remote(remote_branch),
})
}

type MergeBaseCommitGraph<'repo, 'cache> = gix::revwalk::Graph<
'repo,
'cache,
Expand Down Expand Up @@ -1657,11 +1581,14 @@ pub(crate) fn update_commit_message(
bail!("commit {commit_id} not in the branch");
}

let pushed_commit_oids = stack.upstream_head.map_or_else(
let pushed_commit_oids = stack.branches().first().map_or_else(
|| Ok(vec![]),
|upstream_head| {
ctx.repo()
.l(upstream_head, LogUntil::Commit(default_target.sha), false)
|branch| {
ctx.repo().l(
branch.remote_reference(stack.refname()),
LogUntil::Commit(default_target.sha),
false,
)
},
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn forcepush_allowed() -> anyhow::Result<()> {
gitbutler_branch_actions::create_commit(project, branch_id, "commit one", None, false)
.unwrap();

gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

{
// amend another hunk
Expand Down Expand Up @@ -103,7 +103,7 @@ fn forcepush_forbidden() {
gitbutler_branch_actions::create_commit(project, branch_id, "commit one", None, false)
.unwrap();

gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

{
fs::write(repository.path().join("file2.txt"), "content2").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn integration() {

std::fs::write(repository.path().join("file.txt"), "first\n").unwrap();
gitbutler_branch_actions::create_commit(project, branch_id, "first", None, false).unwrap();
gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

let branch = gitbutler_branch_actions::list_virtual_branches(project)
.unwrap()
Expand All @@ -37,7 +37,7 @@ fn integration() {
.find(|branch| branch.id == branch_id)
.unwrap();

let name = branch.upstream.unwrap().name;
let name = branch.series[0].as_ref().unwrap().name.parse().unwrap();

gitbutler_branch_actions::unapply_without_saving_virtual_branch(project, branch_id)
.unwrap();
Expand Down Expand Up @@ -72,7 +72,7 @@ fn integration() {

{
// merge branch into master
gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

let branch = gitbutler_branch_actions::list_virtual_branches(project)
.unwrap()
Expand Down Expand Up @@ -210,7 +210,6 @@ fn conflicts_with_uncommited() {
.unwrap();
assert_eq!(new_branch_id, new_branch.id);
assert_eq!(new_branch.series[0].clone().unwrap().patches.len(), 1);
assert!(new_branch.upstream.is_some());
}

#[test]
Expand Down Expand Up @@ -266,7 +265,6 @@ fn conflicts_with_commited() {
.unwrap();
assert_eq!(new_branch_id, new_branch.id);
assert_eq!(new_branch.series[0].clone().unwrap().patches.len(), 1);
assert!(new_branch.upstream.is_some());
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ fn dirty_non_target() {
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].files.len(), 1);
assert_eq!(branches[0].files[0].hunks.len(), 1);
assert!(branches[0].upstream.is_none());
assert_eq!(branches[0].name, "some-feature");
}

Expand All @@ -92,7 +91,6 @@ fn dirty_target() {
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].files.len(), 1);
assert_eq!(branches[0].files[0].hunks.len(), 1);
assert!(branches[0].upstream.is_none());
assert_eq!(branches[0].name, "master");
}

Expand All @@ -119,7 +117,6 @@ fn commit_on_non_target_local() {
assert_eq!(branches.len(), 1);
assert!(branches[0].files.is_empty());
assert_eq!(branches[0].series[0].clone().unwrap().patches.len(), 1);
assert!(branches[0].upstream.is_none());
assert_eq!(branches[0].name, "some-feature");
}

Expand Down Expand Up @@ -147,7 +144,6 @@ fn commit_on_non_target_remote() {
assert_eq!(branches.len(), 1);
assert!(branches[0].files.is_empty());
assert_eq!(branches[0].series[0].clone().unwrap().patches.len(), 1);
assert!(branches[0].upstream.is_some());
assert_eq!(branches[0].name, "some-feature");
}

Expand All @@ -173,7 +169,6 @@ fn commit_on_target() {
assert_eq!(branches.len(), 1);
assert!(branches[0].files.is_empty());
assert_eq!(branches[0].series[0].clone().unwrap().patches.len(), 1);
assert!(branches[0].upstream.is_none());
assert_eq!(branches[0].name, "master");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ mod push_virtual_branch {
fs::write(repository.path().join("file.txt"), "content").unwrap();

gitbutler_branch_actions::create_commit(project, branch1_id, "test", None, false).unwrap();
gitbutler_branch_actions::push_virtual_branch(project, branch1_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch1_id, false).unwrap();

let list_result = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
let branches = list_result.branches;
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].name, "name");
assert_eq!(
branches[0].upstream.as_ref().unwrap().name.to_string(),
branches[0].series[0].clone().unwrap().name,
"refs/remotes/origin/name"
);

Expand All @@ -254,7 +254,7 @@ mod push_virtual_branch {
.into_iter()
.filter_map(|reference| reference.name().map(|name| name.to_string()))
.collect::<Vec<_>>();
assert!(refnames.contains(&branches[0].upstream.clone().unwrap().name.to_string()));
assert!(refnames.contains(&branches[0].series[0].clone().unwrap().name));
}

#[test]
Expand Down Expand Up @@ -285,8 +285,7 @@ mod push_virtual_branch {
fs::write(repository.path().join("file.txt"), "content").unwrap();
gitbutler_branch_actions::create_commit(project, branch1_id, "test", None, false)
.unwrap();
gitbutler_branch_actions::push_virtual_branch(project, branch1_id, false, None)
.unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch1_id, false).unwrap();
branch1_id
};

Expand Down Expand Up @@ -314,8 +313,7 @@ mod push_virtual_branch {
fs::write(repository.path().join("file.txt"), "updated content").unwrap();
gitbutler_branch_actions::create_commit(project, branch2_id, "test", None, false)
.unwrap();
gitbutler_branch_actions::push_virtual_branch(project, branch2_id, false, None)
.unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch2_id, false).unwrap();
branch2_id
};

Expand All @@ -326,23 +324,23 @@ mod push_virtual_branch {
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].name, "updated name");
assert_eq!(
branches[0].upstream.as_ref().unwrap().name,
"refs/remotes/origin/name".parse().unwrap()
branches[0].series[0].clone().unwrap().name,
"refs/remotes/origin/name"
);
// new branch is pushing to new ref remotely
assert_eq!(branches[1].id, branch2_id);
assert_eq!(branches[1].name, "name");
assert_eq!(
branches[1].upstream.as_ref().unwrap().name,
"refs/remotes/origin/name-1".parse().unwrap()
branches[1].series[0].clone().unwrap().name,
"refs/remotes/origin/name-1"
);

let refnames = repository
.references()
.into_iter()
.filter_map(|reference| reference.name().map(|name| name.to_string()))
.collect::<Vec<_>>();
assert!(refnames.contains(&branches[0].upstream.clone().unwrap().name.to_string()));
assert!(refnames.contains(&branches[1].upstream.clone().unwrap().name.to_string()));
assert!(refnames.contains(&branches[0].series[0].clone().unwrap().name));
assert!(refnames.contains(&branches[1].series[0].clone().unwrap().name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn forcepush_allowed() {
.unwrap()
};

gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

let commit_two_oid = {
fs::write(repository.path().join("file two.txt"), "").unwrap();
Expand Down Expand Up @@ -240,7 +240,7 @@ fn forcepush_forbidden() {
.unwrap()
};

gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

let commit_two_oid = {
fs::write(repository.path().join("file two.txt"), "").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn forcepush_allowed() {
.unwrap()
};

gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

gitbutler_branch_actions::update_commit_message(
project,
Expand Down Expand Up @@ -236,7 +236,7 @@ fn forcepush_forbidden() {
.unwrap()
};

gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
gitbutler_branch_actions::stack::push_stack(project, branch_id, false).unwrap();

assert_eq!(
gitbutler_branch_actions::update_commit_message(
Expand Down
Loading
Loading