Repository: zed-industries/zed
File: crates/git_graph/src/git_graph.rs (Rust)

Code before:
            }
            _ => {}
        }
    }

    fn fetch_initial_graph_data(&mut self, cx: &mut App) {
        if let Some(repository) = self.get_selected_repository(cx) {
            repository.update(cx, |repository, cx| {
                let (commits, _) = repository.graph_data(
                    self.log_source.clone(),
                    self.log_order,
                    0..usize::MAX,
                    cx,
                );
                self.graph_data.add_commits(commits);
            });
        }
    }

    fn get_selected_repository(&self, cx: &App) -> Option<Entity<Repository>> {
        let project = self.project.read(cx);
        self.selected_repo_id
            .as_ref()
            .and_then(|repo_id| project.repositories(cx).get(&repo_id).cloned())
    }

    fn render_chip(&self, name: &SharedString, accent_color: gpui::Hsla) -> impl IntoElement {
        Chip::new(name.clone())
            .label_size(LabelSize::Small)
            .bg_color(accent_color.opacity(0.1))
            .border_color(accent_color.opacity(0.5))
    }

    fn render_table_rows(
        &mut self,
        range: Range<usize>,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Vec<Vec<AnyElement>> {
        let repository = self.get_selected_repository(cx);

        let row_height = self.row_height;

        // We fetch data outside the visible viewport to avoid loading entries when
        // users scroll through the git graph
        if let Some(repository) = repository.as_ref() {
            const FETCH_RANGE: usize = 100;
            repository.update(cx, |repository, cx| {
                self.graph_data.commits[range.start.saturating_sub(FETCH_RANGE)
                    ..(range.end + FETCH_RANGE)
                        .min(self.graph_data.commits.len().saturating_sub(1))]

Review comment (suggestion):
I think we can avoid having `PENDING_SELECT_MAX_RETRIES` in retry_pending select. 

`repository.graph_data(...).1` is true if the graph is still loading commits, we can clear the pending selection if it's false and we haven't found the commit a user wants to see 

Code after:

        this.fetch_initial_graph_data(cx);
        this
    }

    fn on_repository_event(
        &mut self,
        repository: Entity<Repository>,
        event: &RepositoryEvent,
        cx: &mut Context<Self>,
    ) {
        match event {
            RepositoryEvent::GitGraphCountUpdated((order, source), commit_count) => {
                if order != &self.log_order || source != &self.log_source {
                    return;
                }

                let old_count = self.graph_data.commits.len();

                repository.update(cx, |repository, cx| {
                    let (commits, _) =
                        repository.graph_data(source.clone(), *order, old_count..*commit_count, cx);
                    self.graph_data.add_commits(commits);
                });
                cx.notify();
                self.retry_pending_select(cx);
            }
            RepositoryEvent::BranchChanged | RepositoryEvent::MergeHeadsChanged => {
                self.pending_select_sha = None;
                // Only invalidate if we scanned atleast once,
                // meaning we are not inside the initial repo loading state
                // NOTE: this fixes an loading performance regression
                if repository.read(cx).scan_id > 1 {
                    self.graph_data.clear();
                    cx.notify();
                }
            }
            _ => {}
        }
    }

    fn fetch_initial_graph_data(&mut self, cx: &mut App) {
        if let Some(repository) = self.get_selected_repository(cx) {
            repository.update(cx, |repository, cx| {
                let (commits, _) = repository.graph_data(
                    self.log_source.clone(),
                    self.log_order,
                    0..usize::MAX,
                    cx,
                );
                self.graph_data.add_commits(commits);