1use collections::{BTreeMap, HashMap, IndexSet};
2use editor::Editor;
3use git::{
4 BuildCommitPermalinkParams, GitHostingProviderRegistry, GitRemote, Oid, ParsedGitRemote,
5 parse_git_remote_url,
6 repository::{
7 CommitDiff, CommitFile, InitialGraphCommitData, LogOrder, LogSource, RepoPath,
8 SearchCommitArgs,
9 },
10 status::{FileStatus, StatusCode, TrackedStatus},
11};
12use git_ui::{commit_tooltip::CommitAvatar, commit_view::CommitView, git_status_icon};
13use gpui::{
14 AnyElement, App, Bounds, ClickEvent, ClipboardItem, Corner, DefiniteLength, DragMoveEvent,
15 ElementId, Empty, Entity, EventEmitter, FocusHandle, Focusable, Hsla, PathBuilder, Pixels,
16 Point, ScrollStrategy, ScrollWheelEvent, SharedString, Subscription, Task, TextStyleRefinement,
17 UniformListScrollHandle, WeakEntity, Window, actions, anchored, deferred, point, prelude::*,
18 px, uniform_list,
19};
20use language::line_diff;
21use menu::{Cancel, SelectFirst, SelectLast, SelectNext, SelectPrevious};
22use project::git_store::{
23 CommitDataState, GitGraphEvent, GitStore, GitStoreEvent, GraphDataResponse, Repository,
24 RepositoryEvent, RepositoryId,
25};
26use search::{
27 SearchOption, SearchOptions, SearchSource, SelectNextMatch, SelectPreviousMatch,
28 ToggleCaseSensitive, buffer_search,
29};
30use settings::Settings;
31use smallvec::{SmallVec, smallvec};
32use std::{
33 cell::Cell,
34 ops::Range,
35 rc::Rc,
36 sync::{Arc, OnceLock},
37 time::{Duration, Instant},
38};
39use theme::AccentColors;
40use theme_settings::ThemeSettings;
41use time::{OffsetDateTime, UtcOffset, format_description::BorrowedFormatItem};
42use ui::{
43 ButtonLike, Chip, ColumnWidthConfig, CommonAnimationExt as _, ContextMenu, DiffStat, Divider,
44 HeaderResizeInfo, HighlightedLabel, RedistributableColumnsState, ScrollableHandle, Table,
45 TableInteractionState, TableRenderContext, TableResizeBehavior, Tooltip, WithScrollbar,
46 bind_redistributable_columns, prelude::*, render_redistributable_columns_resize_handles,
47 render_table_header, table_row::TableRow,
48};
49use workspace::{
50 Workspace,
51 item::{Item, ItemEvent, TabTooltipContent},
52};
53
54const COMMIT_CIRCLE_RADIUS: Pixels = px(3.5);
55const COMMIT_CIRCLE_STROKE_WIDTH: Pixels = px(1.5);
56const LANE_WIDTH: Pixels = px(16.0);
57const LEFT_PADDING: Pixels = px(12.0);
58const LINE_WIDTH: Pixels = px(1.5);
59const RESIZE_HANDLE_WIDTH: f32 = 8.0;
60const COPIED_STATE_DURATION: Duration = Duration::from_secs(2);
61
62struct CopiedState {
63 copied_at: Option<Instant>,
64}
65
66impl CopiedState {
67 fn new(_window: &mut Window, _cx: &mut Context<Self>) -> Self {
68 Self { copied_at: None }
69 }
70
71 fn is_copied(&self) -> bool {
72 self.copied_at
73 .map(|t| t.elapsed() < COPIED_STATE_DURATION)
74 .unwrap_or(false)
75 }
76
77 fn mark_copied(&mut self) {
78 self.copied_at = Some(Instant::now());
79 }
80}
81
82struct DraggedSplitHandle;
83
84#[derive(Clone)]
85struct ChangedFileEntry {
86 status: FileStatus,
87 file_name: SharedString,
88 dir_path: SharedString,
89 repo_path: RepoPath,
90}
91
92impl ChangedFileEntry {
93 fn from_commit_file(file: &CommitFile, _cx: &App) -> Self {
94 let file_name: SharedString = file
95 .path
96 .file_name()
97 .map(|n| n.to_string())
98 .unwrap_or_default()
99 .into();
100 let dir_path: SharedString = file
101 .path
102 .parent()
103 .map(|p| p.as_unix_str().to_string())
104 .unwrap_or_default()
105 .into();
106
107 let status_code = match (&file.old_text, &file.new_text) {
108 (None, Some(_)) => StatusCode::Added,
109 (Some(_), None) => StatusCode::Deleted,
110 _ => StatusCode::Modified,
111 };
112
113 let status = FileStatus::Tracked(TrackedStatus {
114 index_status: status_code,
115 worktree_status: StatusCode::Unmodified,
116 });
117
118 Self {
119 status,
120 file_name,
121 dir_path,
122 repo_path: file.path.clone(),
123 }
124 }
125
126 fn open_in_commit_view(
127 &self,
128 commit_sha: &SharedString,
129 repository: &WeakEntity<Repository>,
130 workspace: &WeakEntity<Workspace>,
131 window: &mut Window,
132 cx: &mut App,
133 ) {
134 CommitView::open(
135 commit_sha.to_string(),
136 repository.clone(),
137 workspace.clone(),
138 None,
139 Some(self.repo_path.clone()),
140 window,
141 cx,
142 );
143 }
144
145 fn render(
146 &self,
147 ix: usize,
148 commit_sha: SharedString,
149 repository: WeakEntity<Repository>,
150 workspace: WeakEntity<Workspace>,
151 _cx: &App,
152 ) -> AnyElement {
153 let file_name = self.file_name.clone();
154 let dir_path = self.dir_path.clone();
155
156 div()
157 .w_full()
158 .child(
159 ButtonLike::new(("changed-file", ix))
160 .child(
161 h_flex()
162 .min_w_0()
163 .w_full()
164 .gap_1()
165 .overflow_hidden()
166 .child(git_status_icon(self.status))
167 .child(
168 Label::new(file_name.clone())
169 .size(LabelSize::Small)
170 .truncate(),
171 )
172 .when(!dir_path.is_empty(), |this| {
173 this.child(
174 Label::new(dir_path.clone())
175 .size(LabelSize::Small)
176 .color(Color::Muted)
177 .truncate_start(),
178 )
179 }),
180 )
181 .tooltip({
182 let meta = if dir_path.is_empty() {
183 file_name
184 } else {
185 format!("{}/{}", dir_path, file_name).into()
186 };
187 move |_, cx| Tooltip::with_meta("View Changes", None, meta.clone(), cx)
188 })
189 .on_click({
190 let entry = self.clone();
191 move |_, window, cx| {
192 entry.open_in_commit_view(
193 &commit_sha,
194 &repository,
195 &workspace,
196 window,
197 cx,
198 );
199 }
200 }),
201 )
202 .into_any_element()
203 }
204}
205
206enum QueryState {
207 Pending(SharedString),
208 Confirmed((SharedString, Task<()>)),
209 Empty,
210}
211
212impl QueryState {
213 fn next_state(&mut self) {
214 match self {
215 Self::Confirmed((query, _)) => *self = Self::Pending(std::mem::take(query)),
216 _ => {}
217 };
218 }
219}
220
221struct SearchState {
222 case_sensitive: bool,
223 editor: Entity<Editor>,
224 state: QueryState,
225 pub matches: IndexSet<Oid>,
226 pub selected_index: Option<usize>,
227}
228
229pub struct SplitState {
230 left_ratio: f32,
231 visible_left_ratio: f32,
232}
233
234impl SplitState {
235 pub fn new() -> Self {
236 Self {
237 left_ratio: 1.0,
238 visible_left_ratio: 1.0,
239 }
240 }
241
242 pub fn right_ratio(&self) -> f32 {
243 1.0 - self.visible_left_ratio
244 }
245
246 fn on_drag_move(
247 &mut self,
248 drag_event: &DragMoveEvent<DraggedSplitHandle>,
249 _window: &mut Window,
250 _cx: &mut Context<Self>,
251 ) {
252 let drag_position = drag_event.event.position;
253 let bounds = drag_event.bounds;
254 let bounds_width = bounds.right() - bounds.left();
255
256 let min_ratio = 0.1;
257 let max_ratio = 0.9;
258
259 let new_ratio = (drag_position.x - bounds.left()) / bounds_width;
260 self.visible_left_ratio = new_ratio.clamp(min_ratio, max_ratio);
261 }
262
263 fn commit_ratio(&mut self) {
264 self.left_ratio = self.visible_left_ratio;
265 }
266
267 fn on_double_click(&mut self) {
268 self.left_ratio = 1.0;
269 self.visible_left_ratio = 1.0;
270 }
271}
272
273actions!(
274 git_graph,
275 [
276 /// Opens the commit view for the selected commit.
277 OpenCommitView,
278 /// Focuses the search field.
279 FocusSearch,
280 ]
281);
282
283fn timestamp_format() -> &'static [BorrowedFormatItem<'static>] {
284 static FORMAT: OnceLock<Vec<BorrowedFormatItem<'static>>> = OnceLock::new();
285 FORMAT.get_or_init(|| {
286 time::format_description::parse("[day] [month repr:short] [year] [hour]:[minute]")
287 .unwrap_or_default()
288 })
289}
290
291fn format_timestamp(timestamp: i64) -> String {
292 let Ok(datetime) = OffsetDateTime::from_unix_timestamp(timestamp) else {
293 return "Unknown".to_string();
294 };
295
296 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
297 let local_datetime = datetime.to_offset(local_offset);
298
299 local_datetime
300 .format(timestamp_format())
301 .unwrap_or_default()
302}
303
304fn accent_colors_count(accents: &AccentColors) -> usize {
305 accents.0.len()
306}
307
308#[derive(Copy, Clone, Debug)]
309struct BranchColor(u8);
310
311#[derive(Debug)]
312enum LaneState {
313 Empty,
314 Active {
315 child: Oid,
316 parent: Oid,
317 color: Option<BranchColor>,
318 starting_row: usize,
319 starting_col: usize,
320 destination_column: Option<usize>,
321 segments: SmallVec<[CommitLineSegment; 1]>,
322 },
323}
324
325impl LaneState {
326 fn to_commit_lines(
327 &mut self,
328 ending_row: usize,
329 lane_column: usize,
330 parent_column: usize,
331 parent_color: BranchColor,
332 ) -> Option<CommitLine> {
333 let state = std::mem::replace(self, LaneState::Empty);
334
335 match state {
336 LaneState::Active {
337 #[cfg_attr(not(test), allow(unused_variables))]
338 parent,
339 #[cfg_attr(not(test), allow(unused_variables))]
340 child,
341 color,
342 starting_row,
343 starting_col,
344 destination_column,
345 mut segments,
346 } => {
347 let final_destination = destination_column.unwrap_or(parent_column);
348 let final_color = color.unwrap_or(parent_color);
349
350 Some(CommitLine {
351 #[cfg(test)]
352 child,
353 #[cfg(test)]
354 parent,
355 child_column: starting_col,
356 full_interval: starting_row..ending_row,
357 color_idx: final_color.0 as usize,
358 segments: {
359 match segments.last_mut() {
360 Some(CommitLineSegment::Straight { to_row })
361 if *to_row == usize::MAX =>
362 {
363 if final_destination != lane_column {
364 *to_row = ending_row - 1;
365
366 let curved_line = CommitLineSegment::Curve {
367 to_column: final_destination,
368 on_row: ending_row,
369 curve_kind: CurveKind::Checkout,
370 };
371
372 if *to_row == starting_row {
373 let last_index = segments.len() - 1;
374 segments[last_index] = curved_line;
375 } else {
376 segments.push(curved_line);
377 }
378 } else {
379 *to_row = ending_row;
380 }
381 }
382 Some(CommitLineSegment::Curve {
383 on_row,
384 to_column,
385 curve_kind,
386 }) if *on_row == usize::MAX => {
387 if *to_column == usize::MAX {
388 *to_column = final_destination;
389 }
390 if matches!(curve_kind, CurveKind::Merge) {
391 *on_row = starting_row + 1;
392 if *on_row < ending_row {
393 if *to_column != final_destination {
394 segments.push(CommitLineSegment::Straight {
395 to_row: ending_row - 1,
396 });
397 segments.push(CommitLineSegment::Curve {
398 to_column: final_destination,
399 on_row: ending_row,
400 curve_kind: CurveKind::Checkout,
401 });
402 } else {
403 segments.push(CommitLineSegment::Straight {
404 to_row: ending_row,
405 });
406 }
407 } else if *to_column != final_destination {
408 segments.push(CommitLineSegment::Curve {
409 to_column: final_destination,
410 on_row: ending_row,
411 curve_kind: CurveKind::Checkout,
412 });
413 }
414 } else {
415 *on_row = ending_row;
416 if *to_column != final_destination {
417 segments.push(CommitLineSegment::Straight {
418 to_row: ending_row,
419 });
420 segments.push(CommitLineSegment::Curve {
421 to_column: final_destination,
422 on_row: ending_row,
423 curve_kind: CurveKind::Checkout,
424 });
425 }
426 }
427 }
428 Some(CommitLineSegment::Curve {
429 on_row, to_column, ..
430 }) => {
431 if *on_row < ending_row {
432 if *to_column != final_destination {
433 segments.push(CommitLineSegment::Straight {
434 to_row: ending_row - 1,
435 });
436 segments.push(CommitLineSegment::Curve {
437 to_column: final_destination,
438 on_row: ending_row,
439 curve_kind: CurveKind::Checkout,
440 });
441 } else {
442 segments.push(CommitLineSegment::Straight {
443 to_row: ending_row,
444 });
445 }
446 } else if *to_column != final_destination {
447 segments.push(CommitLineSegment::Curve {
448 to_column: final_destination,
449 on_row: ending_row,
450 curve_kind: CurveKind::Checkout,
451 });
452 }
453 }
454 _ => {}
455 }
456
457 segments
458 },
459 })
460 }
461 LaneState::Empty => None,
462 }
463 }
464
465 fn is_empty(&self) -> bool {
466 match self {
467 LaneState::Empty => true,
468 LaneState::Active { .. } => false,
469 }
470 }
471}
472
473struct CommitEntry {
474 data: Arc<InitialGraphCommitData>,
475 lane: usize,
476 color_idx: usize,
477}
478
479type ActiveLaneIdx = usize;
480
481enum AllCommitCount {
482 NotLoaded,
483 Loaded(usize),
484}
485
486#[derive(Debug)]
487enum CurveKind {
488 Merge,
489 Checkout,
490}
491
492#[derive(Debug)]
493enum CommitLineSegment {
494 Straight {
495 to_row: usize,
496 },
497 Curve {
498 to_column: usize,
499 on_row: usize,
500 curve_kind: CurveKind,
501 },
502}
503
504#[derive(Debug)]
505struct CommitLine {
506 #[cfg(test)]
507 child: Oid,
508 #[cfg(test)]
509 parent: Oid,
510 child_column: usize,
511 full_interval: Range<usize>,
512 color_idx: usize,
513 segments: SmallVec<[CommitLineSegment; 1]>,
514}
515
516impl CommitLine {
517 fn get_first_visible_segment_idx(&self, first_visible_row: usize) -> Option<(usize, usize)> {
518 if first_visible_row > self.full_interval.end {
519 return None;
520 } else if first_visible_row <= self.full_interval.start {
521 return Some((0, self.child_column));
522 }
523
524 let mut current_column = self.child_column;
525
526 for (idx, segment) in self.segments.iter().enumerate() {
527 match segment {
528 CommitLineSegment::Straight { to_row } => {
529 if *to_row >= first_visible_row {
530 return Some((idx, current_column));
531 }
532 }
533 CommitLineSegment::Curve {
534 to_column, on_row, ..
535 } => {
536 if *on_row >= first_visible_row {
537 return Some((idx, current_column));
538 }
539 current_column = *to_column;
540 }
541 }
542 }
543
544 None
545 }
546}
547
548#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
549struct CommitLineKey {
550 child: Oid,
551 parent: Oid,
552}
553
554struct GraphData {
555 lane_states: SmallVec<[LaneState; 8]>,
556 lane_colors: HashMap<ActiveLaneIdx, BranchColor>,
557 parent_to_lanes: HashMap<Oid, SmallVec<[usize; 1]>>,
558 next_color: BranchColor,
559 accent_colors_count: usize,
560 commits: Vec<Rc<CommitEntry>>,
561 max_commit_count: AllCommitCount,
562 max_lanes: usize,
563 lines: Vec<Rc<CommitLine>>,
564 active_commit_lines: HashMap<CommitLineKey, usize>,
565 active_commit_lines_by_parent: HashMap<Oid, SmallVec<[usize; 1]>>,
566}
567
568impl GraphData {
569 fn new(accent_colors_count: usize) -> Self {
570 GraphData {
571 lane_states: SmallVec::default(),
572 lane_colors: HashMap::default(),
573 parent_to_lanes: HashMap::default(),
574 next_color: BranchColor(0),
575 accent_colors_count,
576 commits: Vec::default(),
577 max_commit_count: AllCommitCount::NotLoaded,
578 max_lanes: 0,
579 lines: Vec::default(),
580 active_commit_lines: HashMap::default(),
581 active_commit_lines_by_parent: HashMap::default(),
582 }
583 }
584
585 fn clear(&mut self) {
586 self.lane_states.clear();
587 self.lane_colors.clear();
588 self.parent_to_lanes.clear();
589 self.commits.clear();
590 self.lines.clear();
591 self.active_commit_lines.clear();
592 self.active_commit_lines_by_parent.clear();
593 self.next_color = BranchColor(0);
594 self.max_commit_count = AllCommitCount::NotLoaded;
595 self.max_lanes = 0;
596 }
597
598 fn first_empty_lane_idx(&mut self) -> ActiveLaneIdx {
599 self.lane_states
600 .iter()
601 .position(LaneState::is_empty)
602 .unwrap_or_else(|| {
603 self.lane_states.push(LaneState::Empty);
604 self.lane_states.len() - 1
605 })
606 }
607
608 fn get_lane_color(&mut self, lane_idx: ActiveLaneIdx) -> BranchColor {
609 let accent_colors_count = self.accent_colors_count;
610 *self.lane_colors.entry(lane_idx).or_insert_with(|| {
611 let color_idx = self.next_color;
612 self.next_color = BranchColor((self.next_color.0 + 1) % accent_colors_count as u8);
613 color_idx
614 })
615 }
616
617 fn add_commits(&mut self, commits: &[Arc<InitialGraphCommitData>]) {
618 self.commits.reserve(commits.len());
619 self.lines.reserve(commits.len() / 2);
620
621 for commit in commits.iter() {
622 let commit_row = self.commits.len();
623
624 let commit_lane = self
625 .parent_to_lanes
626 .get(&commit.sha)
627 .and_then(|lanes| lanes.first().copied());
628
629 let commit_lane = commit_lane.unwrap_or_else(|| self.first_empty_lane_idx());
630
631 let commit_color = self.get_lane_color(commit_lane);
632
633 if let Some(lanes) = self.parent_to_lanes.remove(&commit.sha) {
634 for lane_column in lanes {
635 let state = &mut self.lane_states[lane_column];
636
637 if let LaneState::Active {
638 starting_row,
639 segments,
640 ..
641 } = state
642 {
643 if let Some(CommitLineSegment::Curve {
644 to_column,
645 curve_kind: CurveKind::Merge,
646 ..
647 }) = segments.first_mut()
648 {
649 let curve_row = *starting_row + 1;
650 let would_overlap =
651 if lane_column != commit_lane && curve_row < commit_row {
652 self.commits[curve_row..commit_row]
653 .iter()
654 .any(|c| c.lane == commit_lane)
655 } else {
656 false
657 };
658
659 if would_overlap {
660 *to_column = lane_column;
661 }
662 }
663 }
664
665 if let Some(commit_line) =
666 state.to_commit_lines(commit_row, lane_column, commit_lane, commit_color)
667 {
668 self.lines.push(Rc::new(commit_line));
669 }
670 }
671 }
672
673 commit
674 .parents
675 .iter()
676 .enumerate()
677 .for_each(|(parent_idx, parent)| {
678 if parent_idx == 0 {
679 self.lane_states[commit_lane] = LaneState::Active {
680 parent: *parent,
681 child: commit.sha,
682 color: Some(commit_color),
683 starting_col: commit_lane,
684 starting_row: commit_row,
685 destination_column: None,
686 segments: smallvec![CommitLineSegment::Straight { to_row: usize::MAX }],
687 };
688
689 self.parent_to_lanes
690 .entry(*parent)
691 .or_default()
692 .push(commit_lane);
693 } else {
694 let new_lane = self.first_empty_lane_idx();
695
696 self.lane_states[new_lane] = LaneState::Active {
697 parent: *parent,
698 child: commit.sha,
699 color: None,
700 starting_col: commit_lane,
701 starting_row: commit_row,
702 destination_column: None,
703 segments: smallvec![CommitLineSegment::Curve {
704 to_column: usize::MAX,
705 on_row: usize::MAX,
706 curve_kind: CurveKind::Merge,
707 },],
708 };
709
710 self.parent_to_lanes
711 .entry(*parent)
712 .or_default()
713 .push(new_lane);
714 }
715 });
716
717 self.max_lanes = self.max_lanes.max(self.lane_states.len());
718
719 self.commits.push(Rc::new(CommitEntry {
720 data: commit.clone(),
721 lane: commit_lane,
722 color_idx: commit_color.0 as usize,
723 }));
724 }
725
726 self.max_commit_count = AllCommitCount::Loaded(self.commits.len());
727 }
728}
729
730pub fn init(cx: &mut App) {
731 workspace::register_serializable_item::<GitGraph>(cx);
732
733 cx.observe_new(|workspace: &mut workspace::Workspace, _, _| {
734 workspace.register_action_renderer(|div, workspace, _, cx| {
735 div.when(
736 workspace.project().read(cx).active_repository(cx).is_some(),
737 |div| {
738 let workspace = workspace.weak_handle();
739
740 div.on_action({
741 let workspace = workspace.clone();
742 move |_: &git_ui::git_panel::Open, window, cx| {
743 workspace
744 .update(cx, |workspace, cx| {
745 let Some(repo) =
746 workspace.project().read(cx).active_repository(cx)
747 else {
748 return;
749 };
750 let selected_repo_id = repo.read(cx).id;
751
752 let existing = workspace
753 .items_of_type::<GitGraph>(cx)
754 .find(|graph| graph.read(cx).repo_id == selected_repo_id);
755 if let Some(existing) = existing {
756 workspace.activate_item(&existing, true, true, window, cx);
757 return;
758 }
759
760 let git_store =
761 workspace.project().read(cx).git_store().clone();
762 let workspace_handle = workspace.weak_handle();
763 let git_graph = cx.new(|cx| {
764 GitGraph::new(
765 selected_repo_id,
766 git_store,
767 workspace_handle,
768 window,
769 cx,
770 )
771 });
772 workspace.add_item_to_active_pane(
773 Box::new(git_graph),
774 None,
775 true,
776 window,
777 cx,
778 );
779 })
780 .ok();
781 }
782 })
783 .on_action(
784 move |action: &git_ui::git_panel::OpenAtCommit, window, cx| {
785 let sha = action.sha.clone();
786 workspace
787 .update(cx, |workspace, cx| {
788 let Some(repo) =
789 workspace.project().read(cx).active_repository(cx)
790 else {
791 return;
792 };
793 let selected_repo_id = repo.read(cx).id;
794
795 let existing = workspace
796 .items_of_type::<GitGraph>(cx)
797 .find(|graph| graph.read(cx).repo_id == selected_repo_id);
798 if let Some(existing) = existing {
799 existing.update(cx, |graph, cx| {
800 graph.select_commit_by_sha(sha.as_str(), cx);
801 });
802 workspace.activate_item(&existing, true, true, window, cx);
803 return;
804 }
805
806 let git_store =
807 workspace.project().read(cx).git_store().clone();
808 let workspace_handle = workspace.weak_handle();
809 let git_graph = cx.new(|cx| {
810 let mut graph = GitGraph::new(
811 selected_repo_id,
812 git_store,
813 workspace_handle,
814 window,
815 cx,
816 );
817 graph.select_commit_by_sha(sha.as_str(), cx);
818 graph
819 });
820 workspace.add_item_to_active_pane(
821 Box::new(git_graph),
822 None,
823 true,
824 window,
825 cx,
826 );
827 })
828 .ok();
829 },
830 )
831 },
832 )
833 });
834 })
835 .detach();
836}
837
838fn lane_center_x(bounds: Bounds<Pixels>, lane: f32) -> Pixels {
839 bounds.origin.x + LEFT_PADDING + lane * LANE_WIDTH + LANE_WIDTH / 2.0
840}
841
842fn to_row_center(
843 to_row: usize,
844 row_height: Pixels,
845 scroll_offset: Pixels,
846 bounds: Bounds<Pixels>,
847) -> Pixels {
848 bounds.origin.y + to_row as f32 * row_height + row_height / 2.0 - scroll_offset
849}
850
851fn draw_commit_circle(center_x: Pixels, center_y: Pixels, color: Hsla, window: &mut Window) {
852 let radius = COMMIT_CIRCLE_RADIUS;
853
854 let mut builder = PathBuilder::fill();
855
856 // Start at the rightmost point of the circle
857 builder.move_to(point(center_x + radius, center_y));
858
859 // Draw the circle using two arc_to calls (top half, then bottom half)
860 builder.arc_to(
861 point(radius, radius),
862 px(0.),
863 false,
864 true,
865 point(center_x - radius, center_y),
866 );
867 builder.arc_to(
868 point(radius, radius),
869 px(0.),
870 false,
871 true,
872 point(center_x + radius, center_y),
873 );
874 builder.close();
875
876 if let Ok(path) = builder.build() {
877 window.paint_path(path, color);
878 }
879}
880
881fn compute_diff_stats(diff: &CommitDiff) -> (usize, usize) {
882 diff.files.iter().fold((0, 0), |(added, removed), file| {
883 let old_text = file.old_text.as_deref().unwrap_or("");
884 let new_text = file.new_text.as_deref().unwrap_or("");
885 let hunks = line_diff(old_text, new_text);
886 hunks
887 .iter()
888 .fold((added, removed), |(a, r), (old_range, new_range)| {
889 (
890 a + (new_range.end - new_range.start) as usize,
891 r + (old_range.end - old_range.start) as usize,
892 )
893 })
894 })
895}
896
897pub struct GitGraph {
898 focus_handle: FocusHandle,
899 search_state: SearchState,
900 graph_data: GraphData,
901 git_store: Entity<GitStore>,
902 workspace: WeakEntity<Workspace>,
903 context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
904 row_height: Pixels,
905 table_interaction_state: Entity<TableInteractionState>,
906 column_widths: Entity<RedistributableColumnsState>,
907 selected_entry_idx: Option<usize>,
908 hovered_entry_idx: Option<usize>,
909 graph_canvas_bounds: Rc<Cell<Option<Bounds<Pixels>>>>,
910 log_source: LogSource,
911 log_order: LogOrder,
912 selected_commit_diff: Option<CommitDiff>,
913 selected_commit_diff_stats: Option<(usize, usize)>,
914 _commit_diff_task: Option<Task<()>>,
915 commit_details_split_state: Entity<SplitState>,
916 repo_id: RepositoryId,
917 changed_files_scroll_handle: UniformListScrollHandle,
918 pending_select_sha: Option<Oid>,
919}
920
921impl GitGraph {
922 fn invalidate_state(&mut self, cx: &mut Context<Self>) {
923 self.graph_data.clear();
924 self.search_state.matches.clear();
925 self.search_state.selected_index = None;
926 self.search_state.state.next_state();
927 cx.notify();
928 }
929
930 fn row_height(cx: &App) -> Pixels {
931 let settings = ThemeSettings::get_global(cx);
932 let font_size = settings.buffer_font_size(cx);
933 font_size + px(12.0)
934 }
935
936 fn graph_canvas_content_width(&self) -> Pixels {
937 (LANE_WIDTH * self.graph_data.max_lanes.max(6) as f32) + LEFT_PADDING * 2.0
938 }
939
940 fn preview_column_fractions(&self, window: &Window, cx: &App) -> [f32; 5] {
941 let fractions = self
942 .column_widths
943 .read(cx)
944 .preview_fractions(window.rem_size());
945 [
946 fractions[0],
947 fractions[1],
948 fractions[2],
949 fractions[3],
950 fractions[4],
951 ]
952 }
953
954 fn table_column_width_config(&self, window: &Window, cx: &App) -> ColumnWidthConfig {
955 let [_, description, date, author, commit] = self.preview_column_fractions(window, cx);
956 let table_total = description + date + author + commit;
957
958 let widths = if table_total > 0.0 {
959 vec![
960 DefiniteLength::Fraction(description / table_total),
961 DefiniteLength::Fraction(date / table_total),
962 DefiniteLength::Fraction(author / table_total),
963 DefiniteLength::Fraction(commit / table_total),
964 ]
965 } else {
966 vec![
967 DefiniteLength::Fraction(0.25),
968 DefiniteLength::Fraction(0.25),
969 DefiniteLength::Fraction(0.25),
970 DefiniteLength::Fraction(0.25),
971 ]
972 };
973
974 ColumnWidthConfig::explicit(widths)
975 }
976
977 fn graph_viewport_width(&self, window: &Window, cx: &App) -> Pixels {
978 self.column_widths
979 .read(cx)
980 .preview_column_width(0, window)
981 .unwrap_or_else(|| self.graph_canvas_content_width())
982 }
983
984 pub fn new(
985 repo_id: RepositoryId,
986 git_store: Entity<GitStore>,
987 workspace: WeakEntity<Workspace>,
988 window: &mut Window,
989 cx: &mut Context<Self>,
990 ) -> Self {
991 let focus_handle = cx.focus_handle();
992 cx.on_focus(&focus_handle, window, |_, _, cx| cx.notify())
993 .detach();
994
995 let accent_colors = cx.theme().accents();
996 let graph = GraphData::new(accent_colors_count(accent_colors));
997 let log_source = LogSource::default();
998 let log_order = LogOrder::default();
999
1000 cx.subscribe(&git_store, |this, _, event, cx| match event {
1001 GitStoreEvent::RepositoryUpdated(updated_repo_id, repo_event, _) => {
1002 if this.repo_id == *updated_repo_id {
1003 if let Some(repository) = this.get_repository(cx) {
1004 this.on_repository_event(repository, repo_event, cx);
1005 }
1006 }
1007 }
1008 _ => {}
1009 })
1010 .detach();
1011
1012 let search_editor = cx.new(|cx| {
1013 let mut editor = Editor::single_line(window, cx);
1014 editor.set_placeholder_text("Search commits…", window, cx);
1015 editor
1016 });
1017
1018 let table_interaction_state = cx.new(|cx| TableInteractionState::new(cx));
1019 let column_widths = cx.new(|_cx| {
1020 RedistributableColumnsState::new(
1021 5,
1022 vec![
1023 DefiniteLength::Fraction(0.14),
1024 DefiniteLength::Fraction(0.6192),
1025 DefiniteLength::Fraction(0.1032),
1026 DefiniteLength::Fraction(0.086),
1027 DefiniteLength::Fraction(0.0516),
1028 ],
1029 vec![
1030 TableResizeBehavior::Resizable,
1031 TableResizeBehavior::Resizable,
1032 TableResizeBehavior::Resizable,
1033 TableResizeBehavior::Resizable,
1034 TableResizeBehavior::Resizable,
1035 ],
1036 )
1037 });
1038 let mut row_height = Self::row_height(cx);
1039
1040 cx.observe_global_in::<settings::SettingsStore>(window, move |this, _window, cx| {
1041 let new_row_height = Self::row_height(cx);
1042 if new_row_height != row_height {
1043 this.row_height = new_row_height;
1044 this.table_interaction_state.update(cx, |state, _cx| {
1045 state.scroll_handle.0.borrow_mut().last_item_size = None;
1046 });
1047 row_height = new_row_height;
1048 cx.notify();
1049 }
1050 })
1051 .detach();
1052
1053 let mut this = GitGraph {
1054 focus_handle,
1055 git_store,
1056 search_state: SearchState {
1057 case_sensitive: false,
1058 editor: search_editor,
1059 matches: IndexSet::default(),
1060 selected_index: None,
1061 state: QueryState::Empty,
1062 },
1063 workspace,
1064 graph_data: graph,
1065 _commit_diff_task: None,
1066 context_menu: None,
1067 row_height,
1068 table_interaction_state,
1069 column_widths,
1070 selected_entry_idx: None,
1071 hovered_entry_idx: None,
1072 graph_canvas_bounds: Rc::new(Cell::new(None)),
1073 selected_commit_diff: None,
1074 selected_commit_diff_stats: None,
1075 log_source,
1076 log_order,
1077 commit_details_split_state: cx.new(|_cx| SplitState::new()),
1078 repo_id,
1079 changed_files_scroll_handle: UniformListScrollHandle::new(),
1080 pending_select_sha: None,
1081 };
1082
1083 this.fetch_initial_graph_data(cx);
1084 this
1085 }
1086
1087 fn on_repository_event(
1088 &mut self,
1089 repository: Entity<Repository>,
1090 event: &RepositoryEvent,
1091 cx: &mut Context<Self>,
1092 ) {
1093 match event {
1094 RepositoryEvent::GraphEvent((source, order), event)
1095 if source == &self.log_source && order == &self.log_order =>
1096 {
1097 match event {
1098 GitGraphEvent::FullyLoaded => {
1099 if let Some(pending_sha_index) =
1100 self.pending_select_sha.take().and_then(|oid| {
1101 repository
1102 .read(cx)
1103 .get_graph_data(source.clone(), *order)
1104 .and_then(|data| data.commit_oid_to_index.get(&oid).copied())
1105 })
1106 {
1107 self.select_entry(pending_sha_index, ScrollStrategy::Nearest, cx);
1108 }
1109 }
1110 GitGraphEvent::LoadingError => {
1111 // todo(git_graph): Wire this up with the UI
1112 }
1113 GitGraphEvent::CountUpdated(commit_count) => {
1114 let old_count = self.graph_data.commits.len();
1115
1116 if let Some(pending_selection_index) =
1117 repository.update(cx, |repository, cx| {
1118 let GraphDataResponse {
1119 commits,
1120 is_loading,
1121 error: _,
1122 } = repository.graph_data(
1123 source.clone(),
1124 *order,
1125 old_count..*commit_count,
1126 cx,
1127 );
1128 self.graph_data.add_commits(commits);
1129
1130 let pending_sha_index = self.pending_select_sha.and_then(|oid| {
1131 repository.get_graph_data(source.clone(), *order).and_then(
1132 |data| data.commit_oid_to_index.get(&oid).copied(),
1133 )
1134 });
1135
1136 if !is_loading && pending_sha_index.is_none() {
1137 self.pending_select_sha.take();
1138 }
1139
1140 pending_sha_index
1141 })
1142 {
1143 self.select_entry(pending_selection_index, ScrollStrategy::Nearest, cx);
1144 self.pending_select_sha.take();
1145 }
1146
1147 cx.notify();
1148 }
1149 }
1150 }
1151 RepositoryEvent::HeadChanged | RepositoryEvent::BranchListChanged => {
1152 self.pending_select_sha = None;
1153 // Only invalidate if we scanned atleast once,
1154 // meaning we are not inside the initial repo loading state
1155 // NOTE: this fixes an loading performance regression
1156 if repository.read(cx).scan_id > 1 {
1157 self.invalidate_state(cx);
1158 }
1159 }
1160 RepositoryEvent::StashEntriesChanged if self.log_source == LogSource::All => {
1161 self.pending_select_sha = None;
1162 if repository.read(cx).scan_id > 1 {
1163 self.invalidate_state(cx);
1164 }
1165 }
1166 RepositoryEvent::GraphEvent(_, _) => {}
1167 _ => {}
1168 }
1169 }
1170
1171 fn fetch_initial_graph_data(&mut self, cx: &mut App) {
1172 if let Some(repository) = self.get_repository(cx) {
1173 repository.update(cx, |repository, cx| {
1174 let commits = repository
1175 .graph_data(self.log_source.clone(), self.log_order, 0..usize::MAX, cx)
1176 .commits;
1177 self.graph_data.add_commits(commits);
1178 });
1179 }
1180 }
1181
1182 fn get_repository(&self, cx: &App) -> Option<Entity<Repository>> {
1183 let git_store = self.git_store.read(cx);
1184 git_store.repositories().get(&self.repo_id).cloned()
1185 }
1186
1187 fn render_chip(&self, name: &SharedString, accent_color: gpui::Hsla) -> impl IntoElement {
1188 Chip::new(name.clone())
1189 .label_size(LabelSize::Small)
1190 .bg_color(accent_color.opacity(0.1))
1191 .border_color(accent_color.opacity(0.5))
1192 }
1193
1194 fn render_table_rows(
1195 &mut self,
1196 range: Range<usize>,
1197 _window: &mut Window,
1198 cx: &mut Context<Self>,
1199 ) -> Vec<Vec<AnyElement>> {
1200 let repository = self.get_repository(cx);
1201
1202 let row_height = self.row_height;
1203
1204 // We fetch data outside the visible viewport to avoid loading entries when
1205 // users scroll through the git graph
1206 if let Some(repository) = repository.as_ref() {
1207 const FETCH_RANGE: usize = 100;
1208 repository.update(cx, |repository, cx| {
1209 self.graph_data.commits[range.start.saturating_sub(FETCH_RANGE)
1210 ..(range.end + FETCH_RANGE)
1211 .min(self.graph_data.commits.len().saturating_sub(1))]
1212 .iter()
1213 .for_each(|commit| {
1214 repository.fetch_commit_data(commit.data.sha, cx);
1215 });
1216 });
1217 }
1218
1219 range
1220 .map(|idx| {
1221 let Some((commit, repository)) =
1222 self.graph_data.commits.get(idx).zip(repository.as_ref())
1223 else {
1224 return vec![
1225 div().h(row_height).into_any_element(),
1226 div().h(row_height).into_any_element(),
1227 div().h(row_height).into_any_element(),
1228 div().h(row_height).into_any_element(),
1229 ];
1230 };
1231
1232 let data = repository.update(cx, |repository, cx| {
1233 repository.fetch_commit_data(commit.data.sha, cx).clone()
1234 });
1235
1236 let short_sha = commit.data.sha.display_short();
1237 let mut formatted_time = String::new();
1238 let subject: SharedString;
1239 let author_name: SharedString;
1240
1241 if let CommitDataState::Loaded(data) = data {
1242 subject = data.subject.clone();
1243 author_name = data.author_name.clone();
1244 formatted_time = format_timestamp(data.commit_timestamp);
1245 } else {
1246 subject = "Loading…".into();
1247 author_name = "".into();
1248 }
1249
1250 let accent_colors = cx.theme().accents();
1251 let accent_color = accent_colors
1252 .0
1253 .get(commit.color_idx)
1254 .copied()
1255 .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
1256
1257 let is_selected = self.selected_entry_idx == Some(idx);
1258 let is_matched = self.search_state.matches.contains(&commit.data.sha);
1259 let column_label = |label: SharedString| {
1260 Label::new(label)
1261 .when(!is_selected, |c| c.color(Color::Muted))
1262 .truncate()
1263 .into_any_element()
1264 };
1265
1266 let subject_label = if is_matched {
1267 let query = match &self.search_state.state {
1268 QueryState::Confirmed((query, _)) => Some(query.clone()),
1269 _ => None,
1270 };
1271 let highlight_ranges = query
1272 .and_then(|q| {
1273 let ranges = if self.search_state.case_sensitive {
1274 subject
1275 .match_indices(q.as_str())
1276 .map(|(start, matched)| start..start + matched.len())
1277 .collect::<Vec<_>>()
1278 } else {
1279 let q = q.to_lowercase();
1280 let subject_lower = subject.to_lowercase();
1281
1282 subject_lower
1283 .match_indices(&q)
1284 .filter_map(|(start, matched)| {
1285 let end = start + matched.len();
1286 subject.is_char_boundary(start).then_some(()).and_then(
1287 |_| subject.is_char_boundary(end).then_some(start..end),
1288 )
1289 })
1290 .collect::<Vec<_>>()
1291 };
1292
1293 (!ranges.is_empty()).then_some(ranges)
1294 })
1295 .unwrap_or_default();
1296 HighlightedLabel::from_ranges(subject.clone(), highlight_ranges)
1297 .when(!is_selected, |c| c.color(Color::Muted))
1298 .truncate()
1299 .into_any_element()
1300 } else {
1301 column_label(subject.clone())
1302 };
1303
1304 vec![
1305 div()
1306 .id(ElementId::NamedInteger("commit-subject".into(), idx as u64))
1307 .overflow_hidden()
1308 .tooltip(Tooltip::text(subject))
1309 .child(
1310 h_flex()
1311 .gap_2()
1312 .overflow_hidden()
1313 .children((!commit.data.ref_names.is_empty()).then(|| {
1314 h_flex().gap_1().children(
1315 commit
1316 .data
1317 .ref_names
1318 .iter()
1319 .map(|name| self.render_chip(name, accent_color)),
1320 )
1321 }))
1322 .child(subject_label),
1323 )
1324 .into_any_element(),
1325 column_label(formatted_time.into()),
1326 column_label(author_name),
1327 column_label(short_sha.into()),
1328 ]
1329 })
1330 .collect()
1331 }
1332
1333 fn cancel(&mut self, _: &Cancel, _window: &mut Window, cx: &mut Context<Self>) {
1334 self.selected_entry_idx = None;
1335 self.selected_commit_diff = None;
1336 self.selected_commit_diff_stats = None;
1337 cx.notify();
1338 }
1339
1340 fn select_first(&mut self, _: &SelectFirst, _window: &mut Window, cx: &mut Context<Self>) {
1341 self.select_entry(0, ScrollStrategy::Nearest, cx);
1342 }
1343
1344 fn select_prev(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {
1345 if let Some(selected_entry_idx) = &self.selected_entry_idx {
1346 self.select_entry(
1347 selected_entry_idx.saturating_sub(1),
1348 ScrollStrategy::Nearest,
1349 cx,
1350 );
1351 } else {
1352 self.select_first(&SelectFirst, window, cx);
1353 }
1354 }
1355
1356 fn select_next(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
1357 if let Some(selected_entry_idx) = &self.selected_entry_idx {
1358 self.select_entry(
1359 selected_entry_idx
1360 .saturating_add(1)
1361 .min(self.graph_data.commits.len().saturating_sub(1)),
1362 ScrollStrategy::Nearest,
1363 cx,
1364 );
1365 } else {
1366 self.select_prev(&SelectPrevious, window, cx);
1367 }
1368 }
1369
1370 fn select_last(&mut self, _: &SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
1371 self.select_entry(
1372 self.graph_data.commits.len().saturating_sub(1),
1373 ScrollStrategy::Nearest,
1374 cx,
1375 );
1376 }
1377
1378 fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
1379 self.open_selected_commit_view(window, cx);
1380 }
1381
1382 fn search(&mut self, query: SharedString, cx: &mut Context<Self>) {
1383 let Some(repo) = self.get_repository(cx) else {
1384 return;
1385 };
1386
1387 self.search_state.matches.clear();
1388 self.search_state.selected_index = None;
1389 self.search_state.editor.update(cx, |editor, _cx| {
1390 editor.set_text_style_refinement(Default::default());
1391 });
1392
1393 if query.as_str().is_empty() {
1394 self.search_state.state = QueryState::Empty;
1395 cx.notify();
1396 return;
1397 }
1398
1399 let (request_tx, request_rx) = smol::channel::unbounded::<Oid>();
1400
1401 repo.update(cx, |repo, cx| {
1402 repo.search_commits(
1403 self.log_source.clone(),
1404 SearchCommitArgs {
1405 query: query.clone(),
1406 case_sensitive: self.search_state.case_sensitive,
1407 },
1408 request_tx,
1409 cx,
1410 );
1411 });
1412
1413 let search_task = cx.spawn(async move |this, cx| {
1414 while let Ok(first_oid) = request_rx.recv().await {
1415 let mut pending_oids = vec![first_oid];
1416 while let Ok(oid) = request_rx.try_recv() {
1417 pending_oids.push(oid);
1418 }
1419
1420 this.update(cx, |this, cx| {
1421 if this.search_state.selected_index.is_none() {
1422 this.search_state.selected_index = Some(0);
1423 this.select_commit_by_sha(first_oid, cx);
1424 }
1425
1426 this.search_state.matches.extend(pending_oids);
1427 cx.notify();
1428 })
1429 .ok();
1430 }
1431
1432 this.update(cx, |this, cx| {
1433 if this.search_state.matches.is_empty() {
1434 this.search_state.editor.update(cx, |editor, cx| {
1435 editor.set_text_style_refinement(TextStyleRefinement {
1436 color: Some(Color::Error.color(cx)),
1437 ..Default::default()
1438 });
1439 });
1440 }
1441 })
1442 .ok();
1443 });
1444
1445 self.search_state.state = QueryState::Confirmed((query, search_task));
1446 }
1447
1448 fn confirm_search(&mut self, _: &menu::Confirm, _window: &mut Window, cx: &mut Context<Self>) {
1449 let query = self.search_state.editor.read(cx).text(cx).into();
1450 self.search(query, cx);
1451 }
1452
1453 fn select_entry(
1454 &mut self,
1455 idx: usize,
1456 scroll_strategy: ScrollStrategy,
1457 cx: &mut Context<Self>,
1458 ) {
1459 if self.selected_entry_idx == Some(idx) {
1460 return;
1461 }
1462
1463 self.selected_entry_idx = Some(idx);
1464 self.selected_commit_diff = None;
1465 self.selected_commit_diff_stats = None;
1466 self.changed_files_scroll_handle
1467 .scroll_to_item(0, ScrollStrategy::Top);
1468 self.table_interaction_state.update(cx, |state, cx| {
1469 state.scroll_handle.scroll_to_item(idx, scroll_strategy);
1470 cx.notify();
1471 });
1472
1473 let Some(commit) = self.graph_data.commits.get(idx) else {
1474 return;
1475 };
1476
1477 let sha = commit.data.sha.to_string();
1478
1479 let Some(repository) = self.get_repository(cx) else {
1480 return;
1481 };
1482
1483 let diff_receiver = repository.update(cx, |repo, _| repo.load_commit_diff(sha));
1484
1485 self._commit_diff_task = Some(cx.spawn(async move |this, cx| {
1486 if let Ok(Ok(diff)) = diff_receiver.await {
1487 this.update(cx, |this, cx| {
1488 let stats = compute_diff_stats(&diff);
1489 this.selected_commit_diff = Some(diff);
1490 this.selected_commit_diff_stats = Some(stats);
1491 cx.notify();
1492 })
1493 .ok();
1494 }
1495 }));
1496
1497 cx.notify();
1498 }
1499
1500 fn select_previous_match(&mut self, cx: &mut Context<Self>) {
1501 if self.search_state.matches.is_empty() {
1502 return;
1503 }
1504
1505 let mut prev_selection = self.search_state.selected_index.unwrap_or_default();
1506
1507 if prev_selection == 0 {
1508 prev_selection = self.search_state.matches.len() - 1;
1509 } else {
1510 prev_selection -= 1;
1511 }
1512
1513 let Some(&oid) = self.search_state.matches.get_index(prev_selection) else {
1514 return;
1515 };
1516
1517 self.search_state.selected_index = Some(prev_selection);
1518 self.select_commit_by_sha(oid, cx);
1519 }
1520
1521 fn select_next_match(&mut self, cx: &mut Context<Self>) {
1522 if self.search_state.matches.is_empty() {
1523 return;
1524 }
1525
1526 let mut next_selection = self
1527 .search_state
1528 .selected_index
1529 .map(|index| index + 1)
1530 .unwrap_or_default();
1531
1532 if next_selection >= self.search_state.matches.len() {
1533 next_selection = 0;
1534 }
1535
1536 let Some(&oid) = self.search_state.matches.get_index(next_selection) else {
1537 return;
1538 };
1539
1540 self.search_state.selected_index = Some(next_selection);
1541 self.select_commit_by_sha(oid, cx);
1542 }
1543
1544 pub fn set_repo_id(&mut self, repo_id: RepositoryId, cx: &mut Context<Self>) {
1545 if repo_id != self.repo_id
1546 && self
1547 .git_store
1548 .read(cx)
1549 .repositories()
1550 .contains_key(&repo_id)
1551 {
1552 self.repo_id = repo_id;
1553 self.invalidate_state(cx);
1554 }
1555 }
1556
1557 pub fn select_commit_by_sha(&mut self, sha: impl TryInto<Oid>, cx: &mut Context<Self>) {
1558 fn inner(this: &mut GitGraph, oid: Oid, cx: &mut Context<GitGraph>) {
1559 let Some(selected_repository) = this.get_repository(cx) else {
1560 return;
1561 };
1562
1563 let Some(index) = selected_repository
1564 .read(cx)
1565 .get_graph_data(this.log_source.clone(), this.log_order)
1566 .and_then(|data| data.commit_oid_to_index.get(&oid))
1567 .copied()
1568 else {
1569 return;
1570 };
1571
1572 this.select_entry(index, ScrollStrategy::Center, cx);
1573 }
1574
1575 if let Ok(oid) = sha.try_into() {
1576 inner(self, oid, cx);
1577 }
1578 }
1579
1580 fn open_selected_commit_view(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1581 let Some(selected_entry_index) = self.selected_entry_idx else {
1582 return;
1583 };
1584
1585 self.open_commit_view(selected_entry_index, window, cx);
1586 }
1587
1588 fn open_commit_view(
1589 &mut self,
1590 entry_index: usize,
1591 window: &mut Window,
1592 cx: &mut Context<Self>,
1593 ) {
1594 let Some(commit_entry) = self.graph_data.commits.get(entry_index) else {
1595 return;
1596 };
1597
1598 let Some(repository) = self.get_repository(cx) else {
1599 return;
1600 };
1601
1602 CommitView::open(
1603 commit_entry.data.sha.to_string(),
1604 repository.downgrade(),
1605 self.workspace.clone(),
1606 None,
1607 None,
1608 window,
1609 cx,
1610 );
1611 }
1612
1613 fn get_remote(
1614 &self,
1615 repository: &Repository,
1616 _window: &mut Window,
1617 cx: &mut App,
1618 ) -> Option<GitRemote> {
1619 let remote_url = repository.default_remote_url()?;
1620 let provider_registry = GitHostingProviderRegistry::default_global(cx);
1621 let (provider, parsed) = parse_git_remote_url(provider_registry, &remote_url)?;
1622 Some(GitRemote {
1623 host: provider,
1624 owner: parsed.owner.into(),
1625 repo: parsed.repo.into(),
1626 })
1627 }
1628
1629 fn render_search_bar(&self, cx: &mut Context<Self>) -> impl IntoElement {
1630 let color = cx.theme().colors();
1631 let query_focus_handle = self.search_state.editor.focus_handle(cx);
1632 let search_options = {
1633 let mut options = SearchOptions::NONE;
1634 options.set(
1635 SearchOptions::CASE_SENSITIVE,
1636 self.search_state.case_sensitive,
1637 );
1638 options
1639 };
1640
1641 h_flex()
1642 .w_full()
1643 .p_1p5()
1644 .gap_1p5()
1645 .border_b_1()
1646 .border_color(color.border_variant)
1647 .child(
1648 h_flex()
1649 .h_8()
1650 .flex_1()
1651 .min_w_0()
1652 .px_1p5()
1653 .gap_1()
1654 .border_1()
1655 .border_color(color.border)
1656 .rounded_md()
1657 .bg(color.toolbar_background)
1658 .on_action(cx.listener(Self::confirm_search))
1659 .child(self.search_state.editor.clone())
1660 .child(SearchOption::CaseSensitive.as_button(
1661 search_options,
1662 SearchSource::Buffer,
1663 query_focus_handle,
1664 )),
1665 )
1666 .child(
1667 h_flex()
1668 .min_w_64()
1669 .gap_1()
1670 .child({
1671 let focus_handle = self.focus_handle.clone();
1672 IconButton::new("git-graph-search-prev", IconName::ChevronLeft)
1673 .shape(ui::IconButtonShape::Square)
1674 .icon_size(IconSize::Small)
1675 .tooltip(move |_, cx| {
1676 Tooltip::for_action_in(
1677 "Select Previous Match",
1678 &SelectPreviousMatch,
1679 &focus_handle,
1680 cx,
1681 )
1682 })
1683 .map(|this| {
1684 if self.search_state.matches.is_empty() {
1685 this.disabled(true)
1686 } else {
1687 this.disabled(false).on_click(cx.listener(|this, _, _, cx| {
1688 this.select_previous_match(cx);
1689 }))
1690 }
1691 })
1692 })
1693 .child({
1694 let focus_handle = self.focus_handle.clone();
1695 IconButton::new("git-graph-search-next", IconName::ChevronRight)
1696 .shape(ui::IconButtonShape::Square)
1697 .icon_size(IconSize::Small)
1698 .tooltip(move |_, cx| {
1699 Tooltip::for_action_in(
1700 "Select Next Match",
1701 &SelectNextMatch,
1702 &focus_handle,
1703 cx,
1704 )
1705 })
1706 .map(|this| {
1707 if self.search_state.matches.is_empty() {
1708 this.disabled(true)
1709 } else {
1710 this.disabled(false).on_click(cx.listener(|this, _, _, cx| {
1711 this.select_next_match(cx);
1712 }))
1713 }
1714 })
1715 })
1716 .child(
1717 h_flex()
1718 .gap_1p5()
1719 .child(
1720 Label::new(format!(
1721 "{}/{}",
1722 self.search_state
1723 .selected_index
1724 .map(|index| index + 1)
1725 .unwrap_or(0),
1726 self.search_state.matches.len()
1727 ))
1728 .size(LabelSize::Small)
1729 .when(self.search_state.matches.is_empty(), |this| {
1730 this.color(Color::Disabled)
1731 }),
1732 )
1733 .when(
1734 matches!(
1735 &self.search_state.state,
1736 QueryState::Confirmed((_, task)) if !task.is_ready()
1737 ),
1738 |this| {
1739 this.child(
1740 Icon::new(IconName::ArrowCircle)
1741 .color(Color::Accent)
1742 .size(IconSize::Small)
1743 .with_rotate_animation(2)
1744 .into_any_element(),
1745 )
1746 },
1747 ),
1748 ),
1749 )
1750 }
1751
1752 fn render_loading_spinner(&self, cx: &App) -> AnyElement {
1753 let rems = TextSize::Large.rems(cx);
1754 Icon::new(IconName::LoadCircle)
1755 .size(IconSize::Custom(rems))
1756 .color(Color::Accent)
1757 .with_rotate_animation(3)
1758 .into_any_element()
1759 }
1760
1761 fn render_commit_detail_panel(
1762 &self,
1763 window: &mut Window,
1764 cx: &mut Context<Self>,
1765 ) -> impl IntoElement {
1766 let Some(selected_idx) = self.selected_entry_idx else {
1767 return Empty.into_any_element();
1768 };
1769
1770 let Some(commit_entry) = self.graph_data.commits.get(selected_idx) else {
1771 return Empty.into_any_element();
1772 };
1773
1774 let Some(repository) = self.get_repository(cx) else {
1775 return Empty.into_any_element();
1776 };
1777
1778 let data = repository.update(cx, |repository, cx| {
1779 repository
1780 .fetch_commit_data(commit_entry.data.sha, cx)
1781 .clone()
1782 });
1783
1784 let full_sha: SharedString = commit_entry.data.sha.to_string().into();
1785 let ref_names = commit_entry.data.ref_names.clone();
1786
1787 let accent_colors = cx.theme().accents();
1788 let accent_color = accent_colors
1789 .0
1790 .get(commit_entry.color_idx)
1791 .copied()
1792 .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
1793
1794 // todo(git graph): We should use the full commit message here
1795 let (author_name, author_email, commit_timestamp, commit_message) = match &data {
1796 CommitDataState::Loaded(data) => (
1797 data.author_name.clone(),
1798 data.author_email.clone(),
1799 Some(data.commit_timestamp),
1800 data.subject.clone(),
1801 ),
1802 CommitDataState::Loading => ("Loading…".into(), "".into(), None, "Loading…".into()),
1803 };
1804
1805 let date_string = commit_timestamp
1806 .and_then(|ts| OffsetDateTime::from_unix_timestamp(ts).ok())
1807 .map(|datetime| {
1808 let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
1809 let local_datetime = datetime.to_offset(local_offset);
1810 let format =
1811 time::format_description::parse("[month repr:short] [day], [year]").ok();
1812 format
1813 .and_then(|f| local_datetime.format(&f).ok())
1814 .unwrap_or_default()
1815 })
1816 .unwrap_or_default();
1817
1818 let remote = repository.update(cx, |repo, cx| self.get_remote(repo, window, cx));
1819
1820 let avatar = {
1821 let author_email_for_avatar = if author_email.is_empty() {
1822 None
1823 } else {
1824 Some(author_email.clone())
1825 };
1826
1827 CommitAvatar::new(&full_sha, author_email_for_avatar, remote.as_ref())
1828 .size(px(40.))
1829 .render(window, cx)
1830 };
1831
1832 let changed_files_count = self
1833 .selected_commit_diff
1834 .as_ref()
1835 .map(|diff| diff.files.len())
1836 .unwrap_or(0);
1837
1838 let (total_lines_added, total_lines_removed) =
1839 self.selected_commit_diff_stats.unwrap_or((0, 0));
1840
1841 let sorted_file_entries: Rc<Vec<ChangedFileEntry>> = Rc::new(
1842 self.selected_commit_diff
1843 .as_ref()
1844 .map(|diff| {
1845 let mut files: Vec<_> = diff.files.iter().collect();
1846 files.sort_by_key(|file| file.status());
1847 files
1848 .into_iter()
1849 .map(|file| ChangedFileEntry::from_commit_file(file, cx))
1850 .collect()
1851 })
1852 .unwrap_or_default(),
1853 );
1854
1855 v_flex()
1856 .min_w(px(300.))
1857 .h_full()
1858 .bg(cx.theme().colors().surface_background)
1859 .flex_basis(DefiniteLength::Fraction(
1860 self.commit_details_split_state.read(cx).right_ratio(),
1861 ))
1862 .child(
1863 v_flex()
1864 .relative()
1865 .w_full()
1866 .p_2()
1867 .gap_2()
1868 .child(
1869 div().absolute().top_2().right_2().child(
1870 IconButton::new("close-detail", IconName::Close)
1871 .icon_size(IconSize::Small)
1872 .on_click(cx.listener(move |this, _, _, cx| {
1873 this.selected_entry_idx = None;
1874 this.selected_commit_diff = None;
1875 this.selected_commit_diff_stats = None;
1876 this._commit_diff_task = None;
1877 cx.notify();
1878 })),
1879 ),
1880 )
1881 .child(
1882 v_flex()
1883 .py_1()
1884 .w_full()
1885 .items_center()
1886 .gap_1()
1887 .child(avatar)
1888 .child(
1889 v_flex()
1890 .items_center()
1891 .child(Label::new(author_name))
1892 .child(
1893 Label::new(date_string)
1894 .color(Color::Muted)
1895 .size(LabelSize::Small),
1896 ),
1897 ),
1898 )
1899 .children((!ref_names.is_empty()).then(|| {
1900 h_flex().gap_1().flex_wrap().justify_center().children(
1901 ref_names
1902 .iter()
1903 .map(|name| self.render_chip(name, accent_color)),
1904 )
1905 }))
1906 .child(
1907 v_flex()
1908 .ml_neg_1()
1909 .gap_1p5()
1910 .when(!author_email.is_empty(), |this| {
1911 let copied_state: Entity<CopiedState> = window.use_keyed_state(
1912 "author-email-copy",
1913 cx,
1914 CopiedState::new,
1915 );
1916 let is_copied = copied_state.read(cx).is_copied();
1917
1918 let (icon, icon_color, tooltip_label) = if is_copied {
1919 (IconName::Check, Color::Success, "Email Copied!")
1920 } else {
1921 (IconName::Envelope, Color::Muted, "Copy Email")
1922 };
1923
1924 let copy_email = author_email.clone();
1925 let author_email_for_tooltip = author_email.clone();
1926
1927 this.child(
1928 Button::new("author-email-copy", author_email.clone())
1929 .start_icon(
1930 Icon::new(icon).size(IconSize::Small).color(icon_color),
1931 )
1932 .label_size(LabelSize::Small)
1933 .truncate(true)
1934 .color(Color::Muted)
1935 .tooltip(move |_, cx| {
1936 Tooltip::with_meta(
1937 tooltip_label,
1938 None,
1939 author_email_for_tooltip.clone(),
1940 cx,
1941 )
1942 })
1943 .on_click(move |_, _, cx| {
1944 copied_state.update(cx, |state, _cx| {
1945 state.mark_copied();
1946 });
1947 cx.write_to_clipboard(ClipboardItem::new_string(
1948 copy_email.to_string(),
1949 ));
1950 let state_id = copied_state.entity_id();
1951 cx.spawn(async move |cx| {
1952 cx.background_executor()
1953 .timer(COPIED_STATE_DURATION)
1954 .await;
1955 cx.update(|cx| {
1956 cx.notify(state_id);
1957 })
1958 })
1959 .detach();
1960 }),
1961 )
1962 })
1963 .child({
1964 let copy_sha = full_sha.clone();
1965 let copied_state: Entity<CopiedState> =
1966 window.use_keyed_state("sha-copy", cx, CopiedState::new);
1967 let is_copied = copied_state.read(cx).is_copied();
1968
1969 let (icon, icon_color, tooltip_label) = if is_copied {
1970 (IconName::Check, Color::Success, "Commit SHA Copied!")
1971 } else {
1972 (IconName::Hash, Color::Muted, "Copy Commit SHA")
1973 };
1974
1975 Button::new("sha-button", &full_sha)
1976 .start_icon(
1977 Icon::new(icon).size(IconSize::Small).color(icon_color),
1978 )
1979 .label_size(LabelSize::Small)
1980 .truncate(true)
1981 .color(Color::Muted)
1982 .tooltip({
1983 let full_sha = full_sha.clone();
1984 move |_, cx| {
1985 Tooltip::with_meta(
1986 tooltip_label,
1987 None,
1988 full_sha.clone(),
1989 cx,
1990 )
1991 }
1992 })
1993 .on_click(move |_, _, cx| {
1994 copied_state.update(cx, |state, _cx| {
1995 state.mark_copied();
1996 });
1997 cx.write_to_clipboard(ClipboardItem::new_string(
1998 copy_sha.to_string(),
1999 ));
2000 let state_id = copied_state.entity_id();
2001 cx.spawn(async move |cx| {
2002 cx.background_executor()
2003 .timer(COPIED_STATE_DURATION)
2004 .await;
2005 cx.update(|cx| {
2006 cx.notify(state_id);
2007 })
2008 })
2009 .detach();
2010 })
2011 })
2012 .when_some(remote.clone(), |this, remote| {
2013 let provider_name = remote.host.name();
2014 let icon = match provider_name.as_str() {
2015 "GitHub" => IconName::Github,
2016 _ => IconName::Link,
2017 };
2018 let parsed_remote = ParsedGitRemote {
2019 owner: remote.owner.as_ref().into(),
2020 repo: remote.repo.as_ref().into(),
2021 };
2022 let params = BuildCommitPermalinkParams {
2023 sha: full_sha.as_ref(),
2024 };
2025 let url = remote
2026 .host
2027 .build_commit_permalink(&parsed_remote, params)
2028 .to_string();
2029
2030 this.child(
2031 Button::new(
2032 "view-on-provider",
2033 format!("View on {}", provider_name),
2034 )
2035 .start_icon(
2036 Icon::new(icon).size(IconSize::Small).color(Color::Muted),
2037 )
2038 .label_size(LabelSize::Small)
2039 .truncate(true)
2040 .color(Color::Muted)
2041 .on_click(
2042 move |_, _, cx| {
2043 cx.open_url(&url);
2044 },
2045 ),
2046 )
2047 }),
2048 ),
2049 )
2050 .child(Divider::horizontal())
2051 .child(div().p_2().child(Label::new(commit_message)))
2052 .child(Divider::horizontal())
2053 .child(
2054 v_flex()
2055 .min_w_0()
2056 .p_2()
2057 .flex_1()
2058 .gap_1()
2059 .child(
2060 h_flex()
2061 .gap_1()
2062 .child(
2063 Label::new(format!("{} Changed Files", changed_files_count))
2064 .size(LabelSize::Small)
2065 .color(Color::Muted),
2066 )
2067 .child(DiffStat::new(
2068 "commit-diff-stat",
2069 total_lines_added,
2070 total_lines_removed,
2071 )),
2072 )
2073 .child(
2074 div()
2075 .id("changed-files-container")
2076 .flex_1()
2077 .min_h_0()
2078 .child({
2079 let entries = sorted_file_entries;
2080 let entry_count = entries.len();
2081 let commit_sha = full_sha.clone();
2082 let repository = repository.downgrade();
2083 let workspace = self.workspace.clone();
2084 uniform_list(
2085 "changed-files-list",
2086 entry_count,
2087 move |range, _window, cx| {
2088 range
2089 .map(|ix| {
2090 entries[ix].render(
2091 ix,
2092 commit_sha.clone(),
2093 repository.clone(),
2094 workspace.clone(),
2095 cx,
2096 )
2097 })
2098 .collect()
2099 },
2100 )
2101 .size_full()
2102 .ml_neg_1()
2103 .track_scroll(&self.changed_files_scroll_handle)
2104 })
2105 .vertical_scrollbar_for(&self.changed_files_scroll_handle, window, cx),
2106 ),
2107 )
2108 .child(Divider::horizontal())
2109 .child(
2110 h_flex().p_1p5().w_full().child(
2111 Button::new("view-commit", "View Commit")
2112 .full_width()
2113 .style(ButtonStyle::Outlined)
2114 .on_click(cx.listener(|this, _, window, cx| {
2115 this.open_selected_commit_view(window, cx);
2116 })),
2117 ),
2118 )
2119 .into_any_element()
2120 }
2121
2122 pub fn render_graph(&self, window: &Window, cx: &mut Context<GitGraph>) -> impl IntoElement {
2123 let row_height = self.row_height;
2124 let table_state = self.table_interaction_state.read(cx);
2125 let viewport_height = table_state
2126 .scroll_handle
2127 .0
2128 .borrow()
2129 .last_item_size
2130 .map(|size| size.item.height)
2131 .unwrap_or(px(600.0));
2132 let loaded_commit_count = self.graph_data.commits.len();
2133
2134 let content_height = row_height * loaded_commit_count;
2135 let max_scroll = (content_height - viewport_height).max(px(0.));
2136 let scroll_offset_y = (-table_state.scroll_offset().y).clamp(px(0.), max_scroll);
2137
2138 let first_visible_row = (scroll_offset_y / row_height).floor() as usize;
2139 let vertical_scroll_offset = scroll_offset_y - (first_visible_row as f32 * row_height);
2140
2141 let graph_viewport_width = self.graph_viewport_width(window, cx);
2142 let graph_width = if self.graph_canvas_content_width() > graph_viewport_width {
2143 self.graph_canvas_content_width()
2144 } else {
2145 graph_viewport_width
2146 };
2147 let last_visible_row =
2148 first_visible_row + (viewport_height / row_height).ceil() as usize + 1;
2149
2150 let viewport_range = first_visible_row.min(loaded_commit_count.saturating_sub(1))
2151 ..(last_visible_row).min(loaded_commit_count);
2152 let rows = self.graph_data.commits[viewport_range.clone()].to_vec();
2153 let commit_lines: Vec<_> = self
2154 .graph_data
2155 .lines
2156 .iter()
2157 .filter(|line| {
2158 line.full_interval.start <= viewport_range.end
2159 && line.full_interval.end >= viewport_range.start
2160 })
2161 .cloned()
2162 .collect();
2163
2164 let mut lines: BTreeMap<usize, Vec<_>> = BTreeMap::new();
2165
2166 let hovered_entry_idx = self.hovered_entry_idx;
2167 let selected_entry_idx = self.selected_entry_idx;
2168 let is_focused = self.focus_handle.is_focused(window);
2169 let graph_canvas_bounds = self.graph_canvas_bounds.clone();
2170
2171 gpui::canvas(
2172 move |_bounds, _window, _cx| {},
2173 move |bounds: Bounds<Pixels>, _: (), window: &mut Window, cx: &mut App| {
2174 graph_canvas_bounds.set(Some(bounds));
2175
2176 window.paint_layer(bounds, |window| {
2177 let accent_colors = cx.theme().accents();
2178
2179 let hover_bg = cx.theme().colors().element_hover.opacity(0.6);
2180 let selected_bg = if is_focused {
2181 cx.theme().colors().element_selected
2182 } else {
2183 cx.theme().colors().element_hover
2184 };
2185
2186 for visible_row_idx in 0..rows.len() {
2187 let absolute_row_idx = first_visible_row + visible_row_idx;
2188 let is_hovered = hovered_entry_idx == Some(absolute_row_idx);
2189 let is_selected = selected_entry_idx == Some(absolute_row_idx);
2190
2191 if is_hovered || is_selected {
2192 let row_y = bounds.origin.y + visible_row_idx as f32 * row_height
2193 - vertical_scroll_offset;
2194
2195 let row_bounds = Bounds::new(
2196 point(bounds.origin.x, row_y),
2197 gpui::Size {
2198 width: bounds.size.width,
2199 height: row_height,
2200 },
2201 );
2202
2203 let bg_color = if is_selected { selected_bg } else { hover_bg };
2204 window.paint_quad(gpui::fill(row_bounds, bg_color));
2205 }
2206 }
2207
2208 for (row_idx, row) in rows.into_iter().enumerate() {
2209 let row_color = accent_colors.color_for_index(row.color_idx as u32);
2210 let row_y_center =
2211 bounds.origin.y + row_idx as f32 * row_height + row_height / 2.0
2212 - vertical_scroll_offset;
2213
2214 let commit_x = lane_center_x(bounds, row.lane as f32);
2215
2216 draw_commit_circle(commit_x, row_y_center, row_color, window);
2217 }
2218
2219 for line in commit_lines {
2220 let Some((start_segment_idx, start_column)) =
2221 line.get_first_visible_segment_idx(first_visible_row)
2222 else {
2223 continue;
2224 };
2225
2226 let line_x = lane_center_x(bounds, start_column as f32);
2227
2228 let start_row = line.full_interval.start as i32 - first_visible_row as i32;
2229
2230 let from_y =
2231 bounds.origin.y + start_row as f32 * row_height + row_height / 2.0
2232 - vertical_scroll_offset
2233 + COMMIT_CIRCLE_RADIUS;
2234
2235 let mut current_row = from_y;
2236 let mut current_column = line_x;
2237
2238 let mut builder = PathBuilder::stroke(LINE_WIDTH);
2239 builder.move_to(point(line_x, from_y));
2240
2241 let segments = &line.segments[start_segment_idx..];
2242 let desired_curve_height = row_height / 3.0;
2243 let desired_curve_width = LANE_WIDTH / 3.0;
2244
2245 for (segment_idx, segment) in segments.iter().enumerate() {
2246 let is_last = segment_idx + 1 == segments.len();
2247
2248 match segment {
2249 CommitLineSegment::Straight { to_row } => {
2250 let mut dest_row = to_row_center(
2251 to_row - first_visible_row,
2252 row_height,
2253 vertical_scroll_offset,
2254 bounds,
2255 );
2256 if is_last {
2257 dest_row -= COMMIT_CIRCLE_RADIUS;
2258 }
2259
2260 let dest_point = point(current_column, dest_row);
2261
2262 current_row = dest_point.y;
2263 builder.line_to(dest_point);
2264 builder.move_to(dest_point);
2265 }
2266 CommitLineSegment::Curve {
2267 to_column,
2268 on_row,
2269 curve_kind,
2270 } => {
2271 let mut to_column = lane_center_x(bounds, *to_column as f32);
2272
2273 let mut to_row = to_row_center(
2274 *on_row - first_visible_row,
2275 row_height,
2276 vertical_scroll_offset,
2277 bounds,
2278 );
2279
2280 // This means that this branch was a checkout
2281 let going_right = to_column > current_column;
2282 let column_shift = if going_right {
2283 COMMIT_CIRCLE_RADIUS + COMMIT_CIRCLE_STROKE_WIDTH
2284 } else {
2285 -COMMIT_CIRCLE_RADIUS - COMMIT_CIRCLE_STROKE_WIDTH
2286 };
2287
2288 match curve_kind {
2289 CurveKind::Checkout => {
2290 if is_last {
2291 to_column -= column_shift;
2292 }
2293
2294 let available_curve_width =
2295 (to_column - current_column).abs();
2296 let available_curve_height =
2297 (to_row - current_row).abs();
2298 let curve_width =
2299 desired_curve_width.min(available_curve_width);
2300 let curve_height =
2301 desired_curve_height.min(available_curve_height);
2302 let signed_curve_width = if going_right {
2303 curve_width
2304 } else {
2305 -curve_width
2306 };
2307 let curve_start =
2308 point(current_column, to_row - curve_height);
2309 let curve_end =
2310 point(current_column + signed_curve_width, to_row);
2311 let curve_control = point(current_column, to_row);
2312
2313 builder.move_to(point(current_column, current_row));
2314 builder.line_to(curve_start);
2315 builder.move_to(curve_start);
2316 builder.curve_to(curve_end, curve_control);
2317 builder.move_to(curve_end);
2318 builder.line_to(point(to_column, to_row));
2319 }
2320 CurveKind::Merge => {
2321 if is_last {
2322 to_row -= COMMIT_CIRCLE_RADIUS;
2323 }
2324
2325 let merge_start = point(
2326 current_column + column_shift,
2327 current_row - COMMIT_CIRCLE_RADIUS,
2328 );
2329 let available_curve_width =
2330 (to_column - merge_start.x).abs();
2331 let available_curve_height =
2332 (to_row - merge_start.y).abs();
2333 let curve_width =
2334 desired_curve_width.min(available_curve_width);
2335 let curve_height =
2336 desired_curve_height.min(available_curve_height);
2337 let signed_curve_width = if going_right {
2338 curve_width
2339 } else {
2340 -curve_width
2341 };
2342 let curve_start = point(
2343 to_column - signed_curve_width,
2344 merge_start.y,
2345 );
2346 let curve_end =
2347 point(to_column, merge_start.y + curve_height);
2348 let curve_control = point(to_column, merge_start.y);
2349
2350 builder.move_to(merge_start);
2351 builder.line_to(curve_start);
2352 builder.move_to(curve_start);
2353 builder.curve_to(curve_end, curve_control);
2354 builder.move_to(curve_end);
2355 builder.line_to(point(to_column, to_row));
2356 }
2357 }
2358 current_row = to_row;
2359 current_column = to_column;
2360 builder.move_to(point(current_column, current_row));
2361 }
2362 }
2363 }
2364
2365 builder.close();
2366 lines.entry(line.color_idx).or_default().push(builder);
2367 }
2368
2369 for (color_idx, builders) in lines {
2370 let line_color = accent_colors.color_for_index(color_idx as u32);
2371
2372 for builder in builders {
2373 if let Ok(path) = builder.build() {
2374 // we paint each color on it's own layer to stop overlapping lines
2375 // of different colors changing the color of a line
2376 window.paint_layer(bounds, |window| {
2377 window.paint_path(path, line_color);
2378 });
2379 }
2380 }
2381 }
2382 })
2383 },
2384 )
2385 .w(graph_width)
2386 .h_full()
2387 }
2388
2389 fn row_at_position(&self, position_y: Pixels, cx: &Context<Self>) -> Option<usize> {
2390 let canvas_bounds = self.graph_canvas_bounds.get()?;
2391 let table_state = self.table_interaction_state.read(cx);
2392 let scroll_offset_y = -table_state.scroll_offset().y;
2393
2394 let local_y = position_y - canvas_bounds.origin.y;
2395
2396 if local_y >= px(0.) && local_y < canvas_bounds.size.height {
2397 let absolute_y = local_y + scroll_offset_y;
2398 let absolute_row = (absolute_y / self.row_height).floor() as usize;
2399
2400 if absolute_row < self.graph_data.commits.len() {
2401 return Some(absolute_row);
2402 }
2403 }
2404
2405 None
2406 }
2407
2408 fn handle_graph_mouse_move(
2409 &mut self,
2410 event: &gpui::MouseMoveEvent,
2411 _window: &mut Window,
2412 cx: &mut Context<Self>,
2413 ) {
2414 if let Some(row) = self.row_at_position(event.position.y, cx) {
2415 if self.hovered_entry_idx != Some(row) {
2416 self.hovered_entry_idx = Some(row);
2417 cx.notify();
2418 }
2419 } else if self.hovered_entry_idx.is_some() {
2420 self.hovered_entry_idx = None;
2421 cx.notify();
2422 }
2423 }
2424
2425 fn handle_graph_click(
2426 &mut self,
2427 event: &ClickEvent,
2428 window: &mut Window,
2429 cx: &mut Context<Self>,
2430 ) {
2431 if let Some(row) = self.row_at_position(event.position().y, cx) {
2432 self.select_entry(row, ScrollStrategy::Nearest, cx);
2433 if event.click_count() >= 2 {
2434 self.open_commit_view(row, window, cx);
2435 }
2436 }
2437 }
2438
2439 fn handle_graph_scroll(
2440 &mut self,
2441 event: &ScrollWheelEvent,
2442 window: &mut Window,
2443 cx: &mut Context<Self>,
2444 ) {
2445 let line_height = window.line_height();
2446 let delta = event.delta.pixel_delta(line_height);
2447
2448 let table_state = self.table_interaction_state.read(cx);
2449 let current_offset = table_state.scroll_offset();
2450
2451 let viewport_height = table_state.scroll_handle.viewport().size.height;
2452
2453 let commit_count = match self.graph_data.max_commit_count {
2454 AllCommitCount::Loaded(count) => count,
2455 AllCommitCount::NotLoaded => self.graph_data.commits.len(),
2456 };
2457 let content_height = self.row_height * commit_count;
2458 let max_vertical_scroll = (viewport_height - content_height).min(px(0.));
2459
2460 let new_y = (current_offset.y + delta.y).clamp(max_vertical_scroll, px(0.));
2461 let new_offset = Point::new(current_offset.x, new_y);
2462
2463 if new_offset != current_offset {
2464 table_state.set_scroll_offset(new_offset);
2465 cx.notify();
2466 }
2467 }
2468
2469 fn render_commit_view_resize_handle(
2470 &self,
2471 _window: &mut Window,
2472 cx: &mut Context<Self>,
2473 ) -> AnyElement {
2474 div()
2475 .id("commit-view-split-resize-container")
2476 .relative()
2477 .h_full()
2478 .flex_shrink_0()
2479 .w(px(1.))
2480 .bg(cx.theme().colors().border_variant)
2481 .child(
2482 div()
2483 .id("commit-view-split-resize-handle")
2484 .absolute()
2485 .left(px(-RESIZE_HANDLE_WIDTH / 2.0))
2486 .w(px(RESIZE_HANDLE_WIDTH))
2487 .h_full()
2488 .cursor_col_resize()
2489 .block_mouse_except_scroll()
2490 .on_click(cx.listener(|this, event: &ClickEvent, _window, cx| {
2491 if event.click_count() >= 2 {
2492 this.commit_details_split_state.update(cx, |state, _| {
2493 state.on_double_click();
2494 });
2495 }
2496 cx.stop_propagation();
2497 }))
2498 .on_drag(DraggedSplitHandle, |_, _, _, cx| cx.new(|_| gpui::Empty)),
2499 )
2500 .into_any_element()
2501 }
2502}
2503
2504impl Render for GitGraph {
2505 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2506 // This happens when we changed branches, we should refresh our search as well
2507 if let QueryState::Pending(query) = &mut self.search_state.state {
2508 let query = std::mem::take(query);
2509 self.search_state.state = QueryState::Empty;
2510 self.search(query, cx);
2511 }
2512 let (commit_count, is_loading) = match self.graph_data.max_commit_count {
2513 AllCommitCount::Loaded(count) => (count, true),
2514 AllCommitCount::NotLoaded => {
2515 let (commit_count, is_loading) = if let Some(repository) = self.get_repository(cx) {
2516 repository.update(cx, |repository, cx| {
2517 // Start loading the graph data if we haven't started already
2518 let GraphDataResponse {
2519 commits,
2520 is_loading,
2521 error: _,
2522 } = repository.graph_data(
2523 self.log_source.clone(),
2524 self.log_order,
2525 0..usize::MAX,
2526 cx,
2527 );
2528 self.graph_data.add_commits(&commits);
2529 (commits.len(), is_loading)
2530 })
2531 } else {
2532 (0, false)
2533 };
2534
2535 (commit_count, is_loading)
2536 }
2537 };
2538
2539 let error = self.get_repository(cx).and_then(|repo| {
2540 repo.read(cx)
2541 .get_graph_data(self.log_source.clone(), self.log_order)
2542 .and_then(|data| data.error.clone())
2543 });
2544
2545 let content = if commit_count == 0 {
2546 let message = if let Some(error) = &error {
2547 format!("Error loading: {}", error)
2548 } else if is_loading {
2549 "Loading".to_string()
2550 } else {
2551 "No commits found".to_string()
2552 };
2553 let label = Label::new(message)
2554 .color(Color::Muted)
2555 .size(LabelSize::Large);
2556 div()
2557 .size_full()
2558 .h_flex()
2559 .gap_1()
2560 .items_center()
2561 .justify_center()
2562 .child(label)
2563 .when(is_loading && error.is_none(), |this| {
2564 this.child(self.render_loading_spinner(cx))
2565 })
2566 } else {
2567 let header_resize_info =
2568 HeaderResizeInfo::from_redistributable(&self.column_widths, cx);
2569 let header_context = TableRenderContext::for_column_widths(
2570 Some(self.column_widths.read(cx).widths_to_render()),
2571 true,
2572 );
2573 let [
2574 graph_fraction,
2575 description_fraction,
2576 date_fraction,
2577 author_fraction,
2578 commit_fraction,
2579 ] = self.preview_column_fractions(window, cx);
2580 let table_fraction =
2581 description_fraction + date_fraction + author_fraction + commit_fraction;
2582 let table_width_config = self.table_column_width_config(window, cx);
2583
2584 h_flex()
2585 .size_full()
2586 .child(
2587 div()
2588 .flex_1()
2589 .min_w_0()
2590 .size_full()
2591 .flex()
2592 .flex_col()
2593 .child(render_table_header(
2594 TableRow::from_vec(
2595 vec![
2596 Label::new("Graph")
2597 .color(Color::Muted)
2598 .truncate()
2599 .into_any_element(),
2600 Label::new("Description")
2601 .color(Color::Muted)
2602 .into_any_element(),
2603 Label::new("Date").color(Color::Muted).into_any_element(),
2604 Label::new("Author").color(Color::Muted).into_any_element(),
2605 Label::new("Commit").color(Color::Muted).into_any_element(),
2606 ],
2607 5,
2608 ),
2609 header_context,
2610 Some(header_resize_info),
2611 Some(self.column_widths.entity_id()),
2612 cx,
2613 ))
2614 .child({
2615 let row_height = self.row_height;
2616 let selected_entry_idx = self.selected_entry_idx;
2617 let hovered_entry_idx = self.hovered_entry_idx;
2618 let weak_self = cx.weak_entity();
2619 let focus_handle = self.focus_handle.clone();
2620
2621 bind_redistributable_columns(
2622 div()
2623 .relative()
2624 .flex_1()
2625 .w_full()
2626 .overflow_hidden()
2627 .child(
2628 h_flex()
2629 .size_full()
2630 .child(
2631 div()
2632 .w(DefiniteLength::Fraction(graph_fraction))
2633 .h_full()
2634 .min_w_0()
2635 .overflow_hidden()
2636 .child(
2637 div()
2638 .id("graph-canvas")
2639 .size_full()
2640 .overflow_hidden()
2641 .child(
2642 div()
2643 .size_full()
2644 .child(self.render_graph(window, cx)),
2645 )
2646 .on_scroll_wheel(
2647 cx.listener(Self::handle_graph_scroll),
2648 )
2649 .on_mouse_move(
2650 cx.listener(Self::handle_graph_mouse_move),
2651 )
2652 .on_click(cx.listener(Self::handle_graph_click))
2653 .on_hover(cx.listener(
2654 |this, &is_hovered: &bool, _, cx| {
2655 if !is_hovered
2656 && this.hovered_entry_idx.is_some()
2657 {
2658 this.hovered_entry_idx = None;
2659 cx.notify();
2660 }
2661 },
2662 )),
2663 ),
2664 )
2665 .child(
2666 div()
2667 .w(DefiniteLength::Fraction(table_fraction))
2668 .h_full()
2669 .min_w_0()
2670 .child(
2671 Table::new(4)
2672 .interactable(&self.table_interaction_state)
2673 .hide_row_borders()
2674 .hide_row_hover()
2675 .width_config(table_width_config)
2676 .map_row(move |(index, row), window, cx| {
2677 let is_selected =
2678 selected_entry_idx == Some(index);
2679 let is_hovered =
2680 hovered_entry_idx == Some(index);
2681 let is_focused =
2682 focus_handle.is_focused(window);
2683 let weak = weak_self.clone();
2684 let weak_for_hover = weak.clone();
2685
2686 let hover_bg = cx
2687 .theme()
2688 .colors()
2689 .element_hover
2690 .opacity(0.6);
2691 let selected_bg = if is_focused {
2692 cx.theme().colors().element_selected
2693 } else {
2694 cx.theme().colors().element_hover
2695 };
2696
2697 row.h(row_height)
2698 .when(is_selected, |row| row.bg(selected_bg))
2699 .when(
2700 is_hovered && !is_selected,
2701 |row| row.bg(hover_bg),
2702 )
2703 .on_hover(move |&is_hovered, _, cx| {
2704 weak_for_hover
2705 .update(cx, |this, cx| {
2706 if is_hovered {
2707 if this.hovered_entry_idx
2708 != Some(index)
2709 {
2710 this.hovered_entry_idx =
2711 Some(index);
2712 cx.notify();
2713 }
2714 } else if this
2715 .hovered_entry_idx
2716 == Some(index)
2717 {
2718 this.hovered_entry_idx =
2719 None;
2720 cx.notify();
2721 }
2722 })
2723 .ok();
2724 })
2725 .on_click(move |event, window, cx| {
2726 let click_count = event.click_count();
2727 weak.update(cx, |this, cx| {
2728 this.select_entry(
2729 index,
2730 ScrollStrategy::Center,
2731 cx,
2732 );
2733 if click_count >= 2 {
2734 this.open_commit_view(
2735 index,
2736 window,
2737 cx,
2738 );
2739 }
2740 })
2741 .ok();
2742 })
2743 .into_any_element()
2744 })
2745 .uniform_list(
2746 "git-graph-commits",
2747 commit_count,
2748 cx.processor(Self::render_table_rows),
2749 ),
2750 ),
2751 ),
2752 )
2753 .child(render_redistributable_columns_resize_handles(
2754 &self.column_widths,
2755 window,
2756 cx,
2757 )),
2758 self.column_widths.clone(),
2759 )
2760 }),
2761 )
2762 .on_drag_move::<DraggedSplitHandle>(cx.listener(|this, event, window, cx| {
2763 this.commit_details_split_state.update(cx, |state, cx| {
2764 state.on_drag_move(event, window, cx);
2765 });
2766 }))
2767 .on_drop::<DraggedSplitHandle>(cx.listener(|this, _event, _window, cx| {
2768 this.commit_details_split_state.update(cx, |state, _cx| {
2769 state.commit_ratio();
2770 });
2771 }))
2772 .when(self.selected_entry_idx.is_some(), |this| {
2773 this.child(self.render_commit_view_resize_handle(window, cx))
2774 .child(self.render_commit_detail_panel(window, cx))
2775 })
2776 };
2777
2778 div()
2779 .key_context("GitGraph")
2780 .track_focus(&self.focus_handle)
2781 .size_full()
2782 .bg(cx.theme().colors().editor_background)
2783 .on_action(cx.listener(|this, _: &OpenCommitView, window, cx| {
2784 this.open_selected_commit_view(window, cx);
2785 }))
2786 .on_action(cx.listener(Self::cancel))
2787 .on_action(cx.listener(|this, _: &FocusSearch, window, cx| {
2788 this.search_state
2789 .editor
2790 .update(cx, |editor, cx| editor.focus_handle(cx).focus(window, cx));
2791 }))
2792 .on_action(cx.listener(Self::select_first))
2793 .on_action(cx.listener(Self::select_prev))
2794 .on_action(cx.listener(Self::select_next))
2795 .on_action(cx.listener(Self::select_last))
2796 .on_action(cx.listener(Self::confirm))
2797 .on_action(cx.listener(|this, _: &SelectNextMatch, _window, cx| {
2798 this.select_next_match(cx);
2799 }))
2800 .on_action(cx.listener(|this, _: &SelectPreviousMatch, _window, cx| {
2801 this.select_previous_match(cx);
2802 }))
2803 .on_action(cx.listener(|this, _: &ToggleCaseSensitive, _window, cx| {
2804 this.search_state.case_sensitive = !this.search_state.case_sensitive;
2805 this.search_state.state.next_state();
2806 cx.notify();
2807 }))
2808 .child(
2809 v_flex()
2810 .size_full()
2811 .child(self.render_search_bar(cx))
2812 .child(div().flex_1().child(content)),
2813 )
2814 .children(self.context_menu.as_ref().map(|(menu, position, _)| {
2815 deferred(
2816 anchored()
2817 .position(*position)
2818 .anchor(Corner::TopLeft)
2819 .child(menu.clone()),
2820 )
2821 .with_priority(1)
2822 }))
2823 .on_action(cx.listener(|_, _: &buffer_search::Deploy, window, cx| {
2824 window.dispatch_action(Box::new(FocusSearch), cx);
2825 cx.stop_propagation();
2826 }))
2827 }
2828}
2829
2830impl EventEmitter<ItemEvent> for GitGraph {}
2831
2832impl Focusable for GitGraph {
2833 fn focus_handle(&self, _cx: &App) -> FocusHandle {
2834 self.focus_handle.clone()
2835 }
2836}
2837
2838impl Item for GitGraph {
2839 type Event = ItemEvent;
2840
2841 fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
2842 Some(Icon::new(IconName::GitGraph))
2843 }
2844
2845 fn tab_tooltip_content(&self, cx: &App) -> Option<TabTooltipContent> {
2846 let repo_name = self.get_repository(cx).and_then(|repo| {
2847 repo.read(cx)
2848 .work_directory_abs_path
2849 .file_name()
2850 .map(|name| name.to_string_lossy().to_string())
2851 });
2852
2853 Some(TabTooltipContent::Custom(Box::new(Tooltip::element({
2854 move |_, _| {
2855 v_flex()
2856 .child(Label::new("Git Graph"))
2857 .when_some(repo_name.clone(), |this, name| {
2858 this.child(Label::new(name).color(Color::Muted).size(LabelSize::Small))
2859 })
2860 .into_any_element()
2861 }
2862 }))))
2863 }
2864
2865 fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString {
2866 self.get_repository(cx)
2867 .and_then(|repo| {
2868 repo.read(cx)
2869 .work_directory_abs_path
2870 .file_name()
2871 .map(|name| name.to_string_lossy().to_string())
2872 })
2873 .map_or_else(|| "Git Graph".into(), |name| SharedString::from(name))
2874 }
2875
2876 fn show_toolbar(&self) -> bool {
2877 false
2878 }
2879
2880 fn to_item_events(event: &Self::Event, f: &mut dyn FnMut(ItemEvent)) {
2881 f(*event)
2882 }
2883}
2884
2885impl workspace::SerializableItem for GitGraph {
2886 fn serialized_item_kind() -> &'static str {
2887 "GitGraph"
2888 }
2889
2890 fn cleanup(
2891 workspace_id: workspace::WorkspaceId,
2892 alive_items: Vec<workspace::ItemId>,
2893 _window: &mut Window,
2894 cx: &mut App,
2895 ) -> Task<gpui::Result<()>> {
2896 workspace::delete_unloaded_items(
2897 alive_items,
2898 workspace_id,
2899 "git_graphs",
2900 &persistence::GitGraphsDb::global(cx),
2901 cx,
2902 )
2903 }
2904
2905 fn deserialize(
2906 project: Entity<project::Project>,
2907 workspace: WeakEntity<Workspace>,
2908 workspace_id: workspace::WorkspaceId,
2909 item_id: workspace::ItemId,
2910 window: &mut Window,
2911 cx: &mut App,
2912 ) -> Task<gpui::Result<Entity<Self>>> {
2913 let db = persistence::GitGraphsDb::global(cx);
2914 let Some(repo_work_path) = db.get_git_graph(item_id, workspace_id).ok().flatten() else {
2915 return Task::ready(Err(anyhow::anyhow!("No git graph to deserialize")));
2916 };
2917
2918 let window_handle = window.window_handle();
2919 let project = project.read(cx);
2920 let git_store = project.git_store().clone();
2921 let wait = project.wait_for_initial_scan(cx);
2922
2923 cx.spawn(async move |cx| {
2924 wait.await;
2925
2926 cx.update_window(window_handle, |_, window, cx| {
2927 let path = repo_work_path.as_path();
2928
2929 let repositories = git_store.read(cx).repositories();
2930 let repo_id = repositories.iter().find_map(|(&repo_id, repo)| {
2931 if repo.read(cx).snapshot().work_directory_abs_path.as_ref() == path {
2932 Some(repo_id)
2933 } else {
2934 None
2935 }
2936 });
2937
2938 let Some(repo_id) = repo_id else {
2939 return Err(anyhow::anyhow!("Repository not found for path: {:?}", path));
2940 };
2941
2942 Ok(cx.new(|cx| GitGraph::new(repo_id, git_store, workspace, window, cx)))
2943 })?
2944 })
2945 }
2946
2947 fn serialize(
2948 &mut self,
2949 workspace: &mut Workspace,
2950 item_id: workspace::ItemId,
2951 _closing: bool,
2952 _window: &mut Window,
2953 cx: &mut Context<Self>,
2954 ) -> Option<Task<gpui::Result<()>>> {
2955 let workspace_id = workspace.database_id()?;
2956 let repo = self.get_repository(cx)?;
2957 let repo_working_path = repo
2958 .read(cx)
2959 .snapshot()
2960 .work_directory_abs_path
2961 .to_string_lossy()
2962 .to_string();
2963
2964 let db = persistence::GitGraphsDb::global(cx);
2965 Some(cx.background_spawn(async move {
2966 db.save_git_graph(item_id, workspace_id, repo_working_path)
2967 .await
2968 }))
2969 }
2970
2971 fn should_serialize(&self, event: &Self::Event) -> bool {
2972 event == &ItemEvent::UpdateTab
2973 }
2974}
2975
2976mod persistence {
2977 use std::path::PathBuf;
2978
2979 use db::{
2980 query,
2981 sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection},
2982 sqlez_macros::sql,
2983 };
2984 use workspace::WorkspaceDb;
2985
2986 pub struct GitGraphsDb(ThreadSafeConnection);
2987
2988 impl Domain for GitGraphsDb {
2989 const NAME: &str = stringify!(GitGraphsDb);
2990
2991 const MIGRATIONS: &[&str] = &[
2992 sql!(
2993 CREATE TABLE git_graphs (
2994 workspace_id INTEGER,
2995 item_id INTEGER UNIQUE,
2996 is_open INTEGER DEFAULT FALSE,
2997
2998 PRIMARY KEY(workspace_id, item_id),
2999 FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
3000 ON DELETE CASCADE
3001 ) STRICT;
3002 ),
3003 sql!(
3004 ALTER TABLE git_graphs ADD COLUMN repo_working_path TEXT;
3005 ),
3006 ];
3007 }
3008
3009 db::static_connection!(GitGraphsDb, [WorkspaceDb]);
3010
3011 impl GitGraphsDb {
3012 query! {
3013 pub async fn save_git_graph(
3014 item_id: workspace::ItemId,
3015 workspace_id: workspace::WorkspaceId,
3016 repo_working_path: String
3017 ) -> Result<()> {
3018 INSERT OR REPLACE INTO git_graphs(item_id, workspace_id, repo_working_path)
3019 VALUES (?, ?, ?)
3020 }
3021 }
3022
3023 query! {
3024 pub fn get_git_graph(
3025 item_id: workspace::ItemId,
3026 workspace_id: workspace::WorkspaceId
3027 ) -> Result<Option<PathBuf>> {
3028 SELECT repo_working_path
3029 FROM git_graphs
3030 WHERE item_id = ? AND workspace_id = ?
3031 }
3032 }
3033 }
3034}
3035
3036#[cfg(test)]
3037mod tests {
3038 use super::*;
3039 use anyhow::{Context, Result, bail};
3040 use collections::{HashMap, HashSet};
3041 use fs::FakeFs;
3042 use git::Oid;
3043 use git::repository::InitialGraphCommitData;
3044 use gpui::TestAppContext;
3045 use project::Project;
3046 use project::git_store::{GitStoreEvent, RepositoryEvent};
3047 use rand::prelude::*;
3048 use serde_json::json;
3049 use settings::SettingsStore;
3050 use smallvec::{SmallVec, smallvec};
3051 use std::path::Path;
3052 use std::sync::{Arc, Mutex};
3053
3054 fn init_test(cx: &mut TestAppContext) {
3055 cx.update(|cx| {
3056 let settings_store = SettingsStore::test(cx);
3057 cx.set_global(settings_store);
3058 theme_settings::init(theme::LoadThemes::JustBase, cx);
3059 });
3060 }
3061
3062 /// Generates a random commit DAG suitable for testing git graph rendering.
3063 ///
3064 /// The commits are ordered newest-first (like git log output), so:
3065 /// - Index 0 = most recent commit (HEAD)
3066 /// - Last index = oldest commit (root, has no parents)
3067 /// - Parents of commit at index I must have index > I
3068 ///
3069 /// When `adversarial` is true, generates complex topologies with many branches
3070 /// and octopus merges. Otherwise generates more realistic linear histories
3071 /// with occasional branches.
3072 fn generate_random_commit_dag(
3073 rng: &mut StdRng,
3074 num_commits: usize,
3075 adversarial: bool,
3076 ) -> Vec<Arc<InitialGraphCommitData>> {
3077 if num_commits == 0 {
3078 return Vec::new();
3079 }
3080
3081 let mut commits: Vec<Arc<InitialGraphCommitData>> = Vec::with_capacity(num_commits);
3082 let oids: Vec<Oid> = (0..num_commits).map(|_| Oid::random(rng)).collect();
3083
3084 for i in 0..num_commits {
3085 let sha = oids[i];
3086
3087 let parents = if i == num_commits - 1 {
3088 smallvec![]
3089 } else {
3090 generate_parents_from_oids(rng, &oids, i, num_commits, adversarial)
3091 };
3092
3093 let ref_names = if i == 0 {
3094 vec!["HEAD".into(), "main".into()]
3095 } else if adversarial && rng.random_bool(0.1) {
3096 vec![format!("branch-{}", i).into()]
3097 } else {
3098 Vec::new()
3099 };
3100
3101 commits.push(Arc::new(InitialGraphCommitData {
3102 sha,
3103 parents,
3104 ref_names,
3105 }));
3106 }
3107
3108 commits
3109 }
3110
3111 fn generate_parents_from_oids(
3112 rng: &mut StdRng,
3113 oids: &[Oid],
3114 current_idx: usize,
3115 num_commits: usize,
3116 adversarial: bool,
3117 ) -> SmallVec<[Oid; 1]> {
3118 let remaining = num_commits - current_idx - 1;
3119 if remaining == 0 {
3120 return smallvec![];
3121 }
3122
3123 if adversarial {
3124 let merge_chance = 0.4;
3125 let octopus_chance = 0.15;
3126
3127 if remaining >= 3 && rng.random_bool(octopus_chance) {
3128 let num_parents = rng.random_range(3..=remaining.min(5));
3129 let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
3130 parent_indices.shuffle(rng);
3131 parent_indices
3132 .into_iter()
3133 .take(num_parents)
3134 .map(|idx| oids[idx])
3135 .collect()
3136 } else if remaining >= 2 && rng.random_bool(merge_chance) {
3137 let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
3138 parent_indices.shuffle(rng);
3139 parent_indices
3140 .into_iter()
3141 .take(2)
3142 .map(|idx| oids[idx])
3143 .collect()
3144 } else {
3145 let parent_idx = rng.random_range(current_idx + 1..num_commits);
3146 smallvec![oids[parent_idx]]
3147 }
3148 } else {
3149 let merge_chance = 0.15;
3150 let skip_chance = 0.1;
3151
3152 if remaining >= 2 && rng.random_bool(merge_chance) {
3153 let first_parent = current_idx + 1;
3154 let second_parent = rng.random_range(current_idx + 2..num_commits);
3155 smallvec![oids[first_parent], oids[second_parent]]
3156 } else if rng.random_bool(skip_chance) && remaining >= 2 {
3157 let skip = rng.random_range(1..remaining.min(3));
3158 smallvec![oids[current_idx + 1 + skip]]
3159 } else {
3160 smallvec![oids[current_idx + 1]]
3161 }
3162 }
3163 }
3164
3165 fn build_oid_to_row_map(graph: &GraphData) -> HashMap<Oid, usize> {
3166 graph
3167 .commits
3168 .iter()
3169 .enumerate()
3170 .map(|(idx, entry)| (entry.data.sha, idx))
3171 .collect()
3172 }
3173
3174 fn verify_commit_order(
3175 graph: &GraphData,
3176 commits: &[Arc<InitialGraphCommitData>],
3177 ) -> Result<()> {
3178 if graph.commits.len() != commits.len() {
3179 bail!(
3180 "Commit count mismatch: graph has {} commits, expected {}",
3181 graph.commits.len(),
3182 commits.len()
3183 );
3184 }
3185
3186 for (idx, (graph_commit, expected_commit)) in
3187 graph.commits.iter().zip(commits.iter()).enumerate()
3188 {
3189 if graph_commit.data.sha != expected_commit.sha {
3190 bail!(
3191 "Commit order mismatch at index {}: graph has {:?}, expected {:?}",
3192 idx,
3193 graph_commit.data.sha,
3194 expected_commit.sha
3195 );
3196 }
3197 }
3198
3199 Ok(())
3200 }
3201
3202 fn verify_line_endpoints(graph: &GraphData, oid_to_row: &HashMap<Oid, usize>) -> Result<()> {
3203 for line in &graph.lines {
3204 let child_row = *oid_to_row
3205 .get(&line.child)
3206 .context("Line references non-existent child commit")?;
3207
3208 let parent_row = *oid_to_row
3209 .get(&line.parent)
3210 .context("Line references non-existent parent commit")?;
3211
3212 if child_row >= parent_row {
3213 bail!(
3214 "child_row ({}) must be < parent_row ({})",
3215 child_row,
3216 parent_row
3217 );
3218 }
3219
3220 if line.full_interval.start != child_row {
3221 bail!(
3222 "full_interval.start ({}) != child_row ({})",
3223 line.full_interval.start,
3224 child_row
3225 );
3226 }
3227
3228 if line.full_interval.end != parent_row {
3229 bail!(
3230 "full_interval.end ({}) != parent_row ({})",
3231 line.full_interval.end,
3232 parent_row
3233 );
3234 }
3235
3236 if let Some(last_segment) = line.segments.last() {
3237 let segment_end_row = match last_segment {
3238 CommitLineSegment::Straight { to_row } => *to_row,
3239 CommitLineSegment::Curve { on_row, .. } => *on_row,
3240 };
3241
3242 if segment_end_row != line.full_interval.end {
3243 bail!(
3244 "last segment ends at row {} but full_interval.end is {}",
3245 segment_end_row,
3246 line.full_interval.end
3247 );
3248 }
3249 }
3250 }
3251
3252 Ok(())
3253 }
3254
3255 fn verify_column_correctness(
3256 graph: &GraphData,
3257 oid_to_row: &HashMap<Oid, usize>,
3258 ) -> Result<()> {
3259 for line in &graph.lines {
3260 let child_row = *oid_to_row
3261 .get(&line.child)
3262 .context("Line references non-existent child commit")?;
3263
3264 let parent_row = *oid_to_row
3265 .get(&line.parent)
3266 .context("Line references non-existent parent commit")?;
3267
3268 let child_lane = graph.commits[child_row].lane;
3269 if line.child_column != child_lane {
3270 bail!(
3271 "child_column ({}) != child's lane ({})",
3272 line.child_column,
3273 child_lane
3274 );
3275 }
3276
3277 let mut current_column = line.child_column;
3278 for segment in &line.segments {
3279 if let CommitLineSegment::Curve { to_column, .. } = segment {
3280 current_column = *to_column;
3281 }
3282 }
3283
3284 let parent_lane = graph.commits[parent_row].lane;
3285 if current_column != parent_lane {
3286 bail!(
3287 "ending column ({}) != parent's lane ({})",
3288 current_column,
3289 parent_lane
3290 );
3291 }
3292 }
3293
3294 Ok(())
3295 }
3296
3297 fn verify_segment_continuity(graph: &GraphData) -> Result<()> {
3298 for line in &graph.lines {
3299 if line.segments.is_empty() {
3300 bail!("Line has no segments");
3301 }
3302
3303 let mut current_row = line.full_interval.start;
3304
3305 for (idx, segment) in line.segments.iter().enumerate() {
3306 let segment_end_row = match segment {
3307 CommitLineSegment::Straight { to_row } => *to_row,
3308 CommitLineSegment::Curve { on_row, .. } => *on_row,
3309 };
3310
3311 if segment_end_row < current_row {
3312 bail!(
3313 "segment {} ends at row {} which is before current row {}",
3314 idx,
3315 segment_end_row,
3316 current_row
3317 );
3318 }
3319
3320 current_row = segment_end_row;
3321 }
3322 }
3323
3324 Ok(())
3325 }
3326
3327 fn verify_line_overlaps(graph: &GraphData) -> Result<()> {
3328 for line in &graph.lines {
3329 let child_row = line.full_interval.start;
3330
3331 let mut current_column = line.child_column;
3332 let mut current_row = child_row;
3333
3334 for segment in &line.segments {
3335 match segment {
3336 CommitLineSegment::Straight { to_row } => {
3337 for row in (current_row + 1)..*to_row {
3338 if row < graph.commits.len() {
3339 let commit_at_row = &graph.commits[row];
3340 if commit_at_row.lane == current_column {
3341 bail!(
3342 "straight segment from row {} to {} in column {} passes through commit {:?} at row {}",
3343 current_row,
3344 to_row,
3345 current_column,
3346 commit_at_row.data.sha,
3347 row
3348 );
3349 }
3350 }
3351 }
3352 current_row = *to_row;
3353 }
3354 CommitLineSegment::Curve {
3355 to_column, on_row, ..
3356 } => {
3357 current_column = *to_column;
3358 current_row = *on_row;
3359 }
3360 }
3361 }
3362 }
3363
3364 Ok(())
3365 }
3366
3367 fn verify_coverage(graph: &GraphData) -> Result<()> {
3368 let mut expected_edges: HashSet<(Oid, Oid)> = HashSet::default();
3369 for entry in &graph.commits {
3370 for parent in &entry.data.parents {
3371 expected_edges.insert((entry.data.sha, *parent));
3372 }
3373 }
3374
3375 let mut found_edges: HashSet<(Oid, Oid)> = HashSet::default();
3376 for line in &graph.lines {
3377 let edge = (line.child, line.parent);
3378
3379 if !found_edges.insert(edge) {
3380 bail!(
3381 "Duplicate line found for edge {:?} -> {:?}",
3382 line.child,
3383 line.parent
3384 );
3385 }
3386
3387 if !expected_edges.contains(&edge) {
3388 bail!(
3389 "Orphan line found: {:?} -> {:?} is not in the commit graph",
3390 line.child,
3391 line.parent
3392 );
3393 }
3394 }
3395
3396 for (child, parent) in &expected_edges {
3397 if !found_edges.contains(&(*child, *parent)) {
3398 bail!("Missing line for edge {:?} -> {:?}", child, parent);
3399 }
3400 }
3401
3402 assert_eq!(
3403 expected_edges.symmetric_difference(&found_edges).count(),
3404 0,
3405 "The symmetric difference should be zero"
3406 );
3407
3408 Ok(())
3409 }
3410
3411 fn verify_merge_line_optimality(
3412 graph: &GraphData,
3413 oid_to_row: &HashMap<Oid, usize>,
3414 ) -> Result<()> {
3415 for line in &graph.lines {
3416 let first_segment = line.segments.first();
3417 let is_merge_line = matches!(
3418 first_segment,
3419 Some(CommitLineSegment::Curve {
3420 curve_kind: CurveKind::Merge,
3421 ..
3422 })
3423 );
3424
3425 if !is_merge_line {
3426 continue;
3427 }
3428
3429 let child_row = *oid_to_row
3430 .get(&line.child)
3431 .context("Line references non-existent child commit")?;
3432
3433 let parent_row = *oid_to_row
3434 .get(&line.parent)
3435 .context("Line references non-existent parent commit")?;
3436
3437 let parent_lane = graph.commits[parent_row].lane;
3438
3439 let Some(CommitLineSegment::Curve { to_column, .. }) = first_segment else {
3440 continue;
3441 };
3442
3443 let curves_directly_to_parent = *to_column == parent_lane;
3444
3445 if !curves_directly_to_parent {
3446 continue;
3447 }
3448
3449 let curve_row = child_row + 1;
3450 let has_commits_in_path = graph.commits[curve_row..parent_row]
3451 .iter()
3452 .any(|c| c.lane == parent_lane);
3453
3454 if has_commits_in_path {
3455 bail!(
3456 "Merge line from {:?} to {:?} curves directly to parent lane {} but there are commits in that lane between rows {} and {}",
3457 line.child,
3458 line.parent,
3459 parent_lane,
3460 curve_row,
3461 parent_row
3462 );
3463 }
3464
3465 let curve_ends_at_parent = curve_row == parent_row;
3466
3467 if curve_ends_at_parent {
3468 if line.segments.len() != 1 {
3469 bail!(
3470 "Merge line from {:?} to {:?} curves directly to parent (curve_row == parent_row), but has {} segments instead of 1 [MergeCurve]",
3471 line.child,
3472 line.parent,
3473 line.segments.len()
3474 );
3475 }
3476 } else {
3477 if line.segments.len() != 2 {
3478 bail!(
3479 "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but has {} segments instead of 2 [MergeCurve, Straight]",
3480 line.child,
3481 line.parent,
3482 line.segments.len()
3483 );
3484 }
3485
3486 let is_straight_segment = matches!(
3487 line.segments.get(1),
3488 Some(CommitLineSegment::Straight { .. })
3489 );
3490
3491 if !is_straight_segment {
3492 bail!(
3493 "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but second segment is not a Straight segment",
3494 line.child,
3495 line.parent
3496 );
3497 }
3498 }
3499 }
3500
3501 Ok(())
3502 }
3503
3504 fn verify_all_invariants(
3505 graph: &GraphData,
3506 commits: &[Arc<InitialGraphCommitData>],
3507 ) -> Result<()> {
3508 let oid_to_row = build_oid_to_row_map(graph);
3509
3510 verify_commit_order(graph, commits).context("commit order")?;
3511 verify_line_endpoints(graph, &oid_to_row).context("line endpoints")?;
3512 verify_column_correctness(graph, &oid_to_row).context("column correctness")?;
3513 verify_segment_continuity(graph).context("segment continuity")?;
3514 verify_merge_line_optimality(graph, &oid_to_row).context("merge line optimality")?;
3515 verify_coverage(graph).context("coverage")?;
3516 verify_line_overlaps(graph).context("line overlaps")?;
3517 Ok(())
3518 }
3519
3520 #[test]
3521 fn test_git_graph_merge_commits() {
3522 let mut rng = StdRng::seed_from_u64(42);
3523
3524 let oid1 = Oid::random(&mut rng);
3525 let oid2 = Oid::random(&mut rng);
3526 let oid3 = Oid::random(&mut rng);
3527 let oid4 = Oid::random(&mut rng);
3528
3529 let commits = vec![
3530 Arc::new(InitialGraphCommitData {
3531 sha: oid1,
3532 parents: smallvec![oid2, oid3],
3533 ref_names: vec!["HEAD".into()],
3534 }),
3535 Arc::new(InitialGraphCommitData {
3536 sha: oid2,
3537 parents: smallvec![oid4],
3538 ref_names: vec![],
3539 }),
3540 Arc::new(InitialGraphCommitData {
3541 sha: oid3,
3542 parents: smallvec![oid4],
3543 ref_names: vec![],
3544 }),
3545 Arc::new(InitialGraphCommitData {
3546 sha: oid4,
3547 parents: smallvec![],
3548 ref_names: vec![],
3549 }),
3550 ];
3551
3552 let mut graph_data = GraphData::new(8);
3553 graph_data.add_commits(&commits);
3554
3555 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
3556 panic!("Graph invariant violation for merge commits:\n{}", error);
3557 }
3558 }
3559
3560 #[test]
3561 fn test_git_graph_linear_commits() {
3562 let mut rng = StdRng::seed_from_u64(42);
3563
3564 let oid1 = Oid::random(&mut rng);
3565 let oid2 = Oid::random(&mut rng);
3566 let oid3 = Oid::random(&mut rng);
3567
3568 let commits = vec![
3569 Arc::new(InitialGraphCommitData {
3570 sha: oid1,
3571 parents: smallvec![oid2],
3572 ref_names: vec!["HEAD".into()],
3573 }),
3574 Arc::new(InitialGraphCommitData {
3575 sha: oid2,
3576 parents: smallvec![oid3],
3577 ref_names: vec![],
3578 }),
3579 Arc::new(InitialGraphCommitData {
3580 sha: oid3,
3581 parents: smallvec![],
3582 ref_names: vec![],
3583 }),
3584 ];
3585
3586 let mut graph_data = GraphData::new(8);
3587 graph_data.add_commits(&commits);
3588
3589 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
3590 panic!("Graph invariant violation for linear commits:\n{}", error);
3591 }
3592 }
3593
3594 #[test]
3595 fn test_git_graph_random_commits() {
3596 for seed in 0..100 {
3597 let mut rng = StdRng::seed_from_u64(seed);
3598
3599 let adversarial = rng.random_bool(0.2);
3600 let num_commits = if adversarial {
3601 rng.random_range(10..100)
3602 } else {
3603 rng.random_range(5..50)
3604 };
3605
3606 let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
3607
3608 assert_eq!(
3609 num_commits,
3610 commits.len(),
3611 "seed={}: Generate random commit dag didn't generate the correct amount of commits",
3612 seed
3613 );
3614
3615 let mut graph_data = GraphData::new(8);
3616 graph_data.add_commits(&commits);
3617
3618 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
3619 panic!(
3620 "Graph invariant violation (seed={}, adversarial={}, num_commits={}):\n{:#}",
3621 seed, adversarial, num_commits, error
3622 );
3623 }
3624 }
3625 }
3626
3627 // The full integration test has less iterations because it's significantly slower
3628 // than the random commit test
3629 #[gpui::test(iterations = 10)]
3630 async fn test_git_graph_random_integration(mut rng: StdRng, cx: &mut TestAppContext) {
3631 init_test(cx);
3632
3633 let adversarial = rng.random_bool(0.2);
3634 let num_commits = if adversarial {
3635 rng.random_range(10..100)
3636 } else {
3637 rng.random_range(5..50)
3638 };
3639
3640 let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
3641
3642 let fs = FakeFs::new(cx.executor());
3643 fs.insert_tree(
3644 Path::new("/project"),
3645 json!({
3646 ".git": {},
3647 "file.txt": "content",
3648 }),
3649 )
3650 .await;
3651
3652 fs.set_graph_commits(Path::new("/project/.git"), commits.clone());
3653
3654 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
3655 cx.run_until_parked();
3656
3657 let repository = project.read_with(cx, |project, cx| {
3658 project
3659 .active_repository(cx)
3660 .expect("should have a repository")
3661 });
3662
3663 repository.update(cx, |repo, cx| {
3664 repo.graph_data(
3665 crate::LogSource::default(),
3666 crate::LogOrder::default(),
3667 0..usize::MAX,
3668 cx,
3669 );
3670 });
3671 cx.run_until_parked();
3672
3673 let graph_commits: Vec<Arc<InitialGraphCommitData>> = repository.update(cx, |repo, cx| {
3674 repo.graph_data(
3675 crate::LogSource::default(),
3676 crate::LogOrder::default(),
3677 0..usize::MAX,
3678 cx,
3679 )
3680 .commits
3681 .to_vec()
3682 });
3683
3684 let mut graph_data = GraphData::new(8);
3685 graph_data.add_commits(&graph_commits);
3686
3687 if let Err(error) = verify_all_invariants(&graph_data, &commits) {
3688 panic!(
3689 "Graph invariant violation (adversarial={}, num_commits={}):\n{:#}",
3690 adversarial, num_commits, error
3691 );
3692 }
3693 }
3694
3695 #[gpui::test]
3696 async fn test_initial_graph_data_not_cleared_on_initial_loading(cx: &mut TestAppContext) {
3697 init_test(cx);
3698
3699 let fs = FakeFs::new(cx.executor());
3700 fs.insert_tree(
3701 Path::new("/project"),
3702 json!({
3703 ".git": {},
3704 "file.txt": "content",
3705 }),
3706 )
3707 .await;
3708
3709 let mut rng = StdRng::seed_from_u64(42);
3710 let commits = generate_random_commit_dag(&mut rng, 10, false);
3711 fs.set_graph_commits(Path::new("/project/.git"), commits.clone());
3712
3713 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
3714 let observed_repository_events = Arc::new(Mutex::new(Vec::new()));
3715 project.update(cx, |project, cx| {
3716 let observed_repository_events = observed_repository_events.clone();
3717 cx.subscribe(project.git_store(), move |_, _, event, _| {
3718 if let GitStoreEvent::RepositoryUpdated(_, repository_event, true) = event {
3719 observed_repository_events
3720 .lock()
3721 .expect("repository event mutex should be available")
3722 .push(repository_event.clone());
3723 }
3724 })
3725 .detach();
3726 });
3727
3728 let repository = project.read_with(cx, |project, cx| {
3729 project
3730 .active_repository(cx)
3731 .expect("should have a repository")
3732 });
3733
3734 repository.update(cx, |repo, cx| {
3735 repo.graph_data(
3736 crate::LogSource::default(),
3737 crate::LogOrder::default(),
3738 0..usize::MAX,
3739 cx,
3740 );
3741 });
3742
3743 project
3744 .update(cx, |project, cx| project.git_scans_complete(cx))
3745 .await;
3746 cx.run_until_parked();
3747
3748 let observed_repository_events = observed_repository_events
3749 .lock()
3750 .expect("repository event mutex should be available");
3751 assert!(
3752 observed_repository_events
3753 .iter()
3754 .any(|event| matches!(event, RepositoryEvent::HeadChanged)),
3755 "initial repository scan should emit HeadChanged"
3756 );
3757 let commit_count_after = repository.read_with(cx, |repo, _| {
3758 repo.get_graph_data(crate::LogSource::default(), crate::LogOrder::default())
3759 .map(|data| data.commit_data.len())
3760 .unwrap()
3761 });
3762 assert_eq!(
3763 commits.len(),
3764 commit_count_after,
3765 "initial_graph_data should remain populated after events emitted by initial repository scan"
3766 );
3767 }
3768
3769 #[gpui::test]
3770 async fn test_initial_graph_data_propagates_error(cx: &mut TestAppContext) {
3771 init_test(cx);
3772
3773 let fs = FakeFs::new(cx.executor());
3774 fs.insert_tree(
3775 Path::new("/project"),
3776 json!({
3777 ".git": {},
3778 "file.txt": "content",
3779 }),
3780 )
3781 .await;
3782
3783 fs.set_graph_error(
3784 Path::new("/project/.git"),
3785 Some("fatal: bad default revision 'HEAD'".to_string()),
3786 );
3787
3788 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
3789
3790 let repository = project.read_with(cx, |project, cx| {
3791 project
3792 .active_repository(cx)
3793 .expect("should have a repository")
3794 });
3795
3796 repository.update(cx, |repo, cx| {
3797 repo.graph_data(
3798 crate::LogSource::default(),
3799 crate::LogOrder::default(),
3800 0..usize::MAX,
3801 cx,
3802 );
3803 });
3804
3805 cx.run_until_parked();
3806
3807 let error = repository.read_with(cx, |repo, _| {
3808 repo.get_graph_data(crate::LogSource::default(), crate::LogOrder::default())
3809 .and_then(|data| data.error.clone())
3810 });
3811
3812 assert!(
3813 error.is_some(),
3814 "graph data should contain an error after initial_graph_data fails"
3815 );
3816 let error_message = error.unwrap();
3817 assert!(
3818 error_message.contains("bad default revision"),
3819 "error should contain the git error message, got: {}",
3820 error_message
3821 );
3822 }
3823
3824 #[gpui::test]
3825 async fn test_graph_data_repopulated_from_cache_after_repo_switch(cx: &mut TestAppContext) {
3826 init_test(cx);
3827
3828 let fs = FakeFs::new(cx.executor());
3829 fs.insert_tree(
3830 Path::new("/project_a"),
3831 json!({
3832 ".git": {},
3833 "file.txt": "content",
3834 }),
3835 )
3836 .await;
3837 fs.insert_tree(
3838 Path::new("/project_b"),
3839 json!({
3840 ".git": {},
3841 "other.txt": "content",
3842 }),
3843 )
3844 .await;
3845
3846 let mut rng = StdRng::seed_from_u64(42);
3847 let commits = generate_random_commit_dag(&mut rng, 10, false);
3848 fs.set_graph_commits(Path::new("/project_a/.git"), commits.clone());
3849
3850 let project = Project::test(
3851 fs.clone(),
3852 [Path::new("/project_a"), Path::new("/project_b")],
3853 cx,
3854 )
3855 .await;
3856 cx.run_until_parked();
3857
3858 let (first_repository, second_repository) = project.read_with(cx, |project, cx| {
3859 let mut first_repository = None;
3860 let mut second_repository = None;
3861
3862 for repository in project.repositories(cx).values() {
3863 let work_directory_abs_path = &repository.read(cx).work_directory_abs_path;
3864 if work_directory_abs_path.as_ref() == Path::new("/project_a") {
3865 first_repository = Some(repository.clone());
3866 } else if work_directory_abs_path.as_ref() == Path::new("/project_b") {
3867 second_repository = Some(repository.clone());
3868 }
3869 }
3870
3871 (
3872 first_repository.expect("should have repository for /project_a"),
3873 second_repository.expect("should have repository for /project_b"),
3874 )
3875 });
3876 first_repository.update(cx, |repository, cx| repository.set_as_active_repository(cx));
3877 cx.run_until_parked();
3878
3879 let (multi_workspace, cx) = cx.add_window_view(|window, cx| {
3880 workspace::MultiWorkspace::test_new(project.clone(), window, cx)
3881 });
3882
3883 let workspace_weak =
3884 multi_workspace.read_with(&*cx, |multi, _| multi.workspace().downgrade());
3885 let git_graph = cx.new_window_entity(|window, cx| {
3886 GitGraph::new(
3887 first_repository.read(cx).id,
3888 project.read(cx).git_store().clone(),
3889 workspace_weak,
3890 window,
3891 cx,
3892 )
3893 });
3894 cx.run_until_parked();
3895
3896 // Verify initial graph data is loaded
3897 let initial_commit_count =
3898 git_graph.read_with(&*cx, |graph, _| graph.graph_data.commits.len());
3899 assert!(
3900 initial_commit_count > 0,
3901 "graph data should have been loaded, got 0 commits"
3902 );
3903
3904 git_graph.update(cx, |graph, cx| {
3905 graph.set_repo_id(second_repository.read(cx).id, cx)
3906 });
3907 cx.run_until_parked();
3908
3909 let commit_count_after_clear =
3910 git_graph.read_with(&*cx, |graph, _| graph.graph_data.commits.len());
3911 assert_eq!(
3912 commit_count_after_clear, 0,
3913 "graph_data should be cleared after switching away"
3914 );
3915
3916 git_graph.update(cx, |graph, cx| {
3917 graph.set_repo_id(first_repository.read(cx).id, cx)
3918 });
3919 cx.run_until_parked();
3920
3921 cx.draw(
3922 point(px(0.), px(0.)),
3923 gpui::size(px(1200.), px(800.)),
3924 |_, _| git_graph.clone().into_any_element(),
3925 );
3926 cx.run_until_parked();
3927
3928 // Verify graph data is reloaded from repository cache on switch back
3929 let reloaded_commit_count =
3930 git_graph.read_with(&*cx, |graph, _| graph.graph_data.commits.len());
3931 assert_eq!(
3932 reloaded_commit_count,
3933 commits.len(),
3934 "graph data should be reloaded after switching back"
3935 );
3936 }
3937
3938 #[gpui::test]
3939 async fn test_graph_data_reloaded_after_stash_change(cx: &mut TestAppContext) {
3940 init_test(cx);
3941
3942 let fs = FakeFs::new(cx.executor());
3943 fs.insert_tree(
3944 Path::new("/project"),
3945 json!({
3946 ".git": {},
3947 "file.txt": "content",
3948 }),
3949 )
3950 .await;
3951
3952 let initial_head = Oid::from_bytes(&[1; 20]).unwrap();
3953 let initial_stash = Oid::from_bytes(&[2; 20]).unwrap();
3954 let updated_head = Oid::from_bytes(&[3; 20]).unwrap();
3955 let updated_stash = Oid::from_bytes(&[4; 20]).unwrap();
3956
3957 fs.set_graph_commits(
3958 Path::new("/project/.git"),
3959 vec![
3960 Arc::new(InitialGraphCommitData {
3961 sha: initial_head,
3962 parents: smallvec![initial_stash],
3963 ref_names: vec!["HEAD".into(), "refs/heads/main".into()],
3964 }),
3965 Arc::new(InitialGraphCommitData {
3966 sha: initial_stash,
3967 parents: smallvec![],
3968 ref_names: vec!["refs/stash".into()],
3969 }),
3970 ],
3971 );
3972 fs.with_git_state(Path::new("/project/.git"), true, |state| {
3973 state.stash_entries = git::stash::GitStash {
3974 entries: vec![git::stash::StashEntry {
3975 index: 0,
3976 oid: initial_stash,
3977 message: "initial stash".to_string(),
3978 branch: Some("main".to_string()),
3979 timestamp: 1,
3980 }]
3981 .into(),
3982 };
3983 })
3984 .unwrap();
3985
3986 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
3987 cx.run_until_parked();
3988
3989 let repository = project.read_with(cx, |project, cx| {
3990 project
3991 .active_repository(cx)
3992 .expect("should have a repository")
3993 });
3994
3995 let (multi_workspace, cx) = cx.add_window_view(|window, cx| {
3996 workspace::MultiWorkspace::test_new(project.clone(), window, cx)
3997 });
3998 let workspace_weak =
3999 multi_workspace.read_with(&*cx, |multi, _| multi.workspace().downgrade());
4000 let git_graph = cx.new_window_entity(|window, cx| {
4001 GitGraph::new(
4002 repository.read(cx).id,
4003 project.read(cx).git_store().clone(),
4004 workspace_weak,
4005 window,
4006 cx,
4007 )
4008 });
4009 cx.run_until_parked();
4010
4011 let initial_shas = git_graph.read_with(&*cx, |graph, _| {
4012 graph
4013 .graph_data
4014 .commits
4015 .iter()
4016 .map(|commit| commit.data.sha)
4017 .collect::<Vec<_>>()
4018 });
4019 assert_eq!(initial_shas, vec![initial_head, initial_stash]);
4020
4021 fs.set_graph_commits(
4022 Path::new("/project/.git"),
4023 vec![
4024 Arc::new(InitialGraphCommitData {
4025 sha: updated_head,
4026 parents: smallvec![updated_stash],
4027 ref_names: vec!["HEAD".into(), "refs/heads/main".into()],
4028 }),
4029 Arc::new(InitialGraphCommitData {
4030 sha: updated_stash,
4031 parents: smallvec![],
4032 ref_names: vec!["refs/stash".into()],
4033 }),
4034 ],
4035 );
4036 fs.with_git_state(Path::new("/project/.git"), true, |state| {
4037 state.stash_entries = git::stash::GitStash {
4038 entries: vec![git::stash::StashEntry {
4039 index: 0,
4040 oid: updated_stash,
4041 message: "updated stash".to_string(),
4042 branch: Some("main".to_string()),
4043 timestamp: 1,
4044 }]
4045 .into(),
4046 };
4047 })
4048 .unwrap();
4049
4050 project
4051 .update(cx, |project, cx| project.git_scans_complete(cx))
4052 .await;
4053 cx.run_until_parked();
4054
4055 cx.draw(
4056 point(px(0.), px(0.)),
4057 gpui::size(px(1200.), px(800.)),
4058 |_, _| git_graph.clone().into_any_element(),
4059 );
4060 cx.run_until_parked();
4061
4062 let reloaded_shas = git_graph.read_with(&*cx, |graph, _| {
4063 graph
4064 .graph_data
4065 .commits
4066 .iter()
4067 .map(|commit| commit.data.sha)
4068 .collect::<Vec<_>>()
4069 });
4070 assert_eq!(reloaded_shas, vec![updated_head, updated_stash]);
4071 }
4072
4073 #[gpui::test]
4074 async fn test_git_graph_row_at_position_rounding(cx: &mut TestAppContext) {
4075 init_test(cx);
4076
4077 let fs = FakeFs::new(cx.executor());
4078 fs.insert_tree(
4079 Path::new("/project"),
4080 serde_json::json!({
4081 ".git": {},
4082 "file.txt": "content",
4083 }),
4084 )
4085 .await;
4086
4087 let mut rng = StdRng::seed_from_u64(42);
4088 let commits = generate_random_commit_dag(&mut rng, 10, false);
4089 fs.set_graph_commits(Path::new("/project/.git"), commits.clone());
4090
4091 let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
4092 cx.run_until_parked();
4093
4094 let repository = project.read_with(cx, |project, cx| {
4095 project
4096 .active_repository(cx)
4097 .expect("should have a repository")
4098 });
4099
4100 let (multi_workspace, cx) = cx.add_window_view(|window, cx| {
4101 workspace::MultiWorkspace::test_new(project.clone(), window, cx)
4102 });
4103
4104 let workspace_weak =
4105 multi_workspace.read_with(&*cx, |multi, _| multi.workspace().downgrade());
4106
4107 let git_graph = cx.new_window_entity(|window, cx| {
4108 GitGraph::new(
4109 repository.read(cx).id,
4110 project.read(cx).git_store().clone(),
4111 workspace_weak,
4112 window,
4113 cx,
4114 )
4115 });
4116 cx.run_until_parked();
4117
4118 git_graph.update(cx, |graph, cx| {
4119 assert!(
4120 graph.graph_data.commits.len() >= 10,
4121 "graph should load dummy commits"
4122 );
4123
4124 graph.row_height = px(20.0);
4125 let origin_y = px(100.0);
4126 graph.graph_canvas_bounds.set(Some(Bounds {
4127 origin: point(px(0.0), origin_y),
4128 size: gpui::size(px(100.0), px(1000.0)),
4129 }));
4130
4131 graph.table_interaction_state.update(cx, |state, _| {
4132 state.set_scroll_offset(point(px(0.0), px(-15.0)))
4133 });
4134 let pos_y = origin_y + px(10.0);
4135 let absolute_calc_row = graph.row_at_position(pos_y, cx);
4136
4137 assert_eq!(
4138 absolute_calc_row,
4139 Some(1),
4140 "Row calculation should yield absolute row exactly"
4141 );
4142 });
4143 }
4144}