git_graph.rs

   1use collections::{BTreeMap, HashMap};
   2use git::{
   3    BuildCommitPermalinkParams, GitHostingProviderRegistry, GitRemote, Oid, ParsedGitRemote,
   4    parse_git_remote_url,
   5    repository::{CommitDiff, InitialGraphCommitData, LogOrder, LogSource},
   6};
   7use git_ui::commit_tooltip::CommitAvatar;
   8use gpui::{
   9    AnyElement, App, Bounds, ClipboardItem, Context, Corner, DefiniteLength, ElementId, Entity,
  10    EventEmitter, FocusHandle, Focusable, FontWeight, Hsla, InteractiveElement, ParentElement,
  11    PathBuilder, Pixels, Point, Render, ScrollWheelEvent, SharedString, Styled, Subscription, Task,
  12    WeakEntity, Window, actions, anchored, deferred, point, px,
  13};
  14use project::{
  15    Project,
  16    git_store::{CommitDataState, GitStoreEvent, Repository, RepositoryEvent},
  17};
  18use settings::Settings;
  19use smallvec::{SmallVec, smallvec};
  20use std::{ops::Range, rc::Rc, sync::Arc, sync::OnceLock};
  21use theme::{AccentColors, ThemeSettings};
  22use time::{OffsetDateTime, UtcOffset, format_description::BorrowedFormatItem};
  23use ui::{ContextMenu, ScrollableHandle, Table, TableInteractionState, Tooltip, prelude::*};
  24use workspace::{
  25    Workspace,
  26    item::{Item, ItemEvent, SerializableItem},
  27};
  28
  29const COMMIT_CIRCLE_RADIUS: Pixels = px(4.5);
  30const COMMIT_CIRCLE_STROKE_WIDTH: Pixels = px(1.5);
  31const LANE_WIDTH: Pixels = px(16.0);
  32const LEFT_PADDING: Pixels = px(12.0);
  33const LINE_WIDTH: Pixels = px(1.5);
  34
  35actions!(
  36    git_graph,
  37    [
  38        /// Opens the Git Graph panel.
  39        Open,
  40        /// Opens the commit view for the selected commit.
  41        OpenCommitView,
  42    ]
  43);
  44
  45fn timestamp_format() -> &'static [BorrowedFormatItem<'static>] {
  46    static FORMAT: OnceLock<Vec<BorrowedFormatItem<'static>>> = OnceLock::new();
  47    FORMAT.get_or_init(|| {
  48        time::format_description::parse("[day] [month repr:short] [year] [hour]:[minute]")
  49            .unwrap_or_default()
  50    })
  51}
  52
  53fn format_timestamp(timestamp: i64) -> String {
  54    let Ok(datetime) = OffsetDateTime::from_unix_timestamp(timestamp) else {
  55        return "Unknown".to_string();
  56    };
  57
  58    let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
  59    let local_datetime = datetime.to_offset(local_offset);
  60
  61    local_datetime
  62        .format(timestamp_format())
  63        .unwrap_or_default()
  64}
  65
  66fn accent_colors_count(accents: &AccentColors) -> usize {
  67    accents.0.len()
  68}
  69
  70#[derive(Copy, Clone, Debug)]
  71struct BranchColor(u8);
  72
  73#[derive(Debug)]
  74enum LaneState {
  75    Empty,
  76    Active {
  77        child: Oid,
  78        parent: Oid,
  79        color: Option<BranchColor>,
  80        starting_row: usize,
  81        starting_col: usize,
  82        destination_column: Option<usize>,
  83        segments: SmallVec<[CommitLineSegment; 1]>,
  84    },
  85}
  86
  87impl LaneState {
  88    fn to_commit_lines(
  89        &mut self,
  90        ending_row: usize,
  91        lane_column: usize,
  92        parent_column: usize,
  93        parent_color: BranchColor,
  94    ) -> Option<CommitLine> {
  95        let state = std::mem::replace(self, LaneState::Empty);
  96
  97        match state {
  98            LaneState::Active {
  99                #[cfg_attr(not(test), allow(unused_variables))]
 100                parent,
 101                #[cfg_attr(not(test), allow(unused_variables))]
 102                child,
 103                color,
 104                starting_row,
 105                starting_col,
 106                destination_column,
 107                mut segments,
 108            } => {
 109                let final_destination = destination_column.unwrap_or(parent_column);
 110                let final_color = color.unwrap_or(parent_color);
 111
 112                Some(CommitLine {
 113                    #[cfg(test)]
 114                    child,
 115                    #[cfg(test)]
 116                    parent,
 117                    child_column: starting_col,
 118                    full_interval: starting_row..ending_row,
 119                    color_idx: final_color.0 as usize,
 120                    segments: {
 121                        match segments.last_mut() {
 122                            Some(CommitLineSegment::Straight { to_row })
 123                                if *to_row == usize::MAX =>
 124                            {
 125                                if final_destination != lane_column {
 126                                    *to_row = ending_row - 1;
 127
 128                                    let curved_line = CommitLineSegment::Curve {
 129                                        to_column: final_destination,
 130                                        on_row: ending_row,
 131                                        curve_kind: CurveKind::Checkout,
 132                                    };
 133
 134                                    if *to_row == starting_row {
 135                                        let last_index = segments.len() - 1;
 136                                        segments[last_index] = curved_line;
 137                                    } else {
 138                                        segments.push(curved_line);
 139                                    }
 140                                } else {
 141                                    *to_row = ending_row;
 142                                }
 143                            }
 144                            Some(CommitLineSegment::Curve {
 145                                on_row,
 146                                to_column,
 147                                curve_kind,
 148                            }) if *on_row == usize::MAX => {
 149                                if *to_column == usize::MAX {
 150                                    *to_column = final_destination;
 151                                }
 152                                if matches!(curve_kind, CurveKind::Merge) {
 153                                    *on_row = starting_row + 1;
 154                                    if *on_row < ending_row {
 155                                        if *to_column != final_destination {
 156                                            segments.push(CommitLineSegment::Straight {
 157                                                to_row: ending_row - 1,
 158                                            });
 159                                            segments.push(CommitLineSegment::Curve {
 160                                                to_column: final_destination,
 161                                                on_row: ending_row,
 162                                                curve_kind: CurveKind::Checkout,
 163                                            });
 164                                        } else {
 165                                            segments.push(CommitLineSegment::Straight {
 166                                                to_row: ending_row,
 167                                            });
 168                                        }
 169                                    } else if *to_column != final_destination {
 170                                        segments.push(CommitLineSegment::Curve {
 171                                            to_column: final_destination,
 172                                            on_row: ending_row,
 173                                            curve_kind: CurveKind::Checkout,
 174                                        });
 175                                    }
 176                                } else {
 177                                    *on_row = ending_row;
 178                                    if *to_column != final_destination {
 179                                        segments.push(CommitLineSegment::Straight {
 180                                            to_row: ending_row,
 181                                        });
 182                                        segments.push(CommitLineSegment::Curve {
 183                                            to_column: final_destination,
 184                                            on_row: ending_row,
 185                                            curve_kind: CurveKind::Checkout,
 186                                        });
 187                                    }
 188                                }
 189                            }
 190                            Some(CommitLineSegment::Curve {
 191                                on_row, to_column, ..
 192                            }) => {
 193                                if *on_row < ending_row {
 194                                    if *to_column != final_destination {
 195                                        segments.push(CommitLineSegment::Straight {
 196                                            to_row: ending_row - 1,
 197                                        });
 198                                        segments.push(CommitLineSegment::Curve {
 199                                            to_column: final_destination,
 200                                            on_row: ending_row,
 201                                            curve_kind: CurveKind::Checkout,
 202                                        });
 203                                    } else {
 204                                        segments.push(CommitLineSegment::Straight {
 205                                            to_row: ending_row,
 206                                        });
 207                                    }
 208                                } else if *to_column != final_destination {
 209                                    segments.push(CommitLineSegment::Curve {
 210                                        to_column: final_destination,
 211                                        on_row: ending_row,
 212                                        curve_kind: CurveKind::Checkout,
 213                                    });
 214                                }
 215                            }
 216                            _ => {}
 217                        }
 218
 219                        segments
 220                    },
 221                })
 222            }
 223            LaneState::Empty => None,
 224        }
 225    }
 226
 227    fn is_empty(&self) -> bool {
 228        match self {
 229            LaneState::Empty => true,
 230            LaneState::Active { .. } => false,
 231        }
 232    }
 233}
 234
 235struct CommitEntry {
 236    data: Arc<InitialGraphCommitData>,
 237    lane: usize,
 238    color_idx: usize,
 239}
 240
 241type ActiveLaneIdx = usize;
 242
 243enum AllCommitCount {
 244    NotLoaded,
 245    Loaded(usize),
 246}
 247
 248#[derive(Debug)]
 249enum CurveKind {
 250    Merge,
 251    Checkout,
 252}
 253
 254#[derive(Debug)]
 255enum CommitLineSegment {
 256    Straight {
 257        to_row: usize,
 258    },
 259    Curve {
 260        to_column: usize,
 261        on_row: usize,
 262        curve_kind: CurveKind,
 263    },
 264}
 265
 266#[derive(Debug)]
 267struct CommitLine {
 268    #[cfg(test)]
 269    child: Oid,
 270    #[cfg(test)]
 271    parent: Oid,
 272    child_column: usize,
 273    full_interval: Range<usize>,
 274    color_idx: usize,
 275    segments: SmallVec<[CommitLineSegment; 1]>,
 276}
 277
 278impl CommitLine {
 279    fn get_first_visible_segment_idx(&self, first_visible_row: usize) -> Option<(usize, usize)> {
 280        if first_visible_row > self.full_interval.end {
 281            return None;
 282        } else if first_visible_row <= self.full_interval.start {
 283            return Some((0, self.child_column));
 284        }
 285
 286        let mut current_column = self.child_column;
 287
 288        for (idx, segment) in self.segments.iter().enumerate() {
 289            match segment {
 290                CommitLineSegment::Straight { to_row } => {
 291                    if *to_row >= first_visible_row {
 292                        return Some((idx, current_column));
 293                    }
 294                }
 295                CommitLineSegment::Curve {
 296                    to_column, on_row, ..
 297                } => {
 298                    if *on_row >= first_visible_row {
 299                        return Some((idx, current_column));
 300                    }
 301                    current_column = *to_column;
 302                }
 303            }
 304        }
 305
 306        None
 307    }
 308}
 309
 310#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 311struct CommitLineKey {
 312    child: Oid,
 313    parent: Oid,
 314}
 315
 316struct GraphData {
 317    lane_states: SmallVec<[LaneState; 8]>,
 318    lane_colors: HashMap<ActiveLaneIdx, BranchColor>,
 319    parent_to_lanes: HashMap<Oid, SmallVec<[usize; 1]>>,
 320    next_color: BranchColor,
 321    accent_colors_count: usize,
 322    commits: Vec<Rc<CommitEntry>>,
 323    max_commit_count: AllCommitCount,
 324    max_lanes: usize,
 325    lines: Vec<Rc<CommitLine>>,
 326    active_commit_lines: HashMap<CommitLineKey, usize>,
 327    active_commit_lines_by_parent: HashMap<Oid, SmallVec<[usize; 1]>>,
 328}
 329
 330impl GraphData {
 331    fn new(accent_colors_count: usize) -> Self {
 332        GraphData {
 333            lane_states: SmallVec::default(),
 334            lane_colors: HashMap::default(),
 335            parent_to_lanes: HashMap::default(),
 336            next_color: BranchColor(0),
 337            accent_colors_count,
 338            commits: Vec::default(),
 339            max_commit_count: AllCommitCount::NotLoaded,
 340            max_lanes: 0,
 341            lines: Vec::default(),
 342            active_commit_lines: HashMap::default(),
 343            active_commit_lines_by_parent: HashMap::default(),
 344        }
 345    }
 346
 347    fn clear(&mut self) {
 348        self.lane_states.clear();
 349        self.lane_colors.clear();
 350        self.parent_to_lanes.clear();
 351        self.commits.clear();
 352        self.lines.clear();
 353        self.active_commit_lines.clear();
 354        self.active_commit_lines_by_parent.clear();
 355        self.next_color = BranchColor(0);
 356        self.max_commit_count = AllCommitCount::NotLoaded;
 357        self.max_lanes = 0;
 358    }
 359
 360    fn first_empty_lane_idx(&mut self) -> ActiveLaneIdx {
 361        self.lane_states
 362            .iter()
 363            .position(LaneState::is_empty)
 364            .unwrap_or_else(|| {
 365                self.lane_states.push(LaneState::Empty);
 366                self.lane_states.len() - 1
 367            })
 368    }
 369
 370    fn get_lane_color(&mut self, lane_idx: ActiveLaneIdx) -> BranchColor {
 371        let accent_colors_count = self.accent_colors_count;
 372        *self.lane_colors.entry(lane_idx).or_insert_with(|| {
 373            let color_idx = self.next_color;
 374            self.next_color = BranchColor((self.next_color.0 + 1) % accent_colors_count as u8);
 375            color_idx
 376        })
 377    }
 378
 379    fn add_commits(&mut self, commits: &[Arc<InitialGraphCommitData>]) {
 380        self.commits.reserve(commits.len());
 381        self.lines.reserve(commits.len() / 2);
 382
 383        for commit in commits.iter() {
 384            let commit_row = self.commits.len();
 385
 386            let commit_lane = self
 387                .parent_to_lanes
 388                .get(&commit.sha)
 389                .and_then(|lanes| lanes.first().copied());
 390
 391            let commit_lane = commit_lane.unwrap_or_else(|| self.first_empty_lane_idx());
 392
 393            let commit_color = self.get_lane_color(commit_lane);
 394
 395            if let Some(lanes) = self.parent_to_lanes.remove(&commit.sha) {
 396                for lane_column in lanes {
 397                    let state = &mut self.lane_states[lane_column];
 398
 399                    if let LaneState::Active {
 400                        starting_row,
 401                        segments,
 402                        ..
 403                    } = state
 404                    {
 405                        if let Some(CommitLineSegment::Curve {
 406                            to_column,
 407                            curve_kind: CurveKind::Merge,
 408                            ..
 409                        }) = segments.first_mut()
 410                        {
 411                            let curve_row = *starting_row + 1;
 412                            let would_overlap =
 413                                if lane_column != commit_lane && curve_row < commit_row {
 414                                    self.commits[curve_row..commit_row]
 415                                        .iter()
 416                                        .any(|c| c.lane == commit_lane)
 417                                } else {
 418                                    false
 419                                };
 420
 421                            if would_overlap {
 422                                *to_column = lane_column;
 423                            }
 424                        }
 425                    }
 426
 427                    if let Some(commit_line) =
 428                        state.to_commit_lines(commit_row, lane_column, commit_lane, commit_color)
 429                    {
 430                        self.lines.push(Rc::new(commit_line));
 431                    }
 432                }
 433            }
 434
 435            commit
 436                .parents
 437                .iter()
 438                .enumerate()
 439                .for_each(|(parent_idx, parent)| {
 440                    if parent_idx == 0 {
 441                        self.lane_states[commit_lane] = LaneState::Active {
 442                            parent: *parent,
 443                            child: commit.sha,
 444                            color: Some(commit_color),
 445                            starting_col: commit_lane,
 446                            starting_row: commit_row,
 447                            destination_column: None,
 448                            segments: smallvec![CommitLineSegment::Straight { to_row: usize::MAX }],
 449                        };
 450
 451                        self.parent_to_lanes
 452                            .entry(*parent)
 453                            .or_default()
 454                            .push(commit_lane);
 455                    } else {
 456                        let new_lane = self.first_empty_lane_idx();
 457
 458                        self.lane_states[new_lane] = LaneState::Active {
 459                            parent: *parent,
 460                            child: commit.sha,
 461                            color: None,
 462                            starting_col: commit_lane,
 463                            starting_row: commit_row,
 464                            destination_column: None,
 465                            segments: smallvec![CommitLineSegment::Curve {
 466                                to_column: usize::MAX,
 467                                on_row: usize::MAX,
 468                                curve_kind: CurveKind::Merge,
 469                            },],
 470                        };
 471
 472                        self.parent_to_lanes
 473                            .entry(*parent)
 474                            .or_default()
 475                            .push(new_lane);
 476                    }
 477                });
 478
 479            self.max_lanes = self.max_lanes.max(self.lane_states.len());
 480
 481            self.commits.push(Rc::new(CommitEntry {
 482                data: commit.clone(),
 483                lane: commit_lane,
 484                color_idx: commit_color.0 as usize,
 485            }));
 486        }
 487    }
 488}
 489
 490pub fn init(cx: &mut App) {
 491    workspace::register_serializable_item::<GitGraph>(cx);
 492
 493    cx.observe_new(|workspace: &mut workspace::Workspace, _, _| {
 494        workspace.register_action_renderer(|div, workspace, _, cx| {
 495            div.when(
 496                workspace.project().read(cx).active_repository(cx).is_some(),
 497                |div| {
 498                    let workspace = workspace.weak_handle();
 499
 500                    div.on_action(move |_: &Open, window, cx| {
 501                        workspace
 502                            .update(cx, |workspace, cx| {
 503                                let project = workspace.project().clone();
 504                                let git_graph = cx.new(|cx| GitGraph::new(project, window, cx));
 505                                workspace.add_item_to_active_pane(
 506                                    Box::new(git_graph),
 507                                    None,
 508                                    true,
 509                                    window,
 510                                    cx,
 511                                );
 512                            })
 513                            .ok();
 514                    })
 515                },
 516            )
 517        });
 518    })
 519    .detach();
 520}
 521
 522fn lane_center_x(bounds: Bounds<Pixels>, lane: f32, horizontal_scroll_offset: Pixels) -> Pixels {
 523    bounds.origin.x + LEFT_PADDING + lane * LANE_WIDTH + LANE_WIDTH / 2.0 - horizontal_scroll_offset
 524}
 525
 526fn to_row_center(
 527    to_row: usize,
 528    row_height: Pixels,
 529    scroll_offset: Pixels,
 530    bounds: Bounds<Pixels>,
 531) -> Pixels {
 532    bounds.origin.y + to_row as f32 * row_height + row_height / 2.0 - scroll_offset
 533}
 534
 535fn draw_commit_circle(center_x: Pixels, center_y: Pixels, color: Hsla, window: &mut Window) {
 536    let radius = COMMIT_CIRCLE_RADIUS;
 537    let stroke_width = COMMIT_CIRCLE_STROKE_WIDTH;
 538
 539    let mut builder = PathBuilder::stroke(stroke_width);
 540
 541    // Start at the rightmost point of the circle
 542    builder.move_to(point(center_x + radius, center_y));
 543
 544    // Draw the circle using two arc_to calls (top half, then bottom half)
 545    builder.arc_to(
 546        point(radius, radius),
 547        px(0.),
 548        false,
 549        true,
 550        point(center_x - radius, center_y),
 551    );
 552    builder.arc_to(
 553        point(radius, radius),
 554        px(0.),
 555        false,
 556        true,
 557        point(center_x + radius, center_y),
 558    );
 559    builder.close();
 560
 561    if let Ok(path) = builder.build() {
 562        window.paint_path(path, color);
 563    }
 564}
 565
 566pub struct GitGraph {
 567    focus_handle: FocusHandle,
 568    graph_data: GraphData,
 569    project: Entity<Project>,
 570    context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
 571    row_height: Pixels,
 572    table_interaction_state: Entity<TableInteractionState>,
 573    horizontal_scroll_offset: Pixels,
 574    graph_viewport_width: Pixels,
 575    selected_entry_idx: Option<usize>,
 576    log_source: LogSource,
 577    log_order: LogOrder,
 578    selected_commit_diff: Option<CommitDiff>,
 579    _commit_diff_task: Option<Task<()>>,
 580    _load_task: Option<Task<()>>,
 581}
 582
 583impl GitGraph {
 584    fn row_height(cx: &App) -> Pixels {
 585        let settings = ThemeSettings::get_global(cx);
 586        let font_size = settings.buffer_font_size(cx);
 587        font_size + px(12.0)
 588    }
 589
 590    fn graph_content_width(&self) -> Pixels {
 591        (LANE_WIDTH * self.graph_data.max_lanes.min(8) as f32) + LEFT_PADDING * 2.0
 592    }
 593
 594    pub fn new(project: Entity<Project>, window: &mut Window, cx: &mut Context<Self>) -> Self {
 595        let focus_handle = cx.focus_handle();
 596        cx.on_focus(&focus_handle, window, |_, _, cx| cx.notify())
 597            .detach();
 598
 599        let git_store = project.read(cx).git_store().clone();
 600        let accent_colors = cx.theme().accents();
 601        let mut graph = GraphData::new(accent_colors_count(accent_colors));
 602        let log_source = LogSource::default();
 603        let log_order = LogOrder::default();
 604
 605        cx.subscribe(&git_store, |this, _, event, cx| match event {
 606            GitStoreEvent::RepositoryUpdated(_, repo_event, is_active) => {
 607                if *is_active {
 608                    if let Some(repository) = this.project.read(cx).active_repository(cx) {
 609                        this.on_repository_event(repository, repo_event, cx);
 610                    }
 611                }
 612            }
 613            GitStoreEvent::ActiveRepositoryChanged(_) => {
 614                this.graph_data.clear();
 615                cx.notify();
 616            }
 617            _ => {}
 618        })
 619        .detach();
 620
 621        if let Some(repository) = project.read(cx).active_repository(cx) {
 622            repository.update(cx, |repository, cx| {
 623                // This won't overlap with loading commits from the repository because
 624                // we either have all commits or commits loaded in chunks and loading commits
 625                // from the repository event is always adding the last chunk of commits.
 626                let commits =
 627                    repository.graph_data(log_source.clone(), log_order, 0..usize::MAX, cx);
 628                graph.add_commits(commits);
 629            });
 630        }
 631
 632        let table_interaction_state = cx.new(|cx| TableInteractionState::new(cx));
 633        let mut row_height = Self::row_height(cx);
 634
 635        cx.observe_global_in::<settings::SettingsStore>(window, move |this, _window, cx| {
 636            let new_row_height = Self::row_height(cx);
 637            if new_row_height != row_height {
 638                this.row_height = new_row_height;
 639                this.table_interaction_state.update(cx, |state, _cx| {
 640                    state.scroll_handle.0.borrow_mut().last_item_size = None;
 641                });
 642                row_height = new_row_height;
 643            }
 644            cx.notify();
 645        })
 646        .detach();
 647
 648        GitGraph {
 649            focus_handle,
 650            project,
 651            graph_data: graph,
 652            _load_task: None,
 653            _commit_diff_task: None,
 654            context_menu: None,
 655            row_height,
 656            table_interaction_state,
 657            horizontal_scroll_offset: px(0.),
 658            graph_viewport_width: px(88.),
 659            selected_entry_idx: None,
 660            selected_commit_diff: None,
 661            log_source,
 662            log_order,
 663        }
 664    }
 665
 666    fn on_repository_event(
 667        &mut self,
 668        repository: Entity<Repository>,
 669        event: &RepositoryEvent,
 670        cx: &mut Context<Self>,
 671    ) {
 672        match event {
 673            RepositoryEvent::GitGraphCountUpdated(_, commit_count) => {
 674                let old_count = self.graph_data.commits.len();
 675
 676                repository.update(cx, |repository, cx| {
 677                    let commits = repository.graph_data(
 678                        self.log_source.clone(),
 679                        self.log_order,
 680                        old_count..*commit_count,
 681                        cx,
 682                    );
 683                    self.graph_data.add_commits(commits);
 684                });
 685
 686                self.graph_data.max_commit_count = AllCommitCount::Loaded(*commit_count);
 687            }
 688            RepositoryEvent::BranchChanged => {
 689                self.graph_data.clear();
 690                cx.notify();
 691            }
 692            _ => {}
 693        }
 694
 695        cx.notify();
 696    }
 697
 698    fn render_badge(&self, name: &SharedString, accent_color: gpui::Hsla) -> impl IntoElement {
 699        div()
 700            .px_1p5()
 701            .py_0p5()
 702            .h(self.row_height - px(4.0))
 703            .flex()
 704            .items_center()
 705            .justify_center()
 706            .rounded_md()
 707            .bg(accent_color.opacity(0.18))
 708            .border_1()
 709            .border_color(accent_color.opacity(0.55))
 710            .child(
 711                Label::new(name.clone())
 712                    .size(LabelSize::Small)
 713                    .color(Color::Default)
 714                    .single_line(),
 715            )
 716    }
 717
 718    fn render_table_rows(
 719        &mut self,
 720        range: Range<usize>,
 721        _window: &mut Window,
 722        cx: &mut Context<Self>,
 723    ) -> Vec<Vec<AnyElement>> {
 724        let repository = self
 725            .project
 726            .read_with(cx, |project, cx| project.active_repository(cx));
 727
 728        let row_height = self.row_height;
 729
 730        // We fetch data outside the visible viewport to avoid loading entries when
 731        // users scroll through the git graph
 732        if let Some(repository) = repository.as_ref() {
 733            const FETCH_RANGE: usize = 100;
 734            repository.update(cx, |repository, cx| {
 735                self.graph_data.commits[range.start.saturating_sub(FETCH_RANGE)
 736                    ..(range.end + FETCH_RANGE)
 737                        .min(self.graph_data.commits.len().saturating_sub(1))]
 738                    .iter()
 739                    .for_each(|commit| {
 740                        repository.fetch_commit_data(commit.data.sha, cx);
 741                    });
 742            });
 743        }
 744
 745        range
 746            .map(|idx| {
 747                let Some((commit, repository)) =
 748                    self.graph_data.commits.get(idx).zip(repository.as_ref())
 749                else {
 750                    return vec![
 751                        div().h(row_height).into_any_element(),
 752                        div().h(row_height).into_any_element(),
 753                        div().h(row_height).into_any_element(),
 754                        div().h(row_height).into_any_element(),
 755                    ];
 756                };
 757
 758                let data = repository.update(cx, |repository, cx| {
 759                    repository.fetch_commit_data(commit.data.sha, cx).clone()
 760                });
 761
 762                let short_sha = commit.data.sha.display_short();
 763                let mut formatted_time = String::new();
 764                let subject;
 765                let author_name;
 766
 767                if let CommitDataState::Loaded(data) = data {
 768                    subject = data.subject.clone();
 769                    author_name = data.author_name.clone();
 770                    formatted_time = format_timestamp(data.commit_timestamp);
 771                } else {
 772                    subject = "Loading...".into();
 773                    author_name = "".into();
 774                }
 775
 776                let accent_colors = cx.theme().accents();
 777                let accent_color = accent_colors
 778                    .0
 779                    .get(commit.color_idx)
 780                    .copied()
 781                    .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
 782                let is_selected = self.selected_entry_idx == Some(idx);
 783                let text_color = if is_selected {
 784                    Color::Default
 785                } else {
 786                    Color::Muted
 787                };
 788
 789                vec![
 790                    div()
 791                        .id(ElementId::NamedInteger("commit-subject".into(), idx as u64))
 792                        .overflow_hidden()
 793                        .tooltip(Tooltip::text(subject.clone()))
 794                        .child(
 795                            h_flex()
 796                                .gap_1()
 797                                .items_center()
 798                                .overflow_hidden()
 799                                .children((!commit.data.ref_names.is_empty()).then(|| {
 800                                    h_flex().flex_shrink().gap_2().items_center().children(
 801                                        commit
 802                                            .data
 803                                            .ref_names
 804                                            .iter()
 805                                            .map(|name| self.render_badge(name, accent_color)),
 806                                    )
 807                                }))
 808                                .child(
 809                                    Label::new(subject)
 810                                        .color(text_color)
 811                                        .truncate()
 812                                        .single_line(),
 813                                ),
 814                        )
 815                        .into_any_element(),
 816                    Label::new(formatted_time)
 817                        .color(text_color)
 818                        .single_line()
 819                        .into_any_element(),
 820                    Label::new(author_name)
 821                        .color(text_color)
 822                        .single_line()
 823                        .into_any_element(),
 824                    Label::new(short_sha)
 825                        .color(text_color)
 826                        .single_line()
 827                        .into_any_element(),
 828                ]
 829            })
 830            .collect()
 831    }
 832
 833    fn select_entry(&mut self, idx: usize, cx: &mut Context<Self>) {
 834        if self.selected_entry_idx == Some(idx) {
 835            return;
 836        }
 837
 838        self.selected_entry_idx = Some(idx);
 839        self.selected_commit_diff = None;
 840
 841        let Some(commit) = self.graph_data.commits.get(idx) else {
 842            return;
 843        };
 844
 845        let sha = commit.data.sha.to_string();
 846        let repository = self
 847            .project
 848            .read_with(cx, |project, cx| project.active_repository(cx));
 849
 850        let Some(repository) = repository else {
 851            return;
 852        };
 853
 854        let diff_receiver = repository.update(cx, |repo, _| repo.load_commit_diff(sha));
 855
 856        self._commit_diff_task = Some(cx.spawn(async move |this, cx| {
 857            if let Ok(Ok(diff)) = diff_receiver.await {
 858                this.update(cx, |this, cx| {
 859                    this.selected_commit_diff = Some(diff);
 860                    cx.notify();
 861                })
 862                .ok();
 863            }
 864        }));
 865
 866        cx.notify();
 867    }
 868
 869    fn get_remote(
 870        &self,
 871        repository: &Repository,
 872        _window: &mut Window,
 873        cx: &mut App,
 874    ) -> Option<GitRemote> {
 875        let remote_url = repository.default_remote_url()?;
 876        let provider_registry = GitHostingProviderRegistry::default_global(cx);
 877        let (provider, parsed) = parse_git_remote_url(provider_registry, &remote_url)?;
 878        Some(GitRemote {
 879            host: provider,
 880            owner: parsed.owner.into(),
 881            repo: parsed.repo.into(),
 882        })
 883    }
 884
 885    fn render_commit_detail_panel(
 886        &self,
 887        window: &mut Window,
 888        cx: &mut Context<Self>,
 889    ) -> impl IntoElement {
 890        let Some(selected_idx) = self.selected_entry_idx else {
 891            return div().into_any_element();
 892        };
 893
 894        let Some(commit_entry) = self.graph_data.commits.get(selected_idx) else {
 895            return div().into_any_element();
 896        };
 897
 898        let repository = self
 899            .project
 900            .read_with(cx, |project, cx| project.active_repository(cx));
 901
 902        let Some(repository) = repository else {
 903            return div().into_any_element();
 904        };
 905
 906        let data = repository.update(cx, |repository, cx| {
 907            repository
 908                .fetch_commit_data(commit_entry.data.sha, cx)
 909                .clone()
 910        });
 911
 912        let full_sha: SharedString = commit_entry.data.sha.to_string().into();
 913        let truncated_sha: SharedString = {
 914            let sha_str = full_sha.as_ref();
 915            if sha_str.len() > 24 {
 916                format!("{}...", &sha_str[..24]).into()
 917            } else {
 918                full_sha.clone()
 919            }
 920        };
 921        let ref_names = commit_entry.data.ref_names.clone();
 922        let accent_colors = cx.theme().accents();
 923        let accent_color = accent_colors
 924            .0
 925            .get(commit_entry.color_idx)
 926            .copied()
 927            .unwrap_or_else(|| accent_colors.0.first().copied().unwrap_or_default());
 928
 929        let (author_name, author_email, commit_timestamp, subject) = match &data {
 930            CommitDataState::Loaded(data) => (
 931                data.author_name.clone(),
 932                data.author_email.clone(),
 933                Some(data.commit_timestamp),
 934                data.subject.clone(),
 935            ),
 936            CommitDataState::Loading => ("Loading...".into(), "".into(), None, "Loading...".into()),
 937        };
 938
 939        let date_string = commit_timestamp
 940            .and_then(|ts| OffsetDateTime::from_unix_timestamp(ts).ok())
 941            .map(|datetime| {
 942                let local_offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
 943                let local_datetime = datetime.to_offset(local_offset);
 944                let format =
 945                    time::format_description::parse("[month repr:short] [day], [year]").ok();
 946                format
 947                    .and_then(|f| local_datetime.format(&f).ok())
 948                    .unwrap_or_default()
 949            })
 950            .unwrap_or_default();
 951
 952        let remote = repository.update(cx, |repo, cx| self.get_remote(repo, window, cx));
 953
 954        let avatar = {
 955            let avatar = CommitAvatar::new(&full_sha, remote.as_ref());
 956            v_flex()
 957                .w(px(64.))
 958                .h(px(64.))
 959                .border_1()
 960                .border_color(cx.theme().colors().border)
 961                .rounded_full()
 962                .justify_center()
 963                .items_center()
 964                .child(
 965                    avatar
 966                        .avatar(window, cx)
 967                        .map(|a| a.size(px(64.)).into_any_element())
 968                        .unwrap_or_else(|| {
 969                            Icon::new(IconName::Person)
 970                                .color(Color::Muted)
 971                                .size(IconSize::XLarge)
 972                                .into_any_element()
 973                        }),
 974                )
 975        };
 976
 977        let changed_files_count = self
 978            .selected_commit_diff
 979            .as_ref()
 980            .map(|diff| diff.files.len())
 981            .unwrap_or(0);
 982
 983        v_flex()
 984            .w(px(300.))
 985            .h_full()
 986            .border_l_1()
 987            .border_color(cx.theme().colors().border)
 988            .bg(cx.theme().colors().surface_background)
 989            .child(
 990                v_flex()
 991                    .p_3()
 992                    .gap_3()
 993                    .child(
 994                        h_flex().justify_between().child(avatar).child(
 995                            IconButton::new("close-detail", IconName::Close)
 996                                .icon_size(IconSize::Small)
 997                                .on_click(cx.listener(move |this, _, _, cx| {
 998                                    this.selected_entry_idx = None;
 999                                    this.selected_commit_diff = None;
1000                                    this._commit_diff_task = None;
1001                                    cx.notify();
1002                                })),
1003                        ),
1004                    )
1005                    .child(
1006                        v_flex()
1007                            .gap_0p5()
1008                            .child(Label::new(author_name.clone()).weight(FontWeight::SEMIBOLD))
1009                            .child(
1010                                Label::new(date_string)
1011                                    .color(Color::Muted)
1012                                    .size(LabelSize::Small),
1013                            ),
1014                    )
1015                    .children((!ref_names.is_empty()).then(|| {
1016                        h_flex().gap_1().flex_wrap().children(
1017                            ref_names
1018                                .iter()
1019                                .map(|name| self.render_badge(name, accent_color)),
1020                        )
1021                    }))
1022                    .child(
1023                        v_flex()
1024                            .gap_1p5()
1025                            .child(
1026                                h_flex()
1027                                    .gap_1()
1028                                    .child(
1029                                        Icon::new(IconName::Person)
1030                                            .size(IconSize::Small)
1031                                            .color(Color::Muted),
1032                                    )
1033                                    .child(
1034                                        Label::new(author_name)
1035                                            .size(LabelSize::Small)
1036                                            .color(Color::Muted),
1037                                    )
1038                                    .when(!author_email.is_empty(), |this| {
1039                                        this.child(
1040                                            Label::new(format!("<{}>", author_email))
1041                                                .size(LabelSize::Small)
1042                                                .color(Color::Ignored),
1043                                        )
1044                                    }),
1045                            )
1046                            .child(
1047                                h_flex()
1048                                    .gap_1()
1049                                    .child(
1050                                        Icon::new(IconName::Hash)
1051                                            .size(IconSize::Small)
1052                                            .color(Color::Muted),
1053                                    )
1054                                    .child({
1055                                        let copy_sha = full_sha.clone();
1056                                        Button::new("sha-button", truncated_sha)
1057                                            .style(ButtonStyle::Transparent)
1058                                            .label_size(LabelSize::Small)
1059                                            .color(Color::Muted)
1060                                            .tooltip(Tooltip::text(format!(
1061                                                "Copy SHA: {}",
1062                                                copy_sha
1063                                            )))
1064                                            .on_click(move |_, _, cx| {
1065                                                cx.write_to_clipboard(ClipboardItem::new_string(
1066                                                    copy_sha.to_string(),
1067                                                ));
1068                                            })
1069                                    }),
1070                            )
1071                            .when_some(remote.clone(), |this, remote| {
1072                                let provider_name = remote.host.name();
1073                                let icon = match provider_name.as_str() {
1074                                    "GitHub" => IconName::Github,
1075                                    _ => IconName::Link,
1076                                };
1077                                let parsed_remote = ParsedGitRemote {
1078                                    owner: remote.owner.as_ref().into(),
1079                                    repo: remote.repo.as_ref().into(),
1080                                };
1081                                let params = BuildCommitPermalinkParams {
1082                                    sha: full_sha.as_ref(),
1083                                };
1084                                let url = remote
1085                                    .host
1086                                    .build_commit_permalink(&parsed_remote, params)
1087                                    .to_string();
1088                                this.child(
1089                                    h_flex()
1090                                        .gap_1()
1091                                        .child(
1092                                            Icon::new(icon)
1093                                                .size(IconSize::Small)
1094                                                .color(Color::Muted),
1095                                        )
1096                                        .child(
1097                                            Button::new(
1098                                                "view-on-provider",
1099                                                format!("View on {}", provider_name),
1100                                            )
1101                                            .style(ButtonStyle::Transparent)
1102                                            .label_size(LabelSize::Small)
1103                                            .color(Color::Muted)
1104                                            .on_click(
1105                                                move |_, _, cx| {
1106                                                    cx.open_url(&url);
1107                                                },
1108                                            ),
1109                                        ),
1110                                )
1111                            }),
1112                    ),
1113            )
1114            .child(
1115                div()
1116                    .border_t_1()
1117                    .border_color(cx.theme().colors().border)
1118                    .p_3()
1119                    .min_w_0()
1120                    .child(
1121                        v_flex()
1122                            .gap_2()
1123                            .child(Label::new(subject).weight(FontWeight::MEDIUM)),
1124                    ),
1125            )
1126            .child(
1127                div()
1128                    .flex_1()
1129                    .overflow_hidden()
1130                    .border_t_1()
1131                    .border_color(cx.theme().colors().border)
1132                    .p_3()
1133                    .child(
1134                        v_flex()
1135                            .gap_2()
1136                            .child(
1137                                Label::new(format!("{} Changed Files", changed_files_count))
1138                                    .size(LabelSize::Small)
1139                                    .color(Color::Muted),
1140                            )
1141                            .children(self.selected_commit_diff.as_ref().map(|diff| {
1142                                v_flex().gap_1().children(diff.files.iter().map(|file| {
1143                                    let file_name: String = file
1144                                        .path
1145                                        .file_name()
1146                                        .map(|n| n.to_string())
1147                                        .unwrap_or_default();
1148                                    let dir_path: String = file
1149                                        .path
1150                                        .parent()
1151                                        .map(|p| p.as_unix_str().to_string())
1152                                        .unwrap_or_default();
1153
1154                                    h_flex()
1155                                        .gap_1()
1156                                        .overflow_hidden()
1157                                        .child(
1158                                            Icon::new(IconName::File)
1159                                                .size(IconSize::Small)
1160                                                .color(Color::Accent),
1161                                        )
1162                                        .child(
1163                                            Label::new(file_name)
1164                                                .size(LabelSize::Small)
1165                                                .single_line(),
1166                                        )
1167                                        .when(!dir_path.is_empty(), |this| {
1168                                            this.child(
1169                                                Label::new(dir_path)
1170                                                    .size(LabelSize::Small)
1171                                                    .color(Color::Muted)
1172                                                    .single_line(),
1173                                            )
1174                                        })
1175                                }))
1176                            })),
1177                    ),
1178            )
1179            .into_any_element()
1180    }
1181
1182    pub fn render_graph(&self, cx: &mut Context<GitGraph>) -> impl IntoElement {
1183        let row_height = self.row_height;
1184        let table_state = self.table_interaction_state.read(cx);
1185        let viewport_height = table_state
1186            .scroll_handle
1187            .0
1188            .borrow()
1189            .last_item_size
1190            .map(|size| size.item.height)
1191            .unwrap_or(px(600.0));
1192        let loaded_commit_count = self.graph_data.commits.len();
1193
1194        let content_height = row_height * loaded_commit_count;
1195        let max_scroll = (content_height - viewport_height).max(px(0.));
1196        let scroll_offset_y = (-table_state.scroll_offset().y).clamp(px(0.), max_scroll);
1197
1198        let first_visible_row = (scroll_offset_y / row_height).floor() as usize;
1199        let vertical_scroll_offset = scroll_offset_y - (first_visible_row as f32 * row_height);
1200        let horizontal_scroll_offset = self.horizontal_scroll_offset;
1201
1202        let max_lanes = self.graph_data.max_lanes.max(6);
1203        let graph_width = LANE_WIDTH * max_lanes as f32 + LEFT_PADDING * 2.0;
1204        let last_visible_row =
1205            first_visible_row + (viewport_height / row_height).ceil() as usize + 1;
1206
1207        let viewport_range = first_visible_row.min(loaded_commit_count.saturating_sub(1))
1208            ..(last_visible_row).min(loaded_commit_count);
1209        let rows = self.graph_data.commits[viewport_range.clone()].to_vec();
1210        let commit_lines: Vec<_> = self
1211            .graph_data
1212            .lines
1213            .iter()
1214            .filter(|line| {
1215                line.full_interval.start <= viewport_range.end
1216                    && line.full_interval.end >= viewport_range.start
1217            })
1218            .cloned()
1219            .collect();
1220
1221        let mut lines: BTreeMap<usize, Vec<_>> = BTreeMap::new();
1222
1223        gpui::canvas(
1224            move |_bounds, _window, _cx| {},
1225            move |bounds: Bounds<Pixels>, _: (), window: &mut Window, cx: &mut App| {
1226                window.paint_layer(bounds, |window| {
1227                    let accent_colors = cx.theme().accents();
1228
1229                    for (row_idx, row) in rows.into_iter().enumerate() {
1230                        let row_color = accent_colors.color_for_index(row.color_idx as u32);
1231                        let row_y_center =
1232                            bounds.origin.y + row_idx as f32 * row_height + row_height / 2.0
1233                                - vertical_scroll_offset;
1234
1235                        let commit_x =
1236                            lane_center_x(bounds, row.lane as f32, horizontal_scroll_offset);
1237
1238                        draw_commit_circle(commit_x, row_y_center, row_color, window);
1239                    }
1240
1241                    for line in commit_lines {
1242                        let Some((start_segment_idx, start_column)) =
1243                            line.get_first_visible_segment_idx(first_visible_row)
1244                        else {
1245                            continue;
1246                        };
1247
1248                        let line_x =
1249                            lane_center_x(bounds, start_column as f32, horizontal_scroll_offset);
1250
1251                        let start_row = line.full_interval.start as i32 - first_visible_row as i32;
1252
1253                        let from_y =
1254                            bounds.origin.y + start_row as f32 * row_height + row_height / 2.0
1255                                - vertical_scroll_offset
1256                                + COMMIT_CIRCLE_RADIUS;
1257
1258                        let mut current_row = from_y;
1259                        let mut current_column = line_x;
1260
1261                        let mut builder = PathBuilder::stroke(LINE_WIDTH);
1262                        builder.move_to(point(line_x, from_y));
1263
1264                        let segments = &line.segments[start_segment_idx..];
1265
1266                        for (segment_idx, segment) in segments.iter().enumerate() {
1267                            let is_last = segment_idx + 1 == segments.len();
1268
1269                            match segment {
1270                                CommitLineSegment::Straight { to_row } => {
1271                                    let mut dest_row = to_row_center(
1272                                        to_row - first_visible_row,
1273                                        row_height,
1274                                        vertical_scroll_offset,
1275                                        bounds,
1276                                    );
1277                                    if is_last {
1278                                        dest_row -= COMMIT_CIRCLE_RADIUS;
1279                                    }
1280
1281                                    let dest_point = point(current_column, dest_row);
1282
1283                                    current_row = dest_point.y;
1284                                    builder.line_to(dest_point);
1285                                    builder.move_to(dest_point);
1286                                }
1287                                CommitLineSegment::Curve {
1288                                    to_column,
1289                                    on_row,
1290                                    curve_kind,
1291                                } => {
1292                                    let mut to_column = lane_center_x(
1293                                        bounds,
1294                                        *to_column as f32,
1295                                        horizontal_scroll_offset,
1296                                    );
1297
1298                                    let mut to_row = to_row_center(
1299                                        *on_row - first_visible_row,
1300                                        row_height,
1301                                        vertical_scroll_offset,
1302                                        bounds,
1303                                    );
1304
1305                                    // This means that this branch was a checkout
1306                                    let going_right = to_column > current_column;
1307                                    let column_shift = if going_right {
1308                                        COMMIT_CIRCLE_RADIUS + COMMIT_CIRCLE_STROKE_WIDTH
1309                                    } else {
1310                                        -COMMIT_CIRCLE_RADIUS - COMMIT_CIRCLE_STROKE_WIDTH
1311                                    };
1312
1313                                    let control = match curve_kind {
1314                                        CurveKind::Checkout => {
1315                                            if is_last {
1316                                                to_column -= column_shift;
1317                                            }
1318                                            builder.move_to(point(current_column, current_row));
1319                                            point(current_column, to_row)
1320                                        }
1321                                        CurveKind::Merge => {
1322                                            if is_last {
1323                                                to_row -= COMMIT_CIRCLE_RADIUS;
1324                                            }
1325                                            builder.move_to(point(
1326                                                current_column + column_shift,
1327                                                current_row - COMMIT_CIRCLE_RADIUS,
1328                                            ));
1329                                            point(to_column, current_row)
1330                                        }
1331                                    };
1332
1333                                    match curve_kind {
1334                                        CurveKind::Checkout
1335                                            if (to_row - current_row).abs() > row_height =>
1336                                        {
1337                                            let start_curve =
1338                                                point(current_column, current_row + row_height);
1339                                            builder.line_to(start_curve);
1340                                            builder.move_to(start_curve);
1341                                        }
1342                                        CurveKind::Merge
1343                                            if (to_column - current_column).abs() > LANE_WIDTH =>
1344                                        {
1345                                            let column_shift =
1346                                                if going_right { LANE_WIDTH } else { -LANE_WIDTH };
1347
1348                                            let start_curve = point(
1349                                                current_column + column_shift,
1350                                                current_row - COMMIT_CIRCLE_RADIUS,
1351                                            );
1352
1353                                            builder.line_to(start_curve);
1354                                            builder.move_to(start_curve);
1355                                        }
1356                                        _ => {}
1357                                    };
1358
1359                                    builder.curve_to(point(to_column, to_row), control);
1360                                    current_row = to_row;
1361                                    current_column = to_column;
1362                                    builder.move_to(point(current_column, current_row));
1363                                }
1364                            }
1365                        }
1366
1367                        builder.close();
1368                        lines.entry(line.color_idx).or_default().push(builder);
1369                    }
1370
1371                    for (color_idx, builders) in lines {
1372                        let line_color = accent_colors.color_for_index(color_idx as u32);
1373
1374                        for builder in builders {
1375                            if let Ok(path) = builder.build() {
1376                                // we paint each color on it's own layer to stop overlapping lines
1377                                // of different colors changing the color of a line
1378                                window.paint_layer(bounds, |window| {
1379                                    window.paint_path(path, line_color);
1380                                });
1381                            }
1382                        }
1383                    }
1384                })
1385            },
1386        )
1387        .w(graph_width)
1388        .h_full()
1389    }
1390
1391    fn handle_graph_scroll(
1392        &mut self,
1393        event: &ScrollWheelEvent,
1394        window: &mut Window,
1395        cx: &mut Context<Self>,
1396    ) {
1397        let line_height = window.line_height();
1398        let delta = event.delta.pixel_delta(line_height);
1399
1400        let table_state = self.table_interaction_state.read(cx);
1401        let current_offset = table_state.scroll_offset();
1402
1403        let viewport_height = table_state.scroll_handle.viewport().size.height;
1404
1405        let commit_count = match self.graph_data.max_commit_count {
1406            AllCommitCount::Loaded(count) => count,
1407            AllCommitCount::NotLoaded => self.graph_data.commits.len(),
1408        };
1409        let content_height = self.row_height * commit_count;
1410        let max_vertical_scroll = (viewport_height - content_height).min(px(0.));
1411
1412        let new_y = (current_offset.y + delta.y).clamp(max_vertical_scroll, px(0.));
1413        let new_offset = Point::new(current_offset.x, new_y);
1414
1415        let max_lanes = self.graph_data.max_lanes.max(1);
1416        let graph_content_width = LANE_WIDTH * max_lanes as f32 + LEFT_PADDING * 2.0;
1417        let max_horizontal_scroll = (graph_content_width - self.graph_viewport_width).max(px(0.));
1418
1419        let new_horizontal_offset =
1420            (self.horizontal_scroll_offset - delta.x).clamp(px(0.), max_horizontal_scroll);
1421
1422        let vertical_changed = new_offset != current_offset;
1423        let horizontal_changed = new_horizontal_offset != self.horizontal_scroll_offset;
1424
1425        if vertical_changed {
1426            table_state.set_scroll_offset(new_offset);
1427        }
1428
1429        if horizontal_changed {
1430            self.horizontal_scroll_offset = new_horizontal_offset;
1431        }
1432
1433        if vertical_changed || horizontal_changed {
1434            cx.notify();
1435        }
1436    }
1437}
1438
1439impl Render for GitGraph {
1440    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1441        let description_width_fraction = 0.72;
1442        let date_width_fraction = 0.12;
1443        let author_width_fraction = 0.10;
1444        let commit_width_fraction = 0.06;
1445
1446        let commit_count = match self.graph_data.max_commit_count {
1447            AllCommitCount::Loaded(count) => count,
1448            AllCommitCount::NotLoaded => {
1449                self.project.update(cx, |project, cx| {
1450                    if let Some(repository) = project.active_repository(cx) {
1451                        repository.update(cx, |repository, cx| {
1452                            // Start loading the graph data if we haven't started already
1453                            repository.graph_data(
1454                                self.log_source.clone(),
1455                                self.log_order,
1456                                0..0,
1457                                cx,
1458                            );
1459                        })
1460                    }
1461                });
1462
1463                self.graph_data.commits.len()
1464            }
1465        };
1466
1467        let content = if self.graph_data.commits.is_empty() {
1468            let message = "No commits found";
1469            div()
1470                .size_full()
1471                .flex()
1472                .items_center()
1473                .justify_center()
1474                .child(Label::new(message).color(Color::Muted))
1475        } else {
1476            div()
1477                .size_full()
1478                .flex()
1479                .flex_row()
1480                .child(
1481                    div()
1482                        .w(self.graph_content_width())
1483                        .h_full()
1484                        .flex()
1485                        .flex_col()
1486                        .child(
1487                            div()
1488                                .p_2()
1489                                .border_b_1()
1490                                .border_color(cx.theme().colors().border)
1491                                .child(Label::new("Graph").color(Color::Muted)),
1492                        )
1493                        .child(
1494                            div()
1495                                .id("graph-canvas")
1496                                .flex_1()
1497                                .overflow_hidden()
1498                                .child(self.render_graph(cx))
1499                                .on_scroll_wheel(cx.listener(Self::handle_graph_scroll)),
1500                        ),
1501                )
1502                .child({
1503                    let row_height = self.row_height;
1504                    let selected_entry_idx = self.selected_entry_idx;
1505                    let weak_self = cx.weak_entity();
1506                    div().flex_1().size_full().child(
1507                        Table::new(4)
1508                            .interactable(&self.table_interaction_state)
1509                            .hide_row_borders()
1510                            .header(vec![
1511                                Label::new("Description")
1512                                    .color(Color::Muted)
1513                                    .into_any_element(),
1514                                Label::new("Date").color(Color::Muted).into_any_element(),
1515                                Label::new("Author").color(Color::Muted).into_any_element(),
1516                                Label::new("Commit").color(Color::Muted).into_any_element(),
1517                            ])
1518                            .column_widths(
1519                                [
1520                                    DefiniteLength::Fraction(description_width_fraction),
1521                                    DefiniteLength::Fraction(date_width_fraction),
1522                                    DefiniteLength::Fraction(author_width_fraction),
1523                                    DefiniteLength::Fraction(commit_width_fraction),
1524                                ]
1525                                .to_vec(),
1526                            )
1527                            .map_row(move |(index, row), _window, cx| {
1528                                let is_selected = selected_entry_idx == Some(index);
1529                                let weak = weak_self.clone();
1530                                row.h(row_height)
1531                                    .when(is_selected, |row| {
1532                                        row.bg(cx.theme().colors().element_selected)
1533                                    })
1534                                    .on_click(move |_, _, cx| {
1535                                        weak.update(cx, |this, cx| {
1536                                            this.select_entry(index, cx);
1537                                        })
1538                                        .ok();
1539                                    })
1540                                    .into_any_element()
1541                            })
1542                            .uniform_list(
1543                                "git-graph-commits",
1544                                commit_count,
1545                                cx.processor(Self::render_table_rows),
1546                            ),
1547                    )
1548                })
1549                .when(self.selected_entry_idx.is_some(), |this| {
1550                    this.child(self.render_commit_detail_panel(window, cx))
1551                })
1552        };
1553
1554        div()
1555            .size_full()
1556            .bg(cx.theme().colors().editor_background)
1557            .key_context("GitGraph")
1558            .track_focus(&self.focus_handle)
1559            .child(content)
1560            .children(self.context_menu.as_ref().map(|(menu, position, _)| {
1561                deferred(
1562                    anchored()
1563                        .position(*position)
1564                        .anchor(Corner::TopLeft)
1565                        .child(menu.clone()),
1566                )
1567                .with_priority(1)
1568            }))
1569    }
1570}
1571
1572impl EventEmitter<ItemEvent> for GitGraph {}
1573
1574impl Focusable for GitGraph {
1575    fn focus_handle(&self, _cx: &App) -> FocusHandle {
1576        self.focus_handle.clone()
1577    }
1578}
1579
1580impl Item for GitGraph {
1581    type Event = ItemEvent;
1582
1583    fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
1584        "Git Graph".into()
1585    }
1586
1587    fn show_toolbar(&self) -> bool {
1588        false
1589    }
1590
1591    fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
1592        f(*event)
1593    }
1594}
1595
1596impl SerializableItem for GitGraph {
1597    fn serialized_item_kind() -> &'static str {
1598        "GitGraph"
1599    }
1600
1601    fn cleanup(
1602        workspace_id: workspace::WorkspaceId,
1603        alive_items: Vec<workspace::ItemId>,
1604        _window: &mut Window,
1605        cx: &mut App,
1606    ) -> Task<gpui::Result<()>> {
1607        workspace::delete_unloaded_items(
1608            alive_items,
1609            workspace_id,
1610            "git_graphs",
1611            &persistence::GIT_GRAPHS,
1612            cx,
1613        )
1614    }
1615
1616    fn deserialize(
1617        project: Entity<Project>,
1618        _: WeakEntity<Workspace>,
1619        workspace_id: workspace::WorkspaceId,
1620        item_id: workspace::ItemId,
1621        window: &mut Window,
1622        cx: &mut App,
1623    ) -> Task<gpui::Result<Entity<Self>>> {
1624        if persistence::GIT_GRAPHS
1625            .get_git_graph(item_id, workspace_id)
1626            .ok()
1627            .is_some_and(|is_open| is_open)
1628        {
1629            let git_graph = cx.new(|cx| GitGraph::new(project, window, cx));
1630            Task::ready(Ok(git_graph))
1631        } else {
1632            Task::ready(Err(anyhow::anyhow!("No git graph to deserialize")))
1633        }
1634    }
1635
1636    fn serialize(
1637        &mut self,
1638        workspace: &mut Workspace,
1639        item_id: workspace::ItemId,
1640        _closing: bool,
1641        _window: &mut Window,
1642        cx: &mut Context<Self>,
1643    ) -> Option<Task<gpui::Result<()>>> {
1644        let workspace_id = workspace.database_id()?;
1645        Some(cx.background_spawn(async move {
1646            persistence::GIT_GRAPHS
1647                .save_git_graph(item_id, workspace_id, true)
1648                .await
1649        }))
1650    }
1651
1652    fn should_serialize(&self, event: &Self::Event) -> bool {
1653        event == &ItemEvent::UpdateTab
1654    }
1655}
1656
1657mod persistence {
1658    use db::{
1659        query,
1660        sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection},
1661        sqlez_macros::sql,
1662    };
1663    use workspace::WorkspaceDb;
1664
1665    pub struct GitGraphsDb(ThreadSafeConnection);
1666
1667    impl Domain for GitGraphsDb {
1668        const NAME: &str = stringify!(GitGraphsDb);
1669
1670        const MIGRATIONS: &[&str] = (&[sql!(
1671            CREATE TABLE git_graphs (
1672                workspace_id INTEGER,
1673                item_id INTEGER UNIQUE,
1674                is_open INTEGER DEFAULT FALSE,
1675
1676                PRIMARY KEY(workspace_id, item_id),
1677                FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
1678                ON DELETE CASCADE
1679            ) STRICT;
1680        )]);
1681    }
1682
1683    db::static_connection!(GIT_GRAPHS, GitGraphsDb, [WorkspaceDb]);
1684
1685    impl GitGraphsDb {
1686        query! {
1687            pub async fn save_git_graph(
1688                item_id: workspace::ItemId,
1689                workspace_id: workspace::WorkspaceId,
1690                is_open: bool
1691            ) -> Result<()> {
1692                INSERT OR REPLACE INTO git_graphs(item_id, workspace_id, is_open)
1693                VALUES (?, ?, ?)
1694            }
1695        }
1696
1697        query! {
1698            pub fn get_git_graph(
1699                item_id: workspace::ItemId,
1700                workspace_id: workspace::WorkspaceId
1701            ) -> Result<bool> {
1702                SELECT is_open
1703                FROM git_graphs
1704                WHERE item_id = ? AND workspace_id = ?
1705            }
1706        }
1707    }
1708}
1709
1710#[cfg(test)]
1711mod tests {
1712    use super::*;
1713    use anyhow::{Context, Result, bail};
1714    use collections::{HashMap, HashSet};
1715    use fs::FakeFs;
1716    use git::Oid;
1717    use git::repository::InitialGraphCommitData;
1718    use gpui::TestAppContext;
1719    use project::Project;
1720    use rand::prelude::*;
1721    use serde_json::json;
1722    use settings::SettingsStore;
1723    use smallvec::{SmallVec, smallvec};
1724    use std::path::Path;
1725    use std::sync::Arc;
1726
1727    fn init_test(cx: &mut TestAppContext) {
1728        cx.update(|cx| {
1729            let settings_store = SettingsStore::test(cx);
1730            cx.set_global(settings_store);
1731        });
1732    }
1733
1734    /// Generates a random commit DAG suitable for testing git graph rendering.
1735    ///
1736    /// The commits are ordered newest-first (like git log output), so:
1737    /// - Index 0 = most recent commit (HEAD)
1738    /// - Last index = oldest commit (root, has no parents)
1739    /// - Parents of commit at index I must have index > I
1740    ///
1741    /// When `adversarial` is true, generates complex topologies with many branches
1742    /// and octopus merges. Otherwise generates more realistic linear histories
1743    /// with occasional branches.
1744    fn generate_random_commit_dag(
1745        rng: &mut StdRng,
1746        num_commits: usize,
1747        adversarial: bool,
1748    ) -> Vec<Arc<InitialGraphCommitData>> {
1749        if num_commits == 0 {
1750            return Vec::new();
1751        }
1752
1753        let mut commits: Vec<Arc<InitialGraphCommitData>> = Vec::with_capacity(num_commits);
1754        let oids: Vec<Oid> = (0..num_commits).map(|_| Oid::random(rng)).collect();
1755
1756        for i in 0..num_commits {
1757            let sha = oids[i];
1758
1759            let parents = if i == num_commits - 1 {
1760                smallvec![]
1761            } else {
1762                generate_parents_from_oids(rng, &oids, i, num_commits, adversarial)
1763            };
1764
1765            let ref_names = if i == 0 {
1766                vec!["HEAD".into(), "main".into()]
1767            } else if adversarial && rng.random_bool(0.1) {
1768                vec![format!("branch-{}", i).into()]
1769            } else {
1770                Vec::new()
1771            };
1772
1773            commits.push(Arc::new(InitialGraphCommitData {
1774                sha,
1775                parents,
1776                ref_names,
1777            }));
1778        }
1779
1780        commits
1781    }
1782
1783    fn generate_parents_from_oids(
1784        rng: &mut StdRng,
1785        oids: &[Oid],
1786        current_idx: usize,
1787        num_commits: usize,
1788        adversarial: bool,
1789    ) -> SmallVec<[Oid; 1]> {
1790        let remaining = num_commits - current_idx - 1;
1791        if remaining == 0 {
1792            return smallvec![];
1793        }
1794
1795        if adversarial {
1796            let merge_chance = 0.4;
1797            let octopus_chance = 0.15;
1798
1799            if remaining >= 3 && rng.random_bool(octopus_chance) {
1800                let num_parents = rng.random_range(3..=remaining.min(5));
1801                let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
1802                parent_indices.shuffle(rng);
1803                parent_indices
1804                    .into_iter()
1805                    .take(num_parents)
1806                    .map(|idx| oids[idx])
1807                    .collect()
1808            } else if remaining >= 2 && rng.random_bool(merge_chance) {
1809                let mut parent_indices: Vec<usize> = (current_idx + 1..num_commits).collect();
1810                parent_indices.shuffle(rng);
1811                parent_indices
1812                    .into_iter()
1813                    .take(2)
1814                    .map(|idx| oids[idx])
1815                    .collect()
1816            } else {
1817                let parent_idx = rng.random_range(current_idx + 1..num_commits);
1818                smallvec![oids[parent_idx]]
1819            }
1820        } else {
1821            let merge_chance = 0.15;
1822            let skip_chance = 0.1;
1823
1824            if remaining >= 2 && rng.random_bool(merge_chance) {
1825                let first_parent = current_idx + 1;
1826                let second_parent = rng.random_range(current_idx + 2..num_commits);
1827                smallvec![oids[first_parent], oids[second_parent]]
1828            } else if rng.random_bool(skip_chance) && remaining >= 2 {
1829                let skip = rng.random_range(1..remaining.min(3));
1830                smallvec![oids[current_idx + 1 + skip]]
1831            } else {
1832                smallvec![oids[current_idx + 1]]
1833            }
1834        }
1835    }
1836
1837    fn build_oid_to_row_map(graph: &GraphData) -> HashMap<Oid, usize> {
1838        graph
1839            .commits
1840            .iter()
1841            .enumerate()
1842            .map(|(idx, entry)| (entry.data.sha, idx))
1843            .collect()
1844    }
1845
1846    fn verify_commit_order(
1847        graph: &GraphData,
1848        commits: &[Arc<InitialGraphCommitData>],
1849    ) -> Result<()> {
1850        if graph.commits.len() != commits.len() {
1851            bail!(
1852                "Commit count mismatch: graph has {} commits, expected {}",
1853                graph.commits.len(),
1854                commits.len()
1855            );
1856        }
1857
1858        for (idx, (graph_commit, expected_commit)) in
1859            graph.commits.iter().zip(commits.iter()).enumerate()
1860        {
1861            if graph_commit.data.sha != expected_commit.sha {
1862                bail!(
1863                    "Commit order mismatch at index {}: graph has {:?}, expected {:?}",
1864                    idx,
1865                    graph_commit.data.sha,
1866                    expected_commit.sha
1867                );
1868            }
1869        }
1870
1871        Ok(())
1872    }
1873
1874    fn verify_line_endpoints(graph: &GraphData, oid_to_row: &HashMap<Oid, usize>) -> Result<()> {
1875        for line in &graph.lines {
1876            let child_row = *oid_to_row
1877                .get(&line.child)
1878                .context("Line references non-existent child commit")?;
1879
1880            let parent_row = *oid_to_row
1881                .get(&line.parent)
1882                .context("Line references non-existent parent commit")?;
1883
1884            if child_row >= parent_row {
1885                bail!(
1886                    "child_row ({}) must be < parent_row ({})",
1887                    child_row,
1888                    parent_row
1889                );
1890            }
1891
1892            if line.full_interval.start != child_row {
1893                bail!(
1894                    "full_interval.start ({}) != child_row ({})",
1895                    line.full_interval.start,
1896                    child_row
1897                );
1898            }
1899
1900            if line.full_interval.end != parent_row {
1901                bail!(
1902                    "full_interval.end ({}) != parent_row ({})",
1903                    line.full_interval.end,
1904                    parent_row
1905                );
1906            }
1907
1908            if let Some(last_segment) = line.segments.last() {
1909                let segment_end_row = match last_segment {
1910                    CommitLineSegment::Straight { to_row } => *to_row,
1911                    CommitLineSegment::Curve { on_row, .. } => *on_row,
1912                };
1913
1914                if segment_end_row != line.full_interval.end {
1915                    bail!(
1916                        "last segment ends at row {} but full_interval.end is {}",
1917                        segment_end_row,
1918                        line.full_interval.end
1919                    );
1920                }
1921            }
1922        }
1923
1924        Ok(())
1925    }
1926
1927    fn verify_column_correctness(
1928        graph: &GraphData,
1929        oid_to_row: &HashMap<Oid, usize>,
1930    ) -> Result<()> {
1931        for line in &graph.lines {
1932            let child_row = *oid_to_row
1933                .get(&line.child)
1934                .context("Line references non-existent child commit")?;
1935
1936            let parent_row = *oid_to_row
1937                .get(&line.parent)
1938                .context("Line references non-existent parent commit")?;
1939
1940            let child_lane = graph.commits[child_row].lane;
1941            if line.child_column != child_lane {
1942                bail!(
1943                    "child_column ({}) != child's lane ({})",
1944                    line.child_column,
1945                    child_lane
1946                );
1947            }
1948
1949            let mut current_column = line.child_column;
1950            for segment in &line.segments {
1951                if let CommitLineSegment::Curve { to_column, .. } = segment {
1952                    current_column = *to_column;
1953                }
1954            }
1955
1956            let parent_lane = graph.commits[parent_row].lane;
1957            if current_column != parent_lane {
1958                bail!(
1959                    "ending column ({}) != parent's lane ({})",
1960                    current_column,
1961                    parent_lane
1962                );
1963            }
1964        }
1965
1966        Ok(())
1967    }
1968
1969    fn verify_segment_continuity(graph: &GraphData) -> Result<()> {
1970        for line in &graph.lines {
1971            if line.segments.is_empty() {
1972                bail!("Line has no segments");
1973            }
1974
1975            let mut current_row = line.full_interval.start;
1976
1977            for (idx, segment) in line.segments.iter().enumerate() {
1978                let segment_end_row = match segment {
1979                    CommitLineSegment::Straight { to_row } => *to_row,
1980                    CommitLineSegment::Curve { on_row, .. } => *on_row,
1981                };
1982
1983                if segment_end_row < current_row {
1984                    bail!(
1985                        "segment {} ends at row {} which is before current row {}",
1986                        idx,
1987                        segment_end_row,
1988                        current_row
1989                    );
1990                }
1991
1992                current_row = segment_end_row;
1993            }
1994        }
1995
1996        Ok(())
1997    }
1998
1999    fn verify_line_overlaps(graph: &GraphData) -> Result<()> {
2000        for line in &graph.lines {
2001            let child_row = line.full_interval.start;
2002
2003            let mut current_column = line.child_column;
2004            let mut current_row = child_row;
2005
2006            for segment in &line.segments {
2007                match segment {
2008                    CommitLineSegment::Straight { to_row } => {
2009                        for row in (current_row + 1)..*to_row {
2010                            if row < graph.commits.len() {
2011                                let commit_at_row = &graph.commits[row];
2012                                if commit_at_row.lane == current_column {
2013                                    bail!(
2014                                        "straight segment from row {} to {} in column {} passes through commit {:?} at row {}",
2015                                        current_row,
2016                                        to_row,
2017                                        current_column,
2018                                        commit_at_row.data.sha,
2019                                        row
2020                                    );
2021                                }
2022                            }
2023                        }
2024                        current_row = *to_row;
2025                    }
2026                    CommitLineSegment::Curve {
2027                        to_column, on_row, ..
2028                    } => {
2029                        current_column = *to_column;
2030                        current_row = *on_row;
2031                    }
2032                }
2033            }
2034        }
2035
2036        Ok(())
2037    }
2038
2039    fn verify_coverage(graph: &GraphData) -> Result<()> {
2040        let mut expected_edges: HashSet<(Oid, Oid)> = HashSet::default();
2041        for entry in &graph.commits {
2042            for parent in &entry.data.parents {
2043                expected_edges.insert((entry.data.sha, *parent));
2044            }
2045        }
2046
2047        let mut found_edges: HashSet<(Oid, Oid)> = HashSet::default();
2048        for line in &graph.lines {
2049            let edge = (line.child, line.parent);
2050
2051            if !found_edges.insert(edge) {
2052                bail!(
2053                    "Duplicate line found for edge {:?} -> {:?}",
2054                    line.child,
2055                    line.parent
2056                );
2057            }
2058
2059            if !expected_edges.contains(&edge) {
2060                bail!(
2061                    "Orphan line found: {:?} -> {:?} is not in the commit graph",
2062                    line.child,
2063                    line.parent
2064                );
2065            }
2066        }
2067
2068        for (child, parent) in &expected_edges {
2069            if !found_edges.contains(&(*child, *parent)) {
2070                bail!("Missing line for edge {:?} -> {:?}", child, parent);
2071            }
2072        }
2073
2074        assert_eq!(
2075            expected_edges.symmetric_difference(&found_edges).count(),
2076            0,
2077            "The symmetric difference should be zero"
2078        );
2079
2080        Ok(())
2081    }
2082
2083    fn verify_merge_line_optimality(
2084        graph: &GraphData,
2085        oid_to_row: &HashMap<Oid, usize>,
2086    ) -> Result<()> {
2087        for line in &graph.lines {
2088            let first_segment = line.segments.first();
2089            let is_merge_line = matches!(
2090                first_segment,
2091                Some(CommitLineSegment::Curve {
2092                    curve_kind: CurveKind::Merge,
2093                    ..
2094                })
2095            );
2096
2097            if !is_merge_line {
2098                continue;
2099            }
2100
2101            let child_row = *oid_to_row
2102                .get(&line.child)
2103                .context("Line references non-existent child commit")?;
2104
2105            let parent_row = *oid_to_row
2106                .get(&line.parent)
2107                .context("Line references non-existent parent commit")?;
2108
2109            let parent_lane = graph.commits[parent_row].lane;
2110
2111            let Some(CommitLineSegment::Curve { to_column, .. }) = first_segment else {
2112                continue;
2113            };
2114
2115            let curves_directly_to_parent = *to_column == parent_lane;
2116
2117            if !curves_directly_to_parent {
2118                continue;
2119            }
2120
2121            let curve_row = child_row + 1;
2122            let has_commits_in_path = graph.commits[curve_row..parent_row]
2123                .iter()
2124                .any(|c| c.lane == parent_lane);
2125
2126            if has_commits_in_path {
2127                bail!(
2128                    "Merge line from {:?} to {:?} curves directly to parent lane {} but there are commits in that lane between rows {} and {}",
2129                    line.child,
2130                    line.parent,
2131                    parent_lane,
2132                    curve_row,
2133                    parent_row
2134                );
2135            }
2136
2137            let curve_ends_at_parent = curve_row == parent_row;
2138
2139            if curve_ends_at_parent {
2140                if line.segments.len() != 1 {
2141                    bail!(
2142                        "Merge line from {:?} to {:?} curves directly to parent (curve_row == parent_row), but has {} segments instead of 1 [MergeCurve]",
2143                        line.child,
2144                        line.parent,
2145                        line.segments.len()
2146                    );
2147                }
2148            } else {
2149                if line.segments.len() != 2 {
2150                    bail!(
2151                        "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but has {} segments instead of 2 [MergeCurve, Straight]",
2152                        line.child,
2153                        line.parent,
2154                        line.segments.len()
2155                    );
2156                }
2157
2158                let is_straight_segment = matches!(
2159                    line.segments.get(1),
2160                    Some(CommitLineSegment::Straight { .. })
2161                );
2162
2163                if !is_straight_segment {
2164                    bail!(
2165                        "Merge line from {:?} to {:?} curves directly to parent lane without overlap, but second segment is not a Straight segment",
2166                        line.child,
2167                        line.parent
2168                    );
2169                }
2170            }
2171        }
2172
2173        Ok(())
2174    }
2175
2176    fn verify_all_invariants(
2177        graph: &GraphData,
2178        commits: &[Arc<InitialGraphCommitData>],
2179    ) -> Result<()> {
2180        let oid_to_row = build_oid_to_row_map(graph);
2181
2182        verify_commit_order(graph, commits).context("commit order")?;
2183        verify_line_endpoints(graph, &oid_to_row).context("line endpoints")?;
2184        verify_column_correctness(graph, &oid_to_row).context("column correctness")?;
2185        verify_segment_continuity(graph).context("segment continuity")?;
2186        verify_merge_line_optimality(graph, &oid_to_row).context("merge line optimality")?;
2187        verify_coverage(graph).context("coverage")?;
2188        verify_line_overlaps(graph).context("line overlaps")?;
2189        Ok(())
2190    }
2191
2192    #[test]
2193    fn test_git_graph_merge_commits() {
2194        let mut rng = StdRng::seed_from_u64(42);
2195
2196        let oid1 = Oid::random(&mut rng);
2197        let oid2 = Oid::random(&mut rng);
2198        let oid3 = Oid::random(&mut rng);
2199        let oid4 = Oid::random(&mut rng);
2200
2201        let commits = vec![
2202            Arc::new(InitialGraphCommitData {
2203                sha: oid1,
2204                parents: smallvec![oid2, oid3],
2205                ref_names: vec!["HEAD".into()],
2206            }),
2207            Arc::new(InitialGraphCommitData {
2208                sha: oid2,
2209                parents: smallvec![oid4],
2210                ref_names: vec![],
2211            }),
2212            Arc::new(InitialGraphCommitData {
2213                sha: oid3,
2214                parents: smallvec![oid4],
2215                ref_names: vec![],
2216            }),
2217            Arc::new(InitialGraphCommitData {
2218                sha: oid4,
2219                parents: smallvec![],
2220                ref_names: vec![],
2221            }),
2222        ];
2223
2224        let mut graph_data = GraphData::new(8);
2225        graph_data.add_commits(&commits);
2226
2227        if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2228            panic!("Graph invariant violation for merge commits:\n{}", error);
2229        }
2230    }
2231
2232    #[test]
2233    fn test_git_graph_linear_commits() {
2234        let mut rng = StdRng::seed_from_u64(42);
2235
2236        let oid1 = Oid::random(&mut rng);
2237        let oid2 = Oid::random(&mut rng);
2238        let oid3 = Oid::random(&mut rng);
2239
2240        let commits = vec![
2241            Arc::new(InitialGraphCommitData {
2242                sha: oid1,
2243                parents: smallvec![oid2],
2244                ref_names: vec!["HEAD".into()],
2245            }),
2246            Arc::new(InitialGraphCommitData {
2247                sha: oid2,
2248                parents: smallvec![oid3],
2249                ref_names: vec![],
2250            }),
2251            Arc::new(InitialGraphCommitData {
2252                sha: oid3,
2253                parents: smallvec![],
2254                ref_names: vec![],
2255            }),
2256        ];
2257
2258        let mut graph_data = GraphData::new(8);
2259        graph_data.add_commits(&commits);
2260
2261        if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2262            panic!("Graph invariant violation for linear commits:\n{}", error);
2263        }
2264    }
2265
2266    #[test]
2267    fn test_git_graph_random_commits() {
2268        for seed in 0..100 {
2269            let mut rng = StdRng::seed_from_u64(seed);
2270
2271            let adversarial = rng.random_bool(0.2);
2272            let num_commits = if adversarial {
2273                rng.random_range(10..100)
2274            } else {
2275                rng.random_range(5..50)
2276            };
2277
2278            let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
2279
2280            assert_eq!(
2281                num_commits,
2282                commits.len(),
2283                "seed={}: Generate random commit dag didn't generate the correct amount of commits",
2284                seed
2285            );
2286
2287            let mut graph_data = GraphData::new(8);
2288            graph_data.add_commits(&commits);
2289
2290            if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2291                panic!(
2292                    "Graph invariant violation (seed={}, adversarial={}, num_commits={}):\n{:#}",
2293                    seed, adversarial, num_commits, error
2294                );
2295            }
2296        }
2297    }
2298
2299    // The full integration test has less iterations because it's significantly slower
2300    // than the random commit test
2301    #[gpui::test(iterations = 5)]
2302    async fn test_git_graph_random_integration(mut rng: StdRng, cx: &mut TestAppContext) {
2303        init_test(cx);
2304
2305        let adversarial = rng.random_bool(0.2);
2306        let num_commits = if adversarial {
2307            rng.random_range(10..100)
2308        } else {
2309            rng.random_range(5..50)
2310        };
2311
2312        let commits = generate_random_commit_dag(&mut rng, num_commits, adversarial);
2313
2314        let fs = FakeFs::new(cx.executor());
2315        fs.insert_tree(
2316            Path::new("/project"),
2317            json!({
2318                ".git": {},
2319                "file.txt": "content",
2320            }),
2321        )
2322        .await;
2323
2324        fs.set_graph_commits(Path::new("/project/.git"), commits.clone());
2325
2326        let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
2327        cx.run_until_parked();
2328
2329        let repository = project.read_with(cx, |project, cx| {
2330            project
2331                .active_repository(cx)
2332                .expect("should have a repository")
2333        });
2334
2335        repository.update(cx, |repo, cx| {
2336            repo.graph_data(
2337                crate::LogSource::default(),
2338                crate::LogOrder::default(),
2339                0..usize::MAX,
2340                cx,
2341            );
2342        });
2343        cx.run_until_parked();
2344
2345        let graph_commits: Vec<Arc<InitialGraphCommitData>> = repository.update(cx, |repo, cx| {
2346            repo.graph_data(
2347                crate::LogSource::default(),
2348                crate::LogOrder::default(),
2349                0..usize::MAX,
2350                cx,
2351            )
2352            .to_vec()
2353        });
2354
2355        let mut graph_data = GraphData::new(8);
2356        graph_data.add_commits(&graph_commits);
2357
2358        if let Err(error) = verify_all_invariants(&graph_data, &commits) {
2359            panic!(
2360                "Graph invariant violation (adversarial={}, num_commits={}):\n{:#}",
2361                adversarial, num_commits, error
2362            );
2363        }
2364    }
2365}