~ecs/tm

ref: 0022137d89f12741ac3e6cf4173aa876cda5a726 tm/tm-commit-tree -rwxr-xr-x 1.4 KiB
0022137dEmber Sawady Fix shellcheck warnings 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh -eu

# shellcheck source=./lib.sh
. "$(dirname -- "$0")/lib.sh"
usage() {
	abort "usage: tm commit-tree [-p <parent>]... <tree>"
}

parents=

while getopts p: opt; do
	case "$opt" in
	p)
		ref="$(tm resolve-ref "$OPTARG")" \
			|| abort "error: invalid ref $OPTARG"
		parents="$parents $ref"
		;;
	?)
		usage
		;;
	esac
done

shift "$((OPTIND - 1))"

if [ $# -ne 1 ]; then
	usage
fi
set +u
[ -z "$TM_AUTHOR" ] && abort "error: TM_AUTHOR unset"
set -u
TM_COMMITTER="${TM_COMMITTER:-$TM_AUTHOR}"
# POSIX equivalent of date -u +%s. Enjoy your nightmares
TM_DATE="${TM_DATE:-$(($(date -u +"((%Y-1600)*365+(%Y-1601)/4-(%Y-1601)/100+(%Y-1601)/400+1%j-1000-135140)*86400+(1%H-100)*3600+(1%M-100)*60+(1%S-100)")))}"
tmp="$TMPDIR/commit-tree"
printf 'commit\n' >>"$tmp"
ref="$(tm resolve-ref "$1")" || abort "error: invalid ref $1"
printf "tree %s\n" "$ref" >>"$tmp"
for parent in $parents; do
	printf "parent %s\n" "$parent" >>"$tmp"
done
printf 'author %s\ncomitter %s\ndate %s\n' "$TM_AUTHOR" \
	"$TM_COMMITTER" "$TM_DATE" >>"$tmp"
tty >/dev/null && printf 'Commit subject:\n' >&2
IFS= read -r "subject"
[ "$(printf "%s" "$subject" | wc -c)" -gt 72 ] \
	&& abort "error: subject is longer than 72 chars"
[ "$(printf "%s" "$subject" | wc -c)" -gt 50 ] \
	&& echo "warning: subject is longer than 50 chars"
printf '%s\n' "$subject" >>"$tmp"
tty >/dev/null && printf 'Commit message:\n' >&2
cat >>"$tmp"

write <"$tmp"