[PATCH] Add git-save script

Subsystems: the rest

STALE3707d

5 messages, 3 authors, 2016-06-15 · open the first message on its own page

[PATCH] Add git-save script

From: Nanako Shiraishi <hidden>
Date: 2016-06-15 22:43:17

When my boss has something to show me and I have to update, for some reason I
am always in the middle of doing something else, and git pull command refuses
to work in such a case.

I wrote this little script to save the changes I made, perform the update,
and then come back to where I was, but on top of the updated commit.

Here is how to use my script:

    $ git save
    $ git pull
    $ git save restore

Signed-off-by: Nanako Shiraishi <redacted>
diff --git a/git-save.sh b/git-save.sh
new file mode 100755
index 0000000..b45652e
--- /dev/null
+++ b/git-save.sh
@@ -0,0 +1,74 @@
+#! /bin/sh
+
+. git-sh-setup
+require_work_tree
+
+function save_work () {
+	save=$( (
+		i_tree=$(git-write-tree)
+
+		TMP=$GIT_DIR/.git-save-tmp
+		GIT_INDEX_FILE=$TMP
+		export GIT_INDEX_FILE
+
+		git-read-tree $i_tree
+		git-add -u
+		w_tree=$(git-write-tree)
+		rm $TMP
+		git-read-tree --prefix=base/ HEAD^{tree}
+		git-read-tree --prefix=indx/ $i_tree
+		git-read-tree --prefix=work/ $w_tree
+		git-write-tree
+		rm $TMP
+	) )
+
+	head=$(git-log --abbrev-commit --pretty=oneline -n 1 HEAD)
+	if branch=$(git symbolic-ref -q HEAD); then
+		branch=${branch#refs/heads/}
+	else
+		branch='(no branch)'
+	fi &&
+	msg=$(printf 'WIP on %s "%s"' "$branch" "$head")
+
+	saved=$(printf '%s' "$msg" | git-commit-tree "$save")
+	git update-ref -m "$msg" refs/heads/saved $saved
+	printf 'Saved %s\n' "$msg" >&2
+}
+
+function list_save () {
+	git-log --abbrev-commit --pretty=oneline -g "$@" saved
+}
+
+function show_save () {
+	save=$(git rev-parse --verify --default saved "$1")
+	git diff-tree --stat $save:base $save:work
+}
+
+function restore_save () {
+	save=$(git rev-parse --verify --default saved "$1")
+	h_tree=$(git rev-parse --verify $save:base)
+	i_tree=$(git rev-parse --verify $save:indx)
+	w_tree=$(git rev-parse --verify $save:work)
+
+	git-merge-recursive $h_tree -- HEAD^{tree} $w_tree
+}
+
+if [ "$1" = list ]; then
+	shift
+	if [ "$#" = 0 ]; then
+		set x -n 10
+		shift
+	fi
+	list_save "$@"
+elif [ "$1" = show ]; then
+	shift
+	show_save "$@"
+elif [ "$1" = restore ]; then
+	shift
+	restore_save "$@"
+else
+	save_work
+	git reset --hard
+fi
+
+
-- 
しらいし ななこ http://ivory.ap.teacup.com/nanako3/

----------------------------------------------------------------------
Get a free email address with REAL anti-spam protection.
http://www.bluebottle.com

Re: [PATCH] Add git-save script

From: Johannes Schindelin <hidden>
Date: 2016-06-15 22:43:17

Hi,

On Sat, 23 Jun 2007, Nanako Shiraishi wrote:
Here is how to use my script:

    $ git save
    $ git pull
    $ git save restore
This use case has been discussed often, under the name "git-stash".

Ciao,
Dscho

Re: [PATCH] Add git-save script

From: Bill Lear <hidden>
Date: 2016-06-15 22:43:17

On Saturday, June 23, 2007 at 16:05:22 (+0100) Johannes Schindelin writes:
Hi,

On Sat, 23 Jun 2007, Nanako Shiraishi wrote:
quoted
Here is how to use my script:

    $ git save
    $ git pull
    $ git save restore
This use case has been discussed often, under the name "git-stash".
Was git-stash added as a patch yet?  If not, do you think Nanako's
patch does not address the concerns that were raised in the
discussion you refer to?


Bill

Re: [PATCH] Add git-save script

From: Johannes Schindelin <hidden>
Date: 2016-06-15 22:43:17

Hi,

On Sat, 23 Jun 2007, Bill Lear wrote:
On Saturday, June 23, 2007 at 16:05:22 (+0100) Johannes Schindelin writes:
quoted
Hi,

On Sat, 23 Jun 2007, Nanako Shiraishi wrote:
quoted
Here is how to use my script:

    $ git save
    $ git pull
    $ git save restore
This use case has been discussed often, under the name "git-stash".
Was git-stash added as a patch yet?  If not, do you think Nanako's
patch does not address the concerns that were raised in the
discussion you refer to?
Obviously, I prefer my approach of installing an alias.

Ciao,
Dscho

Re: [PATCH] Add git-save script

From: しらいしななこ <hidden>
Date: 2016-06-15 22:43:18

Quoting Johannes Schindelin [off-list ref]:
Hi,

On Sat, 23 Jun 2007, Nanako Shiraishi wrote:
quoted
Here is how to use my script:

    $ git save
    $ git pull
    $ git save restore
This use case has been discussed often, under the name "git-stash".

Ciao,
Dscho
Thank you for your comments.  Do you suggest I rename the script to git-stash and re-submit after fixing according to Junio's comments?

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/

----------------------------------------------------------------------
Finally - A spam blocker that actually works.
http://www.bluebottle.com
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help