extract branch refresh to helper

This commit is contained in:
Neel 2026-07-08 16:48:25 +01:00
parent f4ef28d8be
commit 6e1b9cf591

View file

@ -7467,6 +7467,40 @@ impl Repository {
)
}
async fn refresh_branch_list(
this: &WeakEntity<Self>,
backend: Arc<dyn GitRepository>,
updates_tx: Option<mpsc::UnboundedSender<DownstreamUpdate>>,
cx: &mut AsyncApp,
) -> Result<()> {
let branches_scan = backend.branches().await?;
let branch_list_error = branches_scan.error;
let branch_list: Arc<[Branch]> = branches_scan.branches.into();
let branch = branch_list.iter().find(|branch| branch.is_head).cloned();
log::info!("head branch after scan is {branch:?}");
let snapshot = this.update(cx, |this, cx| {
let head_changed = branch != this.snapshot.branch;
let branch_list_changed = *branch_list != *this.snapshot.branch_list;
let branch_list_error_changed = this.snapshot.branch_list_error != branch_list_error;
this.snapshot.branch = branch;
this.snapshot.branch_list = branch_list;
this.snapshot.branch_list_error = branch_list_error;
if head_changed {
cx.emit(RepositoryEvent::HeadChanged);
}
if branch_list_changed || branch_list_error_changed {
cx.emit(RepositoryEvent::BranchListChanged);
}
this.snapshot.clone()
})?;
if let Some(updates_tx) = updates_tx {
updates_tx
.unbounded_send(DownstreamUpdate::UpdateRepository(snapshot))
.ok();
}
Ok(())
}
pub fn fetch(
&mut self,
fetch_options: FetchOptions,
@ -7501,32 +7535,7 @@ impl Repository {
.fetch(fetch_options, askpass, environment, cx.clone())
.await;
if result.is_ok() {
let branches_scan = backend.branches().await?;
let branch_list_error = branches_scan.error;
let branch_list: Arc<[Branch]> = branches_scan.branches.into();
let branch = branch_list.iter().find(|branch| branch.is_head).cloned();
let snapshot = this.update(&mut cx, |this, cx| {
let head_changed = branch != this.snapshot.branch;
let branch_list_changed =
*branch_list != *this.snapshot.branch_list;
let branch_list_error_changed =
this.snapshot.branch_list_error != branch_list_error;
this.snapshot.branch = branch;
this.snapshot.branch_list = branch_list;
this.snapshot.branch_list_error = branch_list_error;
if head_changed {
cx.emit(RepositoryEvent::HeadChanged);
}
if branch_list_changed || branch_list_error_changed {
cx.emit(RepositoryEvent::BranchListChanged);
}
this.snapshot.clone()
})?;
if let Some(updates_tx) = updates_tx {
updates_tx
.unbounded_send(DownstreamUpdate::UpdateRepository(snapshot))
.ok();
}
Self::refresh_branch_list(&this, backend, updates_tx, &mut cx).await?;
}
result
}
@ -7609,30 +7618,7 @@ impl Repository {
.await;
// TODO would be nice to not have to do this manually
if result.is_ok() {
let branches_scan = backend.branches().await?;
let branch_list_error = branches_scan.error;
let branch_list: Arc<[Branch]> = branches_scan.branches.into();
let branch = branch_list.iter().find(|branch| branch.is_head).cloned();
log::info!("head branch after scan is {branch:?}");
let snapshot = this.update(&mut cx, |this, cx| {
let branch_list_changed =
*branch_list != *this.snapshot.branch_list;
let branch_list_error_changed =
this.snapshot.branch_list_error != branch_list_error;
this.snapshot.branch = branch;
this.snapshot.branch_list = branch_list;
this.snapshot.branch_list_error = branch_list_error;
cx.emit(RepositoryEvent::HeadChanged);
if branch_list_changed || branch_list_error_changed {
cx.emit(RepositoryEvent::BranchListChanged);
}
this.snapshot.clone()
})?;
if let Some(updates_tx) = updates_tx {
updates_tx
.unbounded_send(DownstreamUpdate::UpdateRepository(snapshot))
.ok();
}
Self::refresh_branch_list(&this, backend, updates_tx, &mut cx).await?;
}
result
}