Skip to content

Feature Addition: Reverse Option #79

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

Merged
merged 3 commits into from
May 6, 2024
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
4 changes: 2 additions & 2 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,8 @@ fn extract_branches(

/// Traces back branches by following 1st commit parent,
/// until a commit is reached that already has a trace.
fn trace_branch<'repo>(
repository: &'repo Repository,
fn trace_branch(
repository: &Repository,
commits: &mut [CommitInfo],
indices: &HashMap<Oid, usize>,
branches: &mut [BranchInfo],
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ fn from_args() -> Result<(), String> {
git-graph model -> Show repo's current branching models\n \
git-graph model <model> -> Permanently set model <model> for this repo",
)
.arg(
Arg::new("reverse")
.long("reverse")
.short('r')
.help("Reverse the order of commits.")
.required(false)
.num_args(0),
)
.arg(
Arg::new("path")
.long("path")
Expand Down Expand Up @@ -272,6 +280,8 @@ fn from_args() -> Result<(), String> {

let include_remote = !matches.get_flag("local");

let reverse_commit_order = matches.get_flag("reverse");

let svg = matches.get_flag("svg");
let pager = !matches.get_flag("no-pager");
let compact = !matches.get_flag("sparse");
Expand All @@ -281,6 +291,12 @@ fn from_args() -> Result<(), String> {
.map(|s| Characters::from_str(s))
.unwrap_or_else(|| Ok(Characters::thin()))?;

let style = if reverse_commit_order {
style.reverse()
} else {
style
};

let model = get_model(
&repository,
matches.get_one::<String>("model").map(|s| &s[..]),
Expand Down Expand Up @@ -364,6 +380,7 @@ fn from_args() -> Result<(), String> {
};

let settings = Settings {
reverse_commit_order,
debug,
colored,
compact,
Expand Down
9 changes: 9 additions & 0 deletions src/print/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result<UnicodeGra
}
}

if settings.reverse_commit_order {
text_lines.reverse();
grid.reverse();
}

let lines = print_graph(&settings.characters, &grid, text_lines, settings.colored);

Ok((lines.0, lines.1, index_map))
Expand Down Expand Up @@ -682,6 +687,10 @@ impl Grid {
data: vec![initial; width * height],
}
}

pub fn reverse(&mut self) {
self.data.reverse();
}
pub fn index(&self, x: usize, y: usize) -> usize {
y * self.width + x
}
Expand Down
14 changes: 14 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum BranchOrder {

/// Top-level settings
pub struct Settings {
/// Reverse the order of commits
pub reverse_commit_order: bool,
/// Debug printing and drawing
pub debug: bool,
/// Compact text-based graph
Expand Down Expand Up @@ -336,4 +338,16 @@ impl Characters {
chars: " *o|-+'..'||++<>".chars().collect(),
}
}

pub fn reverse(self) -> Self {
let mut chars = self.chars;

chars.swap(6, 8);
chars.swap(7, 9);
chars.swap(10, 11);
chars.swap(12, 13);
chars.swap(14, 15);

Characters { chars }
}
}