Skip to content

Commit e39b1ab

Browse files
authored
Fix: harden utimes, use single quotes (#1109)
* Fix: harden utimes, use single quotes * Remove passing --posix to bash * Remove double-quotes and backslashes * Fix: patch git-utimes (fixes #1118) Fixes #1118 * Fix formatting issue in git-utimes Per #1109 (comment)
1 parent 058cb07 commit e39b1ab

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

bin/git-utimes

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2312,SC2248,SC2250,SC2064,SC2086
2+
# shellcheck disable=SC2312
33
#
44
# Change files modification time to their last commit date
55
#
@@ -34,13 +34,6 @@ fi
3434
if bash --help 2>&1 | grep -q -- '--norc'; then
3535
bash_opts="${bash_opts} --norc"
3636
fi
37-
# sanity check, not required:
38-
if bash --help 2>&1 | grep -q -- '--posix'; then
39-
bash_opts="${bash_opts} --posix"
40-
fi
41-
42-
prefix="$(git rev-parse --show-prefix) "
43-
strip="${#prefix}"
4437

4538
status_opts=
4639
whatchanged_opts=
@@ -55,16 +48,22 @@ if git status --help 2>&1 | grep -q -- "--ignored"; then
5548
status_opts="${status_opts} --ignored=no"
5649
fi
5750

51+
prefix="$(git rev-parse --show-prefix) "
52+
strip="${#prefix}"
53+
5854
tmpfile=$(mktemp)
55+
# shellcheck disable=SC2064
5956
trap "rm -f '${tmpfile}'" 0
6057

6158
# prefix is stripped:
59+
# shellcheck disable=SC2086
6260
git --no-pager status --porcelain --short ${status_opts} . |
6361
cut -c 4- >"${tmpfile}"
6462

6563
# prefix is not stripped:
64+
# shellcheck disable=SC1003,SC2086,SC2248
6665
git --no-pager whatchanged ${whatchanged_opts} --format='%ct' . |
67-
awk $awk_flags \
66+
awk ${awk_flags} \
6867
-F'\t' \
6968
-v date_flags="${date_flags}" \
7069
-v op="${op}" \
@@ -107,8 +106,13 @@ FILENAME==tmpfile {
107106
next
108107
}
109108
seen[$2]=1
110-
# escape quotes:
111-
gsub(/"/, "\\\"", $2)
112-
printf("t %s \"%s\"\n", ct, $2)
109+
# remove double quotes and backslashes that git adds:
110+
if (substr($2, 1, 1) == "\"" && substr($2, length($2), 1) == "\"") {
111+
$2 = substr($2, 2, length($2) - 2)
112+
gsub(/\\/, "", $2)
113+
}
114+
# escape single quotes:
115+
gsub(/'\''/, "'\''\\'\'''\''", $2)
116+
printf("t %s '\''%s'\''\n", ct, $2)
113117
}
114118
' "${tmpfile}" - | BASH_ENV='' bash ${bash_opts} /dev/stdin

0 commit comments

Comments
 (0)