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