Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 1 | #!/bin/sh |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2005 Linus Torvalds |
| 4 | # |
| 5 | |
Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 6 | . git-sh-setup-script || die "Not a git archive" |
| 7 | |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 8 | usage () { |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 9 | die 'git commit [-a] [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]' |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 10 | } |
| 11 | |
Junio C Hamano | 0cfe1d3 | 2005-08-12 23:39:15 -0700 | [diff] [blame] | 12 | all= logfile= use_commit= no_edit= log_given= log_message= verify= signoff= |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 13 | while case "$#" in 0) break;; esac |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 14 | do |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 15 | case "$1" in |
| 16 | -a|--a|--al|--all) |
| 17 | all=t |
| 18 | shift ;; |
| 19 | -F=*|--f=*|--fi=*|--fil=*|--file=*) |
| 20 | log_given=t$log_given |
| 21 | logfile=`expr "$1" : '-[^=]*=\(.*\)'` |
| 22 | no_edit=t |
| 23 | shift ;; |
| 24 | -F|--f|--fi|--fil|--file) |
| 25 | case "$#" in 1) usage ;; esac; shift |
| 26 | log_given=t$log_given |
| 27 | logfile="$1" |
| 28 | no_edit=t |
| 29 | shift ;; |
| 30 | -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*) |
| 31 | log_given=t$log_given |
| 32 | log_message=`expr "$1" : '-[^=]*=\(.*\)'` |
| 33 | no_edit=t |
| 34 | shift ;; |
| 35 | -m|--m|--me|--mes|--mess|--messa|--messag|--message) |
| 36 | case "$#" in 1) usage ;; esac; shift |
| 37 | log_given=t$log_given |
| 38 | log_message="$1" |
| 39 | no_edit=t |
| 40 | shift ;; |
| 41 | -c=*|--ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\ |
| 42 | --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\ |
| 43 | --reedit-messag=*|--reedit-message=*) |
| 44 | log_given=t$log_given |
| 45 | use_commit=`expr "$1" : '-[^=]*=\(.*\)'` |
| 46 | shift ;; |
| 47 | -c|--ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\ |
| 48 | --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message) |
| 49 | case "$#" in 1) usage ;; esac; shift |
| 50 | log_given=t$log_given |
| 51 | use_commit="$1" |
| 52 | shift ;; |
| 53 | -C=*|--reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\ |
| 54 | --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\ |
| 55 | --reuse-message=*) |
| 56 | log_given=t$log_given |
| 57 | use_commit=`expr "$1" : '-[^=]*=\(.*\)'` |
| 58 | no_edit=t |
| 59 | shift ;; |
| 60 | -C|--reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\ |
| 61 | --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message) |
| 62 | case "$#" in 1) usage ;; esac; shift |
| 63 | log_given=t$log_given |
| 64 | use_commit="$1" |
| 65 | no_edit=t |
| 66 | shift ;; |
Junio C Hamano | 0cfe1d3 | 2005-08-12 23:39:15 -0700 | [diff] [blame] | 67 | -s|--s|--si|--sig|--sign|--signo|--signof|--signoff) |
| 68 | signoff=t |
| 69 | shift ;; |
| 70 | -v|--v|--ve|--ver|--veri|--verif|--verify) |
| 71 | verify=t |
| 72 | shift ;; |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 73 | --) |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 74 | shift |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 75 | break ;; |
| 76 | -*) |
| 77 | usage ;; |
| 78 | *) |
| 79 | break ;; |
| 80 | esac |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 81 | done |
| 82 | |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 83 | case "$log_given" in |
| 84 | tt*) |
| 85 | die "Only one of -c/-C/-F/-m can be used." ;; |
| 86 | esac |
| 87 | |
| 88 | case "$all" in |
| 89 | t) |
| 90 | git-diff-files --name-only -z | |
Junio C Hamano | 22cff6a | 2005-08-16 18:08:19 -0700 | [diff] [blame^] | 91 | xargs -0 git-update-cache -q -- |
| 92 | ;; |
| 93 | *) |
| 94 | git-diff-files --name-only -z "$@" | |
| 95 | xargs -0 git-update-cache -q -- |
| 96 | ;; |
| 97 | esac || exit 1 |
| 98 | git-update-cache -q --refresh || exit 1 |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 99 | |
Junio C Hamano | 0cfe1d3 | 2005-08-12 23:39:15 -0700 | [diff] [blame] | 100 | case "$verify" in |
| 101 | t) |
| 102 | # This is slightly modified from Andrew Morton's Perfect Patch. |
| 103 | # Lines you introduce should not have trailing whitespace. |
| 104 | # Also check for an indentation that has SP before a TAB. |
| 105 | perl -e ' |
| 106 | my $fh; |
| 107 | my $found_bad = 0; |
| 108 | my $filename; |
| 109 | my $reported_filename = ""; |
| 110 | my $lineno; |
| 111 | sub bad_line { |
| 112 | my ($why, $line) = @_; |
| 113 | if (!$found_bad) { |
| 114 | print "*\n"; |
| 115 | print "* You have some suspicious patch lines:\n"; |
| 116 | print "*\n"; |
| 117 | $found_bad = 1; |
| 118 | } |
| 119 | if ($reported_filename ne $filename) { |
| 120 | print "* In $filename\n"; |
| 121 | $reported_filename = $filename; |
| 122 | } |
| 123 | print "* $why (line $lineno)\n$line\n"; |
| 124 | } |
| 125 | open $fh, "-|", qw(git-diff-cache -p -M --cached HEAD); |
| 126 | while (<$fh>) { |
| 127 | if (m|^diff --git a/(.*) b/\1$|) { |
| 128 | $filename = $1; |
| 129 | next; |
| 130 | } |
| 131 | if (/^@@ -\S+ \+(\d+)/) { |
| 132 | $lineno = $1 - 1; |
| 133 | next; |
| 134 | } |
| 135 | if (/^ /) { |
| 136 | $lineno++; |
| 137 | next; |
| 138 | } |
| 139 | if (s/^\+//) { |
| 140 | $lineno++; |
| 141 | chomp; |
| 142 | if (/\s$/) { |
| 143 | bad_line("trailing whitespace", $_); |
| 144 | } |
| 145 | if (/^\s* /) { |
| 146 | bad_line("indent SP followed by a TAB", $_); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | exit($found_bad); |
| 151 | ' || exit ;; |
| 152 | esac |
| 153 | |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 154 | PARENTS="-p HEAD" |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 155 | if [ ! -r "$GIT_DIR/HEAD" ]; then |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 156 | if [ -z "$(git-ls-files)" ]; then |
| 157 | echo Nothing to commit 1>&2 |
| 158 | exit 1 |
| 159 | fi |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 160 | { |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 161 | echo "#" |
| 162 | echo "# Initial commit" |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 163 | case "$no_edit" in |
| 164 | t) echo "# (ignoring your commit message for initial commit)" |
| 165 | no_edit= |
| 166 | esac |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 167 | echo "#" |
| 168 | git-ls-files | sed 's/^/# New file: /' |
| 169 | echo "#" |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 170 | } >.editmsg |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 171 | PARENTS="" |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 172 | no_edit= |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 173 | else |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 174 | if [ -f "$GIT_DIR/MERGE_HEAD" ]; then |
Junio C Hamano | 2150cc9 | 2005-08-15 15:37:37 -0700 | [diff] [blame] | 175 | { |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 176 | echo "#" |
| 177 | echo "# It looks like your may be committing a MERGE." |
| 178 | echo "# If this is not correct, please remove the file" |
| 179 | echo "# $GIT_DIR/MERGE_HEAD" |
| 180 | echo "# and try again" |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 181 | case "$no_edit" in |
| 182 | t) echo "# (ignoring your commit message for merge commit)" |
| 183 | no_edit= |
| 184 | esac |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 185 | echo "#" |
Junio C Hamano | 2150cc9 | 2005-08-15 15:37:37 -0700 | [diff] [blame] | 186 | } | |
| 187 | git-stripspace >.editmsg |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 188 | PARENTS="-p HEAD -p MERGE_HEAD" |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 189 | elif test "$log_message" != '' |
| 190 | then |
Junio C Hamano | 2150cc9 | 2005-08-15 15:37:37 -0700 | [diff] [blame] | 191 | echo "$log_message" | |
| 192 | git-stripspace >.editmsg |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 193 | elif test "$logfile" != "" |
| 194 | then |
| 195 | if test "$logfile" = - |
| 196 | then |
| 197 | test -t 0 && |
| 198 | echo >&2 "(reading log message from standard input)" |
| 199 | cat |
| 200 | else |
| 201 | cat <"$logfile" |
Junio C Hamano | 2150cc9 | 2005-08-15 15:37:37 -0700 | [diff] [blame] | 202 | fi | |
| 203 | git-stripspace >.editmsg |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 204 | elif test "$use_commit" != "" |
| 205 | then |
| 206 | pick_author_script=' |
| 207 | /^author /{ |
| 208 | h |
| 209 | s/^author \([^<]*\) <[^>]*> .*$/\1/ |
| 210 | s/'\''/'\''\'\'\''/g |
| 211 | s/.*/GIT_AUTHOR_NAME='\''&'\''/p |
| 212 | |
| 213 | g |
| 214 | s/^author [^<]* <\([^>]*\)> .*$/\1/ |
| 215 | s/'\''/'\''\'\'\''/g |
| 216 | s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p |
| 217 | |
| 218 | g |
| 219 | s/^author [^<]* <[^>]*> \(.*\)$/\1/ |
| 220 | s/'\''/'\''\'\'\''/g |
| 221 | s/.*/GIT_AUTHOR_DATE='\''&'\''/p |
| 222 | |
| 223 | q |
| 224 | } |
| 225 | ' |
| 226 | set_author_env=`git-cat-file commit "$use_commit" | |
| 227 | sed -ne "$pick_author_script"` |
| 228 | eval "$set_author_env" |
| 229 | export GIT_AUTHOR_NAME |
| 230 | export GIT_AUTHOR_EMAIL |
| 231 | export GIT_AUTHOR_DATE |
| 232 | git-cat-file commit "$use_commit" | |
Junio C Hamano | 2150cc9 | 2005-08-15 15:37:37 -0700 | [diff] [blame] | 233 | sed -e '1,/^$/d' | |
| 234 | git-stripspace >.editmsg |
| 235 | fi |
| 236 | |
Junio C Hamano | 0cfe1d3 | 2005-08-12 23:39:15 -0700 | [diff] [blame] | 237 | case "$signoff" in |
| 238 | t) |
| 239 | git-var GIT_COMMITTER_IDENT | sed -e ' |
| 240 | s/>.*/>/ |
| 241 | s/^/Signed-off-by: /' >>.editmsg ;; |
| 242 | esac |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 243 | git-status-script >>.editmsg |
Linus Torvalds | 96069cf | 2005-06-14 10:20:14 -0700 | [diff] [blame] | 244 | fi |
Johannes Schindelin | eca35ec | 2005-08-05 17:34:38 +0200 | [diff] [blame] | 245 | if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ] |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 246 | then |
| 247 | cat .editmsg |
Linus Torvalds | 170241b | 2005-06-19 19:57:01 -0700 | [diff] [blame] | 248 | rm .editmsg |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 249 | exit 1 |
| 250 | fi |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 251 | case "$no_edit" in |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 252 | '') |
| 253 | ${VISUAL:-${EDITOR:-vi}} .editmsg |
| 254 | ;; |
| 255 | esac |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 256 | grep -v '^#' < .editmsg | git-stripspace > .cmitmsg |
Junio C Hamano | 0cfe1d3 | 2005-08-12 23:39:15 -0700 | [diff] [blame] | 257 | grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk |
| 258 | if test -s .cmitchk |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 259 | then |
Linus Torvalds | 170241b | 2005-06-19 19:57:01 -0700 | [diff] [blame] | 260 | tree=$(git-write-tree) && |
| 261 | commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) && |
Junio C Hamano | 5fec3ef | 2005-06-25 02:22:05 -0700 | [diff] [blame] | 262 | echo $commit > "$GIT_DIR/HEAD" && |
| 263 | rm -f -- "$GIT_DIR/MERGE_HEAD" |
Junio C Hamano | 0c09129 | 2005-08-08 17:03:14 -0700 | [diff] [blame] | 264 | else |
| 265 | echo >&2 "* no commit message? aborting commit." |
| 266 | false |
| 267 | fi |
Linus Torvalds | 170241b | 2005-06-19 19:57:01 -0700 | [diff] [blame] | 268 | ret="$?" |
Junio C Hamano | 0cfe1d3 | 2005-08-12 23:39:15 -0700 | [diff] [blame] | 269 | rm -f .cmitmsg .editmsg .cmitchk |
Linus Torvalds | 170241b | 2005-06-19 19:57:01 -0700 | [diff] [blame] | 270 | exit "$ret" |