Skip to content

Commit

Permalink
Add behind count to list command
Browse files Browse the repository at this point in the history
Add the behind count to the list command so that when someone does a gps
list they can see how far behind their stack is from the upstream.

The approach with this is to get the common ancestor between HEAD and
it's associated tracking branch and then to simply count the commits
between the upstream tracking branch and the common ancestor as the
count of those commits is how far behind your patch stack is from its
upstream.

This relates to issue #234.

[changelog]
added: behind count to the list (ls) command

<!-- ps-id: 34d0cebb-533a-42ba-8931-91ddf2e3dbfa -->
  • Loading branch information
drewdeponte committed Dec 26, 2023
1 parent 635af45 commit 1de8166
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/ps/public/list.rs
Expand Up @@ -181,6 +181,34 @@ fn rebase_todo_command_to_row(todo: &RebaseTodoCommand, color: bool) -> list::Li
}
}

fn get_behind_count(
repo: &git2::Repository,
patch_stack: &ps::PatchStack,
patch_stack_upstream_tracking_branch_name: &str,
) -> usize {
let patch_stack_branch_upstream = repo
.find_branch(
patch_stack_upstream_tracking_branch_name,
git2::BranchType::Remote,
)
.expect("cur patch stack branch upstream to exist");

let patch_stack_branch_upstream_oid = patch_stack_branch_upstream
.into_reference()
.target()
.expect("cur patch stack branch upstream to have a target");

let behind_com_anc = git::common_ancestor(
repo,
patch_stack.head.target().expect("HEAD to have an oid"),
patch_stack_branch_upstream_oid,
)
.expect("common ancestor between HEAD and upstream tracking branch to exist");

git::count_commits(repo, patch_stack_branch_upstream_oid, behind_com_anc)
.expect("to be able to count commits from remote tracking branch to common ancestor")
}

pub fn list(color: bool) -> Result<(), ListError> {
let repo = git::create_cwd_repo().map_err(|_| ListError::RepositoryNotFound)?;

Expand Down Expand Up @@ -262,11 +290,14 @@ pub fn list(color: bool) -> Result<(), ListError> {
state_computation::get_list_patch_info(&repo, base_oid, &cur_patch_stack_branch_name)
.unwrap();

let behind_count = get_behind_count(&repo, &patch_stack, &cur_patch_stack_branch_upstream_name);

println!(
"{} tracking {} [ahead {}]",
"{} tracking {} [ahead {}, behind {}]",
&cur_patch_stack_branch_name,
&cur_patch_stack_branch_upstream_name,
list_of_patches.len()
list_of_patches.len(),
behind_count,
);

let list_of_patches_iter: Box<dyn Iterator<Item = _>> = if config.list.reverse_order {
Expand Down

0 comments on commit 1de8166

Please sign in to comment.