Files
zed/crates/git/src/git.rs
T
PriyadharshanandChristopher Biscardi f652da59e8 Added Tracked , Staged options to stash (#62254)
# Objective

Closes #62252 

The Git Panel could only stash *everything* — `Stash All` runs
`git stash push --include-untracked`, sweeping tracked edits and
untracked files
into a single entry. There was no way to stash a subset, so the common
workflows
of "park my tracked edits but keep my new scratch files" and "park what
I've
staged and keep working on the rest" required dropping to the terminal.

## Images

<img width="389" height="358" alt="Screenshot 2026-08-10 at 3 10 50 PM"
src="https://github.com/user-attachments/assets/18e4c943-e320-4802-ada8-59e54bf4cefd"
/>

<img width="504" height="462" alt="Screenshot 2026-08-10 at 3 10 37 PM"
src="https://github.com/user-attachments/assets/783237eb-980d-47bc-a0f5-17b03a23a60c"
/>






## Solution

Add two stash variants alongside `Stash All`, surfaced in the Git
Panel's
overflow menu based on how the list is currently grouped, so the menu
mirrors the
sections the user can actually see:

| Group By | Stash entries offered |
| --- | --- |
| None | Stash All |
| Tracked & Untracked | Stash All, **Stash Tracked** |
| Staged & Unstaged | Stash All, **Stash Staged** |

- **`git::StashTracked`** stashes tracked changes and leaves untracked
files in
place. It reuses the existing pathspec plumbing
(`Repository::stash_entries`),
  filtering the status list down to the paths to stash.
- **`git::StashStaged`** stashes the index only, leaving unstaged
changes in
place. This *cannot* be expressed as a pathspec — a partially staged
file would
have its unstaged hunks stashed too — so it needs git's own `--staged`
flag.
  That meant a new `GitRepository::stash_staged` backend method and an
`optional bool staged` field on `proto::Stash` so remote projects work
too.

Both actions are unbound by default and are dispatchable from the
command palette
when the panel is focused.

One subtlety worth calling out for review: `Stash Tracked` filters on
`FileStatus::is_created()`, not `is_untracked()`. Staging a new file
flips it from
`Untracked` to `Tracked { Added }`, but the panel still lists it under
**Untracked** — using `is_untracked()` meant staged-new files were
silently
stashed. `is_created()` is the same predicate the panel uses to build
that section
(`git_panel.rs`), so the menu item and the list can no longer disagree.

This branch also includes a separate commit adding **per-section
staging**
(`git::StageSection` / `git::UnstageSection`) — right-click a file to
stage or
unstage every entry in its section. Happy to split that into its own PR
if
preferred.

## Testing

Manually tested on macOS against a scratch repo with a mix of states:
modified
tracked files, untracked files, and untracked files that had been
staged.

- `Stash Tracked` with tracked edits + untracked files → only tracked
edits
  stashed; untracked files remain.
- `Stash Tracked` with untracked files **staged** → they remain, staged.
This was
  broken in an earlier revision and drove the `is_created()` fix above.
- `Stash Staged` with one file staged and another modified-but-unstaged
→ only the
  staged file is stashed; the unstaged edit and untracked files survive.
- `Stash Pop` round-trips both cases back to the original state, with no
conflicts.
- Menu contents and disabled states verified in all three Group By
modes.
- Per-section staging covered by a new unit test,
  `test_stage_section_scopes_to_selected_section`.

Not covered by automated tests: the stash actions themselves.
`FakeGitRepository`
leaves every stash method `unimplemented!()`, so stash behavior isn't
reachable
from GPUI tests today — consistent with the existing untested
`StashAll`. Adding
fake-repo stash support looks like a worthwhile follow-up but felt out
of scope here.

Reviewers on non-macOS platforms: nothing here is platform-specific.
Note that
`Stash Staged` requires **git 2.35+** (Jan 2022) for `git stash push
--staged`;
older git surfaces a clear error toast rather than failing opaquely. The
remote
path (`proto::Stash.staged`) has not been exercised against a live
collab session.

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [ ] 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:

- Added `Stash Tracked` and `Stash Staged` options to the Git Panel,
letting you stash only tracked changes or only staged changes.

---------

Co-authored-by: Christopher Biscardi <chris@christopherbiscardi.com>
2026-08-18 05:36:16 +00:00

421 lines
12 KiB
Rust

pub mod blame;
pub mod commit;
mod hosting_provider;
mod remote;
pub mod repository;
pub mod stash;
pub mod status;
pub use crate::hosting_provider::*;
pub use crate::remote::*;
use anyhow::Result;
use gpui::{Action, actions};
pub use repository::RemoteCommandOutput;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Write as _};
use std::str::FromStr;
pub const DOT_GIT: &str = ".git";
pub const GITIGNORE: &str = ".gitignore";
pub const FSMONITOR_DAEMON: &str = "fsmonitor--daemon";
pub const LFS_DIR: &str = "lfs";
pub const OBJECTS_DIR: &str = "objects";
pub const REFS_DIR: &str = "refs";
pub const REFTABLE_DIR: &str = "reftable";
pub const HOOKS_DIR: &str = "hooks";
pub const LOGS_DIR: &str = "logs";
pub const LOGS_REF_STASH: &str = "logs/refs/stash";
pub const REBASE_MERGE_DIR: &str = "rebase-merge";
pub const REBASE_APPLY_DIR: &str = "rebase-apply";
pub const SEQUENCER_DIR: &str = "sequencer";
pub const COMMIT_MESSAGE: &str = "COMMIT_EDITMSG";
pub const FETCH_HEAD: &str = "FETCH_HEAD";
pub const ORIG_HEAD: &str = "ORIG_HEAD";
pub const BISECT_LOG: &str = "BISECT_LOG";
pub const GC_PID: &str = "gc.pid";
pub const INFO_DIR: &str = "info";
pub const REPO_EXCLUDE: &str = "info/exclude";
actions!(
git,
[
// per-hunk
/// Toggles the staged state of the hunk or status entry at cursor.
ToggleStaged,
/// Stage status entries between an anchor entry and the cursor.
StageRange,
/// Stages the current hunk and moves to the next one.
StageAndNext,
/// Unstages the current hunk and moves to the next one.
UnstageAndNext,
/// Restores the selected hunks to their original state.
#[action(deprecated_aliases = ["editor::RevertSelectedHunks"])]
Restore,
/// Restores the selected hunks to their original state and moves to the
/// next one.
RestoreAndNext,
// per-file
/// Shows git blame information for the current file.
#[action(deprecated_aliases = ["editor::ToggleGitBlame"])]
Blame,
/// Shows the git history for the selected file, folder, or project.
FileHistory,
/// Opens the selected file in the editor without a diff view.
ViewFile,
/// Stages the current file.
StageFile,
/// Unstages the current file.
UnstageFile,
// per-section
/// Stages every entry in the section containing the selected entry.
StageSection,
/// Unstages every entry in the section containing the selected entry.
UnstageSection,
// repo-wide
/// Stages all changes in the repository.
StageAll,
/// Unstages all changes in the repository.
UnstageAll,
/// Stashes all changes in the repository, including untracked files.
StashAll,
/// Stashes tracked changes in the repository, leaving untracked files in place.
StashTracked,
/// Stashes staged changes in the repository, leaving unstaged changes in place.
StashStaged,
/// Pops the most recent stash.
StashPop,
/// Apply the most recent stash.
StashApply,
/// Restores all tracked files to their last committed state.
RestoreTrackedFiles,
/// Moves all untracked files to trash.
TrashUntrackedFiles,
/// Undoes the last commit, keeping changes in the working directory.
Uncommit,
/// Pushes commits to the remote repository.
Push,
/// Pushes commits to a specific remote branch.
PushTo,
/// Force pushes commits to the remote repository.
ForcePush,
/// Pulls changes from the remote repository.
Pull,
/// Pulls changes from the remote repository with rebase.
PullRebase,
/// Fetches changes from the remote repository.
Fetch,
/// Fetches changes from a specific remote.
FetchFrom,
/// Creates a new commit with staged changes.
Commit,
/// Runs the next commit with `git commit --no-verify`.
SkipHooks,
/// Amends the last commit with staged changes.
Amend,
/// Enable the --signoff option.
Signoff,
/// Cancels the current git operation.
Cancel,
/// Expands the commit message editor.
ExpandCommitEditor,
/// Toggles whether the commit message editor fills all the available
/// vertical space within the git panel.
ToggleFillCommitEditor,
/// Generates a commit message using AI.
GenerateCommitMessage,
/// Initializes a new git repository.
Init,
/// Opens all modified files in the editor.
OpenModifiedFiles,
/// Opens the current file in a solo diff view.
OpenFileDiff,
/// Clones a repository.
Clone,
ViewCommit,
/// Adds a file to .gitignore.
AddToGitignore,
/// Adds a file to the repository's .git/info/exclude.
AddToGitInfoExclude,
/// Copies the current branch name to the clipboard.
CopyBranchName,
]
);
/// Renames a git branch.
#[derive(Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
#[action(namespace = git)]
#[serde(deny_unknown_fields)]
pub struct RenameBranch {
/// The branch to rename.
///
/// Default: the current branch.
#[serde(default)]
pub branch: Option<String>,
}
/// Restores a file to its last committed state, discarding local changes.
#[derive(Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
#[action(namespace = git, deprecated_aliases = ["editor::RevertFile"])]
#[serde(deny_unknown_fields)]
pub struct RestoreFile {
#[serde(default)]
pub skip_prompt: bool,
}
/// The length of a Git short SHA.
pub const SHORT_SHA_LENGTH: usize = 7;
const SHA1_BYTE_LENGTH: usize = 20;
const SHA256_BYTE_LENGTH: usize = 32;
const SHA1_HEX_LENGTH: usize = SHA1_BYTE_LENGTH * 2;
const SHA256_HEX_LENGTH: usize = SHA256_BYTE_LENGTH * 2;
const HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct Oid {
bytes: [u8; SHA256_BYTE_LENGTH],
format: OidFormat,
}
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
enum OidFormat {
Sha1,
Sha256,
}
impl OidFormat {
fn byte_len(self) -> usize {
match self {
Self::Sha1 => SHA1_BYTE_LENGTH,
Self::Sha256 => SHA256_BYTE_LENGTH,
}
}
fn hex_len(self) -> usize {
match self {
Self::Sha1 => SHA1_HEX_LENGTH,
Self::Sha256 => SHA256_HEX_LENGTH,
}
}
}
impl Oid {
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
let format = match bytes.len() {
SHA1_BYTE_LENGTH => OidFormat::Sha1,
SHA256_BYTE_LENGTH => OidFormat::Sha256,
len => {
anyhow::bail!(
"invalid git oid byte length: expected {SHA1_BYTE_LENGTH} for SHA-1 or {SHA256_BYTE_LENGTH} for SHA-256, got {len}"
);
}
};
let mut oid_bytes = [0u8; SHA256_BYTE_LENGTH];
oid_bytes[..bytes.len()].copy_from_slice(bytes);
Ok(Self {
bytes: oid_bytes,
format,
})
}
#[cfg(any(test, feature = "test-support"))]
pub fn random(rng: &mut impl rand::Rng) -> Self {
let mut bytes = [0u8; SHA256_BYTE_LENGTH];
rng.fill(&mut bytes[..SHA1_BYTE_LENGTH]);
Self {
bytes,
format: OidFormat::Sha1,
}
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.format.byte_len()]
}
pub fn is_zero(&self) -> bool {
self.as_bytes().iter().all(|byte| *byte == 0)
}
/// Returns this [`Oid`] as a short SHA.
pub fn display_short(&self) -> String {
self.hex_string(SHORT_SHA_LENGTH)
}
fn hex_string(&self, len: usize) -> String {
let mut string = String::with_capacity(len);
for index in 0..len {
string.push(self.hex_digit(index));
}
string
}
fn write_hex(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for index in 0..self.format.hex_len() {
f.write_char(self.hex_digit(index))?;
}
Ok(())
}
#[inline(always)]
fn hex_digit(&self, index: usize) -> char {
debug_assert!(index < self.format.hex_len());
let byte = self.as_bytes()[index / 2];
let nibble = if index & 1 == 0 {
byte >> 4
} else {
byte & 0x0f
};
char::from(HEX_DIGITS[nibble as usize])
}
}
impl TryFrom<&str> for Oid {
type Error = anyhow::Error;
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
Oid::from_str(value)
}
}
impl FromStr for Oid {
type Err = anyhow::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let format = match s.len() {
1..=SHA1_HEX_LENGTH => OidFormat::Sha1,
SHA256_HEX_LENGTH => OidFormat::Sha256,
len => {
anyhow::bail!(
"invalid git oid hex length: expected 1..={SHA1_HEX_LENGTH} for SHA-1 or {SHA256_HEX_LENGTH} for SHA-256, got {len}"
);
}
};
let mut bytes = [0u8; SHA256_BYTE_LENGTH];
for (index, byte) in s.bytes().enumerate() {
let digit = decode_hex_digit(byte)
.ok_or_else(|| anyhow::anyhow!("invalid hex digit at byte {index} for git oid"))?;
if index % 2 == 0 {
bytes[index / 2] = digit << 4;
} else {
bytes[index / 2] |= digit;
}
}
Ok(Self { bytes, format })
}
}
fn decode_hex_digit(byte: u8) -> Option<u8> {
match byte {
b'0'..=b'9' => Some(byte - b'0'),
b'a'..=b'f' => Some(byte - b'a' + 10),
b'A'..=b'F' => Some(byte - b'A' + 10),
_ => None,
}
}
impl fmt::Debug for Oid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl fmt::Display for Oid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.write_hex(f)
}
}
impl Serialize for Oid {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.hex_string(self.format.hex_len()))
}
}
impl<'de> Deserialize<'de> for Oid {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse::<Oid>().map_err(serde::de::Error::custom)
}
}
impl From<Oid> for u32 {
fn from(oid: Oid) -> Self {
let mut u32_bytes = [0u8; 4];
u32_bytes.copy_from_slice(&oid.as_bytes()[..4]);
u32::from_ne_bytes(u32_bytes)
}
}
impl From<Oid> for usize {
fn from(oid: Oid) -> Self {
let mut u64_bytes = [0u8; 8];
u64_bytes.copy_from_slice(&oid.as_bytes()[..8]);
u64::from_ne_bytes(u64_bytes) as usize
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_short_sha1_oid_by_padding_trailing_nibbles() {
let oid = "abc1234".parse::<Oid>().expect("failed to parse oid");
assert_eq!(oid.as_bytes().len(), SHA1_BYTE_LENGTH);
assert_eq!(oid.display_short(), "abc1234");
assert_eq!(oid.to_string(), "abc1234000000000000000000000000000000000");
}
#[test]
fn parses_full_sha256_oid() {
let sha = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
let oid = sha.parse::<Oid>().expect("failed to parse oid");
assert_eq!(oid.as_bytes().len(), SHA256_BYTE_LENGTH);
assert_eq!(oid.to_string(), sha);
}
#[test]
fn rejects_invalid_oid_lengths() {
assert!("".parse::<Oid>().is_err());
assert!("a".repeat(SHA1_HEX_LENGTH + 1).parse::<Oid>().is_err());
assert!("a".repeat(SHA256_HEX_LENGTH - 1).parse::<Oid>().is_err());
}
}
#[repr(i32)]
#[derive(Copy, Clone, Debug)]
pub enum RunHook {
PreCommit,
}
impl RunHook {
pub fn as_str(&self) -> &str {
match self {
Self::PreCommit => "pre-commit",
}
}
pub fn to_proto(&self) -> i32 {
*self as i32
}
pub fn from_proto(value: i32) -> Option<Self> {
match value {
0 => Some(Self::PreCommit),
_ => None,
}
}
}