1use collections::{BTreeMap, HashMap};
2use feature_flags::{FeatureFlag, FeatureFlagAppExt as _};
3use git::{
4 BuildCommitPermalinkParams, GitHostingProviderRegistry, GitRemote, Oid, ParsedGitRemote,
5 parse_git_remote_url,
6 repository::{CommitDiff, CommitFile, InitialGraphCommitData, LogOrder, LogSource, RepoPath},
7 status::{FileStatus, StatusCode, TrackedStatus},
8};
9use git_ui::{commit_tooltip::CommitAvatar, commit_view::CommitView, git_status_icon};
10use gpui::{
11 AnyElement, App, Bounds, ClickEvent, ClipboardItem, Corner, DefiniteLength, DragMoveEvent,
12 ElementId, Empty, Entity, EventEmitter, FocusHandle, Focusable, Hsla, PathBuilder, Pixels,
13 Point, ScrollStrategy, ScrollWheelEvent, SharedString, Subscription, Task,
14 UniformListScrollHandle, WeakEntity, Window, actions, anchored, deferred, point, prelude::*,
15 px, uniform_list,
16};
17use language::line_diff;
18use menu::{Cancel, SelectNext, SelectPrevious};
19use project::{
20 Project,
21 git_store::{CommitDataState, GitStoreEvent, Repository, RepositoryEvent, RepositoryId},
22};
23use settings::Settings;
24use smallvec::{SmallVec, smallvec};
25use std::{
26 cell::Cell,
27 ops::Range,
28 rc::Rc,
29 sync::Arc,
30 sync::OnceLock,
31 time::{Duration, Instant},
32};
33use theme::{AccentColors, ThemeSettings};
34use time::{OffsetDateTime, UtcOffset, format_description::BorrowedFormatItem};
35use ui::{
36 ButtonLike, Chip, CommonAnimationExt as _, ContextMenu, DiffStat, Divider, ScrollableHandle,
37 Table, TableColumnWidths, TableInteractionState, TableResizeBehavior, Tooltip, WithScrollbar,
38 prelude::*,
39};
40use workspace::{
41 Workspace,
42 item::{Item, ItemEvent, SerializableItem},
43};
44
45const COMMIT_CIRCLE_RADIUS: Pixels = px(3.5);
46const COMMIT_CIRCLE_STROKE_WIDTH: Pixels = px(1.5);
47const LANE_WIDTH: Pixels = px(16.0);
48const LEFT_PADDING: Pixels = px(12.0);
49const LINE_WIDTH: Pixels = px(1.5);
50const RESIZE_HANDLE_WIDTH: f32 = 8.0;
51const COPIED_STATE_DURATION: Duration = Duration::from_secs(2);
52
53struct CopiedState {
54 copied_at: Option<Instant>,
55}
56
57impl CopiedState {
58 fn new(_window: &mut Window, _cx: &mut Context<Self>) -> Self {
59 Self { copied_at: None }
60 }
61
62 fn is_copied(&self) -> bool {
63 self.copied_at
64 .map(|t| t.elapsed() < COPIED_STATE_DURATION)
65 .unwrap_or(false)
66 }
67
68 fn mark_copied(&mut self) {
69 self.copied_at = Some(Instant::now());
70 }
71}
72
73struct DraggedSplitHandle;
74
75#[derive(Clone)]
76struct ChangedFileEntry {
77 status: FileStatus,
78 file_name: SharedString,
79 dir_path: SharedString,
80 repo_path: RepoPath,
81}
82
83impl ChangedFileEntry {
84 fn from_commit_file(file: &CommitFile, _cx: &App) -> Self {
85 let file_name: SharedString = file
86 .path
87 .file_name()
88 .map(|n| n.to_string())
89 .unwrap_or_default()
90 .into();
91 let dir_path: SharedString = file
92 .path
93 .parent()
94 .map(|p| p.as_unix_str().to_string())
95 .unwrap_or_default()
96 .into();
97
98 let status_code = match (&file.old_text, &file.new_text) {
99 (None, Some(_)) => StatusCode::Added,
100 (Some(_), None) => StatusCode::Deleted,
101 _ => StatusCode::Modified,
102 };
103
104 let status = FileStatus::Tracked(TrackedStatus {
105 index_status: status_code,
106 worktree_status: StatusCode::Unmodified,
107 });
108
109 Self {
110 status,
111 file_name,
112 dir_path,
113 repo_path: file.path.clone(),
114 }
115 }
116
117 fn open_in_commit_view(
118 &self,
119 commit_sha: &SharedString,
120 repository: &WeakEntity<Repository>,
121 workspace: &WeakEntity<Workspace>,
122 window: &mut Window,
123 cx: &mut App,
124 ) {
125 CommitView::open(
126 commit_sha.to_string(),
127 repository.clone(),
128 workspace.clone(),
129 None,
130 Some(self.repo_path.clone()),
131 window,
132 cx,
133 );
134 }
135
136 fn render(
137 &self,
138 ix: usize,
139 commit_sha: SharedString,
140 repository: WeakEntity<Repository>,
141 workspace: WeakEntity<Workspace>,
142 _cx: &App,
143 ) -> AnyElement {
144 let file_name = self.file_name.clone();
145 let dir_path = self.dir_path.clone();
146
147 div()
148 .w_full()
149 .child(
150 ButtonLike::new(("changed-file", ix))
151 .child(
152 h_flex()
153 .min_w_0()
154 .w_full()
155 .gap_1()
156 .overflow_hidden()
157 .child(git_status_icon(self.status))
158 .child(
159 Label::new(file_name.clone())
160 .size(LabelSize::Small)
161 .truncate(),
162 )
163 .when(!dir_path.is_empty(), |this| {
164 this.child(
165 Label::new(dir_path.clone())
166 .size(LabelSize::Small)
167 .color(Color::Muted)
168 .truncate_start(),
169 )
170 }),
171 )
172 .tooltip({
173 let meta = if dir_path.is_empty() {
174 file_name
175 } else {
176 format!("{}/{}", dir_path, file_name).into()
177 };
178 move |_, cx| Tooltip::with_meta("View Changes", None, meta.clone(), cx)
179 })
180 .on_click({
181 let entry = self.clone();
182 move |_, window, cx| {
183 entry.open_in_commit_view(
184 &commit_sha,
185 &repository,
186 &workspace,
187 window,
188 cx,
189 );
190 }
191 }),
192 )
193 .into_any_element()
194 }
195}
196
197pub struct SplitState {
198 left_ratio: f32,
199 visible_left_ratio: f32,
200}
201
202impl SplitState {
203 pub fn new() -> Self {
204 Self {
205 left_ratio: 1.0,
206 visible_left_ratio: 1.0,
207 }
208 }
209
210 pub fn right_ratio(&self) -> f32 {
211 1.0 - self.visible_left_ratio
212 }
213
214 fn on_drag_move(
215 &mut self,
216 drag_event: &DragMoveEvent<DraggedSplitHandle>,
217 _window: &mut Window,
218 _cx: &mut Context<Self>,
219 ) {
220 let drag_position = drag_event.event.position;
221 let bounds = drag_event.bounds;
222 let bounds_width = bounds.right() - bounds.left();
223
224 let min_ratio = 0.1;
225 let max_ratio = 0.9;
226
227 let new_ratio = (drag_position.x - bounds.left()) / bounds_width;
228 self.visible_left_ratio = new_ratio.clamp(min_ratio, max_ratio);
229 }
230
231 fn commit_ratio(&mut self) {
232 self.left_ratio = self.visible_left_ratio;
233 }
234
235 fn on_double_click(&mut self) {
236 self.left_ratio = 1.0;
237 self.visible_left_ratio = 1.0;
238 }
239}
240
241actions!(
242 git_graph,
243 [
244 /// Opens the commit view for the selected commit.
245 OpenCommitView,
246 ]
247);
248
249pub struct GitGraphFeatureFlag;
250
251impl FeatureFlag for GitGraphFeatureFlag {
252 const NAME: &'static str = "git-graph";
253}
254
255fn timestamp_format() -> &'static [BorrowedFormatItem<'static>] {
256 static FORMAT: OnceLock<Vec<BorrowedFormatItem<'static>>> = OnceLock::new();
257 FORMAT.get_or_init(|| {
258 time::format_description::parse("[day] [month repr:short] [year] [hour]:[minute]")
259 .unwrap_or_default()
260 })
261}
262
263fn format_timestamp(timestamp: i64) -> String {
264 let Ok(datetime) = OffsetDateTime::from_unix_timestamp(timestamp) else {
265 return "Unknown".to_string();
266 };
267
268 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
269 let local_datetime = datetime.to_offset(local_offset);
270
271 local_datetime
272 .format(timestamp_format())
273 .unwrap_or_default()
274}
275
276fn accent_colors_count(accents: &AccentColors) -> usize {
277 accents.0.len()
278}
279
280#[derive(Copy, Clone, Debug)]
281struct BranchColor(u8);
282
283#[derive(Debug)]
284enum LaneState {
285 Empty,
286 Active {
287 child: Oid,
288 parent: Oid,
289 color: Option<BranchColor>,
290 starting_row: usize,
291 starting_col: usize,
292 destination_column: Option<usize>,
293 segments: SmallVec<[CommitLineSegment; 1]>,
294 },
295}
296
297impl LaneState {
298 fn to_commit_lines(
299 &mut self,
300 ending_row: usize,
301 lane_column: usize,
302 parent_column: usize,
303 parent_color: BranchColor,
304 ) -> Option<CommitLine> {
305 let state = std::mem::replace(self, LaneState::Empty);
306
307 match state {
308 LaneState::Active {
309 #[cfg_attr(not(test), allow(unused_variables))]
310 parent,
311 #[cfg_attr(not(test), allow(unused_variables))]
312 child,
313 color,
314 starting_row,
315 starting_col,
316 destination_column,
317 mut segments,
318 } => {
319 let final_destination = destination_column.unwrap_or(parent_column);
320 let final_color = color.unwrap_or(parent_color);
321
322 Some(CommitLine {
323 #[cfg(test)]
324 child,
325 #[cfg(test)]
326 parent,
327 child_column: starting_col,
328 full_interval: starting_row..ending_row,
329 color_idx: final_color.0 as usize,
330 segments: {
331 match segments.last_mut() {
332 Some(CommitLineSegment::Straight { to_row })
333 if *to_row == usize::MAX =>
334 {
335 if final_destination != lane_column {
336 *to_row = ending_row - 1;
337
338 let curved_line = CommitLineSegment::Curve {
339 to_column: final_destination,
340 on_row: ending_row,
341 curve_kind: CurveKind::Checkout,
342 };
343
344 if *to_row == starting_row {
345 let last_index = segments.len() - 1;
346 segments[last_index] = curved_line;
347 } else {
348 segments.push(curved_line);
349 }
350 } else {
351 *to_row = ending_row;
352 }
353 }
354 Some(CommitLineSegment::Curve {
355 on_row,
356 to_column,
357 curve_kind,
358 }) if *on_row == usize::MAX => {
359 if *to_column == usize::MAX {
360 *to_column = final_destination;
361 }
362 if matches!(curve_kind, CurveKind::Merge) {
363 *on_row = starting_row + 1;
364 if *on_row < ending_row {
365 if *to_column != final_destination {
366 segments.push(CommitLineSegment::Straight {
367 to_row: ending_row - 1,
368 });
369 segments.push(CommitLineSegment::Curve {
370 to_column: final_destination,
371 on_row: ending_row,
372 curve_kind: CurveKind::Checkout,
373 });
374 } else {
375 segments.push(CommitLineSegment::Straight {
376 to_row: ending_row,
377 });
378 }
379 } else if *to_column != final_destination {
380 segments.push(CommitLineSegment::Curve {
381 to_column: final_destination,
382 on_row: ending_row,
383 curve_kind: CurveKind::Checkout,
384 });
385 }
386 } else {
387 *on_row = ending_row;
388 if *to_column != final_destination {
389 segments.push(CommitLineSegment::Straight {
390 to_row: ending_row,
391 });
392 segments.push(CommitLineSegment::Curve {
393 to_column: final_destination,
394 on_row: ending_row,
395 curve_kind: CurveKind::Checkout,
396 });
397 }
398 }
399 }
400 Some(CommitLineSegment::Curve {
401 on_row, to_column, ..
402 }) => {
403 if *on_row < ending_row {
404 if *to_column != final_destination {
405 segments.push(CommitLineSegment::Straight {
406 to_row: ending_row - 1,
407 });
408 segments.push(CommitLineSegment::Curve {
409 to_column: final_destination,
410 on_row: ending_row,
411 curve_kind: CurveKind::Checkout,
412 });
413 } else {
414 segments.push(CommitLineSegment::Straight {
415 to_row: ending_row,
416 });
417 }
418 } else if *to_column != final_destination {
419 segments.push(CommitLineSegment::Curve {
420 to_column: final_destination,
421 on_row: ending_row,
422 curve_kind: CurveKind::Checkout,
423 });
424 }
425 }
426 _ => {}
427 }
428
429 segments
430 },
431 })
432 }
433 LaneState::Empty => None,
434 }
435 }
436
437 fn is_empty(&self) -> bool {
438 match self {
439 LaneState::Empty => true,
440 LaneState::Active { .. } => false,
441 }
442 }
443}
444
445struct CommitEntry {
446 data: Arc<InitialGraphCommitData>,
447 lane: usize,
448 color_idx: usize,
449}
450
451type ActiveLaneIdx = usize;
452
453enum AllCommitCount {
454 NotLoaded,
455 Loaded(usize),
456}
457
458#[derive(Debug)]
459enum CurveKind {
460 Merge,
461 Checkout,
462}
463
464#[derive(Debug)]
465enum CommitLineSegment {
466 Straight {
467 to_row: usize,
468 },
469 Curve {
470 to_column: usize,
471 on_row: usize,
472 curve_kind: CurveKind,
473 },
474}
475
476#[derive(Debug)]
477struct CommitLine {
478 #[cfg(test)]
479 child: Oid,
480 #[cfg(test)]
481 parent: Oid,
482 child_column: usize,
483 full_interval: Range<usize>,
484 color_idx: usize,
485 segments: SmallVec<[CommitLineSegment; 1]>,
486}
487
488impl CommitLine {
489 fn get_first_visible_segment_idx(&self, first_visible_row: usize) -> Option<(usize, usize)> {
490 if first_visible_row > self.full_interval.end {
491 return None;
492 } else if first_visible_row <= self.full_interval.start {
493 return Some((0, self.child_column));
494 }
495
496 let mut current_column = self.child_column;
497
498 for (idx, segment) in self.segments.iter().enumerate() {
499 match segment {
500 CommitLineSegment::Straight { to_row } => {
501 if *to_row >= first_visible_row {
502 return Some((idx, current_column));
503 }
504 }
505 CommitLineSegment::Curve {
506 to_column, on_row, ..
507 } => {
508 if *on_row >= first_visible_row {
509 return Some((idx, current_column));
510 }
511 current_column = *to_column;
512 }
513 }
514 }
515
516 None
517 }
518}
519
520#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
521struct CommitLineKey {
522 child: Oid,
523 parent: Oid,
524}
525
526struct GraphData {
527 lane_states: SmallVec<[LaneState; 8]>,
528 lane_colors: HashMap<ActiveLaneIdx, BranchColor>,
529 parent_to_lanes: HashMap<Oid, SmallVec<[usize; 1]>>,
530 next_color: BranchColor,
531 accent_colors_count: usize,
532 commits: Vec<Rc<CommitEntry>>,
533 max_commit_count: AllCommitCount,
534 max_lanes: usize,
535 lines: Vec<Rc<CommitLine>>,
536 active_commit_lines: HashMap<CommitLineKey, usize>,
537 active_commit_lines_by_parent: HashMap<Oid, SmallVec<[usize; 1]>>,
538}
539
540impl GraphData {
541 fn new(accent_colors_count: usize) -> Self {
542 GraphData {
543 lane_states: SmallVec::default(),
544 lane_colors: HashMap::default(),
545 parent_to_lanes: HashMap::default(),
546 next_color: BranchColor(0),
547 accent_colors_count,
548 commits: Vec::default(),
549 max_commit_count: AllCommitCount::NotLoaded,
550 max_lanes: 0,
551 lines: Vec::default(),
552 active_commit_lines: HashMap::default(),
553 active_commit_lines_by_parent: HashMap::default(),
554 }
555 }
556
557 fn clear(&mut self) {
558 self.lane_states.clear();
559 self.lane_colors.clear();
560 self.parent_to_lanes.clear();
561 self.commits.clear();
562 self.lines.clear();
563 self.active_commit_lines.clear();
564 self.active_commit_lines_by_parent.clear();
565 self.next_color = BranchColor(0);
566 self.max_commit_count = AllCommitCount::NotLoaded;
567 self.max_lanes = 0;
568 }
569
570 fn first_empty_lane_idx(&mut self) -> ActiveLaneIdx {
571 self.lane_states
572 .iter()
573 .position(LaneState::is_empty)
574 .unwrap_or_else(|| {
575 self.lane_states.push(LaneState::Empty);
576 self.lane_states.len() - 1
577 })
578 }
579
580 fn get_lane_color(&mut self, lane_idx: ActiveLaneIdx) -> BranchColor {
581 let accent_colors_count = self.accent_colors_count;
582 *self.lane_colors.entry(lane_idx).or_insert_with(|| {
583 let color_idx = self.next_color;
584 self.next_color = BranchColor((self.next_color.0 + 1) % accent_colors_count as u8);
585 color_idx
586 })
587 }
588
589 fn add_commits(&mut self, commits: &[Arc<InitialGraphCommitData>]) {
590 self.commits.reserve(commits.len());
591 self.lines.reserve(commits.len() / 2);
592
593 for commit in commits.iter() {
594 let commit_row = self.commits.len();
595
596 let commit_lane = self
597 .parent_to_lanes
598 .get(&commit.sha)
599 .and_then(|lanes| lanes.first().copied());
600
601 let commit_lane = commit_lane.unwrap_or_else(|| self.first_empty_lane_idx());
602
603 let commit_color = self.get_lane_color(commit_lane);
604
605 if let Some(lanes) = self.parent_to_lanes.remove(&commit.sha) {
606 for lane_column in lanes {
607 let state = &mut self.lane_states[lane_column];
608
609 if let LaneState::Active {
610 starting_row,
611 segments,
612 ..
613 } = state
614 {
615 if let Some(CommitLineSegment::Curve {
616 to_column,
617 curve_kind: CurveKind::Merge,
618 ..
619 }) = segments.first_mut()
620 {
621 let curve_row = *starting_row + 1;
622 let would_overlap =
623 if lane_column != commit_lane && curve_row < commit_row {
624 self.commits[curve_row..commit_row]
625 .iter()
626 .any(|c| c.lane == commit_lane)
627 } else {
628 false
629 };
630
631 if would_overlap {
632 *to_column = lane_column;
633 }
634 }
635 }
636
637 if let Some(commit_line) =
638 state.to_commit_lines(commit_row, lane_column, commit_lane, commit_color)
639 {
640 self.lines.push(Rc::new(commit_line));
641 }
642 }
643 }
644
645 commit
646 .parents
647 .iter()
648 .enumerate()
649 .for_each(|(parent_idx, parent)| {
650 if parent_idx == 0 {
651 self.lane_states[commit_lane] = LaneState::Active {
652 parent: *parent,
653 child: commit.sha,
654 color: Some(commit_color),
655 starting_col: commit_lane,
656 starting_row: commit_row,
657 destination_column: None,
658 segments: smallvec![CommitLineSegment::Straight { to_row: usize::MAX }],
659 };
660
661 self.parent_to_lanes
662 .entry(*parent)
663 .or_default()
664 .push(commit_lane);
665 } else {
666 let new_lane = self.first_empty_lane_idx();
667
668 self.lane_states[new_lane] = LaneState::Active {
669 parent: *parent,
670 child: commit.sha,
671 color: None,
672 starting_col: commit_lane,
673 starting_row: commit_row,
674 destination_column: None,
675 segments: smallvec![CommitLineSegment::Curve {
676 to_column: usize::MAX,
677 on_row: usize::MAX,
678 curve_kind: CurveKind::Merge,
679 },],
680 };
681
682 self.parent_to_lanes
683 .entry(*parent)
684 .or_default()
685 .push(new_lane);
686 }
687 });
688
689 self.max_lanes = self.max_lanes.max(self.lane_states.len());
690
691 self.commits.push(Rc::new(CommitEntry {
692 data: commit.clone(),
693 lane: commit_lane,
694 color_idx: commit_color.0 as usize,
695 }));
696 }
697
698 self.max_commit_count = AllCommitCount::Loaded(self.commits.len());
699 }
700}
701
702pub fn init(cx: &mut App) {
703 workspace::register_serializable_item::<GitGraph>(cx);
704
705 cx.observe_new(|workspace: &mut workspace::Workspace, _, _| {
706 workspace.register_action_renderer(|div, workspace, _, cx| {
707 div.when(
708 workspace.project().read(cx).active_repository(cx).is_some()
709 && cx.has_flag::<GitGraphFeatureFlag>(),
710 |div| {
711 let workspace = workspace.weak_handle();
712
713 div.on_action(move |_: &git_ui::git_panel::Open, window, cx| {
714 workspace
715 .update(cx, |workspace, cx| {
716 let existing = workspace.items_of_type::<GitGraph>(cx).next();
717 if let Some(existing) = existing {
718 workspace.activate_item(&existing, true, true, window, cx);
719 return;
720 }
721
722 let project = workspace.project().clone();
723 let workspace_handle = workspace.weak_handle();
724 let git_graph = cx
725 .new(|cx| GitGraph::new(project, workspace_handle, window, cx));
726 workspace.add_item_to_active_pane(
727 Box::new(git_graph),
728 None,
729 true,
730 window,
731 cx,
732 );
733 })
734 .ok();
735 })
736 },
737 )
738 });
739 })
740 .detach();
741}
742
743fn lane_center_x(bounds: Bounds<Pixels>, lane: f32, horizontal_scroll_offset: Pixels) -> Pixels {
744 bounds.origin.x + LEFT_PADDING + lane * LANE_WIDTH + LANE_WIDTH / 2.0 - horizontal_scroll_offset
745}
746
747fn to_row_center(
748 to_row: usize,
749 row_height: Pixels,
750 scroll_offset: Pixels,
751 bounds: Bounds<Pixels>,
752) -> Pixels {
753 bounds.origin.y + to_row as f32 * row_height + row_height / 2.0 - scroll_offset
754}
755
756fn draw_commit_circle(center_x: Pixels, center_y: Pixels, color: Hsla, window: &mut Window) {
757 let radius = COMMIT_CIRCLE_RADIUS;
758
759 let mut builder = PathBuilder::fill();
760
761 // Start at the rightmost point of the circle
762 builder.move_to(point(center_x + radius, center_y));
763
764 // Draw the circle using two arc_to calls (top half, then bottom half)
765 builder.arc_to(
766 point(radius, radius),
767 px(0.),
768 false,
769 true,
770 point(center_x - radius, center_y),
771 );
772 builder.arc_to(
773 point(radius, radius),
774 px(0.),
775 false,
776 true,
777 point(center_x + radius, center_y),
778 );
779 builder.close();
780
781 if let Ok(path) = builder.build() {
782 window.paint_path(path, color);
783 }
784}
785
786fn compute_diff_stats(diff: &CommitDiff) -> (usize, usize) {
787 diff.files.iter().fold((0, 0), |(added, removed), file| {
788 let old_text = file.old_text.as_deref().unwrap_or("");
789 let new_text = file.new_text.as_deref().unwrap_or("");
790 let hunks = line_diff(old_text, new_text);
791 hunks
792 .iter()
793 .fold((added, removed), |(a, r), (old_range, new_range)| {
794 (
795 a + (new_range.end - new_range.start) as usize,
796 r + (old_range.end - old_range.start) as usize,
797 )
798 })
799 })
800}
801
802pub struct GitGraph {
803 focus_handle: FocusHandle,
804 graph_data: GraphData,
805 project: Entity<Project>,
806 workspace: WeakEntity<Workspace>,
807 context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
808 row_height: Pixels,
809 table_interaction_state: Entity<TableInteractionState>,
810 table_column_widths: Entity<TableColumnWidths>,
811 horizontal_scroll_offset: Pixels,
812 graph_viewport_width: Pixels,
813 selected_entry_idx: Option<usize>,
814 hovered_entry_idx: Option<usize>,
815 graph_canvas_bounds: Rc<Cell<Option<Bounds<Pixels>>>>,
816 log_source: LogSource,
817 log_order: LogOrder,
818 selected_commit_diff: Option<CommitDiff>,
819 selected_commit_diff_stats: Option<(usize, usize)>,
820 _commit_diff_task: Option<Task<()>>,
821 commit_details_split_state: Entity<SplitState>,
822 selected_repo_id: Option<RepositoryId>,
823 changed_files_scroll_handle: UniformListScrollHandle,
824}
825
826impl GitGraph {
827 fn row_height(cx: &App) -> Pixels {
828 let settings = ThemeSettings::get_global(cx);
829 let font_size = settings.buffer_font_size(cx);
830 font_size + px(12.0)
831 }
832
833 fn graph_content_width(&self) -> Pixels {
834 (LANE_WIDTH * self.graph_data.max_lanes.min(8) as f32) + LEFT_PADDING * 2.0
835 }
836
837 pub fn new(
838 project: Entity<Project>,
839 workspace: WeakEntity<Workspace>,
840 window: &mut Window,
841 cx: &mut Context<Self>,
842 ) -> Self {
843 let focus_handle = cx.focus_handle();
844 cx.on_focus(&focus_handle, window, |_, _, cx| cx.notify())
845 .detach();
846
847 let git_store = project.read(cx).git_store().clone();
848 let accent_colors = cx.theme().accents();
849 let graph = GraphData::new(accent_colors_count(accent_colors));
850 let log_source = LogSource::default();
851 let log_order = LogOrder::default();
852
853 cx.subscribe(&git_store, |this, _, event, cx| match event {
854 GitStoreEvent::RepositoryUpdated(updated_repo_id, repo_event, _) => {
855 if this
856 .selected_repo_id
857 .as_ref()
858 .is_some_and(|repo_id| repo_id == updated_repo_id)
859 {
860 if let Some(repository) = this.get_selected_repository(cx) {
861 this.on_repository_event(repository, repo_event, cx);
862 }
863 }
864 }
865 GitStoreEvent::ActiveRepositoryChanged(changed_repo_id) => {
866 // todo(git_graph): Make this selectable from UI so we don't have to always use active repository
867 if this.selected_repo_id != *changed_repo_id {
868 this.selected_repo_id = *changed_repo_id;
869 this.graph_data.clear();
870 cx.notify();
871 }
872 }
873 _ => {}
874 })
875 .detach();
876
877 let active_repository = project
878 .read(cx)
879 .active_repository(cx)
880 .map(|repo| repo.read(cx).id);
881
882 let table_interaction_state = cx.new(|cx| TableInteractionState::new(cx));
883 let table_column_widths = cx.new(|cx| TableColumnWidths::new(4, cx));
884 let mut row_height = Self::row_height(cx);
885
886 cx.observe_global_in::<settings::SettingsStore>(window, move |this, _window, cx| {
887 let new_row_height = Self::row_height(cx);
888 if new_row_height != row_height {
889 this.row_height = new_row_height;
890 this.table_interaction_state.update(cx, |state, _cx| {
891 state.scroll_handle.0.borrow_mut().last_item_size = None;
892 });
893 row_height = new_row_height;
894 cx.notify();
895 }
896 })
897 .detach();
898
899 let mut this = GitGraph {
900 focus_handle,
901 project,
902 workspace,
903 graph_data: graph,
904 _commit_diff_task: None,
905 context_menu: None,
906 row_height,
907 table_interaction_state,
908 table_column_widths,
909 horizontal_scroll_offset: px(0.),
910 graph_viewport_width: px(88.),
911 selected_entry_idx: None,
912 hovered_entry_idx: None,
913 graph_canvas_bounds: Rc::new(Cell::new(None)),
914 selected_commit_diff: None,
915 selected_commit_diff_stats: None,
916 log_source,
917 log_order,
918 commit_details_split_state: cx.new(|_cx| SplitState::new()),
919 selected_repo_id: active_repository,
920 changed_files_scroll_handle: UniformListScrollHandle::new(),
921 };
922
923 this.fetch_initial_graph_data(cx);
924 this
925 }
926
927 fn on_repository_event(
928 &mut self,
929 repository: Entity<Repository>,
930 event: &RepositoryEvent,
931 cx: &mut Context<Self>,
932 ) {
933 match event {
934 RepositoryEvent::GitGraphCountUpdated((order, source), commit_count) => {
935 if order != &self.log_order || source != &self.log_source {
936 return;
937 }
938
939 let old_count = self.graph_data.commits.len();
940
941 repository.update(cx, |repository, cx| {
942 let (commits, _) =
943 repository.graph_data(source.clone(), *order, old_count..*commit_count, cx);
944 self.graph_data.add_commits(commits);
945 });
946 cx.notify();
947 }
948 RepositoryEvent::BranchChanged | RepositoryEvent::MergeHeadsChanged => {
949 // Only invalidate if we scanned atleast once,
950 // meaning we are not inside the initial repo loading state
951 // NOTE: this fixes an loading performance regression
952 if repository.read(cx).scan_id > 1 {
953 self.graph_data.clear();
954 cx.notify();
955 }
956 }
957 _ => {}
958 }
959 }
960
961 fn fetch_initial_graph_data(&mut self, cx: &mut App) {
962 if let Some(repository) = self.get_selected_repository(cx) {
963 repository.update(cx, |repository, cx| {
964 let (commits, _) = repository.graph_data(
965 self.log_source.clone(),
966 self.log_order,
967 0..usize::MAX,
968 cx,
969 );
970 self.graph_data.add_commits(commits);
971 });
972 }
973 }
974
975 fn get_selected_repository(&self, cx: &App) -> Option<Entity<Repository>> {
976 let project = self.project.read(cx);
977 self.selected_repo_id
978 .as_ref()
979 .and_then(|repo_id| project.repositories(cx).get(&repo_id).cloned())
980 }
981
982 fn render_chip(&self, name: &SharedString, accent_color: gpui::Hsla) -> impl IntoElement {
983 Chip::new(name.clone())
984 .label_size(LabelSize::Small)
985 .bg_color(accent_color.opacity(0.1))
986 .border_color(accent_color.opacity(0.5))
987 }
988
989 fn render_table_rows(
990 &mut self,
991 range: Range<usize>,
992 _window: &mut Window,
993 cx: &mut Context<Self>,
994 ) -> Vec<Vec<AnyElement>> {
995 let repository = self.get_selected_repository(cx);
996
997 let row_height = self.row_height;
998
999 // We fetch data outside the visible viewport to avoid loading entries when
1000 // users scroll through the git graph
1001 if let Some(repository) = repository.as_ref() {
1002 const FETCH_RANGE: usize = 100;
1003 repository.update(cx, |repository, cx| {
1004 self.graph_data.commits[range.start.saturating_sub(FETCH_RANGE)
1005 ..(range.end + FETCH_RANGE)
1006 .min(self.graph_data.commits.len().saturating_sub(1))]
1007 .iter()
1008 .for_each(|commit| {
1009 repository.fetch_commit_data(commit.data.sha, cx);
1010 });
1011 });
1012 }
1013
1014 range
1015 .map(|idx| {
1016 let Some((commit, repository)) =
1017 self.graph_data.commits.get(idx).zip(repository.as_ref())
1018 else {
1019 return vec![
1020 div().h(row_height).into_any_element(),
1021 div().h(row_height).into_any_element(),
1022 div().h(row_height).into_any_element(),
1023 div().h(row_height).into_any_element(),
1024 ];
1025 };
1026
1027 let data = repository.update(cx, |repository, cx| {
1028 repository.fetch_commit_data(commit.data.sha, cx).clone()
1029 });
1030
1031 let short_sha = commit.data.sha.display_short();
1032 let mut formatted_time = String::new();
1033 let subject: SharedString;
1034 let author_name: SharedString;
1035
1036 if let CommitDataState::Loaded(data) = data {
1037 subject = data.subject.clone();
1038 author_name = data.author_name.clone();
1039 formatted_time = format_timestamp(data.commit_timestamp);
1040 } else {
1041 subject = "Loading…".into();
1042 author_name = "".into();
1043 }
1044
1045 let accent_colors = cx.theme().accents();
1046 let accent_color = accent_colors
1047 .0
1048 .get(commit.color_idx)
1049 .copied()
1050 .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
1051
1052 let is_selected = self.selected_entry_idx == Some(idx);
1053 let column_label = |label: SharedString| {
1054 Label::new(label)
1055 .when(!is_selected, |c| c.color(Color::Muted))
1056 .truncate()
1057 .into_any_element()
1058 };
1059
1060 vec![
1061 div()
1062 .id(ElementId::NamedInteger("commit-subject".into(), idx as u64))
1063 .overflow_hidden()
1064 .tooltip(Tooltip::text(subject.clone()))
1065 .child(
1066 h_flex()
1067 .gap_2()
1068 .overflow_hidden()
1069 .children((!commit.data.ref_names.is_empty()).then(|| {
1070 h_flex().gap_1().children(
1071 commit
1072 .data
1073 .ref_names
1074 .iter()
1075 .map(|name| self.render_chip(name, accent_color)),
1076 )
1077 }))
1078 .child(column_label(subject)),
1079 )
1080 .into_any_element(),
1081 column_label(formatted_time.into()),
1082 column_label(author_name),
1083 column_label(short_sha.into()),
1084 ]
1085 })
1086 .collect()
1087 }
1088
1089 fn cancel(&mut self, _: &Cancel, _window: &mut Window, cx: &mut Context<Self>) {
1090 self.selected_entry_idx = None;
1091 self.selected_commit_diff = None;
1092 self.selected_commit_diff_stats = None;
1093 cx.notify();
1094 }
1095
1096 fn select_prev(&mut self, _: &SelectPrevious, _window: &mut Window, cx: &mut Context<Self>) {
1097 if let Some(selected_entry_idx) = &self.selected_entry_idx {
1098 self.select_entry(selected_entry_idx.saturating_sub(1), cx);
1099 } else {
1100 self.select_entry(0, cx);
1101 }
1102 }
1103
1104 fn select_next(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
1105 if let Some(selected_entry_idx) = &self.selected_entry_idx {
1106 self.select_entry(selected_entry_idx.saturating_add(1), cx);
1107 } else {
1108 self.select_prev(&SelectPrevious, window, cx);
1109 }
1110 }
1111
1112 fn select_entry(&mut self, idx: usize, cx: &mut Context<Self>) {
1113 if self.selected_entry_idx == Some(idx) {
1114 return;
1115 }
1116
1117 self.selected_entry_idx = Some(idx);
1118 self.selected_commit_diff = None;
1119 self.selected_commit_diff_stats = None;
1120 self.changed_files_scroll_handle
1121 .scroll_to_item(0, ScrollStrategy::Top);
1122 self.table_interaction_state.update(cx, |state, cx| {
1123 state
1124 .scroll_handle
1125 .scroll_to_item(idx, ScrollStrategy::Nearest);
1126 cx.notify();
1127 });
1128
1129 let Some(commit) = self.graph_data.commits.get(idx) else {
1130 return;
1131 };
1132
1133 let sha = commit.data.sha.to_string();
1134
1135 let Some(repository) = self.get_selected_repository(cx) else {
1136 return;
1137 };
1138
1139 let diff_receiver = repository.update(cx, |repo, _| repo.load_commit_diff(sha));
1140
1141 self._commit_diff_task = Some(cx.spawn(async move |this, cx| {
1142 if let Ok(Ok(diff)) = diff_receiver.await {
1143 this.update(cx, |this, cx| {
1144 let stats = compute_diff_stats(&diff);
1145 this.selected_commit_diff = Some(diff);
1146 this.selected_commit_diff_stats = Some(stats);
1147 cx.notify();
1148 })
1149 .ok();
1150 }
1151 }));
1152
1153 cx.notify();
1154 }
1155
1156 fn open_selected_commit_view(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1157 let Some(selected_entry_index) = self.selected_entry_idx else {
1158 return;
1159 };
1160
1161 self.open_commit_view(selected_entry_index, window, cx);
1162 }
1163
1164 fn open_commit_view(
1165 &mut self,
1166 entry_index: usize,
1167 window: &mut Window,
1168 cx: &mut Context<Self>,
1169 ) {
1170 let Some(commit_entry) = self.graph_data.commits.get(entry_index) else {
1171 return;
1172 };
1173
1174 let Some(repository) = self.get_selected_repository(cx) else {
1175 return;
1176 };
1177
1178 CommitView::open(
1179 commit_entry.data.sha.to_string(),
1180 repository.downgrade(),
1181 self.workspace.clone(),
1182 None,
1183 None,
1184 window,
1185 cx,
1186 );
1187 }
1188
1189 fn get_remote(
1190 &self,
1191 repository: &Repository,
1192 _window: &mut Window,
1193 cx: &mut App,
1194 ) -> Option<GitRemote> {
1195 let remote_url = repository.default_remote_url()?;
1196 let provider_registry = GitHostingProviderRegistry::default_global(cx);
1197 let (provider, parsed) = parse_git_remote_url(provider_registry, &remote_url)?;
1198 Some(GitRemote {
1199 host: provider,
1200 owner: parsed.owner.into(),
1201 repo: parsed.repo.into(),
1202 })
1203 }
1204
1205 fn render_loading_spinner(&self, cx: &App) -> AnyElement {
1206 let rems = TextSize::Large.rems(cx);
1207 Icon::new(IconName::LoadCircle)
1208 .size(IconSize::Custom(rems))
1209 .color(Color::Accent)
1210 .with_rotate_animation(3)
1211 .into_any_element()
1212 }
1213
1214 fn render_commit_detail_panel(
1215 &self,
1216 window: &mut Window,
1217 cx: &mut Context<Self>,
1218 ) -> impl IntoElement {
1219 let Some(selected_idx) = self.selected_entry_idx else {
1220 return Empty.into_any_element();
1221 };
1222
1223 let Some(commit_entry) = self.graph_data.commits.get(selected_idx) else {
1224 return Empty.into_any_element();
1225 };
1226
1227 let Some(repository) = self.get_selected_repository(cx) else {
1228 return Empty.into_any_element();
1229 };
1230
1231 let data = repository.update(cx, |repository, cx| {
1232 repository
1233 .fetch_commit_data(commit_entry.data.sha, cx)
1234 .clone()
1235 });
1236
1237 let full_sha: SharedString = commit_entry.data.sha.to_string().into();
1238 let ref_names = commit_entry.data.ref_names.clone();
1239
1240 let accent_colors = cx.theme().accents();
1241 let accent_color = accent_colors
1242 .0
1243 .get(commit_entry.color_idx)
1244 .copied()
1245 .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
1246
1247 let (author_name, author_email, commit_timestamp, subject) = match &data {
1248 CommitDataState::Loaded(data) => (
1249 data.author_name.clone(),
1250 data.author_email.clone(),
1251 Some(data.commit_timestamp),
1252 data.subject.clone(),
1253 ),
1254 CommitDataState::Loading => ("Loading…".into(), "".into(), None, "Loading…".into()),
1255 };
1256
1257 let date_string = commit_timestamp
1258 .and_then(|ts| OffsetDateTime::from_unix_timestamp(ts).ok())
1259 .map(|datetime| {
1260 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
1261 let local_datetime = datetime.to_offset(local_offset);
1262 let format =
1263 time::format_description::parse("[month repr:short] [day], [year]").ok();
1264 format
1265 .and_then(|f| local_datetime.format(&f).ok())
1266 .unwrap_or_default()
1267 })
1268 .unwrap_or_default();
1269
1270 let remote = repository.update(cx, |repo, cx| self.get_remote(repo, window, cx));
1271
1272 let avatar = {
1273 let author_email_for_avatar = if author_email.is_empty() {
1274 None
1275 } else {
1276 Some(author_email.clone())
1277 };
1278
1279 CommitAvatar::new(&full_sha, author_email_for_avatar, remote.as_ref())
1280 .size(px(40.))
1281 .render(window, cx)
1282 };
1283
1284 let changed_files_count = self
1285 .selected_commit_diff
1286 .as_ref()
1287 .map(|diff| diff.files.len())
1288 .unwrap_or(0);
1289
1290 let (total_lines_added, total_lines_removed) =
1291 self.selected_commit_diff_stats.unwrap_or((0, 0));
1292
1293 let sorted_file_entries: Rc<Vec<ChangedFileEntry>> = Rc::new(
1294 self.selected_commit_diff
1295 .as_ref()
1296 .map(|diff| {
1297 let mut files: Vec<_> = diff.files.iter().collect();
1298 files.sort_by_key(|file| file.status());
1299 files
1300 .into_iter()
1301 .map(|file| ChangedFileEntry::from_commit_file(file, cx))
1302 .collect()
1303 })
1304 .unwrap_or_default(),
1305 );
1306
1307 v_flex()
1308 .min_w(px(300.))
1309 .h_full()
1310 .bg(cx.theme().colors().surface_background)
1311 .flex_basis(DefiniteLength::Fraction(
1312 self.commit_details_split_state.read(cx).right_ratio(),
1313 ))
1314 .child(
1315 v_flex()
1316 .relative()
1317 .w_full()
1318 .p_2()
1319 .gap_2()
1320 .child(
1321 div().absolute().top_2().right_2().child(
1322 IconButton::new("close-detail", IconName::Close)
1323 .icon_size(IconSize::Small)
1324 .on_click(cx.listener(move |this, _, _, cx| {
1325 this.selected_entry_idx = None;
1326 this.selected_commit_diff = None;
1327 this.selected_commit_diff_stats = None;
1328 this._commit_diff_task = None;
1329 cx.notify();
1330 })),
1331 ),
1332 )
1333 .child(
1334 v_flex()
1335 .py_1()
1336 .w_full()
1337 .items_center()
1338 .gap_1()
1339 .child(avatar)
1340 .child(
1341 v_flex()
1342 .items_center()
1343 .child(Label::new(author_name))
1344 .child(
1345 Label::new(date_string)
1346 .color(Color::Muted)
1347 .size(LabelSize::Small),
1348 ),
1349 ),
1350 )
1351 .children((!ref_names.is_empty()).then(|| {
1352 h_flex().gap_1().flex_wrap().justify_center().children(
1353 ref_names
1354 .iter()
1355 .map(|name| self.render_chip(name, accent_color)),
1356 )
1357 }))
1358 .child(
1359 v_flex()
1360 .ml_neg_1()
1361 .gap_1p5()
1362 .when(!author_email.is_empty(), |this| {
1363 let copied_state: Entity<CopiedState> = window.use_keyed_state(
1364 "author-email-copy",
1365 cx,
1366 CopiedState::new,
1367 );
1368 let is_copied = copied_state.read(cx).is_copied();
1369
1370 let (icon, icon_color, tooltip_label) = if is_copied {
1371 (IconName::Check, Color::Success, "Email Copied!")
1372 } else {
1373 (IconName::Envelope, Color::Muted, "Copy Email")
1374 };
1375
1376 let copy_email = author_email.clone();
1377 let author_email_for_tooltip = author_email.clone();
1378
1379 this.child(
1380 Button::new("author-email-copy", author_email.clone())
1381 .icon(icon)
1382 .icon_size(IconSize::Small)
1383 .icon_color(icon_color)
1384 .icon_position(IconPosition::Start)
1385 .label_size(LabelSize::Small)
1386 .truncate(true)
1387 .color(Color::Muted)
1388 .tooltip(move |_, cx| {
1389 Tooltip::with_meta(
1390 tooltip_label,
1391 None,
1392 author_email_for_tooltip.clone(),
1393 cx,
1394 )
1395 })
1396 .on_click(move |_, _, cx| {
1397 copied_state.update(cx, |state, _cx| {
1398 state.mark_copied();
1399 });
1400 cx.write_to_clipboard(ClipboardItem::new_string(
1401 copy_email.to_string(),
1402 ));
1403 let state_id = copied_state.entity_id();
1404 cx.spawn(async move |cx| {
1405 cx.background_executor()
1406 .timer(COPIED_STATE_DURATION)
1407 .await;
1408 cx.update(|cx| {
1409 cx.notify(state_id);
1410 })
1411 })
1412 .detach();
1413 }),
1414 )
1415 })
1416 .child({
1417 let copy_sha = full_sha.clone();
1418 let copied_state: Entity<CopiedState> =
1419 window.use_keyed_state("sha-copy", cx, CopiedState::new);
1420 let is_copied = copied_state.read(cx).is_copied();
1421
1422 let (icon, icon_color, tooltip_label) = if is_copied {
1423 (IconName::Check, Color::Success, "Commit SHA Copied!")
1424 } else {
1425 (IconName::Hash, Color::Muted, "Copy Commit SHA")
1426 };
1427
1428 Button::new("sha-button", &full_sha)
1429 .icon(icon)
1430 .icon_size(IconSize::Small)
1431 .icon_color(icon_color)
1432 .icon_position(IconPosition::Start)
1433 .label_size(LabelSize::Small)
1434 .truncate(true)
1435 .color(Color::Muted)
1436 .tooltip({
1437 let full_sha = full_sha.clone();
1438 move |_, cx| {
1439 Tooltip::with_meta(
1440 tooltip_label,
1441 None,
1442 full_sha.clone(),
1443 cx,
1444 )
1445 }
1446 })
1447 .on_click(move |_, _, cx| {
1448 copied_state.update(cx, |state, _cx| {
1449 state.mark_copied();
1450 });
1451 cx.write_to_clipboard(ClipboardItem::new_string(
1452 copy_sha.to_string(),
1453 ));
1454 let state_id = copied_state.entity_id();
1455 cx.spawn(async move |cx| {
1456 cx.background_executor()
1457 .timer(COPIED_STATE_DURATION)
1458 .await;
1459 cx.update(|cx| {
1460 cx.notify(state_id);
1461 })
1462 })
1463 .detach();
1464 })
1465 })
1466 .when_some(remote.clone(), |this, remote| {
1467 let provider_name = remote.host.name();
1468 let icon = match provider_name.as_str() {
1469 "GitHub" => IconName::Github,
1470 _ => IconName::Link,
1471 };
1472 let parsed_remote = ParsedGitRemote {
1473 owner: remote.owner.as_ref().into(),
1474 repo: remote.repo.as_ref().into(),
1475 };
1476 let params = BuildCommitPermalinkParams {
1477 sha: full_sha.as_ref(),
1478 };
1479 let url = remote
1480 .host
1481 .build_commit_permalink(&parsed_remote, params)
1482 .to_string();
1483
1484 this.child(
1485 Button::new(
1486 "view-on-provider",
1487 format!("View on {}", provider_name),
1488 )
1489 .icon(icon)
1490 .icon_size(IconSize::Small)
1491 .icon_color(Color::Muted)
1492 .icon_position(IconPosition::Start)
1493 .label_size(LabelSize::Small)
1494 .truncate(true)
1495 .color(Color::Muted)
1496 .on_click(
1497 move |_, _, cx| {
1498 cx.open_url(&url);
1499 },
1500 ),
1501 )
1502 }),
1503 ),
1504 )
1505 .child(Divider::horizontal())
1506 .child(div().min_w_0().p_2().child(Label::new(subject)))
1507 .child(Divider::horizontal())
1508 .child(
1509 v_flex()
1510 .min_w_0()
1511 .p_2()
1512 .flex_1()
1513 .gap_1()
1514 .child(
1515 h_flex()
1516 .gap_1()
1517 .child(
1518 Label::new(format!("{} Changed Files", changed_files_count))
1519 .size(LabelSize::Small)
1520 .color(Color::Muted),
1521 )
1522 .child(DiffStat::new(
1523 "commit-diff-stat",
1524 total_lines_added,
1525 total_lines_removed,
1526 )),
1527 )
1528 .child(
1529 div()
1530 .id("changed-files-container")
1531 .flex_1()
1532 .min_h_0()
1533 .child({
1534 let entries = sorted_file_entries;
1535 let entry_count = entries.len();
1536 let commit_sha = full_sha.clone();
1537 let repository = repository.downgrade();
1538 let workspace = self.workspace.clone();
1539 uniform_list(
1540 "changed-files-list",
1541 entry_count,
1542 move |range, _window, cx| {
1543 range
1544 .map(|ix| {
1545 entries[ix].render(
1546 ix,
1547 commit_sha.clone(),
1548 repository.clone(),
1549 workspace.clone(),
1550 cx,
1551 )
1552 })
1553 .collect()
1554 },
1555 )
1556 .size_full()
1557 .ml_neg_1()
1558 .track_scroll(&self.changed_files_scroll_handle)
1559 })
1560 .vertical_scrollbar_for(&self.changed_files_scroll_handle, window, cx),
1561 ),
1562 )
1563 .child(Divider::horizontal())
1564 .child(
1565 h_flex().p_1p5().w_full().child(
1566 Button::new("view-commit", "View Commit")
1567 .full_width()
1568 .style(ButtonStyle::Outlined)
1569 .on_click(cx.listener(|this, _, window, cx| {
1570 this.open_selected_commit_view(window, cx);
1571 })),
1572 ),
1573 )
1574 .into_any_element()
1575 }
1576
1577 pub fn render_graph(&self, window: &Window, cx: &mut Context<GitGraph>) -> impl IntoElement {
1578 let row_height = self.row_height;
1579 let table_state = self.table_interaction_state.read(cx);
1580 let viewport_height = table_state
1581 .scroll_handle
1582 .0
1583 .borrow()
1584 .last_item_size
1585 .map(|size| size.item.height)
1586 .unwrap_or(px(600.0));
1587 let loaded_commit_count = self.graph_data.commits.len();
1588
1589 let content_height = row_height * loaded_commit_count;
1590 let max_scroll = (content_height - viewport_height).max(px(0.));
1591 let scroll_offset_y = (-table_state.scroll_offset().y).clamp(px(0.), max_scroll);
1592
1593 let first_visible_row = (scroll_offset_y / row_height).floor() as usize;
1594 let vertical_scroll_offset = scroll_offset_y - (first_visible_row as f32 * row_height);
1595 let horizontal_scroll_offset = self.horizontal_scroll_offset;
1596
1597 let max_lanes = self.graph_data.max_lanes.max(6);
1598 let graph_width = LANE_WIDTH * max_lanes as f32 + LEFT_PADDING * 2.0;
1599 let last_visible_row =
1600 first_visible_row + (viewport_height / row_height).ceil() as usize + 1;
1601
1602 let viewport_range = first_visible_row.min(loaded_commit_count.saturating_sub(1))
1603 ..(last_visible_row).min(loaded_commit_count);
1604 let rows = self.graph_data.commits[viewport_range.clone()].to_vec();
1605 let commit_lines: Vec<_> = self
1606 .graph_data
1607 .lines
1608 .iter()
1609 .filter(|line| {
1610 line.full_interval.start <= viewport_range.end
1611 && line.full_interval.end >= viewport_range.start
1612 })
1613 .cloned()
1614 .collect();
1615
1616 let mut lines: BTreeMap<usize, Vec<_>> = BTreeMap::new();
1617
1618 let hovered_entry_idx = self.hovered_entry_idx;
1619 let selected_entry_idx = self.selected_entry_idx;
1620 let is_focused = self.focus_handle.is_focused(window);
1621 let graph_canvas_bounds = self.graph_canvas_bounds.clone();
1622
1623 gpui::canvas(
1624 move |_bounds, _window, _cx| {},
1625 move |bounds: Bounds<Pixels>, _: (), window: &mut Window, cx: &mut App| {
1626 graph_canvas_bounds.set(Some(bounds));
1627
1628 window.paint_layer(bounds, |window| {
1629 let accent_colors = cx.theme().accents();
1630
1631 let hover_bg = cx.theme().colors().element_hover.opacity(0.6);
1632 let selected_bg = if is_focused {
1633 cx.theme().colors().element_selected
1634 } else {
1635 cx.theme().colors().element_hover
1636 };
1637
1638 for visible_row_idx in 0..rows.len() {
1639 let absolute_row_idx = first_visible_row + visible_row_idx;
1640 let is_hovered = hovered_entry_idx == Some(absolute_row_idx);
1641 let is_selected = selected_entry_idx == Some(absolute_row_idx);
1642
1643 if is_hovered || is_selected {
1644 let row_y = bounds.origin.y + visible_row_idx as f32 * row_height
1645 - vertical_scroll_offset;
1646
1647 let row_bounds = Bounds::new(
1648 point(bounds.origin.x, row_y),
1649 gpui::Size {
1650 width: bounds.size.width,
1651 height: row_height,
1652 },
1653 );
1654
1655 let bg_color = if is_selected { selected_bg } else { hover_bg };
1656 window.paint_quad(gpui::fill(row_bounds, bg_color));
1657 }
1658 }
1659
1660 for (row_idx, row) in rows.into_iter().enumerate() {
1661 let row_color = accent_colors.color_for_index(row.color_idx as u32);
1662 let row_y_center =
1663 bounds.origin.y + row_idx as f32 * row_height + row_height / 2.0
1664 - vertical_scroll_offset;
1665
1666 let commit_x =
1667 lane_center_x(bounds, row.lane as f32, horizontal_scroll_offset);
1668
1669 draw_commit_circle(commit_x, row_y_center, row_color, window);
1670 }
1671
1672 for line in commit_lines {
1673 let Some((start_segment_idx, start_column)) =
1674 line.get_first_visible_segment_idx(first_visible_row)
1675 else {
1676 continue;
1677 };
1678
1679 let line_x =
1680 lane_center_x(bounds, start_column as f32, horizontal_scroll_offset);
1681
1682 let start_row = line.full_interval.start as i32 - first_visible_row as i32;
1683
1684 let from_y =
1685 bounds.origin.y + start_row as f32 * row_height + row_height / 2.0
1686 - vertical_scroll_offset
1687 + COMMIT_CIRCLE_RADIUS;
1688
1689 let mut current_row = from_y;
1690 let mut current_column = line_x;
1691
1692 let mut builder = PathBuilder::stroke(LINE_WIDTH);
1693 builder.move_to(point(line_x, from_y));
1694
1695 let segments = &line.segments[start_segment_idx..];
1696
1697 for (segment_idx, segment) in segments.iter().enumerate() {
1698 let is_last = segment_idx + 1 == segments.len();
1699
1700 match segment {
1701 CommitLineSegment::Straight { to_row } => {
1702 let mut dest_row = to_row_center(
1703 to_row - first_visible_row,
1704 row_height,
1705 vertical_scroll_offset,
1706 bounds,
1707 );
1708 if is_last {
1709 dest_row -= COMMIT_CIRCLE_RADIUS;
1710 }
1711
1712 let dest_point = point(current_column, dest_row);
1713
1714 current_row = dest_point.y;
1715 builder.line_to(dest_point);
1716 builder.move_to(dest_point);
1717 }
1718 CommitLineSegment::Curve {
1719 to_column,
1720 on_row,
1721 curve_kind,
1722 } => {
1723 let mut to_column = lane_center_x(
1724 bounds,
1725 *to_column as f32,
1726 horizontal_scroll_offset,
1727 );
1728
1729 let mut to_row = to_row_center(
1730 *on_row - first_visible_row,
1731 row_height,
1732 vertical_scroll_offset,
1733 bounds,
1734 );
1735
1736 // This means that this branch was a checkout
1737 let going_right = to_column > current_column;
1738 let column_shift = if going_right {
1739 COMMIT_CIRCLE_RADIUS + COMMIT_CIRCLE_STROKE_WIDTH
1740 } else {
1741 -COMMIT_CIRCLE_RADIUS - COMMIT_CIRCLE_STROKE_WIDTH
1742 };
1743
1744 let control = match curve_kind {
1745 CurveKind::Checkout => {
1746 if is_last {
1747 to_column -= column_shift;
1748 }
1749 builder.move_to(point(current_column, current_row));
1750 point(current_column, to_row)
1751 }
1752 CurveKind::Merge => {
1753 if is_last {
1754 to_row -= COMMIT_CIRCLE_RADIUS;
1755 }
1756 builder.move_to(point(
1757 current_column + column_shift,
1758 current_row - COMMIT_CIRCLE_RADIUS,
1759 ));
1760 point(to_column, current_row)
1761 }
1762 };
1763
1764 match curve_kind {
1765 CurveKind::Checkout
1766 if (to_row - current_row).abs() > row_height =>
1767 {
1768 let start_curve =
1769 point(current_column, current_row + row_height);
1770 builder.line_to(start_curve);
1771 builder.move_to(start_curve);
1772 }
1773 CurveKind::Merge
1774 if (to_column - current_column).abs() > LANE_WIDTH =>
1775 {
1776 let column_shift =
1777 if going_right { LANE_WIDTH } else { -LANE_WIDTH };
1778
1779 let start_curve = point(
1780 current_column + column_shift,
1781 current_row - COMMIT_CIRCLE_RADIUS,
1782 );
1783
1784 builder.line_to(start_curve);
1785 builder.move_to(start_curve);
1786 }
1787 _ => {}
1788 };
1789
1790 builder.curve_to(point(to_column, to_row), control);
1791 current_row = to_row;
1792 current_column = to_column;
1793 builder.move_to(point(current_column, current_row));
1794 }
1795 }
1796 }
1797
1798 builder.close();
1799 lines.entry(line.color_idx).or_default().push(builder);
1800 }
1801
1802 for (color_idx, builders) in lines {
1803 let line_color = accent_colors.color_for_index(color_idx as u32);
1804
1805 for builder in builders {
1806 if let Ok(path) = builder.build() {
1807 // we paint each color on it's own layer to stop overlapping lines
1808 // of different colors changing the color of a line
1809 window.paint_layer(bounds, |window| {
1810 window.paint_path(path, line_color);
1811 });
1812 }
1813 }
1814 }
1815 })
1816 },
1817 )
1818 .w(graph_width)
1819 .h_full()
1820 }
1821
1822 fn row_at_position(&self, position_y: Pixels, cx: &Context<Self>) -> Option<usize> {
1823 let canvas_bounds = self.graph_canvas_bounds.get()?;
1824 let table_state = self.table_interaction_state.read(cx);
1825 let scroll_offset_y = -table_state.scroll_offset().y;
1826
1827 let local_y = position_y - canvas_bounds.origin.y;
1828
1829 if local_y >= px(0.) && local_y < canvas_bounds.size.height {
1830 let row_in_viewport = (local_y / self.row_height).floor() as usize;
1831 let scroll_rows = (scroll_offset_y / self.row_height).floor() as usize;
1832 let absolute_row = scroll_rows + row_in_viewport;
1833
1834 if absolute_row < self.graph_data.commits.len() {
1835 return Some(absolute_row);
1836 }
1837 }
1838
1839 None
1840 }
1841
1842 fn handle_graph_mouse_move(
1843 &mut self,
1844 event: &gpui::MouseMoveEvent,
1845 _window: &mut Window,
1846 cx: &mut Context<Self>,
1847 ) {
1848 if let Some(row) = self.row_at_position(event.position.y, cx) {
1849 if self.hovered_entry_idx != Some(row) {
1850 self.hovered_entry_idx = Some(row);
1851 cx.notify();
1852 }
1853 } else if self.hovered_entry_idx.is_some() {
1854 self.hovered_entry_idx = None;
1855 cx.notify();
1856 }
1857 }
1858
1859 fn handle_graph_click(
1860 &mut self,
1861 event: &ClickEvent,
1862 window: &mut Window,
1863 cx: &mut Context<Self>,
1864 ) {
1865 if let Some(row) = self.row_at_position(event.position().y, cx) {
1866 self.select_entry(row, cx);
1867 if event.click_count() >= 2 {
1868 self.open_commit_view(row, window, cx);
1869 }
1870 }
1871 }
1872
1873 fn handle_graph_scroll(
1874 &mut self,
1875 event: &ScrollWheelEvent,
1876 window: &mut Window,
1877 cx: &mut Context<Self>,
1878 ) {
1879 let line_height = window.line_height();
1880 let delta = event.delta.pixel_delta(line_height);
1881
1882 let table_state = self.table_interaction_state.read(cx);
1883 let current_offset = table_state.scroll_offset();
1884
1885 let viewport_height = table_state.scroll_handle.viewport().size.height;
1886
1887 let commit_count = match self.graph_data.max_commit_count {
1888 AllCommitCount::Loaded(count) => count,
1889 AllCommitCount::NotLoaded => self.graph_data.commits.len(),
1890 };
1891 let content_height = self.row_height * commit_count;
1892 let max_vertical_scroll = (viewport_height - content_height).min(px(0.));
1893
1894 let new_y = (current_offset.y + delta.y).clamp(max_vertical_scroll, px(0.));
1895 let new_offset = Point::new(current_offset.x, new_y);
1896
1897 let max_lanes = self.graph_data.max_lanes.max(1);
1898 let graph_content_width = LANE_WIDTH * max_lanes as f32 + LEFT_PADDING * 2.0;
1899 let max_horizontal_scroll = (graph_content_width - self.graph_viewport_width).max(px(0.));
1900
1901 let new_horizontal_offset =
1902 (self.horizontal_scroll_offset - delta.x).clamp(px(0.), max_horizontal_scroll);
1903
1904 let vertical_changed = new_offset != current_offset;
1905 let horizontal_changed = new_horizontal_offset != self.horizontal_scroll_offset;
1906
1907 if vertical_changed {
1908 table_state.set_scroll_offset(new_offset);
1909 }
1910
1911 if horizontal_changed {
1912 self.horizontal_scroll_offset = new_horizontal_offset;
1913 }
1914
1915 if vertical_changed || horizontal_changed {
1916 cx.notify();
1917 }
1918 }
1919
1920 fn render_commit_view_resize_handle(
1921 &self,
1922 _window: &mut Window,
1923 cx: &mut Context<Self>,
1924 ) -> AnyElement {
1925 div()
1926 .id("commit-view-split-resize-container")
1927 .relative()
1928 .h_full()
1929 .flex_shrink_0()
1930 .w(px(1.))
1931 .bg(cx.theme().colors().border_variant)
1932 .child(
1933 div()
1934 .id("commit-view-split-resize-handle")
1935 .absolute()
1936 .left(px(-RESIZE_HANDLE_WIDTH / 2.0))
1937 .w(px(RESIZE_HANDLE_WIDTH))
1938 .h_full()
1939 .cursor_col_resize()
1940 .block_mouse_except_scroll()
1941 .on_click(cx.listener(|this, event: &ClickEvent, _window, cx| {
1942 if event.click_count() >= 2 {
1943 this.commit_details_split_state.update(cx, |state, _| {
1944 state.on_double_click();
1945 });
1946 }
1947 cx.stop_propagation();
1948 }))
1949 .on_drag(DraggedSplitHandle, |_, _, _, cx| cx.new(|_| gpui::Empty)),
1950 )
1951 .into_any_element()
1952 }
1953}
1954
1955impl Render for GitGraph {
1956 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1957 let description_width_fraction = 0.72;
1958 let date_width_fraction = 0.12;
1959 let author_width_fraction = 0.10;
1960 let commit_width_fraction = 0.06;
1961
1962 let (commit_count, is_loading) = match self.graph_data.max_commit_count {
1963 AllCommitCount::Loaded(count) => (count, true),
1964 AllCommitCount::NotLoaded => {
1965 let (commit_count, is_loading) =
1966 if let Some(repository) = self.get_selected_repository(cx) {
1967 repository.update(cx, |repository, cx| {
1968 // Start loading the graph data if we haven't started already
1969 let (commits, is_loading) = repository.graph_data(
1970 self.log_source.clone(),
1971 self.log_order,
1972 0..usize::MAX,
1973 cx,
1974 );
1975 self.graph_data.add_commits(&commits);
1976 (commits.len(), is_loading)
1977 })
1978 } else {
1979 (0, false)
1980 };
1981
1982 (commit_count, is_loading)
1983 }
1984 };
1985
1986 let content = if commit_count == 0 {
1987 let message = if is_loading {
1988 "Loading"
1989 } else {
1990 "No commits found"
1991 };
1992 let label = Label::new(message)
1993 .color(Color::Muted)
1994 .size(LabelSize::Large);
1995 div()
1996 .size_full()
1997 .h_flex()
1998 .gap_1()
1999 .items_center()
2000 .justify_center()
2001 .child(label)
2002 .when(is_loading, |this| {
2003 this.child(self.render_loading_spinner(cx))
2004 })
2005 } else {
2006 div()
2007 .size_full()
2008 .flex()
2009 .flex_row()
2010 .child(
2011 div()
2012 .w(self.graph_content_width())
2013 .h_full()
2014 .flex()
2015 .flex_col()
2016 .child(
2017 div()
2018 .p_2()
2019 .border_b_1()
2020 .whitespace_nowrap()
2021 .border_color(cx.theme().colors().border)
2022 .child(Label::new("Graph").color(Color::Muted)),
2023 )
2024 .child(
2025 div()
2026 .id("graph-canvas")
2027 .flex_1()
2028 .overflow_hidden()
2029 .child(self.render_graph(window, cx))
2030 .on_scroll_wheel(cx.listener(Self::handle_graph_scroll))
2031 .on_mouse_move(cx.listener(Self::handle_graph_mouse_move))
2032 .on_click(cx.listener(Self::handle_graph_click))
2033 .on_hover(cx.listener(|this, &is_hovered: &bool, _, cx| {
2034 if !is_hovered && this.hovered_entry_idx.is_some() {
2035 this.hovered_entry_idx = None;
2036 cx.notify();
2037 }
2038 })),
2039 ),
2040 )
2041 .child({
2042 let row_height = self.row_height;
2043 let selected_entry_idx = self.selected_entry_idx;
2044 let hovered_entry_idx = self.hovered_entry_idx;
2045 let weak_self = cx.weak_entity();
2046 let focus_handle = self.focus_handle.clone();
2047 div().flex_1().size_full().child(
2048 Table::new(4)
2049 .interactable(&self.table_interaction_state)
2050 .hide_row_borders()
2051 .hide_row_hover()
2052 .header(vec![
2053 Label::new("Description")
2054 .color(Color::Muted)
2055 .into_any_element(),
2056 Label::new("Date").color(Color::Muted).into_any_element(),
2057 Label::new("Author").color(Color::Muted).into_any_element(),
2058 Label::new("Commit").color(Color::Muted).into_any_element(),
2059 ])
2060 .column_widths(
2061 [
2062 DefiniteLength::Fraction(description_width_fraction),
2063 DefiniteLength::Fraction(date_width_fraction),
2064 DefiniteLength::Fraction(author_width_fraction),
2065 DefiniteLength::Fraction(commit_width_fraction),
2066 ]
2067 .to_vec(),
2068 )
2069 .resizable_columns(
2070 vec![
2071 TableResizeBehavior::Resizable,
2072 TableResizeBehavior::Resizable,
2073 TableResizeBehavior::Resizable,
2074 TableResizeBehavior::Resizable,
2075 ],
2076 &self.table_column_widths,
2077 cx,
2078 )
2079 .map_row(move |(index, row), window, cx| {
2080 let is_selected = selected_entry_idx == Some(index);
2081 let is_hovered = hovered_entry_idx == Some(index);
2082 let is_focused = focus_handle.is_focused(window);
2083 let weak = weak_self.clone();
2084 let weak_for_hover = weak.clone();
2085
2086 let hover_bg = cx.theme().colors().element_hover.opacity(0.6);
2087 let selected_bg = if is_focused {
2088 cx.theme().colors().element_selected
2089 } else {
2090 cx.theme().colors().element_hover
2091 };
2092
2093 row.h(row_height)
2094 .when(is_selected, |row| row.bg(selected_bg))
2095 .when(is_hovered && !is_selected, |row| row.bg(hover_bg))
2096 .on_hover(move |&is_hovered, _, cx| {
2097 weak_for_hover
2098 .update(cx, |this, cx| {
2099 if is_hovered {
2100 if this.hovered_entry_idx != Some(index) {
2101 this.hovered_entry_idx = Some(index);
2102 cx.notify();
2103 }
2104 } else if this.hovered_entry_idx == Some(index) {
2105 // Only clear if this row was the hovered one
2106 this.hovered_entry_idx = None;
2107 cx.notify();
2108 }
2109 })
2110 .ok();
2111 })
2112 .on_click(move |event, window, cx| {
2113 let click_count = event.click_count();
2114 weak.update(cx, |this, cx| {
2115 this.select_entry(index, cx);
2116 if click_count >= 2 {
2117 this.open_commit_view(index, window, cx);
2118 }
2119 })
2120 .ok();
2121 })
2122 .into_any_element()
2123 })
2124 .uniform_list(
2125 "git-graph-commits",
2126 commit_count,
2127 cx.processor(Self::render_table_rows),
2128 ),
2129 )
2130 })
2131 .on_drag_move::<DraggedSplitHandle>(cx.listener(|this, event, window, cx| {
2132 this.commit_details_split_state.update(cx, |state, cx| {
2133 state.on_drag_move(event, window, cx);
2134 });
2135 }))
2136 .on_drop::<DraggedSplitHandle>(cx.listener(|this, _event, _window, cx| {
2137 this.commit_details_split_state.update(cx, |state, _cx| {
2138 state.commit_ratio();
2139 });
2140 }))
2141 .when(self.selected_entry_idx.is_some(), |this| {
2142 this.child(self.render_commit_view_resize_handle(window, cx))
2143 .child(self.render_commit_detail_panel(window, cx))
2144 })
2145 };
2146
2147 div()
2148 .size_full()
2149 .bg(cx.theme().colors().editor_background)
2150 .key_context("GitGraph")
2151 .track_focus(&self.focus_handle)
2152 .on_action(cx.listener(|this, _: &OpenCommitView, window, cx| {
2153 this.open_selected_commit_view(window, cx);
2154 }))
2155 .on_action(cx.listener(Self::cancel))
2156 .on_action(cx.listener(Self::select_prev))
2157 .on_action(cx.listener(Self::select_next))
2158 .child(content)
2159 .children(self.context_menu.as_ref().map(|(menu, position, _)| {
2160 deferred(
2161 anchored()
2162 .position(*position)
2163 .anchor(Corner::TopLeft)
2164 .child(menu.clone()),
2165 )
2166 .with_priority(1)
2167 }))
2168 }
2169}
2170
2171impl EventEmitter<ItemEvent> for GitGraph {}
2172
2173impl Focusable for GitGraph {
2174 fn focus_handle(&self, _cx: &App) -> FocusHandle {
2175 self.focus_handle.clone()
2176 }
2177}
2178
2179impl Item for GitGraph {
2180 type Event = ItemEvent;
2181
2182 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
2183 "Git Graph".into()
2184 }
2185
2186 fn show_toolbar(&self) -> bool {
2187 false
2188 }
2189
2190 fn to_item_events(event: &Self::Event, f: &mut dyn FnMut(ItemEvent)) {
2191 f(*event)
2192 }
2193}
2194
2195impl SerializableItem for GitGraph {
2196 fn serialized_item_kind() -> &'static str {
2197 "GitGraph"
2198 }
2199
2200 fn cleanup(
2201 workspace_id: workspace::WorkspaceId,
2202 alive_items: Vec<workspace::ItemId>,
2203 _window: &mut Window,
2204 cx: &mut App,
2205 ) -> Task<gpui::Result<()>> {
2206 workspace::delete_unloaded_items(
2207 alive_items,
2208 workspace_id,
2209 "git_graphs",
2210 &persistence::GIT_GRAPHS,
2211 cx,
2212 )
2213 }
2214
2215 fn deserialize(
2216 project: Entity<Project>,
2217 workspace: WeakEntity<Workspace>,
2218 workspace_id: workspace::WorkspaceId,
2219 item_id: workspace::ItemId,
2220 window: &mut Window,
2221 cx: &mut App,
2222 ) -> Task<gpui::Result<Entity<Self>>> {
2223 if persistence::GIT_GRAPHS
2224 .get_git_graph(item_id, workspace_id)
2225 .ok()
2226 .is_some_and(|is_open| is_open)
2227 {
2228 let git_graph = cx.new(|cx| GitGraph::new(project, workspace, window, cx));
2229 Task::ready(Ok(git_graph))
2230 } else {
2231 Task::ready(Err(anyhow::anyhow!("No git graph to deserialize")))
2232 }
2233 }
2234
2235 fn serialize(
2236 &mut self,
2237 workspace: &mut Workspace,
2238 item_id: workspace::ItemId,
2239 _closing: bool,
2240 _window: &mut Window,
2241 cx: &mut Context<Self>,
2242 ) -> Option<Task<gpui::Result<()>>> {
2243 let workspace_id = workspace.database_id()?;
2244 Some(cx.background_spawn(async move {
2245 persistence::GIT_GRAPHS
2246 .save_git_graph(item_id, workspace_id, true)
2247 .await
2248 }))
2249 }
2250
2251 fn should_serialize(&self, event: &Self::Event) -> bool {
2252 event == &ItemEvent::UpdateTab
2253 }
2254}
2255
2256mod persistence {
2257 use db::{
2258 query,
2259 sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection},
2260 sqlez_macros::sql,
2261 };
2262 use workspace::WorkspaceDb;
2263
2264 pub struct GitGraphsDb(ThreadSafeConnection);
2265
2266 impl Domain for GitGraphsDb {
2267 const NAME: &str = stringify!(GitGraphsDb);
2268
2269 const MIGRATIONS: &[&str] = (&[sql!(
2270 CREATE TABLE git_graphs (
2271 workspace_id INTEGER,
2272 item_id INTEGER UNIQUE,
2273 is_open INTEGER DEFAULT FALSE,
2274
2275 PRIMARY KEY(workspace_id, item_id),
2276 FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
2277 ON DELETE CASCADE
2278 ) STRICT;
2279 )]);
2280 }
2281
2282 db::static_connection!(GIT_GRAPHS, GitGraphsDb, [WorkspaceDb]);
2283
2284 impl GitGraphsDb {
2285 query! {
2286 pub async fn save_git_graph(
2287 item_id: workspace::ItemId,
2288 workspace_id: workspace::WorkspaceId,
2289 is_open: bool
2290 ) -> Result<()> {
2291 INSERT OR REPLACE INTO git_graphs(item_id, workspace_id, is_open)
2292 VALUES (?, ?, ?)
2293 }
2294 }
2295
2296 query! {
2297 pub fn get_git_graph(
2298 item_id: workspace::ItemId,
2299 workspace_id: workspace::WorkspaceId
2300 ) -> Result<bool> {
2301 SELECT is_open
2302 FROM git_graphs
2303 WHERE item_id = ? AND workspace_id = ?
2304 }
2305 }
2306 }
2307}
2308
2309#[cfg(test)]
2310mod tests {
2311 use super::*;
2312 use anyhow::{Context, Result, bail};
2313 use collections::{HashMap, HashSet};
2314 use fs::FakeFs;
2315 use git::Oid;
2316 use git::repository::InitialGraphCommitData;
2317 use gpui::TestAppContext;
2318 use project::Project;
2319 use project::git_store::{GitStoreEvent, RepositoryEvent};
2320 use rand::prelude::*;
2321 use serde_json::json;
2322 use settings::SettingsStore;
2323 use smallvec::{SmallVec, smallvec};
2324 use std::path::Path;
2325 use std::sync::{Arc, Mutex};
2326 use workspace::MultiWorkspace;
2327
2328 fn init_test(cx: &mut TestAppContext) {
2329 cx.update(|cx| {
2330 let settings_store = SettingsStore::test(cx);
2331 cx.set_global(settings_store);
2332 });
2333 }
2334
2335 fn init_test_with_theme(cx: &mut TestAppContext) {
2336 cx.update(|cx| {
2337 let settings_store = SettingsStore::test(cx);
2338 cx.set_global(settings_store);
2339 theme::init(theme::LoadThemes::JustBase, cx);
2340 });
2341 }
2342
2343 /// Generates a random commit DAG suitable for testing git graph rendering.
2344 ///
2345 /// The commits are ordered newest-first (like git log output), so:
2346 /// - Index 0 = most recent commit (HEAD)
2347 /// - Last index = oldest commit (root, has no parents)
2348 /// - Parents of commit at index I must have index > I
2349 ///
2350 /// When `adversarial` is true, generates complex topologies with many branches
2351 /// and octopus merges. Otherwise generates more realistic linear histories
2352 /// with occasional branches.
2353 fn generate_random_commit_dag(
2354 rng: &mut StdRng,
2355 num_commits: usize,
2356 adversarial: bool,
2357 ) -> Vec<Arc<InitialGraphCommitData>> {
2358 if num_commits == 0 {
2359 return Vec::new();
2360 }
2361
2362 let mut commits: Vec<Arc<InitialGraphCommitData>> = Vec::with_capacity(num_commits);
2363 let oids: Vec<Oid> = (0..num_commits).map(|_| Oid::random(rng)).collect();
2364
2365 for i in 0..num_commits {
2366 let sha = oids[i];
2367
2368 let parents = if i == num_commits - 1 {
2369 smallvec![]
2370 } else {
2371 generate_parents_from_oids(rng, &oids, i, num_commits, adversarial)
2372 };
2373
2374 let ref_names = if i == 0 {
2375 vec!["HEAD".into(), "main".into()]
2376 } else if adversarial && rng.random_bool(0.1) {
2377 vec![format!("branch-{}", i).into()]
2378 } else {
2379 Vec::new()
2380 };
2381
2382 commits.push(Arc::new(InitialGraphCommitData {
2383 sha,
2384 parents,
2385 ref_names,
2386 }));
2387 }
2388
2389 commits
2390 }
2391
2392 fn generate_parents_from_oids(
2393 rng: &mut StdRng,
2394 oids: &[Oid],
2395 current_idx: usize,
2396 num_commits: usize,
2397 adversarial: bool,
2398 ) -> SmallVec<[Oid; 1]> {
2399 let remaining = num_commits - current_idx - 1;
2400 if remaining == 0 {
2401 return smallvec![];
2402 }
2403
2404 if adversarial {
2405 let merge_chance = 0.4;
2406 let octopus_chance = 0.15;
2407
2408 if remaining >= 3 && rng.random_bool(octopus_chance) {
2409 let num_parents = rng.random_range(3..=remaining.min(5));
2410 let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
2411 parent_indices.shuffle(rng);
2412 parent_indices
2413 .into_iter()
2414 .take(num_parents)
2415 .map(|idx| oids[idx])
2416 .collect()
2417 } else if remaining >= 2 && rng.random_bool(merge_chance) {
2418 let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
2419 parent_indices.shuffle(rng);
2420 parent_indices
2421 .into_iter()
2422 .take(2)
2423 .map(|idx| oids[idx])
2424 .collect()
2425 } else {
2426 let parent_idx = rng.random_range(current_idx + 1..num_commits);
2427 smallvec![oids[parent_idx]]
2428 }
2429 } else {
2430 let merge_chance = 0.15;
2431 let skip_chance = 0.1;
2432
2433 if remaining >= 2 && rng.random_bool(merge_chance) {
2434 let first_parent = current_idx + 1;
2435 let second_parent = rng.random_range(current_idx + 2..num_commits);
2436 smallvec![oids[first_parent], oids[second_parent]]
2437 } else if rng.random_bool(skip_chance) && remaining >= 2 {
2438 let skip = rng.random_range(1..remaining.min(3));
2439 smallvec![oids[current_idx + 1 + skip]]
2440 } else {
2441 smallvec![oids[current_idx + 1]]
2442 }
2443 }
2444 }
2445
2446 fn build_oid_to_row_map(graph: &GraphData) -> HashMap<Oid, usize> {
2447 graph
2448 .commits
2449 .iter()
2450 .enumerate()
2451 .map(|(idx, entry)| (entry.data.sha, idx))
2452 .collect()
2453 }
2454
2455 fn verify_commit_order(
2456 graph: &GraphData,
2457 commits: &[Arc<InitialGraphCommitData>],
2458 ) -> Result<()> {
2459 if graph.commits.len() != commits.len() {
2460 bail!(
2461 "Commit count mismatch: graph has {} commits, expected {}",
2462 graph.commits.len(),
2463 commits.len()
2464 );
2465 }
2466
2467 for (idx, (graph_commit, expected_commit)) in
2468 graph.commits.iter().zip(commits.iter()).enumerate()
2469 {
2470 if graph_commit.data.sha != expected_commit.sha {
2471 bail!(
2472 "Commit order mismatch at index {}: graph has {:?}, expected {:?}",
2473 idx,
2474 graph_commit.data.sha,
2475 expected_commit.sha
2476 );
2477 }
2478 }
2479
2480 Ok(())
2481 }
2482
2483 fn verify_line_endpoints(graph: &GraphData, oid_to_row: &HashMap<Oid, usize>) -> Result<()> {
2484 for line in &graph.lines {
2485 let child_row = *oid_to_row
2486 .get(&line.child)
2487 .context("Line references non-existent child commit")?;
2488
2489 let parent_row = *oid_to_row
2490 .get(&line.parent)
2491 .context("Line references non-existent parent commit")?;
2492
2493 if child_row >= parent_row {
2494 bail!(
2495 "child_row ({}) must be < parent_row ({})",
2496 child_row,
2497 parent_row
2498 );
2499 }
2500
2501 if line.full_interval.start != child_row {
2502 bail!(
2503 "full_interval.start ({}) != child_row ({})",
2504 line.full_interval.start,
2505 child_row
2506 );
2507 }
2508
2509 if line.full_interval.end != parent_row {
2510 bail!(
2511 "full_interval.end ({}) != parent_row ({})",
2512 line.full_interval.end,
2513 parent_row
2514 );
2515 }
2516
2517 if let Some(last_segment) = line.segments.last() {
2518 let segment_end_row = match last_segment {
2519 CommitLineSegment::Straight { to_row } => *to_row,
2520 CommitLineSegment::Curve { on_row, .. } => *on_row,
2521 };
2522
2523 if segment_end_row != line.full_interval.end {
2524 bail!(
2525 "last segment ends at row {} but full_interval.end is {}",
2526 segment_end_row,
2527 line.full_interval.end
2528 );
2529 }
2530 }
2531 }
2532
2533 Ok(())
2534 }
2535
2536 fn verify_column_correctness(
2537 graph: &GraphData,
2538 oid_to_row: &HashMap<Oid, usize>,
2539 ) -> Result<()> {
2540 for line in &graph.lines {
2541 let child_row = *oid_to_row
2542 .get(&line.child)
2543 .context("Line references non-existent child commit")?;
2544
2545 let parent_row = *oid_to_row
2546 .get(&line.parent)
2547 .context("Line references non-existent parent commit")?;
2548
2549 let child_lane = graph.commits[child_row].lane;
2550 if line.child_column != child_lane {
2551 bail!(
2552 "child_column ({}) != child's lane ({})",
2553 line.child_column,
2554 child_lane
2555 );
2556 }
2557
2558 let mut current_column = line.child_column;
2559 for segment in &line.segments {
2560 if let CommitLineSegment::Curve { to_column, .. } = segment {
2561 current_column = *to_column;
2562 }
2563 }
2564
2565 let parent_lane = graph.commits[parent_row].lane;
2566 if current_column != parent_lane {
2567 bail!(
2568 "ending column ({}) != parent's lane ({})",
2569 current_column,
2570 parent_lane
2571 );
2572 }
2573 }
2574
2575 Ok(())
2576 }
2577
2578 fn verify_segment_continuity(graph: &GraphData) -> Result<()> {
2579 for line in &graph.lines {
2580 if line.segments.is_empty() {
2581 bail!("Line has no segments");
2582 }
2583
2584 let mut current_row = line.full_interval.start;
2585
2586 for (idx, segment) in line.segments.iter().enumerate() {
2587 let segment_end_row = match segment {
2588 CommitLineSegment::Straight { to_row } => *to_row,
2589 CommitLineSegment::Curve { on_row, .. } => *on_row,
2590 };
2591
2592 if segment_end_row < current_row {
2593 bail!(
2594 "segment {} ends at row {} which is before current row {}",
2595 idx,
2596 segment_end_row,
2597 current_row
2598 );
2599 }
2600
2601 current_row = segment_end_row;
2602 }
2603 }
2604
2605 Ok(())
2606 }
2607
2608 fn verify_line_overlaps(graph: &GraphData) -> Result<()> {
2609 for line in &graph.lines {
2610 let child_row = line.full_interval.start;
2611
2612 let mut current_column = line.child_column;
2613 let mut current_row = child_row;
2614
2615 for segment in &line.segments {
2616 match segment {
2617 CommitLineSegment::Straight { to_row } => {
2618 for row in (current_row + 1)..*to_row {
2619 if row < graph.commits.len() {
2620 let commit_at_row = &graph.commits[row];
2621 if commit_at_row.lane == current_column {
2622 bail!(
2623 "straight segment from row {} to {} in column {} passes through commit {:?} at row {}",
2624 current_row,
2625 to_row,
2626 current_column,
2627 commit_at_row.data.sha,
2628 row
2629 );
2630 }
2631 }
2632 }
2633 current_row = *to_row;
2634 }
2635 CommitLineSegment::Curve {
2636 to_column, on_row, ..
2637 } => {
2638 current_column = *to_column;
2639 current_row = *on_row;
2640 }
2641 }
2642 }
2643 }
2644
2645 Ok(())
2646 }
2647
2648 fn verify_coverage(graph: &GraphData) -> Result<()> {
2649 let mut expected_edges: HashSet<(Oid, Oid)> = HashSet::default();
2650 for entry in &graph.commits {
2651 for parent in &entry.data.parents {
2652 expected_edges.insert((entry.data.sha, *parent));
2653 }
2654 }
2655
2656 let mut found_edges: HashSet<(Oid, Oid)> = HashSet::default();
2657 for line in &graph.lines {
2658 let edge = (line.child, line.parent);
2659
2660 if !found_edges.insert(edge) {
2661 bail!(
2662 "Duplicate line found for edge {:?} -> {:?}",
2663 line.child,
2664 line.parent
2665 );
2666 }
2667
2668 if !expected_edges.contains(&edge) {
2669 bail!(
2670 "Orphan line found: {:?} -> {:?} is not in the commit graph",
2671 line.child,
2672 line.parent
2673 );
2674 }
2675 }
2676
2677 for (child, parent) in &expected_edges {
2678 if !found_edges.contains(&(*child, *parent)) {
2679 bail!("Missing line for edge {:?} -> {:?}", child, parent);
2680 }
2681 }
2682
2683 assert_eq!(
2684 expected_edges.symmetric_difference(&found_edges).count(),
2685 0,
2686 "The symmetric difference should be zero"
2687 );
2688
2689 Ok(())
2690 }
2691
2692 fn verify_merge_line_optimality(
2693 graph: &GraphData,
2694 oid_to_row: &HashMap<Oid, usize>,
2695 ) -> Result<()> {
2696 for line in &graph.lines {
2697 let first_segment = line.segments.first();
2698 let is_merge_line = matches!(
2699 first_segment,
2700 Some(CommitLineSegment::Curve {
2701 curve_kind: CurveKind::Merge,
2702 ..
2703 })
2704 );
2705
2706 if !is_merge_line {
2707 continue;
2708 }
2709
2710 let child_row = *oid_to_row
2711 .get(&line.child)
2712 .context("Line references non-existent child commit")?;
2713
2714 let parent_row = *oid_to_row
2715 .get(&line.parent)
2716 .context("Line references non-existent parent commit")?;
2717
2718 let parent_lane = graph.commits[parent_row].lane;
2719
2720 let Some(CommitLineSegment::Curve { to_column, .. }) = first_segment else {
2721 continue;
2722 };
2723
2724 let curves_directly_to_parent = *to_column == parent_lane;
2725
2726 if !curves_directly_to_parent {
2727 continue;
2728 }
2729
2730 let curve_row = child_row + 1;
2731 let has_commits_in_path = graph.commits[curve_row..parent_row]
2732 .iter()
2733 .any(|c| c.lane == parent_lane);
2734
2735 if has_commits_in_path {
2736 bail!(
2737 "Merge line from {:?} to {:?} curves directly to parent lane {} but there are commits in that lane between rows {} and {}",
2738 line.child,
2739 line.parent,
2740 parent_lane,
2741 curve_row,
2742 parent_row
2743 );
2744 }
2745
2746 let curve_ends_at_parent = curve_row == parent_row;
2747
2748 if curve_ends_at_parent {
2749 if line.segments.len() != 1 {
2750 bail!(
2751 "Merge line from {:?} to {:?} curves directly to parent (curve_row == parent_row), but has {} segments instead of 1 [MergeCurve]",
2752 line.child,
2753 line.parent,
2754 line.segments.len()
2755 );
2756 }
2757 } else {
2758 if line.segments.len() != 2 {
2759 bail!(
2760 "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but has {} segments instead of 2 [MergeCurve, Straight]",
2761 line.child,
2762 line.parent,
2763 line.segments.len()
2764 );
2765 }
2766
2767 let is_straight_segment = matches!(
2768 line.segments.get(1),
2769 Some(CommitLineSegment::Straight { .. })
2770 );
2771
2772 if !is_straight_segment {
2773 bail!(
2774 "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but second segment is not a Straight segment",
2775 line.child,
2776 line.parent
2777 );
2778 }
2779 }
2780 }
2781
2782 Ok(())
2783 }
2784
2785 fn verify_all_invariants(
2786 graph: &GraphData,
2787 commits: &[Arc<InitialGraphCommitData>],
2788 ) -> Result<()> {
2789 let oid_to_row = build_oid_to_row_map(graph);
2790
2791 verify_commit_order(graph, commits).context("commit order")?;
2792 verify_line_endpoints(graph, &oid_to_row).context("line endpoints")?;
2793 verify_column_correctness(graph, &oid_to_row).context("column correctness")?;
2794 verify_segment_continuity(graph).context("segment continuity")?;
2795 verify_merge_line_optimality(graph, &oid_to_row).context("merge line optimality")?;
2796 verify_coverage(graph).context("coverage")?;
2797 verify_line_overlaps(graph).context("line overlaps")?;
2798 Ok(())
2799 }
2800
2801 #[test]
2802 fn test_git_graph_merge_commits() {
2803 let mut rng = StdRng::seed_from_u64(42);
2804
2805 let oid1 = Oid::random(&mut rng);
2806 let oid2 = Oid::random(&mut rng);
2807 let oid3 = Oid::random(&mut rng);
2808 let oid4 = Oid::random(&mut rng);
2809
2810 let commits = vec![
2811 Arc::new(InitialGraphCommitData {
2812 sha: oid1,
2813 parents: smallvec![oid2, oid3],
2814 ref_names: vec!["HEAD".into()],
2815 }),
2816 Arc::new(InitialGraphCommitData {
2817 sha: oid2,
2818 parents: smallvec![oid4],
2819 ref_names: vec![],
2820 }),
2821 Arc::new(InitialGraphCommitData {
2822 sha: oid3,
2823 parents: smallvec![oid4],
2824 ref_names: vec![],
2825 }),
2826 Arc::new(InitialGraphCommitData {
2827 sha: oid4,
2828 parents: smallvec![],
2829 ref_names: vec![],
2830 }),
2831 ];
2832
2833 let mut graph_data = GraphData::new(8);
2834 graph_data.add_commits(&commits);
2835
2836 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2837 panic!("Graph invariant violation for merge commits:\n{}", error);
2838 }
2839 }
2840
2841 #[test]
2842 fn test_git_graph_linear_commits() {
2843 let mut rng = StdRng::seed_from_u64(42);
2844
2845 let oid1 = Oid::random(&mut rng);
2846 let oid2 = Oid::random(&mut rng);
2847 let oid3 = Oid::random(&mut rng);
2848
2849 let commits = vec![
2850 Arc::new(InitialGraphCommitData {
2851 sha: oid1,
2852 parents: smallvec![oid2],
2853 ref_names: vec!["HEAD".into()],
2854 }),
2855 Arc::new(InitialGraphCommitData {
2856 sha: oid2,
2857 parents: smallvec![oid3],
2858 ref_names: vec![],
2859 }),
2860 Arc::new(InitialGraphCommitData {
2861 sha: oid3,
2862 parents: smallvec![],
2863 ref_names: vec![],
2864 }),
2865 ];
2866
2867 let mut graph_data = GraphData::new(8);
2868 graph_data.add_commits(&commits);
2869
2870 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2871 panic!("Graph invariant violation for linear commits:\n{}", error);
2872 }
2873 }
2874
2875 #[test]
2876 fn test_git_graph_random_commits() {
2877 for seed in 0..100 {
2878 let mut rng = StdRng::seed_from_u64(seed);
2879
2880 let adversarial = rng.random_bool(0.2);
2881 let num_commits = if adversarial {
2882 rng.random_range(10..100)
2883 } else {
2884 rng.random_range(5..50)
2885 };
2886
2887 let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
2888
2889 assert_eq!(
2890 num_commits,
2891 commits.len(),
2892 "seed={}: Generate random commit dag didn't generate the correct amount of commits",
2893 seed
2894 );
2895
2896 let mut graph_data = GraphData::new(8);
2897 graph_data.add_commits(&commits);
2898
2899 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2900 panic!(
2901 "Graph invariant violation (seed={}, adversarial={}, num_commits={}):\n{:#}",
2902 seed, adversarial, num_commits, error
2903 );
2904 }
2905 }
2906 }
2907
2908 // The full integration test has less iterations because it's significantly slower
2909 // than the random commit test
2910 #[gpui::test(iterations = 10)]
2911 async fn test_git_graph_random_integration(mut rng: StdRng, cx: &mut TestAppContext) {
2912 init_test(cx);
2913
2914 let adversarial = rng.random_bool(0.2);
2915 let num_commits = if adversarial {
2916 rng.random_range(10..100)
2917 } else {
2918 rng.random_range(5..50)
2919 };
2920
2921 let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
2922
2923 let fs = FakeFs::new(cx.executor());
2924 fs.insert_tree(
2925 Path::new("/project"),
2926 json!({
2927 ".git": {},
2928 "file.txt": "content",
2929 }),
2930 )
2931 .await;
2932
2933 fs.set_graph_commits(Path::new("/project/.git"), commits.clone());
2934
2935 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
2936 cx.run_until_parked();
2937
2938 let repository = project.read_with(cx, |project, cx| {
2939 project
2940 .active_repository(cx)
2941 .expect("should have a repository")
2942 });
2943
2944 repository.update(cx, |repo, cx| {
2945 repo.graph_data(
2946 crate::LogSource::default(),
2947 crate::LogOrder::default(),
2948 0..usize::MAX,
2949 cx,
2950 );
2951 });
2952 cx.run_until_parked();
2953
2954 let graph_commits: Vec<Arc<InitialGraphCommitData>> = repository.update(cx, |repo, cx| {
2955 repo.graph_data(
2956 crate::LogSource::default(),
2957 crate::LogOrder::default(),
2958 0..usize::MAX,
2959 cx,
2960 )
2961 .0
2962 .to_vec()
2963 });
2964
2965 let mut graph_data = GraphData::new(8);
2966 graph_data.add_commits(&graph_commits);
2967
2968 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2969 panic!(
2970 "Graph invariant violation (adversarial={}, num_commits={}):\n{:#}",
2971 adversarial, num_commits, error
2972 );
2973 }
2974 }
2975
2976 #[gpui::test]
2977 async fn test_initial_graph_data_not_cleared_on_initial_loading(cx: &mut TestAppContext) {
2978 init_test(cx);
2979
2980 let fs = FakeFs::new(cx.executor());
2981 fs.insert_tree(
2982 Path::new("/project"),
2983 json!({
2984 ".git": {},
2985 "file.txt": "content",
2986 }),
2987 )
2988 .await;
2989
2990 let mut rng = StdRng::seed_from_u64(42);
2991 let commits = generate_random_commit_dag(&mut rng, 10, false);
2992 fs.set_graph_commits(Path::new("/project/.git"), commits.clone());
2993
2994 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
2995 let observed_repository_events = Arc::new(Mutex::new(Vec::new()));
2996 project.update(cx, |project, cx| {
2997 let observed_repository_events = observed_repository_events.clone();
2998 cx.subscribe(project.git_store(), move |_, _, event, _| {
2999 if let GitStoreEvent::RepositoryUpdated(_, repository_event, true) = event {
3000 observed_repository_events
3001 .lock()
3002 .expect("repository event mutex should be available")
3003 .push(repository_event.clone());
3004 }
3005 })
3006 .detach();
3007 });
3008
3009 let repository = project.read_with(cx, |project, cx| {
3010 project
3011 .active_repository(cx)
3012 .expect("should have a repository")
3013 });
3014
3015 repository.update(cx, |repo, cx| {
3016 repo.graph_data(
3017 crate::LogSource::default(),
3018 crate::LogOrder::default(),
3019 0..usize::MAX,
3020 cx,
3021 );
3022 });
3023
3024 project
3025 .update(cx, |project, cx| project.git_scans_complete(cx))
3026 .await;
3027 cx.run_until_parked();
3028
3029 let observed_repository_events = observed_repository_events
3030 .lock()
3031 .expect("repository event mutex should be available");
3032 assert!(
3033 observed_repository_events
3034 .iter()
3035 .any(|event| matches!(event, RepositoryEvent::BranchChanged)),
3036 "initial repository scan should emit BranchChanged"
3037 );
3038 assert!(
3039 observed_repository_events
3040 .iter()
3041 .any(|event| matches!(event, RepositoryEvent::MergeHeadsChanged)),
3042 "initial repository scan should emit MergeHeadsChanged"
3043 );
3044
3045 let graph_data_key = (crate::LogOrder::default(), crate::LogSource::default());
3046 let commit_count_after = repository.read_with(cx, |repo, _| {
3047 repo.initial_graph_data
3048 .get(&graph_data_key)
3049 .map(|(_, data)| data.len())
3050 .unwrap_or(0)
3051 });
3052 assert_eq!(
3053 commits.len(),
3054 commit_count_after,
3055 "initial_graph_data should remain populated after events emitted by initial repository scan"
3056 );
3057 }
3058
3059 #[gpui::test]
3060 async fn test_graph_data_repopulated_from_cache_after_repo_switch(cx: &mut TestAppContext) {
3061 init_test_with_theme(cx);
3062
3063 let fs = FakeFs::new(cx.executor());
3064 fs.insert_tree(
3065 Path::new("/project_a"),
3066 json!({
3067 ".git": {},
3068 "file.txt": "content",
3069 }),
3070 )
3071 .await;
3072 fs.insert_tree(
3073 Path::new("/project_b"),
3074 json!({
3075 ".git": {},
3076 "other.txt": "content",
3077 }),
3078 )
3079 .await;
3080
3081 let mut rng = StdRng::seed_from_u64(42);
3082 let commits = generate_random_commit_dag(&mut rng, 10, false);
3083 fs.set_graph_commits(Path::new("/project_a/.git"), commits.clone());
3084
3085 let project = Project::test(
3086 fs.clone(),
3087 [Path::new("/project_a"), Path::new("/project_b")],
3088 cx,
3089 )
3090 .await;
3091 cx.run_until_parked();
3092
3093 let (first_repository, second_repository) = project.read_with(cx, |project, cx| {
3094 let mut first_repository = None;
3095 let mut second_repository = None;
3096
3097 for repository in project.repositories(cx).values() {
3098 let work_directory_abs_path = &repository.read(cx).work_directory_abs_path;
3099 if work_directory_abs_path.as_ref() == Path::new("/project_a") {
3100 first_repository = Some(repository.clone());
3101 } else if work_directory_abs_path.as_ref() == Path::new("/project_b") {
3102 second_repository = Some(repository.clone());
3103 }
3104 }
3105
3106 (
3107 first_repository.expect("should have repository for /project_a"),
3108 second_repository.expect("should have repository for /project_b"),
3109 )
3110 });
3111 first_repository.update(cx, |repository, cx| repository.set_as_active_repository(cx));
3112 cx.run_until_parked();
3113
3114 let (multi_workspace, cx) =
3115 cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
3116
3117 let workspace_weak =
3118 multi_workspace.read_with(&*cx, |multi, _| multi.workspace().downgrade());
3119 let git_graph = cx.new_window_entity(|window, cx| {
3120 GitGraph::new(project.clone(), workspace_weak, window, cx)
3121 });
3122 cx.run_until_parked();
3123
3124 // Verify initial graph data is loaded
3125 let initial_commit_count =
3126 git_graph.read_with(&*cx, |graph, _| graph.graph_data.commits.len());
3127 assert!(
3128 initial_commit_count > 0,
3129 "graph data should have been loaded, got 0 commits"
3130 );
3131
3132 second_repository.update(&mut *cx, |repository, cx| {
3133 repository.set_as_active_repository(cx)
3134 });
3135 cx.run_until_parked();
3136
3137 let commit_count_after_clear =
3138 git_graph.read_with(&*cx, |graph, _| graph.graph_data.commits.len());
3139 assert_eq!(
3140 commit_count_after_clear, 0,
3141 "graph_data should be cleared after switching away"
3142 );
3143
3144 first_repository.update(&mut *cx, |repository, cx| {
3145 repository.set_as_active_repository(cx)
3146 });
3147
3148 git_graph.update_in(&mut *cx, |this, window, cx| {
3149 this.render(window, cx);
3150 });
3151 cx.run_until_parked();
3152
3153 let commit_count_after_switch_back =
3154 git_graph.read_with(&*cx, |graph, _| graph.graph_data.commits.len());
3155 assert_eq!(
3156 initial_commit_count, commit_count_after_switch_back,
3157 "graph_data should be repopulated from cache after switching back to the same repo"
3158 );
3159 }
3160}