git_graph.rs

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