@matheusbsbs / matheus-bagliano / commits / cf7bb589af
Fork 0

git-status -v

This revamps the git-status command to take the same set of parameters as git commit. It gives a preview of what is being committed with that command. With -v flag, it shows the diff output between the HEAD commit and the index that would be committed if these flags were given to git-commit command. git-commit also acquires -v flag (it used to mean "verify" but that is the default anyway and there is --no-verify to turn it off, so not much is lost), which uses the updated git-status -v to seed the commit log buffer. This is handy for writing a log message while reviewing the changes one last time. Now, git-commit and git-status are internally share the same implementation. Unlike previous git-commit change, this uses a temporary index to prepare the index file that would become the real index file after a successful commit, and moves it to the real index file once the commit is actually made. This makes it safer than the previous scheme, which stashed away the original index file and restored it after an aborted commit. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 10, 2006 at 00:45 UTC cf7bb589af739563c90dc32b4901bea73aaaa9d0
3 files changed +255 -177
Makefile
+5 -2
@@ -107,7 +107,7 @@ SCRIPT_SH = \
107 git-merge-one-file.sh git-parse-remote.sh \
108 git-prune.sh git-pull.sh git-push.sh git-rebase.sh \
109 git-repack.sh git-request-pull.sh git-reset.sh \
110 - git-resolve.sh git-revert.sh git-sh-setup.sh git-status.sh \
110 + git-resolve.sh git-revert.sh git-sh-setup.sh \
111 git-tag.sh git-verify-tag.sh git-whatchanged.sh \
112 git-applymbox.sh git-applypatch.sh git-am.sh \
113 git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
@@ -125,7 +125,7 @@ SCRIPT_PYTHON = \
125 SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \
126 $(patsubst %.perl,%,$(SCRIPT_PERL)) \
127 $(patsubst %.py,%,$(SCRIPT_PYTHON)) \
128 - git-cherry-pick git-show
128 + git-cherry-pick git-show git-status
129
130 # The ones that do not have to link with lcrypto nor lz.
131 SIMPLE_PROGRAMS = \
@@ -443,6 +443,9 @@ git-cherry-pick: git-revert
443 git-show: git-whatchanged
444 cp $< $@
445
446 +git-status: git-commit
447 + cp $< $@
448 +
449 # These can record GIT_VERSION
450 git$X git.spec \
451 $(patsubst %.sh,%,$(SCRIPT_SH)) \
git-commit.sh
+250 -60
@@ -3,13 +3,21 @@
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
5
6 -USAGE='[-a] [-i] [-s] [-v | --no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [<path>...]'
7 -
6 +USAGE='[-a] [-i] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [<path>...]'
7 SUBDIRECTORY_OK=Yes
8 . git-sh-setup
9
11 -git-rev-parse --verify HEAD >/dev/null 2>&1 ||
12 -initial_commit=t
10 +git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11 +branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
12 +
13 +case "$0" in
14 +*status)
15 + status_only=t
16 + unmerged_ok_if_status=--unmerged ;;
17 +*commit)
18 + status_only=
19 + unmerged_ok_if_status= ;;
20 +esac
21
22 refuse_partial () {
23 echo >&2 "$1"
@@ -17,23 +25,155 @@ refuse_partial () {
25 exit 1
26 }
27
20 -SAVE_INDEX="$GIT_DIR/save-index$$"
28 +THIS_INDEX="$GIT_DIR/index"
29 +NEXT_INDEX="$GIT_DIR/next-index$$"
30 +rm -f "$NEXT_INDEX"
31 save_index () {
22 - cp "$GIT_DIR/index" "$SAVE_INDEX"
32 + cp "$THIS_INDEX" "$NEXT_INDEX"
33 +}
34 +
35 +report () {
36 + header="#
37 +# $1:
38 +# ($2)
39 +#
40 +"
41 + trailer=""
42 + while read status name newname
43 + do
44 + printf '%s' "$header"
45 + header=""
46 + trailer="#
47 +"
48 + case "$status" in
49 + M ) echo "# modified: $name";;
50 + D*) echo "# deleted: $name";;
51 + T ) echo "# typechange: $name";;
52 + C*) echo "# copied: $name -> $newname";;
53 + R*) echo "# renamed: $name -> $newname";;
54 + A*) echo "# new file: $name";;
55 + U ) echo "# unmerged: $name";;
56 + esac
57 + done
58 + printf '%s' "$trailer"
59 + [ "$header" ]
60 }
61
62 run_status () {
26 - (
27 - cd "$TOP"
28 - if test '' != "$TMP_INDEX"
63 + (
64 + # We always show status for the whole tree.
65 + cd "$TOP"
66 +
67 + # If TMP_INDEX is defined, that means we are doing
68 + # "--only" partial commit, and that index file is used
69 + # to build the tree for the commit. Otherwise, if
70 + # NEXT_INDEX exists, that is the index file used to
71 + # make the commit. Otherwise we are using as-is commit
72 + # so the regular index file is what we use to compare.
73 + if test '' != "$TMP_INDEX"
74 + then
75 + GIT_INDEX_FILE="$TMP_INDEX"
76 + export GIT_INDEX_FILE
77 + elif test -f "$NEXT_INDEX"
78 + then
79 + GIT_INDEX_FILE="$NEXT_INDEX"
80 + export GIT_INDEX_FILE
81 + fi
82 +
83 + case "$branch" in
84 + refs/heads/master) ;;
85 + *) echo "# On branch $branch" ;;
86 + esac
87 +
88 + if test -z "$initial_commit"
89 + then
90 + if test -z "$verbose"
91 + then
92 + git-diff-index -M --cached --name-status \
93 + --diff-filter=MDTCRA HEAD |
94 + sed -e '
95 + s/\\/\\\\/g
96 + s/ /\\ /g
97 + ' |
98 + report "Updated but not checked in" "will commit"
99 + else
100 + if git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD |
101 + grep .
102 then
30 - GIT_INDEX_FILE="$TMP_INDEX" git-status
103 + false
104 else
32 - git-status
105 + true
106 fi
34 - )
107 + fi
108 + committable="$?"
109 + else
110 + echo '#
111 +# Initial commit
112 +#'
113 + git-ls-files |
114 + sed -e '
115 + s/\\/\\\\/g
116 + s/ /\\ /g
117 + s/^/A /
118 + ' |
119 + report "Updated but not checked in" "will commit"
120 +
121 + committable="$?"
122 + fi
123 +
124 + git-diff-files --name-status |
125 + sed -e '
126 + s/\\/\\\\/g
127 + s/ /\\ /g
128 + ' |
129 + report "Changed but not updated" \
130 + "use git-update-index to mark for commit"
131 +
132 + if test -f "$GIT_DIR/info/exclude"
133 + then
134 + git-ls-files -z --others --directory \
135 + --exclude-from="$GIT_DIR/info/exclude" \
136 + --exclude-per-directory=.gitignore
137 + else
138 + git-ls-files -z --others --directory \
139 + --exclude-per-directory=.gitignore
140 + fi |
141 + perl -e '$/ = "\0";
142 + my $shown = 0;
143 + while (<>) {
144 + chomp;
145 + s|\\|\\\\|g;
146 + s|\t|\\t|g;
147 + s|\n|\\n|g;
148 + s/^/# /;
149 + if (!$shown) {
150 + print "#\n# Untracked files:\n";
151 + print "# (use \"git add\" to add to commit)\n";
152 + print "#\n";
153 + $shown = 1;
154 + }
155 + print "$_\n";
156 + }
157 + '
158 + case "$committable" in
159 + 0)
160 + echo "nothing to commit"
161 + exit 1
162 + esac
163 + exit 0
164 + )
165 }
166
167 +trap '
168 + test -z "$TMP_INDEX" || {
169 + test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
170 + }
171 + rm -f "$NEXT_INDEX"
172 +' 0
173 +
174 +################################################################
175 +# Command line argument parsing and sanity checking
176 +
177 all=
178 also=
179 only=
@@ -43,6 +183,7 @@ no_edit=
183 log_given=
184 log_message=
185 verify=t
186 +verbose=
187 signoff=
188 force_author=
189 while case "$#" in 0) break;; esac
@@ -172,9 +313,9 @@ do
313 signoff=t
314 shift
315 ;;
175 - -v|--v|--ve|--ver|--veri|--verif|--verify)
176 - verify=t
177 - shift
316 + -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
317 + verbose=t
318 + shift
319 ;;
320 --)
321 shift
@@ -189,6 +330,9 @@ do
330 esac
331 done
332
333 +################################################################
334 +# Sanity check options
335 +
336 case "$log_given" in
337 tt*)
338 die "Only one of -c/-C/-F/-m can be used." ;;
@@ -207,9 +351,24 @@ case "$#,$also$only" in
351 # Later when switch the defaults, we will replace them with these:
352 # echo >&2 "assuming --only paths..."
353 # also=
354 +
355 + # If we are going to launch an editor, the message won't be
356 + # shown without this...
357 + test -z "$log_given$status_only" && sleep 1
358 ;;
359 esac
360 unset only
361 +case "$all,$also,$#" in
362 +t,t,*)
363 + die "Cannot use -a and -i at the same time." ;;
364 +t,,[1-9]*)
365 + die "Paths with -a does not make sense." ;;
366 +,t,0)
367 + die "No paths with -i does not make sense." ;;
368 +esac
369 +
370 +################################################################
371 +# Prepare index to have a tree to be committed
372
373 TOP=`git-rev-parse --show-cdup`
374 if test -z "$TOP"
@@ -218,29 +377,25 @@ then
377 fi
378
379 case "$all,$also" in
221 -t,t)
222 - die "Cannot use -a and -i at the same time." ;;
380 t,)
224 - case "$#" in
225 - 0) ;;
226 - *) die "Paths with -a does not make sense." ;;
227 - esac
228 -
381 save_index &&
382 (
383 cd "$TOP"
384 + GIT_INDEX_FILE="$NEXT_INDEX"
385 + export GIT_INDEX_FILE
386 git-diff-files --name-only -z |
387 git-update-index --remove -z --stdin
388 )
389 ;;
390 ,t)
237 - case "$#" in
238 - 0) die "No paths with -i does not make sense." ;;
239 - esac
240 -
391 save_index &&
392 git-diff-files --name-only -z -- "$@" |
243 - (cd "$TOP" && git-update-index --remove -z --stdin)
393 + (
394 + cd "$TOP"
395 + GIT_INDEX_FILE="$NEXT_INDEX"
396 + export GIT_INDEX_FILE
397 + git-update-index --remove -z --stdin
398 + )
399 ;;
400 ,)
401 case "$#" in
@@ -262,43 +417,68 @@ t,)
417 refuse_partial "Different in index and the last commit:
418 $dirty_in_index"
419 fi
265 - commit_only=`git-ls-files -- "$@"` ;;
420 + commit_only=`git-ls-files -- "$@"`
421 +
422 + # Build the temporary index and update the real index
423 + # the same way.
424 + if test -z "$initial_commit"
425 + then
426 + cp "$THIS_INDEX" "$TMP_INDEX"
427 + GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
428 + else
429 + rm -f "$TMP_INDEX"
430 + fi || exit
431 +
432 + echo "$commit_only" |
433 + GIT_INDEX_FILE="$TMP_INDEX" \
434 + git-update-index --add --remove --stdin &&
435 +
436 + save_index &&
437 + echo "$commit_only" |
438 + (
439 + GIT_INDEX_FILE="$NEXT_INDEX"
440 + export GIT_INDEX_FILE
441 + git-update-index --remove --stdin
442 + ) || exit
443 + ;;
444 esac
445 ;;
446 esac
447
270 -git-update-index -q --refresh || exit 1
271 -
272 -trap '
273 - test -z "$TMP_INDEX" || {
274 - test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
275 - }
276 - test -f "$SAVE_INDEX" && mv -f "$SAVE_INDEX" "$GIT_DIR/index"
277 -' 0
448 +################################################################
449 +# If we do as-is commit, the index file will be THIS_INDEX,
450 +# otherwise NEXT_INDEX after we make this commit. We leave
451 +# the index as is if we abort.
452
279 -if test "$TMP_INDEX"
453 +if test -f "$NEXT_INDEX"
454 then
281 - if test -z "$initial_commit"
282 - then
283 - GIT_INDEX_FILE="$TMP_INDEX" git-read-tree HEAD
284 - else
285 - rm -f "$TMP_INDEX"
286 - fi || exit
287 - echo "$commit_only" |
288 - GIT_INDEX_FILE="$TMP_INDEX" git-update-index --add --remove --stdin &&
289 - save_index &&
290 - echo "$commit_only" |
291 - git-update-index --remove --stdin ||
292 - exit
455 + USE_INDEX="$NEXT_INDEX"
456 +else
457 + USE_INDEX="$THIS_INDEX"
458 fi
459
460 +GIT_INDEX_FILE="$USE_INDEX" \
461 + git-update-index -q $unmerged_ok_if_status --refresh || exit
462 +
463 +################################################################
464 +# If the request is status, just show it and exit.
465 +
466 +case "$0" in
467 +*status)
468 + run_status
469 + exit $?
470 +esac
471 +
472 +################################################################
473 +# Grab commit message, write out tree and make commit.
474 +
475 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
476 then
477 if test "$TMP_INDEX"
478 then
479 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
480 else
301 - "$GIT_DIR"/hooks/pre-commit
481 + GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
482 fi || exit
483 fi
484
@@ -398,8 +578,10 @@ else
578 PARENTS=""
579 fi
580
401 -
402 -run_status >>"$GIT_DIR"/COMMIT_EDITMSG
581 +{
582 + test -z "$verbose" || echo '---'
583 + run_status
584 +} >>"$GIT_DIR"/COMMIT_EDITMSG
585 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
586 then
587 rm -f "$GIT_DIR/COMMIT_EDITMSG"
@@ -429,8 +611,14 @@ t)
611 fi
612 esac
613
432 -grep -v '^#' < "$GIT_DIR"/COMMIT_EDITMSG |
433 -git-stripspace > "$GIT_DIR"/COMMIT_MSG
614 +sed -e '
615 + /^---$/{
616 + s///
617 + q
618 + }
619 + /^#/d
620 +' "$GIT_DIR"/COMMIT_EDITMSG |
621 +git-stripspace >"$GIT_DIR"/COMMIT_MSG
622
623 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
624 git-stripspace |
@@ -439,14 +627,20 @@ if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
627 then
628 if test -z "$TMP_INDEX"
629 then
442 - tree=$(git-write-tree)
630 + tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
631 else
632 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
633 rm -f "$TMP_INDEX"
634 fi &&
635 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
636 git-update-ref HEAD $commit $current &&
449 - rm -f -- "$GIT_DIR/MERGE_HEAD"
637 + rm -f -- "$GIT_DIR/MERGE_HEAD" &&
638 + if test -f "$NEXT_INDEX"
639 + then
640 + mv "$NEXT_INDEX" "$THIS_INDEX"
641 + else
642 + : ;# happy
643 + fi
644 else
645 echo >&2 "* no commit message? aborting commit."
646 false
@@ -459,8 +653,4 @@ if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
653 then
654 "$GIT_DIR"/hooks/post-commit
655 fi
462 -if test 0 -eq "$ret"
463 -then
464 - rm -f "$SAVE_INDEX"
465 -fi
656 exit "$ret"
git-status.sh deleted
-115
@@ -1,115 +0,0 @@
1 -#!/bin/sh
2 -#
3 -# Copyright (c) 2005 Linus Torvalds
4 -#
5 -
6 -USAGE=''
7 -SUBDIRECTORY_OK='Yes'
8 -
9 -. git-sh-setup
10 -
11 -if [ "$#" != "0" ]
12 -then
13 - usage
14 -fi
15 -
16 -report () {
17 - header="#
18 -# $1:
19 -# ($2)
20 -#
21 -"
22 - trailer=""
23 - while read status name newname
24 - do
25 - printf '%s' "$header"
26 - header=""
27 - trailer="#
28 -"
29 - case "$status" in
30 - M ) echo "# modified: $name";;
31 - D*) echo "# deleted: $name";;
32 - T ) echo "# typechange: $name";;
33 - C*) echo "# copied: $name -> $newname";;
34 - R*) echo "# renamed: $name -> $newname";;
35 - A*) echo "# new file: $name";;
36 - U ) echo "# unmerged: $name";;
37 - esac
38 - done
39 - printf '%s' "$trailer"
40 - [ "$header" ]
41 -}
42 -
43 -branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
44 -case "$branch" in
45 -refs/heads/master) ;;
46 -*) echo "# On branch $branch" ;;
47 -esac
48 -
49 -git-update-index -q --unmerged --refresh || exit
50 -
51 -if GIT_DIR="$GIT_DIR" git-rev-parse --verify HEAD >/dev/null 2>&1
52 -then
53 - git-diff-index -M --cached --name-status --diff-filter=MDTCRA HEAD |
54 - sed -e '
55 - s/\\/\\\\/g
56 - s/ /\\ /g
57 - ' |
58 - report "Updated but not checked in" "will commit"
59 -
60 - committable="$?"
61 -else
62 - echo '#
63 -# Initial commit
64 -#'
65 - git-ls-files |
66 - sed -e '
67 - s/\\/\\\\/g
68 - s/ /\\ /g
69 - s/^/A /
70 - ' |
71 - report "Updated but not checked in" "will commit"
72 -
73 - committable="$?"
74 -fi
75 -
76 -git-diff-files --name-status |
77 -sed -e '
78 - s/\\/\\\\/g
79 - s/ /\\ /g
80 -' |
81 -report "Changed but not updated" "use git-update-index to mark for commit"
82 -
83 -
84 -if test -f "$GIT_DIR/info/exclude"
85 -then
86 - git-ls-files -z --others --directory \
87 - --exclude-from="$GIT_DIR/info/exclude" \
88 - --exclude-per-directory=.gitignore
89 -else
90 - git-ls-files -z --others --directory \
91 - --exclude-per-directory=.gitignore
92 -fi |
93 -perl -e '$/ = "\0";
94 - my $shown = 0;
95 - while (<>) {
96 - chomp;
97 - s|\\|\\\\|g;
98 - s|\t|\\t|g;
99 - s|\n|\\n|g;
100 - s/^/# /;
101 - if (!$shown) {
102 - print "#\n# Untracked files:\n";
103 - print "# (use \"git add\" to add to commit)\n#\n";
104 - $shown = 1;
105 - }
106 - print "$_\n";
107 - }
108 -'
109 -
110 -case "$committable" in
111 -0)
112 - echo "nothing to commit"
113 - exit 1
114 -esac
115 -exit 0