# git_ui: Improve discard tracked changes context menu option \(\#62872\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/f1cdbaad04ae1650fb66a477511d4a2b4a2a25dd)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: f1cdbaad04ae1650fb66a477511d4a2b4a2a25dd

Requested commit: f1cdbaad04ae1650fb66a477511d4a2b4a2a25dd

Commit: f1cdbaad04ae1650fb66a477511d4a2b4a2a25dd

Tree: 6fa76a3285575c214e72505fbd37c6640626e47f

Author: Dino

Committer: GitHub

## Message

```
git_ui: Improve discard tracked changes context menu option (#62872)

# Objective

Ensure that the "Discard Tracked Changes" option shown in the context
menu that can be opened in the Git Panel's "Changes" tab is only
displayed when it can actually be used. Currently, it is also shown when
the "Unstaged" section has tracked files, even though clicking will not
actually do anything.

Besides fixing that, the changes in this Pull Request also update how
the "Discard Tracked Changes" action works when picked from a subfolder,
with only changes in that subfolder being discarded.

Closes #62535

## Solution

- Update `git_ui::git_panel::git_panel_context_menu` to now accept a
`has_staged_tracked_changes` boolean, in order to be able to
differentiate between the panel having tracked changes, be it unstaged
or staged, and specifically staged tracked changes, as we only want the
"Discard Tracked Changes" to be enabled and affect staged changes.
- Add `git_ui::git_panel::GitPanel::directory_context_descendants` in
order to be able to obtain the list of entries under the directory where
the context menu was deployed, if any.
- Update `git_ui::git_panel::GitPanel::restored_tracked_files` to
leverage the new `directory_context_descendants` method, ensuring that
only the entries under the context menu's directory are restored,
instead of all staged tracked files

## Testing

Tested both manually, as shown in the "Showcase" section, as well as
introduced a new test for these changes –
`git_ui::git_panel::tests::test_directory_discard_tracked_changes`.

## 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

## Showcase

<details>
  <summary>Before</summary>


https://github.com/user-attachments/assets/24bbb23c-78fb-4fe3-82da-d8bcaec0d79c
</details>

<details>
  <summary>After</summary>
  

https://github.com/user-attachments/assets/70db9189-9122-4ee4-b03f-648935c4f0a4
</details>

---

Release Notes:

- Fixed the "Discard Tracked Changes" option being enabled for files in
the "Unstaged" section in the Git Panel
- Updated the "Discard Tracked Changes" option to only affects files in
the directory where the context menu was deployed
```

## Parents

- [242fe31a399f695103dd0cfe7dcef33a59fbf596](https://git.cafe/gitcafe/zed/commit/242fe31a399f695103dd0cfe7dcef33a59fbf596?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/f1cdbaad04ae1650fb66a477511d4a2b4a2a25dd?format=markdown)
