2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # From the flashrom project but heavily modified since then.
9 # Make sure we don't get translated output
11 # nor local times or dates
15 git_has_local_changes
() {
16 git update-index
-q --refresh >/dev
/null
17 ! git diff-index
--quiet HEAD
-- "$1"
21 git log
--pretty=format
:"%h" -1 -- "$1"
24 git_is_file_tracked
() {
25 git ls-files
--error-unmatch -- "$1" >/dev
/null
2>&1
29 git_is_file_tracked
"$1"
32 # Tries to find a remote source for the changes committed locally.
33 # This includes the URL of the remote repository including the last commit and a suitable branch name.
34 # Takes one optional argument: the path to inspect
36 # Note: This may not work as expected if multiple remotes are fetched from.
37 echo $
(git remote
-v |
grep "^origin\>" | \
38 awk '/fetch/ {print $2; exit 0}' |
sed "s,^.*@,,")
41 # Returns a string indicating where others can get the current source code (excluding uncommitted changes)
42 # Takes one optional argument: the path to inspect
51 # Retrieve timestamp since last modification. If the sources are pristine,
52 # then the timestamp will match that of the SCM's most recent modification
57 # date syntaxes are manifold:
58 # gnu date [-d input]... [+FORMAT]
59 # netbsd date [-ajnu] [-d date] [-r seconds] [+format] [[[[[[CC]yy]mm]dd]HH]MM[.SS]]
60 # freebsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
61 # dragonflybsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
62 # openbsd date [-aju] [-d dst] [-r seconds] [+format] [[[[[[cc]yy]mm]dd]HH]MM[.SS]] [...]
63 if git_is_file_tracked
"$2" ; then
64 # are there local changes?
65 if git_has_local_changes
"$2" ; then
68 # No local changes, get date of the last commit
70 # Most BSD dates do not support parsing date values from user input with -d but all of
71 # them support parsing epoch seconds with -r. Thanks to git we can easily use that:
72 NetBSD|OpenBSD|DragonFly|FreeBSD
)
73 t
=$
(date -u -r "$(git log --pretty=format:%ct -1 -- $2)" "$1" 2>/dev
/null
);;
75 t
=$
(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev
/null
);;
83 echo "Warning: Could not determine timestamp." 2>/dev
/null
86 # output the time, changing colons to underscores.
87 # gnu make doesn't work in directories with colons
88 echo "${t}" |
tr ':' '_'
91 # Retrieve local SCM revision info. This is useful if we're working in a different SCM than upstream and/or
96 if git_is_file_tracked
"$1" ; then
97 r
=$
(git_last_commit
"$1")
99 if git_has_local_changes
"$1" ; then
103 return ${EXIT_FAILURE}
109 # Similar to local_revision but uses "git describe" instead of "git log" which
110 # includes number of commits since most recent tag.
114 if git_is_file_tracked
"$1" ; then
115 r
=$
(git describe
--tags --dirty)
117 return ${EXIT_FAILURE}
123 upstream_revision
() {
126 r
=$
(git log remotes
/origin
/main
-1 --format=format
:%h
)
129 r
="unknown" # default to unknown
136 ${0} <command> [path]
142 local revision information including an indicator for uncommitted changes
146 similar to -l, but uses \"git describe\" to obtain revision info with tags
148 URL associated with the latest commit
150 date of most recent modification
152 timestamp of most recent modification
158 if [ -n "$action" ]; then
159 echo "Error: Multiple actions given.">&2
168 # The is the main loop
177 action
=local_revision
181 action
=tagged_revision
185 action
=upstream_revision
193 action
="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC
197 action
="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601
201 echo "Error: Invalid option: ${1}"
202 exit ${EXIT_FAILURE};;
204 if [ -z "$query_path" ] ; then
205 if [ ! -e "$1" ] ; then
206 echo "Error: Path \"${1}\" does not exist.">&2
211 echo "Warning: Ignoring over-abundant parameter: \"${1}\"">&2
217 # default to current directory (usually equals the whole repository)
218 if [ -z "$query_path" ] ; then
221 if ! is_file_tracked
"$query_path" ; then
222 echo "Warning: Path \"${query_path}\" is not under version control.">&2
224 if [ -z "$action" ] ; then
226 echo "Error: No actions specified"
230 $action "$query_path"