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
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ usage: git rstash push <stash> [<remote>]'
or: git rstash drop <stash> [<remote>]'
or: git rstash import <SHA>'
or: git rstash push-all [<remote>]'
or: git rstash drop-all [<remote>]'
or: git rstash import-all'
```

Expand Down
51 changes: 39 additions & 12 deletions git-rstash
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function usage() {
echo ' or: git rstash drop <stash> [<remote>]'
echo ' or: git rstash import <SHA>'
echo ' or: git rstash push-all [<remote>]'
echo ' or: git rstash drop-all [<remote>]'
echo ' or: git rstash import-all'
}

Expand All @@ -16,7 +17,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 +35,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 +59,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 @@ -71,20 +85,33 @@ function push-all() {
done
}

function drop-all() {
remote=origin
[ -z "$1" ] || remote="$1"

for i in $(seq 0 $(expr $(git rev-list --walk-reflogs --count stash) - 1))
do
drop $i "$remote"
done
}

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
}

case "$1" in
push|push-all|fetch|import|import-all)
push|push-all|fetch|import|import-all|drop-all)
"$@"
;;
drop|remove|delete)
Expand Down