Skip to content

Commit 552158c

Browse files
committed
git-commit-filetree: remove duplicate code; slight re-organization.
* Give err() a return status param, and use it for all error returns. * Return error status 255 for any internal error. * Move some code around for clarity. * Indicate why we export GIT_INDEX_FILE where we do.
1 parent b5003fa commit 552158c

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

‎git-commit-filetree‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,32 @@
22
set -e
33

44
index_file=$(mktemp)
5-
65
internal_error=true
7-
trap 'rm -f $index_file; $internal_error && err "INTERNAL ERROR. FAILED!"' 0
6+
trap 'rm -f $index_file; $internal_error && err 255 "INTERNAL ERROR. FAILED!"' 0
87

9-
usage="Usage: git commit-filetree <branch> <path>"
10-
[ -z "$2" ] && {
11-
echo 1>&2 "$usage"
8+
err() {
9+
local exitcode="$1"; shift
1210
internal_error=false
13-
exit 129
11+
echo 1>&2 "$@"
12+
exit $exitcode
1413
}
15-
branch=refs/heads/$(echo $1 | sed -e 's,^refs/heads/,,')
1614

17-
err() { internal_error=false; echo 1>&2 "$@"; exit 1; }
15+
[ -z "$2" ] && err 129 "Usage: git commit-filetree <branch> <path>"
1816

1917
# Check that we have no uncommited or unknown files.
2018
git diff-index --quiet HEAD \
21-
|| err "Cannot commit with uncommited files in working copy."
19+
|| err 1 "Cannot commit with uncommited files in working copy."
2220
[ -z "$(git ls-files --exclude-standard --others)" ] \
23-
|| err "Cannot commit with untracked files in working copy."
21+
|| err 1 "Cannot commit with untracked files in working copy."
2422

25-
git show-ref -q --verify $branch || {
26-
internal_error=false
27-
echo 1>&2 "Invalid ref: $branch"
28-
exit 128
29-
}
23+
branch=refs/heads/$(echo $1 | sed -e 's,^refs/heads/,,')
24+
git show-ref -q --verify $branch || err 128 "Invalid ref: $branch"
3025

3126
source_sha=$(git show --quiet --pretty='format:%h')
3227

28+
# Switch to an index separate from repo working copy.
3329
export GIT_INDEX_FILE=$index_file
30+
3431
git read-tree --empty
3532
git --work-tree=$2 add -A
3633
tree_sha=$(git write-tree)
@@ -40,8 +37,6 @@ parent_tree_sha=$(git log -1 $branch --format='%T')
4037
commit_msg="Build from source commit $source_sha."
4138
commit_sha=$(echo "$commit_msg" | git commit-tree $tree_sha -p $branch)
4239

43-
44-
4540
# XXX use -m below to update the log file as well
4641
# (See /usr/share/doc/git/contrib/examples/git-commit.sh for an example.)
4742
git update-ref $branch $commit_sha

0 commit comments

Comments
 (0)