git: Add diff_base setting for showing changes since the default branch (#61501)
# Objective Let git indicators — the editor gutter, file colors, and `git::Diff` — show all changes on the current branch relative to its merge base with the default branch, instead of only uncommitted changes. Supersedes #60398; thanks to @samuelcolvin for the original implementation and motivation. Closes FR-135 ## Solution - New `git.diff_base` setting (`"head"` | `"default_branch"`), applied live and toggleable per session from the editor controls menu ("Diff Against Default Branch"). - Statuses come from a real merge-base-to-worktree tree diff (`git diff --merge-base`), so local edits that revert branch changes correctly show as unchanged. - `GitStore` shares one `DiffBufferList` per repository with the Branch Diff view; `repo_snapshots` and `project_path_git_status` keep returning index/worktree truth, while display surfaces use separate `display_*` APIs. - `BufferDiff` now records what its base is (`DiffBaseKind`); hunks whose base isn't HEAD are read-only in the gutter — stage/restore buttons and keybindings are inert, so committed work can't be silently rewritten. - `git::Diff` follows the setting; new `git::DiffHead` always opens the HEAD diff; `git::BranchDiff` is renamed `git::DiffBranch` (deprecated alias kept). Tradeoffs / known limitations: - Hunk-level staging is unavailable while in `default_branch` mode (whole-file staging via the git panel still works). Staging just the uncommitted sub-ranges of a branch hunk is a follow-up. - Remote hosts running an older server ignore the new `GetTreeDiff.includes_worktree` proto field and degrade to committed-changes-only branch diffs. - Repositories with no resolvable default branch fall back to HEAD-relative behavior; a failed first resolution retries on the next branch-list change. ## Testing - Real-git-repo tests for the merge-base-to-worktree diff's edge cases: files recreated after index deletion, committed deletions recreated on disk, and symlinks. - GPUI tests for status semantics (a branch change reverted on disk shows clean), `git::Diff` routing, live setting changes, and read-only hunk enforcement (restore/stage leave buffer and index untouched). ## Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content adheres to Zed's UI standards ([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) and [icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md) guidelines) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable --- Release Notes: - Git: Added a `git.diff_base` setting (`"head"` or `"default_branch"`) that makes the editor gutter, file colors, and diff view show all changes on the current branch since its merge base with the default branch, instead of only uncommitted changes. --------- Co-authored-by: Ben Kunkle <ben@zed.dev>
This commit is contained in:
1 parent
5c6dbc3ab2
commit
0500fd9ee9
35 files changed
+1742
-465
No files matched your search
@@ -25,6 +25,23 @@ pub struct BufferDiff {
|
||||
diff_snapshot: Option<BufferDiffSnapshot>,
|
||||
secondary_diff: Option<Entity<BufferDiff>>,
|
||||
buffer_snapshot: text::BufferSnapshot,
|
||||
base_kind: DiffBaseKind,
|
||||
}
|
||||
|
||||
/// Where this diff's base text came from. Only diffs whose base is HEAD
|
||||
/// support staging and restoring hunks; a diff against any other base (e.g.
|
||||
/// the merge base with another branch) would rewrite committed work.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum DiffBaseKind {
|
||||
/// The buffer's committed (HEAD) content.
|
||||
Head,
|
||||
/// The buffer's index (staged) content.
|
||||
Index,
|
||||
/// An arbitrary blob, such as the merge base with another branch.
|
||||
Oid,
|
||||
/// Arbitrary caller-provided text, such as an agent's original text,
|
||||
/// the clipboard, or another file.
|
||||
Custom,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -1553,6 +1570,7 @@ impl BufferDiff {
|
||||
buffer: &text::BufferSnapshot,
|
||||
language: Option<Arc<Language>>,
|
||||
language_registry: Option<Arc<LanguageRegistry>>,
|
||||
base_kind: DiffBaseKind,
|
||||
cx: &mut App,
|
||||
) -> Self {
|
||||
let base_text = cx.new(|cx| {
|
||||
@@ -1571,12 +1589,14 @@ impl BufferDiff {
|
||||
diff_snapshot: None,
|
||||
buffer_snapshot: buffer.clone(),
|
||||
secondary_diff: None,
|
||||
base_kind,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_base_text_buffer(
|
||||
buffer: &text::BufferSnapshot,
|
||||
base_text_buffer: Entity<language::Buffer>,
|
||||
base_kind: DiffBaseKind,
|
||||
_cx: &mut App,
|
||||
) -> Self {
|
||||
BufferDiff {
|
||||
@@ -1585,6 +1605,7 @@ impl BufferDiff {
|
||||
diff_snapshot: None,
|
||||
buffer_snapshot: buffer.clone(),
|
||||
secondary_diff: None,
|
||||
base_kind,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1592,6 +1613,7 @@ impl BufferDiff {
|
||||
buffer: &text::BufferSnapshot,
|
||||
language: Option<Arc<Language>>,
|
||||
language_registry: Option<Arc<LanguageRegistry>>,
|
||||
base_kind: DiffBaseKind,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
let base_text = buffer.text();
|
||||
@@ -1622,6 +1644,7 @@ impl BufferDiff {
|
||||
diff_snapshot: Some(diff_snapshot),
|
||||
buffer_snapshot: buffer.clone(),
|
||||
secondary_diff: None,
|
||||
base_kind,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1631,7 +1654,7 @@ impl BufferDiff {
|
||||
buffer: &text::BufferSnapshot,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
let mut this = BufferDiff::new(buffer, None, None, cx);
|
||||
let mut this = BufferDiff::new(buffer, None, None, DiffBaseKind::Head, cx);
|
||||
let mut base_text = base_text.to_owned();
|
||||
text::LineEnding::normalize(&mut base_text);
|
||||
let base_text_buffer = cx.new(|cx| {
|
||||
@@ -1655,6 +1678,16 @@ impl BufferDiff {
|
||||
self.secondary_diff = Some(diff);
|
||||
}
|
||||
|
||||
pub fn base_kind(&self) -> DiffBaseKind {
|
||||
self.base_kind
|
||||
}
|
||||
|
||||
/// Whether hunks in this diff can be staged or restored: true only when
|
||||
/// the diff's base is HEAD.
|
||||
pub fn is_stageable(&self) -> bool {
|
||||
self.base_kind == DiffBaseKind::Head
|
||||
}
|
||||
|
||||
pub fn secondary_diff(&self) -> Option<Entity<BufferDiff>> {
|
||||
self.secondary_diff.clone()
|
||||
}
|
||||
@@ -2449,7 +2482,8 @@ mod tests {
|
||||
],
|
||||
);
|
||||
|
||||
diff = cx.update(|cx| BufferDiff::new(&buffer, None, None, cx).snapshot(cx));
|
||||
diff = cx
|
||||
.update(|cx| BufferDiff::new(&buffer, None, None, DiffBaseKind::Head, cx).snapshot(cx));
|
||||
assert_hunks::<&str, _>(
|
||||
diff.hunks_intersecting_range(
|
||||
Anchor::min_max_range_for_buffer(buffer.remote_id()),
|
||||
@@ -3154,7 +3188,8 @@ mod tests {
|
||||
|
||||
let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text_1);
|
||||
|
||||
let empty_diff = cx.update(|cx| BufferDiff::new(&buffer, None, None, cx).snapshot(cx));
|
||||
let empty_diff = cx
|
||||
.update(|cx| BufferDiff::new(&buffer, None, None, DiffBaseKind::Head, cx).snapshot(cx));
|
||||
let diff_1 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
|
||||
let DiffChanged {
|
||||
changed_range,
|
||||
@@ -4312,7 +4347,8 @@ mod tests {
|
||||
);
|
||||
let buffer_snapshot = buffer.snapshot();
|
||||
|
||||
let diff = cx.new(|cx| BufferDiff::new(&buffer_snapshot, None, None, cx));
|
||||
let diff =
|
||||
cx.new(|cx| BufferDiff::new(&buffer_snapshot, None, None, DiffBaseKind::Head, cx));
|
||||
diff.update(cx, |diff, cx| {
|
||||
diff.set_base_text(Some(Arc::from(base_text_crlf)), buffer_snapshot.clone(), cx)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user