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