1use collections::{BTreeMap, HashMap};
2use git::{
3 BuildCommitPermalinkParams, GitHostingProviderRegistry, GitRemote, Oid, ParsedGitRemote,
4 parse_git_remote_url,
5 repository::{CommitDiff, InitialGraphCommitData, LogOrder, LogSource},
6};
7use git_ui::commit_tooltip::CommitAvatar;
8use gpui::{
9 AnyElement, App, Bounds, ClipboardItem, Context, Corner, DefiniteLength, ElementId, Entity,
10 EventEmitter, FocusHandle, Focusable, FontWeight, Hsla, InteractiveElement, ParentElement,
11 PathBuilder, Pixels, Point, Render, ScrollWheelEvent, SharedString, Styled, Subscription, Task,
12 WeakEntity, Window, actions, anchored, deferred, point, px,
13};
14use project::{
15 Project,
16 git_store::{CommitDataState, GitStoreEvent, Repository, RepositoryEvent},
17};
18use settings::Settings;
19use smallvec::{SmallVec, smallvec};
20use std::{ops::Range, rc::Rc, sync::Arc, sync::OnceLock};
21use theme::{AccentColors, ThemeSettings};
22use time::{OffsetDateTime, UtcOffset, format_description::BorrowedFormatItem};
23use ui::{ContextMenu, ScrollableHandle, Table, TableInteractionState, Tooltip, prelude::*};
24use workspace::{
25 Workspace,
26 item::{Item, ItemEvent, SerializableItem},
27};
28
29const COMMIT_CIRCLE_RADIUS: Pixels = px(4.5);
30const COMMIT_CIRCLE_STROKE_WIDTH: Pixels = px(1.5);
31const LANE_WIDTH: Pixels = px(16.0);
32const LEFT_PADDING: Pixels = px(12.0);
33const LINE_WIDTH: Pixels = px(1.5);
34
35actions!(
36 git_graph,
37 [
38 /// Opens the Git Graph panel.
39 Open,
40 /// Opens the commit view for the selected commit.
41 OpenCommitView,
42 ]
43);
44
45fn timestamp_format() -> &'static [BorrowedFormatItem<'static>] {
46 static FORMAT: OnceLock<Vec<BorrowedFormatItem<'static>>> = OnceLock::new();
47 FORMAT.get_or_init(|| {
48 time::format_description::parse("[day] [month repr:short] [year] [hour]:[minute]")
49 .unwrap_or_default()
50 })
51}
52
53fn format_timestamp(timestamp: i64) -> String {
54 let Ok(datetime) = OffsetDateTime::from_unix_timestamp(timestamp) else {
55 return "Unknown".to_string();
56 };
57
58 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
59 let local_datetime = datetime.to_offset(local_offset);
60
61 local_datetime
62 .format(timestamp_format())
63 .unwrap_or_default()
64}
65
66fn accent_colors_count(accents: &AccentColors) -> usize {
67 accents.0.len()
68}
69
70#[derive(Copy, Clone, Debug)]
71struct BranchColor(u8);
72
73#[derive(Debug)]
74enum LaneState {
75 Empty,
76 Active {
77 child: Oid,
78 parent: Oid,
79 color: Option<BranchColor>,
80 starting_row: usize,
81 starting_col: usize,
82 destination_column: Option<usize>,
83 segments: SmallVec<[CommitLineSegment; 1]>,
84 },
85}
86
87impl LaneState {
88 fn to_commit_lines(
89 &mut self,
90 ending_row: usize,
91 lane_column: usize,
92 parent_column: usize,
93 parent_color: BranchColor,
94 ) -> Option<CommitLine> {
95 let state = std::mem::replace(self, LaneState::Empty);
96
97 match state {
98 LaneState::Active {
99 #[cfg_attr(not(test), allow(unused_variables))]
100 parent,
101 #[cfg_attr(not(test), allow(unused_variables))]
102 child,
103 color,
104 starting_row,
105 starting_col,
106 destination_column,
107 mut segments,
108 } => {
109 let final_destination = destination_column.unwrap_or(parent_column);
110 let final_color = color.unwrap_or(parent_color);
111
112 Some(CommitLine {
113 #[cfg(test)]
114 child,
115 #[cfg(test)]
116 parent,
117 child_column: starting_col,
118 full_interval: starting_row..ending_row,
119 color_idx: final_color.0 as usize,
120 segments: {
121 match segments.last_mut() {
122 Some(CommitLineSegment::Straight { to_row })
123 if *to_row == usize::MAX =>
124 {
125 if final_destination != lane_column {
126 *to_row = ending_row - 1;
127
128 let curved_line = CommitLineSegment::Curve {
129 to_column: final_destination,
130 on_row: ending_row,
131 curve_kind: CurveKind::Checkout,
132 };
133
134 if *to_row == starting_row {
135 let last_index = segments.len() - 1;
136 segments[last_index] = curved_line;
137 } else {
138 segments.push(curved_line);
139 }
140 } else {
141 *to_row = ending_row;
142 }
143 }
144 Some(CommitLineSegment::Curve {
145 on_row,
146 to_column,
147 curve_kind,
148 }) if *on_row == usize::MAX => {
149 if *to_column == usize::MAX {
150 *to_column = final_destination;
151 }
152 if matches!(curve_kind, CurveKind::Merge) {
153 *on_row = starting_row + 1;
154 if *on_row < ending_row {
155 if *to_column != final_destination {
156 segments.push(CommitLineSegment::Straight {
157 to_row: ending_row - 1,
158 });
159 segments.push(CommitLineSegment::Curve {
160 to_column: final_destination,
161 on_row: ending_row,
162 curve_kind: CurveKind::Checkout,
163 });
164 } else {
165 segments.push(CommitLineSegment::Straight {
166 to_row: ending_row,
167 });
168 }
169 } else if *to_column != final_destination {
170 segments.push(CommitLineSegment::Curve {
171 to_column: final_destination,
172 on_row: ending_row,
173 curve_kind: CurveKind::Checkout,
174 });
175 }
176 } else {
177 *on_row = ending_row;
178 if *to_column != final_destination {
179 segments.push(CommitLineSegment::Straight {
180 to_row: ending_row,
181 });
182 segments.push(CommitLineSegment::Curve {
183 to_column: final_destination,
184 on_row: ending_row,
185 curve_kind: CurveKind::Checkout,
186 });
187 }
188 }
189 }
190 Some(CommitLineSegment::Curve {
191 on_row, to_column, ..
192 }) => {
193 if *on_row < ending_row {
194 if *to_column != final_destination {
195 segments.push(CommitLineSegment::Straight {
196 to_row: ending_row - 1,
197 });
198 segments.push(CommitLineSegment::Curve {
199 to_column: final_destination,
200 on_row: ending_row,
201 curve_kind: CurveKind::Checkout,
202 });
203 } else {
204 segments.push(CommitLineSegment::Straight {
205 to_row: ending_row,
206 });
207 }
208 } else if *to_column != final_destination {
209 segments.push(CommitLineSegment::Curve {
210 to_column: final_destination,
211 on_row: ending_row,
212 curve_kind: CurveKind::Checkout,
213 });
214 }
215 }
216 _ => {}
217 }
218
219 segments
220 },
221 })
222 }
223 LaneState::Empty => None,
224 }
225 }
226
227 fn is_empty(&self) -> bool {
228 match self {
229 LaneState::Empty => true,
230 LaneState::Active { .. } => false,
231 }
232 }
233}
234
235struct CommitEntry {
236 data: Arc<InitialGraphCommitData>,
237 lane: usize,
238 color_idx: usize,
239}
240
241type ActiveLaneIdx = usize;
242
243enum AllCommitCount {
244 NotLoaded,
245 Loaded(usize),
246}
247
248#[derive(Debug)]
249enum CurveKind {
250 Merge,
251 Checkout,
252}
253
254#[derive(Debug)]
255enum CommitLineSegment {
256 Straight {
257 to_row: usize,
258 },
259 Curve {
260 to_column: usize,
261 on_row: usize,
262 curve_kind: CurveKind,
263 },
264}
265
266#[derive(Debug)]
267struct CommitLine {
268 #[cfg(test)]
269 child: Oid,
270 #[cfg(test)]
271 parent: Oid,
272 child_column: usize,
273 full_interval: Range<usize>,
274 color_idx: usize,
275 segments: SmallVec<[CommitLineSegment; 1]>,
276}
277
278impl CommitLine {
279 fn get_first_visible_segment_idx(&self, first_visible_row: usize) -> Option<(usize, usize)> {
280 if first_visible_row > self.full_interval.end {
281 return None;
282 } else if first_visible_row <= self.full_interval.start {
283 return Some((0, self.child_column));
284 }
285
286 let mut current_column = self.child_column;
287
288 for (idx, segment) in self.segments.iter().enumerate() {
289 match segment {
290 CommitLineSegment::Straight { to_row } => {
291 if *to_row >= first_visible_row {
292 return Some((idx, current_column));
293 }
294 }
295 CommitLineSegment::Curve {
296 to_column, on_row, ..
297 } => {
298 if *on_row >= first_visible_row {
299 return Some((idx, current_column));
300 }
301 current_column = *to_column;
302 }
303 }
304 }
305
306 None
307 }
308}
309
310#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
311struct CommitLineKey {
312 child: Oid,
313 parent: Oid,
314}
315
316struct GraphData {
317 lane_states: SmallVec<[LaneState; 8]>,
318 lane_colors: HashMap<ActiveLaneIdx, BranchColor>,
319 parent_to_lanes: HashMap<Oid, SmallVec<[usize; 1]>>,
320 next_color: BranchColor,
321 accent_colors_count: usize,
322 commits: Vec<Rc<CommitEntry>>,
323 max_commit_count: AllCommitCount,
324 max_lanes: usize,
325 lines: Vec<Rc<CommitLine>>,
326 active_commit_lines: HashMap<CommitLineKey, usize>,
327 active_commit_lines_by_parent: HashMap<Oid, SmallVec<[usize; 1]>>,
328}
329
330impl GraphData {
331 fn new(accent_colors_count: usize) -> Self {
332 GraphData {
333 lane_states: SmallVec::default(),
334 lane_colors: HashMap::default(),
335 parent_to_lanes: HashMap::default(),
336 next_color: BranchColor(0),
337 accent_colors_count,
338 commits: Vec::default(),
339 max_commit_count: AllCommitCount::NotLoaded,
340 max_lanes: 0,
341 lines: Vec::default(),
342 active_commit_lines: HashMap::default(),
343 active_commit_lines_by_parent: HashMap::default(),
344 }
345 }
346
347 fn clear(&mut self) {
348 self.lane_states.clear();
349 self.lane_colors.clear();
350 self.parent_to_lanes.clear();
351 self.commits.clear();
352 self.lines.clear();
353 self.active_commit_lines.clear();
354 self.active_commit_lines_by_parent.clear();
355 self.next_color = BranchColor(0);
356 self.max_commit_count = AllCommitCount::NotLoaded;
357 self.max_lanes = 0;
358 }
359
360 fn first_empty_lane_idx(&mut self) -> ActiveLaneIdx {
361 self.lane_states
362 .iter()
363 .position(LaneState::is_empty)
364 .unwrap_or_else(|| {
365 self.lane_states.push(LaneState::Empty);
366 self.lane_states.len() - 1
367 })
368 }
369
370 fn get_lane_color(&mut self, lane_idx: ActiveLaneIdx) -> BranchColor {
371 let accent_colors_count = self.accent_colors_count;
372 *self.lane_colors.entry(lane_idx).or_insert_with(|| {
373 let color_idx = self.next_color;
374 self.next_color = BranchColor((self.next_color.0 + 1) % accent_colors_count as u8);
375 color_idx
376 })
377 }
378
379 fn add_commits(&mut self, commits: &[Arc<InitialGraphCommitData>]) {
380 self.commits.reserve(commits.len());
381 self.lines.reserve(commits.len() / 2);
382
383 for commit in commits.iter() {
384 let commit_row = self.commits.len();
385
386 let commit_lane = self
387 .parent_to_lanes
388 .get(&commit.sha)
389 .and_then(|lanes| lanes.first().copied());
390
391 let commit_lane = commit_lane.unwrap_or_else(|| self.first_empty_lane_idx());
392
393 let commit_color = self.get_lane_color(commit_lane);
394
395 if let Some(lanes) = self.parent_to_lanes.remove(&commit.sha) {
396 for lane_column in lanes {
397 let state = &mut self.lane_states[lane_column];
398
399 if let LaneState::Active {
400 starting_row,
401 segments,
402 ..
403 } = state
404 {
405 if let Some(CommitLineSegment::Curve {
406 to_column,
407 curve_kind: CurveKind::Merge,
408 ..
409 }) = segments.first_mut()
410 {
411 let curve_row = *starting_row + 1;
412 let would_overlap =
413 if lane_column != commit_lane && curve_row < commit_row {
414 self.commits[curve_row..commit_row]
415 .iter()
416 .any(|c| c.lane == commit_lane)
417 } else {
418 false
419 };
420
421 if would_overlap {
422 *to_column = lane_column;
423 }
424 }
425 }
426
427 if let Some(commit_line) =
428 state.to_commit_lines(commit_row, lane_column, commit_lane, commit_color)
429 {
430 self.lines.push(Rc::new(commit_line));
431 }
432 }
433 }
434
435 commit
436 .parents
437 .iter()
438 .enumerate()
439 .for_each(|(parent_idx, parent)| {
440 if parent_idx == 0 {
441 self.lane_states[commit_lane] = LaneState::Active {
442 parent: *parent,
443 child: commit.sha,
444 color: Some(commit_color),
445 starting_col: commit_lane,
446 starting_row: commit_row,
447 destination_column: None,
448 segments: smallvec![CommitLineSegment::Straight { to_row: usize::MAX }],
449 };
450
451 self.parent_to_lanes
452 .entry(*parent)
453 .or_default()
454 .push(commit_lane);
455 } else {
456 let new_lane = self.first_empty_lane_idx();
457
458 self.lane_states[new_lane] = LaneState::Active {
459 parent: *parent,
460 child: commit.sha,
461 color: None,
462 starting_col: commit_lane,
463 starting_row: commit_row,
464 destination_column: None,
465 segments: smallvec![CommitLineSegment::Curve {
466 to_column: usize::MAX,
467 on_row: usize::MAX,
468 curve_kind: CurveKind::Merge,
469 },],
470 };
471
472 self.parent_to_lanes
473 .entry(*parent)
474 .or_default()
475 .push(new_lane);
476 }
477 });
478
479 self.max_lanes = self.max_lanes.max(self.lane_states.len());
480
481 self.commits.push(Rc::new(CommitEntry {
482 data: commit.clone(),
483 lane: commit_lane,
484 color_idx: commit_color.0 as usize,
485 }));
486 }
487 }
488}
489
490pub fn init(cx: &mut App) {
491 workspace::register_serializable_item::<GitGraph>(cx);
492
493 cx.observe_new(|workspace: &mut workspace::Workspace, _, _| {
494 workspace.register_action_renderer(|div, workspace, _, cx| {
495 div.when(
496 workspace.project().read(cx).active_repository(cx).is_some(),
497 |div| {
498 let workspace = workspace.weak_handle();
499
500 div.on_action(move |_: &Open, window, cx| {
501 workspace
502 .update(cx, |workspace, cx| {
503 let project = workspace.project().clone();
504 let git_graph = cx.new(|cx| GitGraph::new(project, window, cx));
505 workspace.add_item_to_active_pane(
506 Box::new(git_graph),
507 None,
508 true,
509 window,
510 cx,
511 );
512 })
513 .ok();
514 })
515 },
516 )
517 });
518 })
519 .detach();
520}
521
522fn lane_center_x(bounds: Bounds<Pixels>, lane: f32, horizontal_scroll_offset: Pixels) -> Pixels {
523 bounds.origin.x + LEFT_PADDING + lane * LANE_WIDTH + LANE_WIDTH / 2.0 - horizontal_scroll_offset
524}
525
526fn to_row_center(
527 to_row: usize,
528 row_height: Pixels,
529 scroll_offset: Pixels,
530 bounds: Bounds<Pixels>,
531) -> Pixels {
532 bounds.origin.y + to_row as f32 * row_height + row_height / 2.0 - scroll_offset
533}
534
535fn draw_commit_circle(center_x: Pixels, center_y: Pixels, color: Hsla, window: &mut Window) {
536 let radius = COMMIT_CIRCLE_RADIUS;
537 let stroke_width = COMMIT_CIRCLE_STROKE_WIDTH;
538
539 let mut builder = PathBuilder::stroke(stroke_width);
540
541 // Start at the rightmost point of the circle
542 builder.move_to(point(center_x + radius, center_y));
543
544 // Draw the circle using two arc_to calls (top half, then bottom half)
545 builder.arc_to(
546 point(radius, radius),
547 px(0.),
548 false,
549 true,
550 point(center_x - radius, center_y),
551 );
552 builder.arc_to(
553 point(radius, radius),
554 px(0.),
555 false,
556 true,
557 point(center_x + radius, center_y),
558 );
559 builder.close();
560
561 if let Ok(path) = builder.build() {
562 window.paint_path(path, color);
563 }
564}
565
566pub struct GitGraph {
567 focus_handle: FocusHandle,
568 graph_data: GraphData,
569 project: Entity<Project>,
570 context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
571 row_height: Pixels,
572 table_interaction_state: Entity<TableInteractionState>,
573 horizontal_scroll_offset: Pixels,
574 graph_viewport_width: Pixels,
575 selected_entry_idx: Option<usize>,
576 log_source: LogSource,
577 log_order: LogOrder,
578 selected_commit_diff: Option<CommitDiff>,
579 _commit_diff_task: Option<Task<()>>,
580 _load_task: Option<Task<()>>,
581}
582
583impl GitGraph {
584 fn row_height(cx: &App) -> Pixels {
585 let settings = ThemeSettings::get_global(cx);
586 let font_size = settings.buffer_font_size(cx);
587 font_size + px(12.0)
588 }
589
590 fn graph_content_width(&self) -> Pixels {
591 (LANE_WIDTH * self.graph_data.max_lanes.min(8) as f32) + LEFT_PADDING * 2.0
592 }
593
594 pub fn new(project: Entity<Project>, window: &mut Window, cx: &mut Context<Self>) -> Self {
595 let focus_handle = cx.focus_handle();
596 cx.on_focus(&focus_handle, window, |_, _, cx| cx.notify())
597 .detach();
598
599 let git_store = project.read(cx).git_store().clone();
600 let accent_colors = cx.theme().accents();
601 let mut graph = GraphData::new(accent_colors_count(accent_colors));
602 let log_source = LogSource::default();
603 let log_order = LogOrder::default();
604
605 cx.subscribe(&git_store, |this, _, event, cx| match event {
606 GitStoreEvent::RepositoryUpdated(_, repo_event, is_active) => {
607 if *is_active {
608 if let Some(repository) = this.project.read(cx).active_repository(cx) {
609 this.on_repository_event(repository, repo_event, cx);
610 }
611 }
612 }
613 GitStoreEvent::ActiveRepositoryChanged(_) => {
614 this.graph_data.clear();
615 cx.notify();
616 }
617 _ => {}
618 })
619 .detach();
620
621 if let Some(repository) = project.read(cx).active_repository(cx) {
622 repository.update(cx, |repository, cx| {
623 // This won't overlap with loading commits from the repository because
624 // we either have all commits or commits loaded in chunks and loading commits
625 // from the repository event is always adding the last chunk of commits.
626 let commits =
627 repository.graph_data(log_source.clone(), log_order, 0..usize::MAX, cx);
628 graph.add_commits(commits);
629 });
630 }
631
632 let table_interaction_state = cx.new(|cx| TableInteractionState::new(cx));
633 let mut row_height = Self::row_height(cx);
634
635 cx.observe_global_in::<settings::SettingsStore>(window, move |this, _window, cx| {
636 let new_row_height = Self::row_height(cx);
637 if new_row_height != row_height {
638 this.row_height = new_row_height;
639 this.table_interaction_state.update(cx, |state, _cx| {
640 state.scroll_handle.0.borrow_mut().last_item_size = None;
641 });
642 row_height = new_row_height;
643 }
644 cx.notify();
645 })
646 .detach();
647
648 GitGraph {
649 focus_handle,
650 project,
651 graph_data: graph,
652 _load_task: None,
653 _commit_diff_task: None,
654 context_menu: None,
655 row_height,
656 table_interaction_state,
657 horizontal_scroll_offset: px(0.),
658 graph_viewport_width: px(88.),
659 selected_entry_idx: None,
660 selected_commit_diff: None,
661 log_source,
662 log_order,
663 }
664 }
665
666 fn on_repository_event(
667 &mut self,
668 repository: Entity<Repository>,
669 event: &RepositoryEvent,
670 cx: &mut Context<Self>,
671 ) {
672 match event {
673 RepositoryEvent::GitGraphCountUpdated(_, commit_count) => {
674 let old_count = self.graph_data.commits.len();
675
676 repository.update(cx, |repository, cx| {
677 let commits = repository.graph_data(
678 self.log_source.clone(),
679 self.log_order,
680 old_count..*commit_count,
681 cx,
682 );
683 self.graph_data.add_commits(commits);
684 });
685
686 self.graph_data.max_commit_count = AllCommitCount::Loaded(*commit_count);
687 }
688 RepositoryEvent::BranchChanged => {
689 self.graph_data.clear();
690 cx.notify();
691 }
692 _ => {}
693 }
694
695 cx.notify();
696 }
697
698 fn render_badge(&self, name: &SharedString, accent_color: gpui::Hsla) -> impl IntoElement {
699 div()
700 .px_1p5()
701 .py_0p5()
702 .h(self.row_height - px(4.0))
703 .flex()
704 .items_center()
705 .justify_center()
706 .rounded_md()
707 .bg(accent_color.opacity(0.18))
708 .border_1()
709 .border_color(accent_color.opacity(0.55))
710 .child(
711 Label::new(name.clone())
712 .size(LabelSize::Small)
713 .color(Color::Default)
714 .single_line(),
715 )
716 }
717
718 fn render_table_rows(
719 &mut self,
720 range: Range<usize>,
721 _window: &mut Window,
722 cx: &mut Context<Self>,
723 ) -> Vec<Vec<AnyElement>> {
724 let repository = self
725 .project
726 .read_with(cx, |project, cx| project.active_repository(cx));
727
728 let row_height = self.row_height;
729
730 // We fetch data outside the visible viewport to avoid loading entries when
731 // users scroll through the git graph
732 if let Some(repository) = repository.as_ref() {
733 const FETCH_RANGE: usize = 100;
734 repository.update(cx, |repository, cx| {
735 self.graph_data.commits[range.start.saturating_sub(FETCH_RANGE)
736 ..(range.end + FETCH_RANGE)
737 .min(self.graph_data.commits.len().saturating_sub(1))]
738 .iter()
739 .for_each(|commit| {
740 repository.fetch_commit_data(commit.data.sha, cx);
741 });
742 });
743 }
744
745 range
746 .map(|idx| {
747 let Some((commit, repository)) =
748 self.graph_data.commits.get(idx).zip(repository.as_ref())
749 else {
750 return vec![
751 div().h(row_height).into_any_element(),
752 div().h(row_height).into_any_element(),
753 div().h(row_height).into_any_element(),
754 div().h(row_height).into_any_element(),
755 ];
756 };
757
758 let data = repository.update(cx, |repository, cx| {
759 repository.fetch_commit_data(commit.data.sha, cx).clone()
760 });
761
762 let short_sha = commit.data.sha.display_short();
763 let mut formatted_time = String::new();
764 let subject;
765 let author_name;
766
767 if let CommitDataState::Loaded(data) = data {
768 subject = data.subject.clone();
769 author_name = data.author_name.clone();
770 formatted_time = format_timestamp(data.commit_timestamp);
771 } else {
772 subject = "Loading...".into();
773 author_name = "".into();
774 }
775
776 let accent_colors = cx.theme().accents();
777 let accent_color = accent_colors
778 .0
779 .get(commit.color_idx)
780 .copied()
781 .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
782 let is_selected = self.selected_entry_idx == Some(idx);
783 let text_color = if is_selected {
784 Color::Default
785 } else {
786 Color::Muted
787 };
788
789 vec![
790 div()
791 .id(ElementId::NamedInteger("commit-subject".into(), idx as u64))
792 .overflow_hidden()
793 .tooltip(Tooltip::text(subject.clone()))
794 .child(
795 h_flex()
796 .gap_1()
797 .items_center()
798 .overflow_hidden()
799 .children((!commit.data.ref_names.is_empty()).then(|| {
800 h_flex().flex_shrink().gap_2().items_center().children(
801 commit
802 .data
803 .ref_names
804 .iter()
805 .map(|name| self.render_badge(name, accent_color)),
806 )
807 }))
808 .child(
809 Label::new(subject)
810 .color(text_color)
811 .truncate()
812 .single_line(),
813 ),
814 )
815 .into_any_element(),
816 Label::new(formatted_time)
817 .color(text_color)
818 .single_line()
819 .into_any_element(),
820 Label::new(author_name)
821 .color(text_color)
822 .single_line()
823 .into_any_element(),
824 Label::new(short_sha)
825 .color(text_color)
826 .single_line()
827 .into_any_element(),
828 ]
829 })
830 .collect()
831 }
832
833 fn select_entry(&mut self, idx: usize, cx: &mut Context<Self>) {
834 if self.selected_entry_idx == Some(idx) {
835 return;
836 }
837
838 self.selected_entry_idx = Some(idx);
839 self.selected_commit_diff = None;
840
841 let Some(commit) = self.graph_data.commits.get(idx) else {
842 return;
843 };
844
845 let sha = commit.data.sha.to_string();
846 let repository = self
847 .project
848 .read_with(cx, |project, cx| project.active_repository(cx));
849
850 let Some(repository) = repository else {
851 return;
852 };
853
854 let diff_receiver = repository.update(cx, |repo, _| repo.load_commit_diff(sha));
855
856 self._commit_diff_task = Some(cx.spawn(async move |this, cx| {
857 if let Ok(Ok(diff)) = diff_receiver.await {
858 this.update(cx, |this, cx| {
859 this.selected_commit_diff = Some(diff);
860 cx.notify();
861 })
862 .ok();
863 }
864 }));
865
866 cx.notify();
867 }
868
869 fn get_remote(
870 &self,
871 repository: &Repository,
872 _window: &mut Window,
873 cx: &mut App,
874 ) -> Option<GitRemote> {
875 let remote_url = repository.default_remote_url()?;
876 let provider_registry = GitHostingProviderRegistry::default_global(cx);
877 let (provider, parsed) = parse_git_remote_url(provider_registry, &remote_url)?;
878 Some(GitRemote {
879 host: provider,
880 owner: parsed.owner.into(),
881 repo: parsed.repo.into(),
882 })
883 }
884
885 fn render_commit_detail_panel(
886 &self,
887 window: &mut Window,
888 cx: &mut Context<Self>,
889 ) -> impl IntoElement {
890 let Some(selected_idx) = self.selected_entry_idx else {
891 return div().into_any_element();
892 };
893
894 let Some(commit_entry) = self.graph_data.commits.get(selected_idx) else {
895 return div().into_any_element();
896 };
897
898 let repository = self
899 .project
900 .read_with(cx, |project, cx| project.active_repository(cx));
901
902 let Some(repository) = repository else {
903 return div().into_any_element();
904 };
905
906 let data = repository.update(cx, |repository, cx| {
907 repository
908 .fetch_commit_data(commit_entry.data.sha, cx)
909 .clone()
910 });
911
912 let full_sha: SharedString = commit_entry.data.sha.to_string().into();
913 let truncated_sha: SharedString = {
914 let sha_str = full_sha.as_ref();
915 if sha_str.len() > 24 {
916 format!("{}...", &sha_str[..24]).into()
917 } else {
918 full_sha.clone()
919 }
920 };
921 let ref_names = commit_entry.data.ref_names.clone();
922 let accent_colors = cx.theme().accents();
923 let accent_color = accent_colors
924 .0
925 .get(commit_entry.color_idx)
926 .copied()
927 .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
928
929 let (author_name, author_email, commit_timestamp, subject) = match &data {
930 CommitDataState::Loaded(data) => (
931 data.author_name.clone(),
932 data.author_email.clone(),
933 Some(data.commit_timestamp),
934 data.subject.clone(),
935 ),
936 CommitDataState::Loading => ("Loading...".into(), "".into(), None, "Loading...".into()),
937 };
938
939 let date_string = commit_timestamp
940 .and_then(|ts| OffsetDateTime::from_unix_timestamp(ts).ok())
941 .map(|datetime| {
942 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
943 let local_datetime = datetime.to_offset(local_offset);
944 let format =
945 time::format_description::parse("[month repr:short] [day], [year]").ok();
946 format
947 .and_then(|f| local_datetime.format(&f).ok())
948 .unwrap_or_default()
949 })
950 .unwrap_or_default();
951
952 let remote = repository.update(cx, |repo, cx| self.get_remote(repo, window, cx));
953
954 let avatar = {
955 let avatar = CommitAvatar::new(&full_sha, remote.as_ref());
956 v_flex()
957 .w(px(64.))
958 .h(px(64.))
959 .border_1()
960 .border_color(cx.theme().colors().border)
961 .rounded_full()
962 .justify_center()
963 .items_center()
964 .child(
965 avatar
966 .avatar(window, cx)
967 .map(|a| a.size(px(64.)).into_any_element())
968 .unwrap_or_else(|| {
969 Icon::new(IconName::Person)
970 .color(Color::Muted)
971 .size(IconSize::XLarge)
972 .into_any_element()
973 }),
974 )
975 };
976
977 let changed_files_count = self
978 .selected_commit_diff
979 .as_ref()
980 .map(|diff| diff.files.len())
981 .unwrap_or(0);
982
983 v_flex()
984 .w(px(300.))
985 .h_full()
986 .border_l_1()
987 .border_color(cx.theme().colors().border)
988 .bg(cx.theme().colors().surface_background)
989 .child(
990 v_flex()
991 .p_3()
992 .gap_3()
993 .child(
994 h_flex().justify_between().child(avatar).child(
995 IconButton::new("close-detail", IconName::Close)
996 .icon_size(IconSize::Small)
997 .on_click(cx.listener(move |this, _, _, cx| {
998 this.selected_entry_idx = None;
999 this.selected_commit_diff = None;
1000 this._commit_diff_task = None;
1001 cx.notify();
1002 })),
1003 ),
1004 )
1005 .child(
1006 v_flex()
1007 .gap_0p5()
1008 .child(Label::new(author_name.clone()).weight(FontWeight::SEMIBOLD))
1009 .child(
1010 Label::new(date_string)
1011 .color(Color::Muted)
1012 .size(LabelSize::Small),
1013 ),
1014 )
1015 .children((!ref_names.is_empty()).then(|| {
1016 h_flex().gap_1().flex_wrap().children(
1017 ref_names
1018 .iter()
1019 .map(|name| self.render_badge(name, accent_color)),
1020 )
1021 }))
1022 .child(
1023 v_flex()
1024 .gap_1p5()
1025 .child(
1026 h_flex()
1027 .gap_1()
1028 .child(
1029 Icon::new(IconName::Person)
1030 .size(IconSize::Small)
1031 .color(Color::Muted),
1032 )
1033 .child(
1034 Label::new(author_name)
1035 .size(LabelSize::Small)
1036 .color(Color::Muted),
1037 )
1038 .when(!author_email.is_empty(), |this| {
1039 this.child(
1040 Label::new(format!("<{}>", author_email))
1041 .size(LabelSize::Small)
1042 .color(Color::Ignored),
1043 )
1044 }),
1045 )
1046 .child(
1047 h_flex()
1048 .gap_1()
1049 .child(
1050 Icon::new(IconName::Hash)
1051 .size(IconSize::Small)
1052 .color(Color::Muted),
1053 )
1054 .child({
1055 let copy_sha = full_sha.clone();
1056 Button::new("sha-button", truncated_sha)
1057 .style(ButtonStyle::Transparent)
1058 .label_size(LabelSize::Small)
1059 .color(Color::Muted)
1060 .tooltip(Tooltip::text(format!(
1061 "Copy SHA: {}",
1062 copy_sha
1063 )))
1064 .on_click(move |_, _, cx| {
1065 cx.write_to_clipboard(ClipboardItem::new_string(
1066 copy_sha.to_string(),
1067 ));
1068 })
1069 }),
1070 )
1071 .when_some(remote.clone(), |this, remote| {
1072 let provider_name = remote.host.name();
1073 let icon = match provider_name.as_str() {
1074 "GitHub" => IconName::Github,
1075 _ => IconName::Link,
1076 };
1077 let parsed_remote = ParsedGitRemote {
1078 owner: remote.owner.as_ref().into(),
1079 repo: remote.repo.as_ref().into(),
1080 };
1081 let params = BuildCommitPermalinkParams {
1082 sha: full_sha.as_ref(),
1083 };
1084 let url = remote
1085 .host
1086 .build_commit_permalink(&parsed_remote, params)
1087 .to_string();
1088 this.child(
1089 h_flex()
1090 .gap_1()
1091 .child(
1092 Icon::new(icon)
1093 .size(IconSize::Small)
1094 .color(Color::Muted),
1095 )
1096 .child(
1097 Button::new(
1098 "view-on-provider",
1099 format!("View on {}", provider_name),
1100 )
1101 .style(ButtonStyle::Transparent)
1102 .label_size(LabelSize::Small)
1103 .color(Color::Muted)
1104 .on_click(
1105 move |_, _, cx| {
1106 cx.open_url(&url);
1107 },
1108 ),
1109 ),
1110 )
1111 }),
1112 ),
1113 )
1114 .child(
1115 div()
1116 .border_t_1()
1117 .border_color(cx.theme().colors().border)
1118 .p_3()
1119 .child(
1120 v_flex()
1121 .gap_2()
1122 .child(Label::new(subject).weight(FontWeight::MEDIUM)),
1123 ),
1124 )
1125 .child(
1126 div()
1127 .flex_1()
1128 .overflow_hidden()
1129 .border_t_1()
1130 .border_color(cx.theme().colors().border)
1131 .p_3()
1132 .child(
1133 v_flex()
1134 .gap_2()
1135 .child(
1136 Label::new(format!("{} Changed Files", changed_files_count))
1137 .size(LabelSize::Small)
1138 .color(Color::Muted),
1139 )
1140 .children(self.selected_commit_diff.as_ref().map(|diff| {
1141 v_flex().gap_1().children(diff.files.iter().map(|file| {
1142 let file_name: String = file
1143 .path
1144 .file_name()
1145 .map(|n| n.to_string())
1146 .unwrap_or_default();
1147 let dir_path: String = file
1148 .path
1149 .parent()
1150 .map(|p| p.as_unix_str().to_string())
1151 .unwrap_or_default();
1152
1153 h_flex()
1154 .gap_1()
1155 .overflow_hidden()
1156 .child(
1157 Icon::new(IconName::File)
1158 .size(IconSize::Small)
1159 .color(Color::Accent),
1160 )
1161 .child(
1162 Label::new(file_name)
1163 .size(LabelSize::Small)
1164 .single_line(),
1165 )
1166 .when(!dir_path.is_empty(), |this| {
1167 this.child(
1168 Label::new(dir_path)
1169 .size(LabelSize::Small)
1170 .color(Color::Muted)
1171 .single_line(),
1172 )
1173 })
1174 }))
1175 })),
1176 ),
1177 )
1178 .into_any_element()
1179 }
1180
1181 pub fn render_graph(&self, cx: &mut Context<GitGraph>) -> impl IntoElement {
1182 let row_height = self.row_height;
1183 let table_state = self.table_interaction_state.read(cx);
1184 let viewport_height = table_state
1185 .scroll_handle
1186 .0
1187 .borrow()
1188 .last_item_size
1189 .map(|size| size.item.height)
1190 .unwrap_or(px(600.0));
1191 let loaded_commit_count = self.graph_data.commits.len();
1192
1193 let content_height = row_height * loaded_commit_count;
1194 let max_scroll = (content_height - viewport_height).max(px(0.));
1195 let scroll_offset_y = (-table_state.scroll_offset().y).clamp(px(0.), max_scroll);
1196
1197 let first_visible_row = (scroll_offset_y / row_height).floor() as usize;
1198 let vertical_scroll_offset = scroll_offset_y - (first_visible_row as f32 * row_height);
1199 let horizontal_scroll_offset = self.horizontal_scroll_offset;
1200
1201 let max_lanes = self.graph_data.max_lanes.max(6);
1202 let graph_width = LANE_WIDTH * max_lanes as f32 + LEFT_PADDING * 2.0;
1203 let last_visible_row =
1204 first_visible_row + (viewport_height / row_height).ceil() as usize + 1;
1205
1206 let viewport_range = first_visible_row.min(loaded_commit_count.saturating_sub(1))
1207 ..(last_visible_row).min(loaded_commit_count);
1208 let rows = self.graph_data.commits[viewport_range.clone()].to_vec();
1209 let commit_lines: Vec<_> = self
1210 .graph_data
1211 .lines
1212 .iter()
1213 .filter(|line| {
1214 line.full_interval.start <= viewport_range.end
1215 && line.full_interval.end >= viewport_range.start
1216 })
1217 .cloned()
1218 .collect();
1219
1220 let mut lines: BTreeMap<usize, Vec<_>> = BTreeMap::new();
1221
1222 gpui::canvas(
1223 move |_bounds, _window, _cx| {},
1224 move |bounds: Bounds<Pixels>, _: (), window: &mut Window, cx: &mut App| {
1225 window.paint_layer(bounds, |window| {
1226 let accent_colors = cx.theme().accents();
1227
1228 for (row_idx, row) in rows.into_iter().enumerate() {
1229 let row_color = accent_colors.color_for_index(row.color_idx as u32);
1230 let row_y_center =
1231 bounds.origin.y + row_idx as f32 * row_height + row_height / 2.0
1232 - vertical_scroll_offset;
1233
1234 let commit_x =
1235 lane_center_x(bounds, row.lane as f32, horizontal_scroll_offset);
1236
1237 draw_commit_circle(commit_x, row_y_center, row_color, window);
1238 }
1239
1240 for line in commit_lines {
1241 let Some((start_segment_idx, start_column)) =
1242 line.get_first_visible_segment_idx(first_visible_row)
1243 else {
1244 continue;
1245 };
1246
1247 let line_x =
1248 lane_center_x(bounds, start_column as f32, horizontal_scroll_offset);
1249
1250 let start_row = line.full_interval.start as i32 - first_visible_row as i32;
1251
1252 let from_y =
1253 bounds.origin.y + start_row as f32 * row_height + row_height / 2.0
1254 - vertical_scroll_offset
1255 + COMMIT_CIRCLE_RADIUS;
1256
1257 let mut current_row = from_y;
1258 let mut current_column = line_x;
1259
1260 let mut builder = PathBuilder::stroke(LINE_WIDTH);
1261 builder.move_to(point(line_x, from_y));
1262
1263 let segments = &line.segments[start_segment_idx..];
1264
1265 for (segment_idx, segment) in segments.iter().enumerate() {
1266 let is_last = segment_idx + 1 == segments.len();
1267
1268 match segment {
1269 CommitLineSegment::Straight { to_row } => {
1270 let mut dest_row = to_row_center(
1271 to_row - first_visible_row,
1272 row_height,
1273 vertical_scroll_offset,
1274 bounds,
1275 );
1276 if is_last {
1277 dest_row -= COMMIT_CIRCLE_RADIUS;
1278 }
1279
1280 let dest_point = point(current_column, dest_row);
1281
1282 current_row = dest_point.y;
1283 builder.line_to(dest_point);
1284 builder.move_to(dest_point);
1285 }
1286 CommitLineSegment::Curve {
1287 to_column,
1288 on_row,
1289 curve_kind,
1290 } => {
1291 let mut to_column = lane_center_x(
1292 bounds,
1293 *to_column as f32,
1294 horizontal_scroll_offset,
1295 );
1296
1297 let mut to_row = to_row_center(
1298 *on_row - first_visible_row,
1299 row_height,
1300 vertical_scroll_offset,
1301 bounds,
1302 );
1303
1304 // This means that this branch was a checkout
1305 let going_right = to_column > current_column;
1306 let column_shift = if going_right {
1307 COMMIT_CIRCLE_RADIUS + COMMIT_CIRCLE_STROKE_WIDTH
1308 } else {
1309 -COMMIT_CIRCLE_RADIUS - COMMIT_CIRCLE_STROKE_WIDTH
1310 };
1311
1312 let control = match curve_kind {
1313 CurveKind::Checkout => {
1314 if is_last {
1315 to_column -= column_shift;
1316 }
1317 builder.move_to(point(current_column, current_row));
1318 point(current_column, to_row)
1319 }
1320 CurveKind::Merge => {
1321 if is_last {
1322 to_row -= COMMIT_CIRCLE_RADIUS;
1323 }
1324 builder.move_to(point(
1325 current_column + column_shift,
1326 current_row - COMMIT_CIRCLE_RADIUS,
1327 ));
1328 point(to_column, current_row)
1329 }
1330 };
1331
1332 match curve_kind {
1333 CurveKind::Checkout
1334 if (to_row - current_row).abs() > row_height =>
1335 {
1336 let start_curve =
1337 point(current_column, current_row + row_height);
1338 builder.line_to(start_curve);
1339 builder.move_to(start_curve);
1340 }
1341 CurveKind::Merge
1342 if (to_column - current_column).abs() > LANE_WIDTH =>
1343 {
1344 let column_shift =
1345 if going_right { LANE_WIDTH } else { -LANE_WIDTH };
1346
1347 let start_curve = point(
1348 current_column + column_shift,
1349 current_row - COMMIT_CIRCLE_RADIUS,
1350 );
1351
1352 builder.line_to(start_curve);
1353 builder.move_to(start_curve);
1354 }
1355 _ => {}
1356 };
1357
1358 builder.curve_to(point(to_column, to_row), control);
1359 current_row = to_row;
1360 current_column = to_column;
1361 builder.move_to(point(current_column, current_row));
1362 }
1363 }
1364 }
1365
1366 builder.close();
1367 lines.entry(line.color_idx).or_default().push(builder);
1368 }
1369
1370 for (color_idx, builders) in lines {
1371 let line_color = accent_colors.color_for_index(color_idx as u32);
1372
1373 for builder in builders {
1374 if let Ok(path) = builder.build() {
1375 // we paint each color on it's own layer to stop overlapping lines
1376 // of different colors changing the color of a line
1377 window.paint_layer(bounds, |window| {
1378 window.paint_path(path, line_color);
1379 });
1380 }
1381 }
1382 }
1383 })
1384 },
1385 )
1386 .w(graph_width)
1387 .h_full()
1388 }
1389
1390 fn handle_graph_scroll(
1391 &mut self,
1392 event: &ScrollWheelEvent,
1393 window: &mut Window,
1394 cx: &mut Context<Self>,
1395 ) {
1396 let line_height = window.line_height();
1397 let delta = event.delta.pixel_delta(line_height);
1398
1399 let table_state = self.table_interaction_state.read(cx);
1400 let current_offset = table_state.scroll_offset();
1401
1402 let viewport_height = table_state.scroll_handle.viewport().size.height;
1403
1404 let commit_count = match self.graph_data.max_commit_count {
1405 AllCommitCount::Loaded(count) => count,
1406 AllCommitCount::NotLoaded => self.graph_data.commits.len(),
1407 };
1408 let content_height = self.row_height * commit_count;
1409 let max_vertical_scroll = (viewport_height - content_height).min(px(0.));
1410
1411 let new_y = (current_offset.y + delta.y).clamp(max_vertical_scroll, px(0.));
1412 let new_offset = Point::new(current_offset.x, new_y);
1413
1414 let max_lanes = self.graph_data.max_lanes.max(1);
1415 let graph_content_width = LANE_WIDTH * max_lanes as f32 + LEFT_PADDING * 2.0;
1416 let max_horizontal_scroll = (graph_content_width - self.graph_viewport_width).max(px(0.));
1417
1418 let new_horizontal_offset =
1419 (self.horizontal_scroll_offset - delta.x).clamp(px(0.), max_horizontal_scroll);
1420
1421 let vertical_changed = new_offset != current_offset;
1422 let horizontal_changed = new_horizontal_offset != self.horizontal_scroll_offset;
1423
1424 if vertical_changed {
1425 table_state.set_scroll_offset(new_offset);
1426 }
1427
1428 if horizontal_changed {
1429 self.horizontal_scroll_offset = new_horizontal_offset;
1430 }
1431
1432 if vertical_changed || horizontal_changed {
1433 cx.notify();
1434 }
1435 }
1436}
1437
1438impl Render for GitGraph {
1439 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1440 let description_width_fraction = 0.72;
1441 let date_width_fraction = 0.12;
1442 let author_width_fraction = 0.10;
1443 let commit_width_fraction = 0.06;
1444
1445 let commit_count = match self.graph_data.max_commit_count {
1446 AllCommitCount::Loaded(count) => count,
1447 AllCommitCount::NotLoaded => {
1448 self.project.update(cx, |project, cx| {
1449 if let Some(repository) = project.active_repository(cx) {
1450 repository.update(cx, |repository, cx| {
1451 // Start loading the graph data if we haven't started already
1452 repository.graph_data(
1453 self.log_source.clone(),
1454 self.log_order,
1455 0..0,
1456 cx,
1457 );
1458 })
1459 }
1460 });
1461
1462 self.graph_data.commits.len()
1463 }
1464 };
1465
1466 let content = if self.graph_data.commits.is_empty() {
1467 let message = "No commits found";
1468 div()
1469 .size_full()
1470 .flex()
1471 .items_center()
1472 .justify_center()
1473 .child(Label::new(message).color(Color::Muted))
1474 } else {
1475 div()
1476 .size_full()
1477 .flex()
1478 .flex_row()
1479 .child(
1480 div()
1481 .w(self.graph_content_width())
1482 .h_full()
1483 .flex()
1484 .flex_col()
1485 .child(
1486 div()
1487 .p_2()
1488 .border_b_1()
1489 .border_color(cx.theme().colors().border)
1490 .child(Label::new("Graph").color(Color::Muted)),
1491 )
1492 .child(
1493 div()
1494 .id("graph-canvas")
1495 .flex_1()
1496 .overflow_hidden()
1497 .child(self.render_graph(cx))
1498 .on_scroll_wheel(cx.listener(Self::handle_graph_scroll)),
1499 ),
1500 )
1501 .child({
1502 let row_height = self.row_height;
1503 let selected_entry_idx = self.selected_entry_idx;
1504 let weak_self = cx.weak_entity();
1505 div().flex_1().size_full().child(
1506 Table::new(4)
1507 .interactable(&self.table_interaction_state)
1508 .hide_row_borders()
1509 .header(vec![
1510 Label::new("Description")
1511 .color(Color::Muted)
1512 .into_any_element(),
1513 Label::new("Date").color(Color::Muted).into_any_element(),
1514 Label::new("Author").color(Color::Muted).into_any_element(),
1515 Label::new("Commit").color(Color::Muted).into_any_element(),
1516 ])
1517 .column_widths(
1518 [
1519 DefiniteLength::Fraction(description_width_fraction),
1520 DefiniteLength::Fraction(date_width_fraction),
1521 DefiniteLength::Fraction(author_width_fraction),
1522 DefiniteLength::Fraction(commit_width_fraction),
1523 ]
1524 .to_vec(),
1525 )
1526 .map_row(move |(index, row), _window, cx| {
1527 let is_selected = selected_entry_idx == Some(index);
1528 let weak = weak_self.clone();
1529 row.h(row_height)
1530 .when(is_selected, |row| {
1531 row.bg(cx.theme().colors().element_selected)
1532 })
1533 .on_click(move |_, _, cx| {
1534 weak.update(cx, |this, cx| {
1535 this.select_entry(index, cx);
1536 })
1537 .ok();
1538 })
1539 .into_any_element()
1540 })
1541 .uniform_list(
1542 "git-graph-commits",
1543 commit_count,
1544 cx.processor(Self::render_table_rows),
1545 ),
1546 )
1547 })
1548 .when(self.selected_entry_idx.is_some(), |this| {
1549 this.child(self.render_commit_detail_panel(window, cx))
1550 })
1551 };
1552
1553 div()
1554 .size_full()
1555 .bg(cx.theme().colors().editor_background)
1556 .key_context("GitGraph")
1557 .track_focus(&self.focus_handle)
1558 .child(content)
1559 .children(self.context_menu.as_ref().map(|(menu, position, _)| {
1560 deferred(
1561 anchored()
1562 .position(*position)
1563 .anchor(Corner::TopLeft)
1564 .child(menu.clone()),
1565 )
1566 .with_priority(1)
1567 }))
1568 }
1569}
1570
1571impl EventEmitter<ItemEvent> for GitGraph {}
1572
1573impl Focusable for GitGraph {
1574 fn focus_handle(&self, _cx: &App) -> FocusHandle {
1575 self.focus_handle.clone()
1576 }
1577}
1578
1579impl Item for GitGraph {
1580 type Event = ItemEvent;
1581
1582 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
1583 "Git Graph".into()
1584 }
1585
1586 fn show_toolbar(&self) -> bool {
1587 false
1588 }
1589
1590 fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
1591 f(*event)
1592 }
1593}
1594
1595impl SerializableItem for GitGraph {
1596 fn serialized_item_kind() -> &'static str {
1597 "GitGraph"
1598 }
1599
1600 fn cleanup(
1601 workspace_id: workspace::WorkspaceId,
1602 alive_items: Vec<workspace::ItemId>,
1603 _window: &mut Window,
1604 cx: &mut App,
1605 ) -> Task<gpui::Result<()>> {
1606 workspace::delete_unloaded_items(
1607 alive_items,
1608 workspace_id,
1609 "git_graphs",
1610 &persistence::GIT_GRAPHS,
1611 cx,
1612 )
1613 }
1614
1615 fn deserialize(
1616 project: Entity<Project>,
1617 _: WeakEntity<Workspace>,
1618 workspace_id: workspace::WorkspaceId,
1619 item_id: workspace::ItemId,
1620 window: &mut Window,
1621 cx: &mut App,
1622 ) -> Task<gpui::Result<Entity<Self>>> {
1623 if persistence::GIT_GRAPHS
1624 .get_git_graph(item_id, workspace_id)
1625 .ok()
1626 .is_some_and(|is_open| is_open)
1627 {
1628 let git_graph = cx.new(|cx| GitGraph::new(project, window, cx));
1629 Task::ready(Ok(git_graph))
1630 } else {
1631 Task::ready(Err(anyhow::anyhow!("No git graph to deserialize")))
1632 }
1633 }
1634
1635 fn serialize(
1636 &mut self,
1637 workspace: &mut Workspace,
1638 item_id: workspace::ItemId,
1639 _closing: bool,
1640 _window: &mut Window,
1641 cx: &mut Context<Self>,
1642 ) -> Option<Task<gpui::Result<()>>> {
1643 let workspace_id = workspace.database_id()?;
1644 Some(cx.background_spawn(async move {
1645 persistence::GIT_GRAPHS
1646 .save_git_graph(item_id, workspace_id, true)
1647 .await
1648 }))
1649 }
1650
1651 fn should_serialize(&self, event: &Self::Event) -> bool {
1652 event == &ItemEvent::UpdateTab
1653 }
1654}
1655
1656mod persistence {
1657 use db::{
1658 query,
1659 sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection},
1660 sqlez_macros::sql,
1661 };
1662 use workspace::WorkspaceDb;
1663
1664 pub struct GitGraphsDb(ThreadSafeConnection);
1665
1666 impl Domain for GitGraphsDb {
1667 const NAME: &str = stringify!(GitGraphsDb);
1668
1669 const MIGRATIONS: &[&str] = (&[sql!(
1670 CREATE TABLE git_graphs (
1671 workspace_id INTEGER,
1672 item_id INTEGER UNIQUE,
1673 is_open INTEGER DEFAULT FALSE,
1674
1675 PRIMARY KEY(workspace_id, item_id),
1676 FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
1677 ON DELETE CASCADE
1678 ) STRICT;
1679 )]);
1680 }
1681
1682 db::static_connection!(GIT_GRAPHS, GitGraphsDb, [WorkspaceDb]);
1683
1684 impl GitGraphsDb {
1685 query! {
1686 pub async fn save_git_graph(
1687 item_id: workspace::ItemId,
1688 workspace_id: workspace::WorkspaceId,
1689 is_open: bool
1690 ) -> Result<()> {
1691 INSERT OR REPLACE INTO git_graphs(item_id, workspace_id, is_open)
1692 VALUES (?, ?, ?)
1693 }
1694 }
1695
1696 query! {
1697 pub fn get_git_graph(
1698 item_id: workspace::ItemId,
1699 workspace_id: workspace::WorkspaceId
1700 ) -> Result<bool> {
1701 SELECT is_open
1702 FROM git_graphs
1703 WHERE item_id = ? AND workspace_id = ?
1704 }
1705 }
1706 }
1707}
1708
1709#[cfg(test)]
1710mod tests {
1711 use super::*;
1712 use anyhow::{Context, Result, bail};
1713 use collections::{HashMap, HashSet};
1714 use fs::FakeFs;
1715 use git::Oid;
1716 use git::repository::InitialGraphCommitData;
1717 use gpui::TestAppContext;
1718 use project::Project;
1719 use rand::prelude::*;
1720 use serde_json::json;
1721 use settings::SettingsStore;
1722 use smallvec::{SmallVec, smallvec};
1723 use std::path::Path;
1724 use std::sync::Arc;
1725
1726 fn init_test(cx: &mut TestAppContext) {
1727 cx.update(|cx| {
1728 let settings_store = SettingsStore::test(cx);
1729 cx.set_global(settings_store);
1730 });
1731 }
1732
1733 /// Generates a random commit DAG suitable for testing git graph rendering.
1734 ///
1735 /// The commits are ordered newest-first (like git log output), so:
1736 /// - Index 0 = most recent commit (HEAD)
1737 /// - Last index = oldest commit (root, has no parents)
1738 /// - Parents of commit at index I must have index > I
1739 ///
1740 /// When `adversarial` is true, generates complex topologies with many branches
1741 /// and octopus merges. Otherwise generates more realistic linear histories
1742 /// with occasional branches.
1743 fn generate_random_commit_dag(
1744 rng: &mut StdRng,
1745 num_commits: usize,
1746 adversarial: bool,
1747 ) -> Vec<Arc<InitialGraphCommitData>> {
1748 if num_commits == 0 {
1749 return Vec::new();
1750 }
1751
1752 let mut commits: Vec<Arc<InitialGraphCommitData>> = Vec::with_capacity(num_commits);
1753 let oids: Vec<Oid> = (0..num_commits).map(|_| Oid::random(rng)).collect();
1754
1755 for i in 0..num_commits {
1756 let sha = oids[i];
1757
1758 let parents = if i == num_commits - 1 {
1759 smallvec![]
1760 } else {
1761 generate_parents_from_oids(rng, &oids, i, num_commits, adversarial)
1762 };
1763
1764 let ref_names = if i == 0 {
1765 vec!["HEAD".into(), "main".into()]
1766 } else if adversarial && rng.random_bool(0.1) {
1767 vec![format!("branch-{}", i).into()]
1768 } else {
1769 Vec::new()
1770 };
1771
1772 commits.push(Arc::new(InitialGraphCommitData {
1773 sha,
1774 parents,
1775 ref_names,
1776 }));
1777 }
1778
1779 commits
1780 }
1781
1782 fn generate_parents_from_oids(
1783 rng: &mut StdRng,
1784 oids: &[Oid],
1785 current_idx: usize,
1786 num_commits: usize,
1787 adversarial: bool,
1788 ) -> SmallVec<[Oid; 1]> {
1789 let remaining = num_commits - current_idx - 1;
1790 if remaining == 0 {
1791 return smallvec![];
1792 }
1793
1794 if adversarial {
1795 let merge_chance = 0.4;
1796 let octopus_chance = 0.15;
1797
1798 if remaining >= 3 && rng.random_bool(octopus_chance) {
1799 let num_parents = rng.random_range(3..=remaining.min(5));
1800 let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
1801 parent_indices.shuffle(rng);
1802 parent_indices
1803 .into_iter()
1804 .take(num_parents)
1805 .map(|idx| oids[idx])
1806 .collect()
1807 } else if remaining >= 2 && rng.random_bool(merge_chance) {
1808 let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
1809 parent_indices.shuffle(rng);
1810 parent_indices
1811 .into_iter()
1812 .take(2)
1813 .map(|idx| oids[idx])
1814 .collect()
1815 } else {
1816 let parent_idx = rng.random_range(current_idx + 1..num_commits);
1817 smallvec![oids[parent_idx]]
1818 }
1819 } else {
1820 let merge_chance = 0.15;
1821 let skip_chance = 0.1;
1822
1823 if remaining >= 2 && rng.random_bool(merge_chance) {
1824 let first_parent = current_idx + 1;
1825 let second_parent = rng.random_range(current_idx + 2..num_commits);
1826 smallvec![oids[first_parent], oids[second_parent]]
1827 } else if rng.random_bool(skip_chance) && remaining >= 2 {
1828 let skip = rng.random_range(1..remaining.min(3));
1829 smallvec![oids[current_idx + 1 + skip]]
1830 } else {
1831 smallvec![oids[current_idx + 1]]
1832 }
1833 }
1834 }
1835
1836 fn build_oid_to_row_map(graph: &GraphData) -> HashMap<Oid, usize> {
1837 graph
1838 .commits
1839 .iter()
1840 .enumerate()
1841 .map(|(idx, entry)| (entry.data.sha, idx))
1842 .collect()
1843 }
1844
1845 fn verify_commit_order(
1846 graph: &GraphData,
1847 commits: &[Arc<InitialGraphCommitData>],
1848 ) -> Result<()> {
1849 if graph.commits.len() != commits.len() {
1850 bail!(
1851 "Commit count mismatch: graph has {} commits, expected {}",
1852 graph.commits.len(),
1853 commits.len()
1854 );
1855 }
1856
1857 for (idx, (graph_commit, expected_commit)) in
1858 graph.commits.iter().zip(commits.iter()).enumerate()
1859 {
1860 if graph_commit.data.sha != expected_commit.sha {
1861 bail!(
1862 "Commit order mismatch at index {}: graph has {:?}, expected {:?}",
1863 idx,
1864 graph_commit.data.sha,
1865 expected_commit.sha
1866 );
1867 }
1868 }
1869
1870 Ok(())
1871 }
1872
1873 fn verify_line_endpoints(graph: &GraphData, oid_to_row: &HashMap<Oid, usize>) -> Result<()> {
1874 for line in &graph.lines {
1875 let child_row = *oid_to_row
1876 .get(&line.child)
1877 .context("Line references non-existent child commit")?;
1878
1879 let parent_row = *oid_to_row
1880 .get(&line.parent)
1881 .context("Line references non-existent parent commit")?;
1882
1883 if child_row >= parent_row {
1884 bail!(
1885 "child_row ({}) must be < parent_row ({})",
1886 child_row,
1887 parent_row
1888 );
1889 }
1890
1891 if line.full_interval.start != child_row {
1892 bail!(
1893 "full_interval.start ({}) != child_row ({})",
1894 line.full_interval.start,
1895 child_row
1896 );
1897 }
1898
1899 if line.full_interval.end != parent_row {
1900 bail!(
1901 "full_interval.end ({}) != parent_row ({})",
1902 line.full_interval.end,
1903 parent_row
1904 );
1905 }
1906
1907 if let Some(last_segment) = line.segments.last() {
1908 let segment_end_row = match last_segment {
1909 CommitLineSegment::Straight { to_row } => *to_row,
1910 CommitLineSegment::Curve { on_row, .. } => *on_row,
1911 };
1912
1913 if segment_end_row != line.full_interval.end {
1914 bail!(
1915 "last segment ends at row {} but full_interval.end is {}",
1916 segment_end_row,
1917 line.full_interval.end
1918 );
1919 }
1920 }
1921 }
1922
1923 Ok(())
1924 }
1925
1926 fn verify_column_correctness(
1927 graph: &GraphData,
1928 oid_to_row: &HashMap<Oid, usize>,
1929 ) -> Result<()> {
1930 for line in &graph.lines {
1931 let child_row = *oid_to_row
1932 .get(&line.child)
1933 .context("Line references non-existent child commit")?;
1934
1935 let parent_row = *oid_to_row
1936 .get(&line.parent)
1937 .context("Line references non-existent parent commit")?;
1938
1939 let child_lane = graph.commits[child_row].lane;
1940 if line.child_column != child_lane {
1941 bail!(
1942 "child_column ({}) != child's lane ({})",
1943 line.child_column,
1944 child_lane
1945 );
1946 }
1947
1948 let mut current_column = line.child_column;
1949 for segment in &line.segments {
1950 if let CommitLineSegment::Curve { to_column, .. } = segment {
1951 current_column = *to_column;
1952 }
1953 }
1954
1955 let parent_lane = graph.commits[parent_row].lane;
1956 if current_column != parent_lane {
1957 bail!(
1958 "ending column ({}) != parent's lane ({})",
1959 current_column,
1960 parent_lane
1961 );
1962 }
1963 }
1964
1965 Ok(())
1966 }
1967
1968 fn verify_segment_continuity(graph: &GraphData) -> Result<()> {
1969 for line in &graph.lines {
1970 if line.segments.is_empty() {
1971 bail!("Line has no segments");
1972 }
1973
1974 let mut current_row = line.full_interval.start;
1975
1976 for (idx, segment) in line.segments.iter().enumerate() {
1977 let segment_end_row = match segment {
1978 CommitLineSegment::Straight { to_row } => *to_row,
1979 CommitLineSegment::Curve { on_row, .. } => *on_row,
1980 };
1981
1982 if segment_end_row < current_row {
1983 bail!(
1984 "segment {} ends at row {} which is before current row {}",
1985 idx,
1986 segment_end_row,
1987 current_row
1988 );
1989 }
1990
1991 current_row = segment_end_row;
1992 }
1993 }
1994
1995 Ok(())
1996 }
1997
1998 fn verify_line_overlaps(graph: &GraphData) -> Result<()> {
1999 for line in &graph.lines {
2000 let child_row = line.full_interval.start;
2001
2002 let mut current_column = line.child_column;
2003 let mut current_row = child_row;
2004
2005 for segment in &line.segments {
2006 match segment {
2007 CommitLineSegment::Straight { to_row } => {
2008 for row in (current_row + 1)..*to_row {
2009 if row < graph.commits.len() {
2010 let commit_at_row = &graph.commits[row];
2011 if commit_at_row.lane == current_column {
2012 bail!(
2013 "straight segment from row {} to {} in column {} passes through commit {:?} at row {}",
2014 current_row,
2015 to_row,
2016 current_column,
2017 commit_at_row.data.sha,
2018 row
2019 );
2020 }
2021 }
2022 }
2023 current_row = *to_row;
2024 }
2025 CommitLineSegment::Curve {
2026 to_column, on_row, ..
2027 } => {
2028 current_column = *to_column;
2029 current_row = *on_row;
2030 }
2031 }
2032 }
2033 }
2034
2035 Ok(())
2036 }
2037
2038 fn verify_coverage(graph: &GraphData) -> Result<()> {
2039 let mut expected_edges: HashSet<(Oid, Oid)> = HashSet::default();
2040 for entry in &graph.commits {
2041 for parent in &entry.data.parents {
2042 expected_edges.insert((entry.data.sha, *parent));
2043 }
2044 }
2045
2046 let mut found_edges: HashSet<(Oid, Oid)> = HashSet::default();
2047 for line in &graph.lines {
2048 let edge = (line.child, line.parent);
2049
2050 if !found_edges.insert(edge) {
2051 bail!(
2052 "Duplicate line found for edge {:?} -> {:?}",
2053 line.child,
2054 line.parent
2055 );
2056 }
2057
2058 if !expected_edges.contains(&edge) {
2059 bail!(
2060 "Orphan line found: {:?} -> {:?} is not in the commit graph",
2061 line.child,
2062 line.parent
2063 );
2064 }
2065 }
2066
2067 for (child, parent) in &expected_edges {
2068 if !found_edges.contains(&(*child, *parent)) {
2069 bail!("Missing line for edge {:?} -> {:?}", child, parent);
2070 }
2071 }
2072
2073 assert_eq!(
2074 expected_edges.symmetric_difference(&found_edges).count(),
2075 0,
2076 "The symmetric difference should be zero"
2077 );
2078
2079 Ok(())
2080 }
2081
2082 fn verify_merge_line_optimality(
2083 graph: &GraphData,
2084 oid_to_row: &HashMap<Oid, usize>,
2085 ) -> Result<()> {
2086 for line in &graph.lines {
2087 let first_segment = line.segments.first();
2088 let is_merge_line = matches!(
2089 first_segment,
2090 Some(CommitLineSegment::Curve {
2091 curve_kind: CurveKind::Merge,
2092 ..
2093 })
2094 );
2095
2096 if !is_merge_line {
2097 continue;
2098 }
2099
2100 let child_row = *oid_to_row
2101 .get(&line.child)
2102 .context("Line references non-existent child commit")?;
2103
2104 let parent_row = *oid_to_row
2105 .get(&line.parent)
2106 .context("Line references non-existent parent commit")?;
2107
2108 let parent_lane = graph.commits[parent_row].lane;
2109
2110 let Some(CommitLineSegment::Curve { to_column, .. }) = first_segment else {
2111 continue;
2112 };
2113
2114 let curves_directly_to_parent = *to_column == parent_lane;
2115
2116 if !curves_directly_to_parent {
2117 continue;
2118 }
2119
2120 let curve_row = child_row + 1;
2121 let has_commits_in_path = graph.commits[curve_row..parent_row]
2122 .iter()
2123 .any(|c| c.lane == parent_lane);
2124
2125 if has_commits_in_path {
2126 bail!(
2127 "Merge line from {:?} to {:?} curves directly to parent lane {} but there are commits in that lane between rows {} and {}",
2128 line.child,
2129 line.parent,
2130 parent_lane,
2131 curve_row,
2132 parent_row
2133 );
2134 }
2135
2136 let curve_ends_at_parent = curve_row == parent_row;
2137
2138 if curve_ends_at_parent {
2139 if line.segments.len() != 1 {
2140 bail!(
2141 "Merge line from {:?} to {:?} curves directly to parent (curve_row == parent_row), but has {} segments instead of 1 [MergeCurve]",
2142 line.child,
2143 line.parent,
2144 line.segments.len()
2145 );
2146 }
2147 } else {
2148 if line.segments.len() != 2 {
2149 bail!(
2150 "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but has {} segments instead of 2 [MergeCurve, Straight]",
2151 line.child,
2152 line.parent,
2153 line.segments.len()
2154 );
2155 }
2156
2157 let is_straight_segment = matches!(
2158 line.segments.get(1),
2159 Some(CommitLineSegment::Straight { .. })
2160 );
2161
2162 if !is_straight_segment {
2163 bail!(
2164 "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but second segment is not a Straight segment",
2165 line.child,
2166 line.parent
2167 );
2168 }
2169 }
2170 }
2171
2172 Ok(())
2173 }
2174
2175 fn verify_all_invariants(
2176 graph: &GraphData,
2177 commits: &[Arc<InitialGraphCommitData>],
2178 ) -> Result<()> {
2179 let oid_to_row = build_oid_to_row_map(graph);
2180
2181 verify_commit_order(graph, commits).context("commit order")?;
2182 verify_line_endpoints(graph, &oid_to_row).context("line endpoints")?;
2183 verify_column_correctness(graph, &oid_to_row).context("column correctness")?;
2184 verify_segment_continuity(graph).context("segment continuity")?;
2185 verify_merge_line_optimality(graph, &oid_to_row).context("merge line optimality")?;
2186 verify_coverage(graph).context("coverage")?;
2187 verify_line_overlaps(graph).context("line overlaps")?;
2188 Ok(())
2189 }
2190
2191 #[test]
2192 fn test_git_graph_merge_commits() {
2193 let mut rng = StdRng::seed_from_u64(42);
2194
2195 let oid1 = Oid::random(&mut rng);
2196 let oid2 = Oid::random(&mut rng);
2197 let oid3 = Oid::random(&mut rng);
2198 let oid4 = Oid::random(&mut rng);
2199
2200 let commits = vec![
2201 Arc::new(InitialGraphCommitData {
2202 sha: oid1,
2203 parents: smallvec![oid2, oid3],
2204 ref_names: vec!["HEAD".into()],
2205 }),
2206 Arc::new(InitialGraphCommitData {
2207 sha: oid2,
2208 parents: smallvec![oid4],
2209 ref_names: vec![],
2210 }),
2211 Arc::new(InitialGraphCommitData {
2212 sha: oid3,
2213 parents: smallvec![oid4],
2214 ref_names: vec![],
2215 }),
2216 Arc::new(InitialGraphCommitData {
2217 sha: oid4,
2218 parents: smallvec![],
2219 ref_names: vec![],
2220 }),
2221 ];
2222
2223 let mut graph_data = GraphData::new(8);
2224 graph_data.add_commits(&commits);
2225
2226 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2227 panic!("Graph invariant violation for merge commits:\n{}", error);
2228 }
2229 }
2230
2231 #[test]
2232 fn test_git_graph_linear_commits() {
2233 let mut rng = StdRng::seed_from_u64(42);
2234
2235 let oid1 = Oid::random(&mut rng);
2236 let oid2 = Oid::random(&mut rng);
2237 let oid3 = Oid::random(&mut rng);
2238
2239 let commits = vec![
2240 Arc::new(InitialGraphCommitData {
2241 sha: oid1,
2242 parents: smallvec![oid2],
2243 ref_names: vec!["HEAD".into()],
2244 }),
2245 Arc::new(InitialGraphCommitData {
2246 sha: oid2,
2247 parents: smallvec![oid3],
2248 ref_names: vec![],
2249 }),
2250 Arc::new(InitialGraphCommitData {
2251 sha: oid3,
2252 parents: smallvec![],
2253 ref_names: vec![],
2254 }),
2255 ];
2256
2257 let mut graph_data = GraphData::new(8);
2258 graph_data.add_commits(&commits);
2259
2260 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2261 panic!("Graph invariant violation for linear commits:\n{}", error);
2262 }
2263 }
2264
2265 #[test]
2266 fn test_git_graph_random_commits() {
2267 for seed in 0..100 {
2268 let mut rng = StdRng::seed_from_u64(seed);
2269
2270 let adversarial = rng.random_bool(0.2);
2271 let num_commits = if adversarial {
2272 rng.random_range(10..100)
2273 } else {
2274 rng.random_range(5..50)
2275 };
2276
2277 let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
2278
2279 assert_eq!(
2280 num_commits,
2281 commits.len(),
2282 "seed={}: Generate random commit dag didn't generate the correct amount of commits",
2283 seed
2284 );
2285
2286 let mut graph_data = GraphData::new(8);
2287 graph_data.add_commits(&commits);
2288
2289 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2290 panic!(
2291 "Graph invariant violation (seed={}, adversarial={}, num_commits={}):\n{:#}",
2292 seed, adversarial, num_commits, error
2293 );
2294 }
2295 }
2296 }
2297
2298 // The full integration test has less iterations because it's significantly slower
2299 // than the random commit test
2300 #[gpui::test(iterations = 5)]
2301 async fn test_git_graph_random_integration(mut rng: StdRng, cx: &mut TestAppContext) {
2302 init_test(cx);
2303
2304 let adversarial = rng.random_bool(0.2);
2305 let num_commits = if adversarial {
2306 rng.random_range(10..100)
2307 } else {
2308 rng.random_range(5..50)
2309 };
2310
2311 let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
2312
2313 let fs = FakeFs::new(cx.executor());
2314 fs.insert_tree(
2315 Path::new("/project"),
2316 json!({
2317 ".git": {},
2318 "file.txt": "content",
2319 }),
2320 )
2321 .await;
2322
2323 fs.set_graph_commits(Path::new("/project/.git"), commits.clone());
2324
2325 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
2326 cx.run_until_parked();
2327
2328 let repository = project.read_with(cx, |project, cx| {
2329 project
2330 .active_repository(cx)
2331 .expect("should have a repository")
2332 });
2333
2334 repository.update(cx, |repo, cx| {
2335 repo.graph_data(
2336 crate::LogSource::default(),
2337 crate::LogOrder::default(),
2338 0..usize::MAX,
2339 cx,
2340 );
2341 });
2342 cx.run_until_parked();
2343
2344 let graph_commits: Vec<Arc<InitialGraphCommitData>> = repository.update(cx, |repo, cx| {
2345 repo.graph_data(
2346 crate::LogSource::default(),
2347 crate::LogOrder::default(),
2348 0..usize::MAX,
2349 cx,
2350 )
2351 .to_vec()
2352 });
2353
2354 let mut graph_data = GraphData::new(8);
2355 graph_data.add_commits(&graph_commits);
2356
2357 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2358 panic!(
2359 "Graph invariant violation (adversarial={}, num_commits={}):\n{:#}",
2360 adversarial, num_commits, error
2361 );
2362 }
2363 }
2364}