Skip to content

Commit

Permalink
cli: list non-tracking remote branch as standalone entry
Browse files Browse the repository at this point in the history
I'm not sure if this is the best way to render non-tracking branches, but
it helps to write CLI tests. Maybe we can add some hint or decoration to
non-tracking branches, but I'd like to avoid bikeshedding at this point.

Since we haven't migrated the push function yet, a deleted branch can be
pushed to non-tracking remotes. This will be addressed later.

#1136
  • Loading branch information
yuja committed Oct 16, 2023
1 parent 4af6788 commit f74d793
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
39 changes: 26 additions & 13 deletions cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ pub struct BranchDeleteArgs {

/// List branches and their targets
///
/// A remote branch will be included only if its target is different from
/// the local target. For a conflicted branch (both local and remote), old
/// target revisions are preceded by a "-" and new target revisions are
/// preceded by a "+". For information about branches, see
/// A tracking remote branch will be included only if its target is different
/// from the local target. For a conflicted branch (both local and remote), old
/// target revisions are preceded by a "-" and new target revisions are preceded
/// by a "+". For information about branches, see
/// https://github.com/martinvonz/jj/blob/main/docs/branches.md.
#[derive(clap::Args, Clone, Debug)]
pub struct BranchListArgs {
Expand Down Expand Up @@ -389,14 +389,22 @@ fn cmd_branch_list(
.map_or(true, |branch_names| branch_names.contains(name))
});
for (name, branch_target) in branches_to_list {
write!(formatter.labeled("branch"), "{name}")?;
if branch_target.local_target.is_present() {
print_branch_target(formatter, branch_target.local_target)?;
} else {
writeln!(formatter, " (deleted)")?;
let (tracking_remote_refs, untracked_remote_refs) =
branch_target
.remote_refs
.into_iter()
.partition::<Vec<_>, _>(|&(_, remote_ref)| remote_ref.is_tracking());

if branch_target.local_target.is_present() || !tracking_remote_refs.is_empty() {
write!(formatter.labeled("branch"), "{name}")?;
if branch_target.local_target.is_present() {
print_branch_target(formatter, branch_target.local_target)?;
} else {
writeln!(formatter, " (deleted)")?;
}
}

for &(remote, remote_ref) in &branch_target.remote_refs {
for &(remote, remote_ref) in &tracking_remote_refs {
if remote_ref.target == *branch_target.local_target {
continue;
}
Expand Down Expand Up @@ -425,9 +433,8 @@ fn cmd_branch_list(
print_branch_target(formatter, &remote_ref.target)?;
}

if branch_target.local_target.is_absent() {
let found_non_git_remote = branch_target
.remote_refs
if branch_target.local_target.is_absent() && !tracking_remote_refs.is_empty() {
let found_non_git_remote = tracking_remote_refs
.iter()
.any(|&(remote, _)| remote != git::REMOTE_NAME_FOR_LOCAL_GIT_REPO);
if found_non_git_remote {
Expand All @@ -444,6 +451,12 @@ fn cmd_branch_list(
)?;
}
}

// TODO: hide non-tracking remotes by default?
for &(remote, remote_ref) in &untracked_remote_refs {
write!(formatter.labeled("branch"), "{name}@{remote}")?;
print_branch_target(formatter, &remote_ref.target)?;
}
}

Ok(())
Expand Down
11 changes: 7 additions & 4 deletions cli/tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,11 +1159,14 @@ fn test_git_fetch_remote_only_branch() {
// Fetch using git.auto_local_branch = false
test_env.add_config("git.auto-local-branch = false");
test_env.jj_cmd_ok(&repo_path, &["git", "fetch", "--remote=origin"]);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
◉ 9f01a0e04879 message feature1 feature2@origin
│ @ 230dd059e1b0
├─╯
◉ 000000000000
"###);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: mzyxwzks 9f01a0e0 message
feature2 (deleted)
@origin: mzyxwzks 9f01a0e0 message
(this branch will be *deleted permanently* on the remote on the
next `jj git push`. Use `jj branch forget` to prevent this)
feature2@origin: mzyxwzks 9f01a0e0 message
"###);
}

0 comments on commit f74d793

Please sign in to comment.