recover lost tags

This commit is contained in:
Bhupesh-V 2021-04-13 19:05:48 +05:30
parent a854217c4b
commit a7753291e4
2 changed files with 39 additions and 23 deletions

View file

@ -30,9 +30,10 @@
- [x] Undo `git pull`
- [x] Undo `git reset`
- [ ] Undo `git merge`
- [ ] Undo `git tag -d` (tag delete)
- [x] Undo `git tag -d` (tag delete)
- [x] Undo `git stash apply`
- [x] Undo `git stash pop/drop/clear`
- [ ] Undo `git tag` (rename a tag)
## Installation

59
ugit
View file

@ -105,36 +105,51 @@ undo_git_merge() {
}
recover_lost_stash() {
LOST_STASH_HASH=$(git fsck --no-progress --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --oneline --merges --no-walk | fzf --ansi --height 20% --reverse --header="Choose commit associated with stash" | awk '{print $1}')
read -p "Enter Stash Message/Comment: " -r STASH_MSG
if git update-ref refs/stash "$LOST_STASH_HASH" --create-reflog -m "$STASH_MSG"; then
printf "%s\n" "Stash Successfully Recovered"
git stash list | grep "$STASH_MSG"
LOST_STASH=$(git fsck --no-progress --unreachable | grep commit | cut -d ' ' -f3 | \
xargs git log --oneline --merges --no-walk | \
fzf --ansi --height 20% --reverse --header="Choose commit associated with stash" | \
awk '{print $1}')
read -p "Enter Stash Message/Comment: " -r STASH_MSG
if git update-ref refs/stash "$LOST_STASH" --create-reflog -m "$STASH_MSG"; then
printf "%s\n" "Stash Successfully Recovered"
git stash list | grep "$STASH_MSG"
fi
}
undo_git_stash_apply() {
# TODO: option to select which stash to unapply?
# check if diff coloring is set to auto in git config if not the reverse apply command will fail
is_diff_color=$(git config --get color.diff | tr -d '\n')
if [[ "$is_diff_color" == "auto" ]]; then
if git stash show -p | git apply --reverse; then
printf "%s\n" "Done 👍️"
fi
else
printf "%s\n" "Please change diff color to auto in .gitconfig & run ugit again"
printf "%s\n" "Or use the following command [git config --global color.diff \"auto\"]"
fi
}
undo_git_stash_apply() {
# TODO: check if diff coloring is on in git config otherwise the reverse apply command will fail
# TODO: option to select which stash to unapply?
if git stash show -p | git apply --reverse; then
printf "%s\n" "Done 👍️"
fi
}
# WIP
recover_deleted_tag() {
# only works for annotead tags? :(
# only works for annotated tags? :(
# git fsck --unreachable | grep tag
# to see changes git show <COMMIT_HASH>
# git update-ref refs/tags/<tag-name> <COMMIT_HASH>
COMMIT_HASH=$(git fsck --no-progress --unreachable | grep tag | cut -d ' ' -f3)
[[ -z "$COMMIT_HASH" ]] && printf "%s\n" "Unable to find any deleted tags :("; exit
read -p "Enter Lost tag name: " -r TAG_NAME
if git update-ref refs/tags "$TAG_NAME" --create-reflog "$COMMIT_HASH"; then
echo "Tag $TAG_NAME Successfully Recovered"
git tag -l | grep "$TAG_NAME"
read -p "Enter lost Tag name (e.g v1.2): " -r TAG_NAME
COMMIT_HASH=$(git fsck --no-progress --unreachable --tags | awk '/tagged/ {print $6}')
[[ -z "$COMMIT_HASH" ]] && printf "%s\n" "Unable to find any deleted tags :(" && exit 1
OBJECT_TYPE=$(git cat-file -t "$COMMIT_HASH")
if [[ "$OBJECT_TYPE" == "tag" ]] ; then
printf "%s\n" "Found Deleted tag"
fi
DELETED_TAG=$(git cat-file -p "$COMMIT_HASH" | awk '/tag / { print$2 }')
if [[ "$DELETED_TAG" != "$TAG_NAME" ]]; then
printf "%s" "Input tag name $TAG_NAME doesn't match with previously deleted tag $DELETED_TAG"
elif git update-ref refs/tags/"$TAG_NAME" --create-reflog "$COMMIT_HASH"; then
printf "%s\n" "Tag $TAG_NAME Successfully Recovered 👍️"
git cat-file -p "$COMMIT_HASH"
fi
}