Skip to content
Merged
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
98 changes: 12 additions & 86 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 59 additions & 13 deletions src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ pub fn run(ctx: &Ctx) -> Result<()> {
// Gather git info
let merge_base = ctx.git.merge_base(&parent, &current)?;
let commits = ctx.git.log_oneline(&merge_base, "HEAD", 100)?;
let parent_sha = ctx.git.rev_parse(&parent)?;
let needs_rebase = merge_base != parent_sha;
let behind_parent: usize = ctx
.git
.run(&["rev-list", "--count", &format!("{merge_base}..{parent}")])
.ok()
.and_then(|s| s.trim().parse().ok())
.unwrap_or(0);
let needs_rebase = behind_parent > 0;

// Remote status
let remote_status = remote_info(ctx, &current);
Expand Down Expand Up @@ -127,16 +132,27 @@ pub fn run(ctx: &Ctx) -> Result<()> {
RemoteStatus::NeedsPush(ahead) => {
flags.push(format!("{}", format!("{ahead} ahead, needs push").yellow()));
}
RemoteStatus::Diverged => {
flags.push(format!("{}", "diverged from remote".yellow()));
RemoteStatus::Behind(behind) => {
flags.push(format!("{}", format!("{behind} behind remote").yellow()));
}
RemoteStatus::Diverged { ahead, behind } => {
flags.push(
format!("diverged ({ahead} ahead, {behind} behind remote)")
.yellow()
.to_string(),
);
}
RemoteStatus::NoRemote => {
flags.push(format!("{}", "not pushed".dimmed()));
}
}

if needs_rebase {
flags.push(format!("{}", "needs rebase".yellow()));
flags.push(
format!("{behind_parent} behind parent, needs rebase")
.yellow()
.to_string(),
);
}

for flag in &flags {
Expand Down Expand Up @@ -196,7 +212,8 @@ fn working_tree_info(ctx: &Ctx) -> WorkingTree {
enum RemoteStatus {
UpToDate,
NeedsPush(usize),
Diverged,
Behind(usize),
Diverged { ahead: usize, behind: usize },
NoRemote,
}

Expand Down Expand Up @@ -224,19 +241,48 @@ fn remote_info(ctx: &Ctx, branch: &str) -> RemoteStatus {
return RemoteStatus::UpToDate;
}

if track.contains("gone") {
return RemoteStatus::NoRemote;
}

if track.contains("ahead") && track.contains("behind") {
return RemoteStatus::Diverged;
let ahead = parse_count_after(track, "ahead ");
let behind = parse_count_after(track, "behind ");
return RemoteStatus::Diverged { ahead, behind };
}

if track.contains("ahead") {
// Parse the number from "[ahead N]"
let ahead = track
.split("ahead ")
.nth(1)
.and_then(|s| s.trim_end_matches(']').parse::<usize>().ok())
.unwrap_or(1);
let ahead = parse_count_after(track, "ahead ");
return RemoteStatus::NeedsPush(ahead);
}

if track.contains("behind") {
let behind = parse_count_after(track, "behind ");
return RemoteStatus::Behind(behind);
}

RemoteStatus::UpToDate
}

fn parse_count_after(s: &str, prefix: &str) -> usize {
s.split(prefix)
.nth(1)
.and_then(|rest| rest.split(|c: char| !c.is_ascii_digit()).next())
.and_then(|n| n.parse().ok())
.unwrap_or(1)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_count_after() {
assert_eq!(parse_count_after("[ahead 3]", "ahead "), 3);
assert_eq!(parse_count_after("[behind 7]", "behind "), 7);
assert_eq!(parse_count_after("[ahead 2, behind 5]", "ahead "), 2);
assert_eq!(parse_count_after("[ahead 2, behind 5]", "behind "), 5);
assert_eq!(parse_count_after("[ahead 100]", "ahead "), 100);
assert_eq!(parse_count_after("garbage", "ahead "), 1); // fallback
}
}
6 changes: 6 additions & 0 deletions src/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ fn format_branch_line(
match ri.remote_status {
RemoteStatus::Diverged => tags.push("diverged".yellow().to_string()),
RemoteStatus::NeedsPush => tags.push("needs push".yellow().to_string()),
RemoteStatus::Behind => tags.push("behind remote".yellow().to_string()),
_ => {}
}
}
Expand All @@ -425,6 +426,7 @@ fn format_branch_line(
enum RemoteStatus {
UpToDate,
NeedsPush,
Behind,
Diverged,
NoRemote,
}
Expand Down Expand Up @@ -460,10 +462,14 @@ fn batch_ref_info(ctx: &Ctx) -> HashMap<String, RefInfo> {
RemoteStatus::NoRemote
} else if track.is_empty() {
RemoteStatus::UpToDate
} else if track.contains("gone") {
RemoteStatus::NoRemote
} else if track.contains("ahead") && track.contains("behind") {
RemoteStatus::Diverged
} else if track.contains("ahead") {
RemoteStatus::NeedsPush
} else if track.contains("behind") {
RemoteStatus::Behind
} else {
RemoteStatus::UpToDate
};
Expand Down