From d12feb1fa40f9f73412f2609a6bcf845197e2cf8 Mon Sep 17 00:00:00 2001 From: allenyllee Date: Tue, 18 May 2021 17:06:26 +0800 Subject: [PATCH 1/5] import-all() function support git submodule --- git-rstash | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/git-rstash b/git-rstash index b75edc2..55ae762 100755 --- a/git-rstash +++ b/git-rstash @@ -72,12 +72,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//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 From 67109202529910d233cc05407f1176b0cf3a731d Mon Sep 17 00:00:00 2001 From: allenyllee Date: Tue, 18 May 2021 17:07:54 +0800 Subject: [PATCH 2/5] fix import() function prompt error when its first time stash commit --- git-rstash | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/git-rstash b/git-rstash index 55ae762..3147b25 100755 --- a/git-rstash +++ b/git-rstash @@ -51,12 +51,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 } From fdb0a70c3f62b8000453cc498e480373e218b91a Mon Sep 17 00:00:00 2001 From: allenyllee Date: Tue, 18 May 2021 17:12:41 +0800 Subject: [PATCH 3/5] push() and drop() function use full length SHA instead of short SHA (because I've encounter a problem of different short SHA length between machines) --- git-rstash | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/git-rstash b/git-rstash index 3147b25..0d1b428 100755 --- a/git-rstash +++ b/git-rstash @@ -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() { @@ -32,9 +34,11 @@ 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? " answer if [ "$answer" == 'y' ] then git push $remote :refs/stashes/$stashCommit From b0533cf073250e19598ee378fcac4b041b2d7ea1 Mon Sep 17 00:00:00 2001 From: allenyllee Date: Tue, 18 May 2021 17:13:49 +0800 Subject: [PATCH 4/5] Also delete local refs/stashes when drop() remote refs/stashes --- git-rstash | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/git-rstash b/git-rstash index 0d1b428..997a2d3 100755 --- a/git-rstash +++ b/git-rstash @@ -38,10 +38,13 @@ function drop() { # 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:0:8}) 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 fi } From 91ee352902d0c7a0881b89068bdd1b714dd1744e Mon Sep 17 00:00:00 2001 From: allenyllee Date: Tue, 18 May 2021 17:15:14 +0800 Subject: [PATCH 5/5] add drop-all() function --- README.md | 1 + git-rstash | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 108737a..10a2290 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ usage: git rstash push []' or: git rstash drop []' or: git rstash import ' or: git rstash push-all []' + or: git rstash drop-all []' or: git rstash import-all' ``` diff --git a/git-rstash b/git-rstash index 997a2d3..fb8c3a6 100755 --- a/git-rstash +++ b/git-rstash @@ -6,6 +6,7 @@ function usage() { echo ' or: git rstash drop []' echo ' or: git rstash import ' echo ' or: git rstash push-all []' + echo ' or: git rstash drop-all []' echo ' or: git rstash import-all' } @@ -84,6 +85,16 @@ 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() { # 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//refs @@ -100,7 +111,7 @@ function import-all() { } case "$1" in - push|push-all|fetch|import|import-all) + push|push-all|fetch|import|import-all|drop-all) "$@" ;; drop|remove|delete)