git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH (3rd try)] Add git-stash script

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 30, 2007, 17:19 UTC
Message-ID
<7v8xa1e4wo.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0706301641000.4438@racer.site>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 5 quoted lines
>> diff --git a/git-stash.sh b/git-stash.sh
>> [...]
>> +	printf >&2 'Saved WIP on %s\n' "$msg"
>
> You have an awful lot of printfs in the code. Why not just use echos?

I had the same impression, but I would say it is Ok (actually printf is even better). Judging from our recent changes (e.g. a23bfae) I think it is prudent to avoid echo when printing user supplied messages. Not that corrupted backslash sequence matters that much on stderr, though ;-).

>> +list_stash () {
>> +	git-log --pretty=oneline -g "$@" $ref_stash |
>
> Wouldn't you want "--default $ref_stash" here?

But "git log --pretty=oneline -g master --default refs/stash" would not make much sense would it? The design currently seems to follow what I suggested earlier, namely to use a single stash ref per repository, so $ref_stash is spelled as a variable but it is a constant. IOW, you do not specify "alternate stash" from the command line.

Unfortunately we do not have a good way to reject non-flag rev parameters in "$@" to error out "git stash list master". What we would want here is a way to allow things like --since=1.hour and -20 while rejecting branch/tag names and other parameters.

Show 6 quoted lines
>> +apply_stash () {
>> +	git-diff-files --quiet ||
>> +		die 'Cannot restore on top of a dirty state'
>
> You meant "no_changes", right? I think you miss changes in the index 
> otherwise.

Interestingly, I think this is actually correct in the sense that the code is internally consistent, as it uses the current index, not the HEAD, as the base of application. It however is debatable if this sequence (which is allowed because it does not use no_changes here) makes sense:

	: hack hack hack
	: get interrupted
	$ git stash
	: do something else on a clean slate
	$ git commit
        : hack hack hack
        $ git add -u
        $ git unstash
Show 6 quoted lines
>> +		git-diff --cached --name-only --diff-filter=A $c_tree >"$a" &&
>> +		git-read-tree --reset $c_tree &&
>> +		git-update-index --add --stdin <"$a" ||
>> +			die "Cannot unstage modified files"
>
> Isn't there a way to avoid the temporary file here?
We could obviously do
	files=$(git-diff --cached ...) &&
        git-read-tree --reset $c_tree &&
        echo "$files" | git-update-index --add --stdin
Oops, the last "echo" may have to be "printf '%s'" ;-)

There might be quite many files that have been added to make the $files variable too big to be given to echo, though.

	git-diff --cached ... |
        (
        	git-read-tree --reset $c_tree &&
                git-update-index --add --stdin
	)

would not work, unless you copy the index into a temporary file and have the upstream git-diff use it. But then we are using a temporary file anyway.

Show 5 quoted lines
>> +		set x -n 10
>> +		shift
>
> This is more elegantly written as "set -- -n 10", or in our context even 
> "set -- -10".

That is Ok in POSIX only world, but so far we have stayed away from using "set -- potentially-dangerous-string" in any of our scripts. I would feel x + shift is safer and more comfortable but probably that is largely because I am old fashioned.

Previous: Johannes SchindelinNext: しらいしななこ
Message 7 of 34 in “Add git-stash script”
  1. Add git-stash scriptしらいしななこ, Jun 30, 2007
  2. Johannes SchindelinJun 30, 2007
  3. Add git-stash scriptしらいしななこ, Jun 30, 2007
  4. Jeff KingJun 30, 2007
  5. Junio C HamanoJun 30, 2007
  6. Johannes SchindelinJun 30, 2007
  7. Junio C HamanoJun 30, 2007
  8. しらいしななこJun 30, 2007
  9. Add a manual page for git-stashJohannes Schindelin, Jun 30, 2007
  10. Frank LichtenheldJun 30, 2007
  11. Johannes SchindelinJun 30, 2007
  12. Frank LichtenheldJun 30, 2007
  13. Junio C HamanoJun 30, 2007
  14. Johannes SchindelinJun 30, 2007
  15. Junio C HamanoJun 30, 2007
  16. Johannes SchindelinJun 30, 2007
  17. Document git-stashしらいしななこ, Jul 1, 2007
  18. Junio C HamanoJul 1, 2007
  19. Jeff KingJul 1, 2007
  20. Junio C HamanoJul 1, 2007
  21. しらいしななこJul 1, 2007
  22. Junio C HamanoJul 1, 2007
  23. Johannes SchindelinJul 1, 2007
  24. Jeff KingJul 2, 2007
  25. Johannes SchindelinJul 2, 2007
  26. git-stash: Make "save" the default operation againJohannes Schindelin, Jul 2, 2007
  27. Jeff KingJul 2, 2007
  28. Johannes SchindelinJul 2, 2007
  29. Junio C HamanoJul 2, 2007
  30. Junio C HamanoJul 1, 2007
  31. Add tests for git-stashJohannes Schindelin, Jun 30, 2007
  32. Jeff KingJul 1, 2007
  33. Junio C HamanoJul 1, 2007
  34. Jeff KingJul 2, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.