💩 use better version to restore files

This commit is contained in:
Bhupesh-V 2021-04-30 19:53:06 +05:30
parent f8cf84deca
commit aa3c295ee8
3 changed files with 9 additions and 6 deletions

View file

@ -1,4 +1,4 @@
# Contributing to sample-project
# Contributing to `ugit`
:+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
@ -7,3 +7,4 @@ Make sure you follow below guidelines before contributing.
1. Raise an issue before sending any PR.
2. Make you changes to `feature` branch.
3. See if there is already an open PR for the same issue.

View file

@ -25,7 +25,7 @@
## What's in the box
`ugit` supports undoing following operations, some are a WIP. If you know of any operation that can be undone and is not in the list, make sure to send a quick PR 💛️
`ugit` supports undoing following operations, some are a WIP. If you know of any other operations that can be undone and is not in the list, make sure to send a quick PR 💛️
- [x] Undo `git commit`
- [x] Undo `git add`
@ -48,9 +48,10 @@
**ugit** dependencies:
1. `Bash>=3`
2. GNU utils like `awk`, `grep`, `tput` etc
3. [fzf](https://github.com/junegunn/fzf)
- `Bash`>=3
- GNU utils like `awk`, `grep`, `tput` etc
- [fzf](https://github.com/junegunn/fzf)
1. Install the script using `wget`
```bash

3
ugit
View file

@ -194,12 +194,13 @@ undo_file_delete() {
restore_file() {
FILE=$(git ls-files | fzf --ansi --height 20% --reverse --header="Choose file to restore" | awk '{print $1}')
COMMIT=$(git log --oneline "$FILE" | fzf --ansi --height 80% --reverse --prompt="Choose commit to restore file to" --header="Use ctrl-j/ctrl-k to navigate file preview. ctrl+space to toggle preview" --preview "echo {} | cut -d' ' -f1 | xargs -I{} git show {}:$FILE" --bind 'j:down,k:up,ctrl-j:preview-down,ctrl-k:preview-up,ctrl-space:toggle-preview' --preview-window down:60%)
COMMIT=$(printf "%s" "$COMMIT" | awk '{print $1}')
if ! git diff -s --quiet "$FILE"; then
# check if any local changes
printf "%s\n" "$FILE seems to be modified. Please either commit or discard those changes."
exit 0
elif git reset "$COMMIT" "$FILE"; then
elif git restore --source="$COMMIT" "$FILE"; then
printf "%s\n" "$FILE restored to version at $COMMIT"
fi
}