Debian Codegraph @

util/board_status/getrevision.sh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. sh
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 #
4 # From the flashrom project but heavily modified since then.
5
6 EXIT_SUCCESS=0
7 EXIT_FAILURE=1
8
9 # Make sure we don't get translated output
10 export LC_ALL=C
11 # nor local times or dates
12 export TZ=UTC0
13
14 # Helper functions
15 git_has_local_changesscip-shell shell coreboot 25.09+dfsg-1 git_has_local_changes().() {
16 gitscip-shell system . . git. update-index -q --refresh >/dev/nullscip-shell filesystem . . `/dev/null`.
17 ! gitscip-shell system . . git. diff-index --quiet HEAD -- "$1"
18 }
19
20 git_last_commitscip-shell shell coreboot 25.09+dfsg-1 git_last_commit().() {
21 gitscip-shell system . . git. log --pretty=format:"%h" -1 -- "$1"
22 }
23
24 git_is_file_trackedscip-shell shell coreboot 25.09+dfsg-1 git_is_file_tracked().() {
25 gitscip-shell system . . git. ls-files --error-unmatch -- "$1" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
26 }
27
28 is_file_trackedscip-shell shell coreboot 25.09+dfsg-1 is_file_tracked().() {
29 git_is_file_trackedscip-shell shell coreboot 25.09+dfsg-1 git_is_file_tracked(). "$1"
30 }
31
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
35 git_urlscip-shell shell coreboot 25.09+dfsg-1 git_url().() {
36 # Note: This may not work as expected if multiple remotes are fetched from.
37 echoscip-shell system . . echo. $(gitscip-shell system . . git. remote -v | grepscip-shell system . . grep. "^origin\>" | \
38 awk '/fetch/ {print $2; exit 0}' | sed "s,^.*@,,")
39 }
40
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
43 scm_urlscip-shell shell coreboot 25.09+dfsg-1 scm_url().() {
44 local url
45
46 url="$(git_urlscip-shell shell coreboot 25.09+dfsg-1 git_url(). "$1")"
47
48 echoscip-shell system . . echo. "${url}"
49 }
50
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
53 # date.
54 timestampscip-shell shell coreboot 25.09+dfsg-1 timestamp().() {
55 local t
56
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_trackedscip-shell shell coreboot 25.09+dfsg-1 git_is_file_tracked(). "$2" ; then
64 # are there local changes?
65 if git_has_local_changesscip-shell shell coreboot 25.09+dfsg-1 git_has_local_changes(). "$2" ; then
66 t=$(datescip-shell system . . date. -u "${1}")
67 else
68 # No local changes, get date of the last commit
69 case $(unamescip-shell system . . uname.) in
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=$(datescip-shell system . . date. -u -r "$(gitscip-shell system . . git. log --pretty=format:%ct -1 -- $2)" "$1" 2>/dev/nullscip-shell filesystem . . `/dev/null`.);;
74 *)
75 t=$(datescip-shell system . . date. -d "$(gitscip-shell system . . git. log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev/nullscip-shell filesystem . . `/dev/null`.);;
76 esac
77 fi
78 else
79 t=$(datescip-shell system . . date. -u "$1")
80 fi
81
82 if [scip-shell system . . `[`. -z "$t" ]; then
83 echoscip-shell system . . echo. "Warning: Could not determine timestamp." 2>/dev/nullscip-shell filesystem . . `/dev/null`.
84 fi
85
86 # output the time, changing colons to underscores.
87 # gnu make doesn't work in directories with colons
88 echoscip-shell system . . echo. "${t}" | trscip-shell system . . tr. ':' '_'
89 }
90
91 # Retrieve local SCM revision info. This is useful if we're working in a different SCM than upstream and/or
92 # have local changes.
93 local_revisionscip-shell shell coreboot 25.09+dfsg-1 local_revision().() {
94 local r
95
96 if git_is_file_trackedscip-shell shell coreboot 25.09+dfsg-1 git_is_file_tracked(). "$1" ; then
97 r=$(git_last_commitscip-shell shell coreboot 25.09+dfsg-1 git_last_commit(). "$1")
98
99 if git_has_local_changesscip-shell shell coreboot 25.09+dfsg-1 git_has_local_changes(). "$1" ; then
100 r="$r-dirty"
101 fi
102 else
103 return ${EXIT_FAILURE}
104 fi
105
106 echoscip-shell system . . echo. "${r}"
107 }
108
109 # Similar to local_revision but uses "git describe" instead of "git log" which
110 # includes number of commits since most recent tag.
111 tagged_revisionscip-shell shell coreboot 25.09+dfsg-1 tagged_revision().() {
112 local r
113
114 if git_is_file_trackedscip-shell shell coreboot 25.09+dfsg-1 git_is_file_tracked(). "$1" ; then
115 r=$(gitscip-shell system . . git. describe --dirty)
116 else
117 return ${EXIT_FAILURE}
118 fi
119
120 echoscip-shell system . . echo. "${r}"
121 }
122
123 upstream_revisionscip-shell shell coreboot 25.09+dfsg-1 upstream_revision().() {
124 local r=
125
126 r=$(gitscip-shell system . . git. log remotes/origin/main -1 --format=format:%h)
127
128 if [scip-shell system . . `[`. -z "$r" ]; then
129 r="unknown" # default to unknown
130 fi
131 echoscip-shell system . . echo. "${r}"
132 }
133
134 show_helpscip-shell shell coreboot 25.09+dfsg-1 show_help().() {
135 echoscip-shell system . . echo. "Usage:
136 ${0} <command> [path]
137
138 Commands
139 -h or --help
140 this message
141 -l or --local
142 local revision information including an indicator for uncommitted changes
143 -u or --upstream
144 upstream revision
145 -T or --tags
146 similar to -l, but uses \"git describe\" to obtain revision info with tags
147 -U or --url
148 URL associated with the latest commit
149 -d or --date
150 date of most recent modification
151 -t or --timestamp
152 timestamp of most recent modification
153 "
154 return
155 }
156
157 check_actionscip-shell shell coreboot 25.09+dfsg-1 check_action().() {
158 if [scip-shell system . . `[`. -n "$action" ]; then
159 echoscip-shell system . . echo. "Error: Multiple actions given.">&2
160 exit ${EXIT_FAILURE}
161 fi
162 }
163
164 mainscip-shell shell coreboot 25.09+dfsg-1 main().() {
165 local query_path=
166 local action=
167
168 # The is the main loop
169 while [scip-shell system . . `[`. $# -gt 0 ];
170 do
171 case ${1} in
172 -h|--help)
173 action=show_help;
174 shift;;
175 -l|--local)
176 check_actionscip-shell shell coreboot 25.09+dfsg-1 check_action(). $1
177 action=local_revision
178 shift;;
179 -T|--tags)
180 check_actionscip-shell shell coreboot 25.09+dfsg-1 check_action(). $1
181 action=tagged_revision
182 shift;;
183 -u|--upstream)
184 check_actionscip-shell shell coreboot 25.09+dfsg-1 check_action(). $1
185 action=upstream_revision
186 shift;;
187 -U|--url)
188 check_actionscip-shell shell coreboot 25.09+dfsg-1 check_action(). $1
189 action=scm_url
190 shift;;
191 -d|--date)
192 check_actionscip-shell shell coreboot 25.09+dfsg-1 check_action(). $1
193 action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC
194 shift;;
195 -t|--timestamp)
196 check_actionscip-shell shell coreboot 25.09+dfsg-1 check_action(). $1
197 action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601
198 shift;;
199 -*)
200 show_helpscip-shell shell coreboot 25.09+dfsg-1 show_help().;
201 echoscip-shell system . . echo. "Error: Invalid option: ${1}"
202 exit ${EXIT_FAILURE};;
203 *)
204 if [scip-shell system . . `[`. -z "$query_path" ] ; then
205 if [scip-shell system . . `[`. ! -e "$1" ] ; then
206 echoscip-shell system . . echo. "Error: Path \"${1}\" does not exist.">&2
207 exit ${EXIT_FAILURE}
208 fi
209 query_path=$1
210 else
211 echoscip-shell system . . echo. "Warning: Ignoring over-abundant parameter: \"${1}\"">&2
212 fi
213 shift;;
214 esac;
215 done
216
217 # default to current directory (usually equals the whole repository)
218 if [scip-shell system . . `[`. -z "$query_path" ] ; then
219 query_path=.
220 fi
221 if ! is_file_trackedscip-shell shell coreboot 25.09+dfsg-1 is_file_tracked(). "$query_path" ; then
222 echoscip-shell system . . echo. "Warning: Path \"${query_path}\" is not under version control.">&2
223 fi
224 if [scip-shell system . . `[`. -z "$action" ] ; then
225 show_helpscip-shell shell coreboot 25.09+dfsg-1 show_help().
226 echoscip-shell system . . echo. "Error: No actions specified"
227 exit ${EXIT_FAILURE}
228 fi
229
230 $action "$query_path"
231 }
232
233 mainscip-shell shell coreboot 25.09+dfsg-1 main(). $@