Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import-all() support git submodule and add drop-all() function #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions git-rstash
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function push() {

echo "Pushing stash $1 to $remote."

git push $remote stash@{$1}:refs/stashes/$(git rev-parse --short stash@{$1})
# use full length of SHA, because length of --short SHA depends on
# core.abbrev configuration variable, it can be varied from machine to machine.
git push $remote stash@{$1}:refs/stashes/$(git rev-parse stash@{$1})
}

function fetch() {
Expand All @@ -32,12 +34,17 @@ function drop() {
stashNum="$1"
shift
[ -z "$1" ] || remote="$1"
stashCommit=$(git rev-parse --short stash@{$stashNum})
# use full length of SHA, because length of --short SHA depends on
# core.abbrev configuration variable, it can be varied from machine to machine.
stashCommit=$(git rev-parse stash@{$stashNum})

read -p "Really delete stash $stashNum (commit $stashCommit) from $remote? " answer
read -p "Really delete stash $stashNum (commit ${stashCommit:0:8}) from $remote and local refs/stashes? " answer
if [ "$answer" == 'y' ]
then
# remove from remote refs/stashes
git push $remote :refs/stashes/$stashCommit
# remove from local refs/stashes
git update-ref -d refs/stashes/$stashCommit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important; I added this as a comment to the original Stack Overflow answer (and obviously credited you).

fi
}

Expand All @@ -51,12 +58,18 @@ function import() {
SHA="$1"
fi

# Start at 1 because Git won't save if duplicate of stash 0.
for i in $(seq 1 $(expr $(git rev-list --walk-reflogs --count stash) - 1))
do
# If commit matches existing stash, abort early.
diff <(git rev-parse stash@{$i}) <(git rev-parse $SHA) >/dev/null && return 0
done
# check if refs/stash already exist. If not, it is the first stash commit,
# just do the first stash commit. If yes, get the total stash count and
# check if current SHA match the existing stash.
if [ -n "$(git show-ref | grep -e stash$)" ]
then
# Start at 1 because Git won't save if duplicate of stash 0.
for i in $(seq 1 $(expr $(git rev-list --walk-reflogs --count stash) - 1))
do
# If commit matches existing stash, abort early.
diff <(git rev-parse stash@{$i}) <(git rev-parse $SHA) >/dev/null && return 0
done
fi

git stash store --message "$(git show --no-patch --format=format:%s $SHA)" $SHA
}
Expand All @@ -72,12 +85,15 @@ function push-all() {
}

function import-all() {
if [ ! -d .git/refs/stashes ]
# use git show-ref instead of check the existence of directory .git/refs/stashes,
# because in the submodule case, the path of refs was located at .git/modules/<sumodule_name>/refs
stashes=$(git show-ref | grep stashes | awk {'print $2'} | awk -F "/" {'print $3'})
if [ -z "$stashes" ]
then
echo 'Error: no stashes fetched or not in a Git repo.'
fi

for stash in $(ls .git/refs/stashes)
for stash in $stashes
do
import "$stash"
done
Expand Down