F1: remote checkout without a remote, git.lastMessage, shared git status store

- vcs: checking out a remote-tracking branch reads remote.<name>.fetch;
  without a matching refspec it creates the local branch with --no-track
  instead of leaving a dirty tree after git fails to set up tracking.
- vcs + engine: git.lastMessage returns HEAD's full message (null before
  the first commit); the Changes window prefills Amend with it.
- ui: stores/vcsStatus.ts is the only git.status caller; the Changes
  window, tree/tab colours and the branch item read its snapshot, so a
  gitChanged costs one status query. RepoSummary carries upstream,
  ahead and behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-27 17:16:53 +01:00
parent d567f3957d
commit 4bdfe521f2
14 changed files with 649 additions and 314 deletions

View file

@ -10,7 +10,7 @@ use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};
use crate::VcsError;
use crate::porcelain::{self, FileStatus, Head, REF_FORMAT, Status, Version};
use crate::porcelain::{self, FetchSpec, FileStatus, Head, REF_FORMAT, Status, Version};
use crate::runner::{self, Call, HOOK_TIMEOUT};
/// Oldest supported git: 2.30 (porcelain v2 headers, `--pathspec-from-file`
@ -564,20 +564,55 @@ impl Repo {
.success())
}
/// Checks out a local branch, or creates a local branch tracking a
/// remote one (`origin/feature` → `feature`). Hooks run (120 s).
/// The configured `remote.<name>.fetch` refspecs.
///
/// # Errors
/// The runner's errors; no configured remote is an empty list.
fn fetch_specs(&self) -> Result<Vec<FetchSpec>, VcsError> {
let output = self.run(Call::read(
&self.root,
&["config", "-z", "--get-regexp", r"^remote\..*\.fetch$"],
SMALL_CAP,
))?;
if !output.success() {
return Ok(Vec::new());
}
Ok(porcelain::parse_fetch_specs(&output.stdout))
}
/// Checks out a local branch, or creates a local branch from a remote
/// one (`origin/feature` → `feature`). Hooks run (120 s).
///
/// The new branch tracks the remote one when a configured fetch refspec
/// maps a branch to it (the name comes from that refspec, as git
/// derives it). Without one (the remote was removed, or the ref was
/// created by hand) git could not set up tracking and would fail after
/// updating the work tree, so the branch is created with `--no-track`,
/// named after the part behind the first `/`.
///
/// # Errors
/// [`VcsError::InvalidName`], [`VcsError::Git`] for an unknown branch
/// or when git refuses (local changes would be overwritten); git's
/// message is passed on.
/// or when git refuses (local changes would be overwritten, the local
/// branch exists already); git's message is passed on.
pub fn checkout(&self, branch: &str) -> Result<(), VcsError> {
check_name(branch)?;
let _guard = self.lock();
let remote_ref = format!("refs/remotes/{branch}");
let local;
let args: Vec<&str> = if self.ref_exists(&format!("refs/heads/{branch}"))? {
vec!["switch", "-q", branch]
} else if self.ref_exists(&format!("refs/remotes/{branch}"))? {
vec!["switch", "-q", "--track", branch]
} else if self.ref_exists(&remote_ref)? {
let tracked = porcelain::tracking_name(&self.fetch_specs()?, &remote_ref);
let track = if tracked.is_some() {
"--track"
} else {
"--no-track"
};
local = tracked
.or_else(|| branch.split_once('/').map(|(_, rest)| rest.to_string()))
.unwrap_or_default();
check_name(&local)?;
vec!["switch", "-q", "-c", &local, track, branch]
} else {
return Err(VcsError::Git(format!("no branch named {branch}")));
};
@ -586,6 +621,29 @@ impl Repo {
Ok(())
}
/// The full message of the last commit, for prefilling an amend.
///
/// # Returns
/// `None` before the first commit; otherwise the raw message (`%B`)
/// with trailing line breaks removed.
///
/// # Errors
/// The runner's errors or git's refusal.
pub fn last_message(&self) -> Result<Option<String>, VcsError> {
if !self.has_head()? {
return Ok(None);
}
let output = self
.run(Call::read(
&self.root,
&["show", "-s", "--format=%B", "HEAD"],
SMALL_CAP,
))?
.checked()?;
let text = String::from_utf8_lossy(&output.stdout);
Ok(Some(text.trim_end_matches(['\n', '\r']).to_string()))
}
/// Creates a branch.
///
/// # Parameters