git_graph.rs

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