Skip to content

Commit bfbcd35

Browse files
authored
Merge pull request #42 from mlange-42/feature/branches-and-tags
Give access to actual branches and tags * Renamed branches to all_branches (breaking change) * Store actual branches and tags as indices into all_branches
2 parents 3da6c00 + c1375f2 commit bfbcd35

File tree

7 files changed

+55
-23
lines changed

7 files changed

+55
-23
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "git-graph"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = ["Martin Lange <martin_lange_@gmx.net>"]
55
description = "Command line tool to show clear git graphs arranged for your branching model"
66
repository = "https://github.com/mlange-42/git-graph.git"

src/graph.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ const ORIGIN: &str = "origin/";
1313
pub struct GitGraph {
1414
pub repository: Repository,
1515
pub commits: Vec<CommitInfo>,
16+
/// Mapping from commit id to index in `commits`
1617
pub indices: HashMap<Oid, usize>,
17-
pub branches: Vec<BranchInfo>,
18+
/// All detected branches and tags, including merged and deleted
19+
pub all_branches: Vec<BranchInfo>,
20+
/// Indices of all real (still existing) branches in `all_branches`
21+
pub branches: Vec<usize>,
22+
/// Indices of all tags in `all_branches`
23+
pub tags: Vec<usize>,
24+
/// The current HEAD
1825
pub head: HeadInfo,
1926
}
2027

@@ -66,8 +73,8 @@ impl GitGraph {
6673

6774
assign_children(&mut commits, &indices);
6875

69-
let mut branches = assign_branches(&repository, &mut commits, &indices, &settings)?;
70-
assign_sources_targets(&commits, &indices, &mut branches);
76+
let mut all_branches = assign_branches(&repository, &mut commits, &indices, &settings)?;
77+
assign_sources_targets(&commits, &indices, &mut all_branches);
7178

7279
let (shortest_first, forward) = match settings.branch_order {
7380
BranchOrder::ShortestFirst(fwd) => (true, fwd),
@@ -77,7 +84,7 @@ impl GitGraph {
7784
assign_branch_columns(
7885
&commits,
7986
&indices,
80-
&mut branches,
87+
&mut all_branches,
8188
&settings.branches,
8289
shortest_first,
8390
forward,
@@ -99,7 +106,7 @@ impl GitGraph {
99106
.map(|(oid, index)| (*index, filtered_indices.get(oid)))
100107
.collect();
101108

102-
for branch in branches.iter_mut() {
109+
for branch in all_branches.iter_mut() {
103110
if let Some(mut start_idx) = branch.range.0 {
104111
let mut idx0 = index_map[&start_idx];
105112
while idx0.is_none() {
@@ -118,11 +125,36 @@ impl GitGraph {
118125
}
119126
}
120127

128+
let branches = all_branches
129+
.iter()
130+
.enumerate()
131+
.filter_map(|(idx, br)| {
132+
if !br.is_merged && !br.is_tag {
133+
Some(idx)
134+
} else {
135+
None
136+
}
137+
})
138+
.collect();
139+
let tags = all_branches
140+
.iter()
141+
.enumerate()
142+
.filter_map(|(idx, br)| {
143+
if !br.is_merged && br.is_tag {
144+
Some(idx)
145+
} else {
146+
None
147+
}
148+
})
149+
.collect();
150+
121151
Ok(GitGraph {
122152
repository,
123153
commits: filtered_commits,
124154
indices: filtered_indices,
155+
all_branches,
125156
branches,
157+
tags,
126158
head,
127159
})
128160
}

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ fn run(
392392
let duration_graph = now.elapsed().as_micros();
393393

394394
if settings.debug {
395-
for branch in &graph.branches {
395+
for branch in &graph.all_branches {
396396
eprintln!(
397397
"{} (col {}) ({:?}) {} s: {:?}, t: {:?}",
398398
branch.name,

src/print/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ fn get_deviate_index(graph: &GitGraph, index: usize, par_index: usize) -> usize
1616
let info = &graph.commits[index];
1717

1818
let par_info = &graph.commits[par_index];
19-
let par_branch = &graph.branches[par_info.branch_trace.unwrap()];
19+
let par_branch = &graph.all_branches[par_info.branch_trace.unwrap()];
2020

2121
let mut min_split_idx = index;
2222
for sibling_oid in &par_info.children {
2323
if let Some(&sibling_index) = graph.indices.get(sibling_oid) {
2424
if let Some(sibling) = graph.commits.get(sibling_index) {
2525
if let Some(sibling_trace) = sibling.branch_trace {
26-
let sibling_branch = &graph.branches[sibling_trace];
26+
let sibling_branch = &graph.all_branches[sibling_trace];
2727
if sibling_oid != &info.oid
2828
&& sibling_branch.visual.column == par_branch.visual.column
2929
&& sibling_index > min_split_idx

src/print/svg.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
1414
let mut max_column = 0;
1515

1616
if settings.debug {
17-
for branch in &graph.branches {
17+
for branch in &graph.all_branches {
1818
if let (Some(start), Some(end)) = branch.range {
1919
document = document.add(bold_line(
2020
start,
@@ -29,7 +29,7 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
2929

3030
for (idx, info) in graph.commits.iter().enumerate() {
3131
if let Some(trace) = info.branch_trace {
32-
let branch = &graph.branches[trace];
32+
let branch = &graph.all_branches[trace];
3333
let branch_color = &branch.visual.svg_color;
3434

3535
if branch.visual.column.unwrap() > max_column {
@@ -40,7 +40,7 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
4040
if let Some(par_oid) = info.parents[p] {
4141
if let Some(par_idx) = graph.indices.get(&par_oid) {
4242
let par_info = &graph.commits[*par_idx];
43-
let par_branch = &graph.branches[par_info.branch_trace.unwrap()];
43+
let par_branch = &graph.all_branches[par_info.branch_trace.unwrap()];
4444

4545
let color = if info.is_merge {
4646
&par_branch.visual.svg_color

src/print/unicode.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn print_unicode(
3939
settings: &Settings,
4040
) -> Result<(Vec<String>, Vec<usize>), String> {
4141
let num_cols = 2 * graph
42-
.branches
42+
.all_branches
4343
.iter()
4444
.map(|b| b.visual.column.unwrap_or(0))
4545
.max()
@@ -114,7 +114,7 @@ pub fn print_unicode(
114114

115115
for (idx, info) in graph.commits.iter().enumerate() {
116116
if let Some(trace) = info.branch_trace {
117-
let branch = &graph.branches[trace];
117+
let branch = &graph.all_branches[trace];
118118
let column = branch.visual.column.unwrap();
119119
let idx_map = index_map[idx];
120120

@@ -133,7 +133,7 @@ pub fn print_unicode(
133133
if let Some(par_idx) = graph.indices.get(&par_oid) {
134134
let par_idx_map = index_map[*par_idx];
135135
let par_info = &graph.commits[*par_idx];
136-
let par_branch = &graph.branches[par_info.branch_trace.unwrap()];
136+
let par_branch = &graph.all_branches[par_info.branch_trace.unwrap()];
137137
let par_column = par_branch.visual.column.unwrap();
138138

139139
let (color, pers) = if info.is_merge {
@@ -397,7 +397,7 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
397397
let mut inserts: HashMap<usize, Vec<Vec<Occ>>> = HashMap::new();
398398

399399
for (idx, info) in graph.commits.iter().enumerate() {
400-
let column = graph.branches[info.branch_trace.unwrap()]
400+
let column = graph.all_branches[info.branch_trace.unwrap()]
401401
.visual
402402
.column
403403
.unwrap();
@@ -407,14 +407,14 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
407407

408408
for (idx, info) in graph.commits.iter().enumerate() {
409409
if let Some(trace) = info.branch_trace {
410-
let branch = &graph.branches[trace];
410+
let branch = &graph.all_branches[trace];
411411
let column = branch.visual.column.unwrap();
412412

413413
for p in 0..2 {
414414
if let Some(par_oid) = info.parents[p] {
415415
if let Some(par_idx) = graph.indices.get(&par_oid) {
416416
let par_info = &graph.commits[*par_idx];
417-
let par_branch = &graph.branches[par_info.branch_trace.unwrap()];
417+
let par_branch = &graph.all_branches[par_info.branch_trace.unwrap()];
418418
let par_column = par_branch.visual.column.unwrap();
419419
let column_range = sorted(column, par_column);
420420

@@ -563,7 +563,7 @@ pub fn format_branches(
563563
) -> Result<String, String> {
564564
let curr_color = info
565565
.branch_trace
566-
.map(|branch_idx| &graph.branches[branch_idx].visual.term_color);
566+
.map(|branch_idx| &graph.all_branches[branch_idx].visual.term_color);
567567

568568
let mut branch_str = String::new();
569569

@@ -584,14 +584,14 @@ pub fn format_branches(
584584

585585
let branches = info.branches.iter().sorted_by_key(|br| {
586586
if let Some(head) = head {
587-
head.name != graph.branches[**br].name
587+
head.name != graph.all_branches[**br].name
588588
} else {
589589
false
590590
}
591591
});
592592

593593
for (idx, branch_index) in branches.enumerate() {
594-
let branch = &graph.branches[*branch_index];
594+
let branch = &graph.all_branches[*branch_index];
595595
let branch_color = branch.visual.term_color;
596596

597597
if let Some(head) = head {
@@ -622,7 +622,7 @@ pub fn format_branches(
622622
if !info.tags.is_empty() {
623623
write!(branch_str, " [").map_err(|err| err.to_string())?;
624624
for (idx, tag_index) in info.tags.iter().enumerate() {
625-
let tag = &graph.branches[*tag_index];
625+
let tag = &graph.all_branches[*tag_index];
626626
let tag_color = curr_color.unwrap_or(&tag.visual.term_color);
627627

628628
if color {

0 commit comments

Comments
 (0)