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"
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=
183
log_given=
184
log_message=
185
verify=t
186
+verbose=
187
signoff=
188
force_author=
189
while case "$#" in 0) break;; esac
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
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." ;;
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"
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
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
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"
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 |
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
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"