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