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