Title: Feature: Add functionality to discard changes · Issue #356 · github-for-unity/Unity · GitHub
Open Graph Title: Feature: Add functionality to discard changes · Issue #356 · github-for-unity/Unity
X Title: Feature: Add functionality to discard changes · Issue #356 · github-for-unity/Unity
Description: GitHub for Unity. Contribute to github-for-unity/Unity development by creating an account on GitHub.
Open Graph Description: GitHub for Unity. Contribute to github-for-unity/Unity development by creating an account on GitHub.
X Description: GitHub for Unity. Contribute to github-for-unity/Unity development by creating an account on GitHub.
Opengraph URL: https://github.com/github-for-unity/Unity
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Feature: Add functionality to discard changes","articleBody":"# Discarding changes\r\n\r\n## Problem\r\n\r\nWhen there are changes to files that the user isn't interested in keeping, they need a way of throwing away these changes. This is normally implemented via a `Discard` action for a specific file, or for all files. \r\n\r\n### Git overview\r\n\r\nGit provides a `checkout` command that changes the working directory back to a previous state. This command can take:\r\n\r\n- a commit (usually a branch name) - updates the working directory to the state of that commit\r\n- a filename or list of files - updates the named files to the state of the commit (the current HEAD if no commit is specified)\r\n\r\n`git checkout [file]` or `git checkout -- [files]`, will only operate on files that are already tracked by git and will not work for new files. Also, if a file has already been added to the staging area, `checkout [file]` will not affect the changes in the staging area, it will only affect changes that have not been staged yet.\r\n\r\n\r\n\r\nIn this situation above, in order to discard changes to the different files, we need to do:\r\n\r\n- **README.me**: `git checkout HEAD -- README.md` \r\n- **package.sh**: `git checkout -- package.sh` (`git checkout HEAD -- package.sh` also works)\r\n- **a**: `git checkout` won't work, we need to manually delete the file. \r\n\r\n`git clean -f` throws away new files / changes to untracked files, but won't reset tracked file changes back to what they were.\r\n\r\n`git reset --hard` throws away all changes to tracked files but doesn't touch untracked (new) files\r\n\r\n## Solution\r\n\r\n### Technical backend - git operations\r\n\r\n#### Discard changes to a single file\r\n\r\nTo implement \"Discard changes to a particular file\", we should:\r\n\r\n- Check whether the file is new\r\n\t- If yes, delete it\r\n\t- If no, `git checkout HEAD -- [file]`\r\n\r\n#### Discard changes to all files\r\n\r\nTo implement \"Discard all changes\", there's several alternatives:\r\n\r\n1. **clean and reset**\r\n- Run `git clean -f` to throw away all new (untracked) files\r\n- Run `git reset --hard` to reset all changed (tracked) files back to unchanged\r\n\r\n2. **checkout and delete**\r\n- Categorize all files by new/existing (untracked/tracked)\r\n- Delete all new (untracked) files\r\n- Run `git checkout HEAD -- [files]` on all existing (tracked) changed files\r\n\r\n### UI/UX\r\n\r\nTBD\r\n\r\n### API technical details\r\n\r\nGiven that both discard and discard all requires at least two separate actions (as talked about in https://github.com/github-for-unity/Unity/issues/356), that should probably be implemented in `RepositoryManager`. What we then need is:\r\n\r\n**`RepositoryManager.cs` (pseudocode)**:\r\n\r\n```\r\nDiscardAll():\r\n\r\n GitClient.CleanUntrackedFiles()\r\n GitClient.Checkout(\"HEAD\")\r\n```\r\n\r\n```\r\nDiscardFiles(List\u003cGitFile> files):\r\n \r\n where file in files.Added: Delete File via System.IO calls\r\n all other files: GitClient.Checkout(\"HEAD\", files)\r\n```\r\n\r\nWe already have `GitSwitchBranchesTask` which basically does a `git checkout`. We should probably rename that one to `GitCheckout` and add an optional `files` parameter to it, calling either `git checkout [branch]` or `git checkout [branch] -- files` depending on whether there's a list of files to checkout. Checking out a branch is the same as checking out `HEAD`, there's no difference there\r\n\r\n\r\n**old issue body below**\r\n----\r\n\r\nTo implement a discard changes function in the user interface we first need to add the functionality to our api.\r\n\r\n1. Create `GitCheckoutTask`,\r\n a. The goal of this task is to run `git checkout`.\r\n 1. It should be able to checkout on a specific file\r\n `git checkout path/to/file/to/revert`\r\n 2. It should be able to checkout all files\r\n `git checkout -- .`\r\n b. You can use `GitAddTask` as an example.\r\n https://github.com/github-for-unity/Unity/blob/master/src/GitHub.Api/Git/Tasks/GitAddTask.cs\r\n2. Add the functionality to use `GitCheckoutTask` to `GitClient`\r\n a. Create a `Discard` and `DiscardAll` method\r\n b. You can use the methods `Add` and `AddAll` as an example\r\n https://github.com/github-for-unity/Unity/blob/master/src/GitHub.Api/Git/GitClient.cs#L371-393\r\n3. Add some integration tests\r\n https://github.com/github-for-unity/Unity/blob/master/src/tests/IntegrationTests/Events/RepositoryManagerTests.cs\r\n a. We need some integration tests to prove what you did is working correctly\r\n b. You can use the `ShouldDetectFileChanges` test as an example, it makes changes to the system and checks if they are there.\r\n https://github.com/github-for-unity/Unity/blob/master/src/tests/IntegrationTests/Events/RepositoryManagerTests.cs#L24-L62\r\n\r\n","author":{"url":"https://github.com/neuroKip","@type":"Person","name":"neuroKip"},"datePublished":"2017-10-11T14:44:37Z","url":"https://github.com/github-for-unity/Unity/issues/356"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:a615c017-3366-e554-ba50-a78135ca3da1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9752:38C5CF:5ADB01:772AD9:6B00CD31 |
| html-safe-nonce | 5f2a9cb7564fc8e6c23d2f54674ce65768a6f3169897c2544d42c28b44cfc750 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzUyOjM4QzVDRjo1QURCMDE6NzcyQUQ5OjZCMDBDRDMxIiwidmlzaXRvcl9pZCI6IjMwOTc0NzExNjIzMDE1OTkwMjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a2ffd1e6bb1195e2cd7ca7e4ace2e0f10324e9ff73935ca28ddddf628ca063ee |
| hovercard-subject-tag | repository:94770860 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/github-for-unity/Unity/issues/356 |
| twitter:image | https://opengraph.githubassets.com/c304a76c888259a02bd524603e459010beee41d3e05d9274f7bdc09ec8c21ead/github-for-unity/Unity |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c304a76c888259a02bd524603e459010beee41d3e05d9274f7bdc09ec8c21ead/github-for-unity/Unity |
| og:image:alt | GitHub for Unity. Contribute to github-for-unity/Unity development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 85bbdfe2b6ba16e234513bc6ceff5d848ef8449820c900692a2b4e67061931e5 |
| turbo-cache-control | no-cache |
| go-import | github.com/github-for-unity/Unity git https://github.com/github-for-unity/Unity.git |
| octolytics-dimension-user_id | 28719038 |
| octolytics-dimension-user_login | github-for-unity |
| octolytics-dimension-repository_id | 94770860 |
| octolytics-dimension-repository_nwo | github-for-unity/Unity |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 94770860 |
| octolytics-dimension-repository_network_root_nwo | github-for-unity/Unity |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4943239a6d2c591325dad6e5c313e1cafda877f6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width