Skip to content

Commit 6357f11

Browse files
committed
add git_tag_metadata func and modernize jq usage
updated our old jq object building usage in common.sh to use --arg, which guarantees the json to be valid. Signed-off-by: Taylor Silva <dev@taydev.net>
1 parent 0ecb1a8 commit 6357f11

2 files changed

Lines changed: 56 additions & 32 deletions

File tree

assets/common.sh

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,31 @@ configure_git_ssl_verification() {
120120
}
121121

122122
add_git_metadata_basic() {
123-
local commit=$(git rev-parse HEAD | jq -R .)
124-
local author=$(git log -1 --format=format:%an | jq -s -R .)
125-
local author_date=$(git log -1 --format=format:%ai | jq -R .)
126-
127-
jq ". + [
128-
{name: \"commit\", value: ${commit}},
129-
{name: \"author\", value: ${author}},
130-
{name: \"author_date\", value: ${author_date}, type: \"time\"}
131-
]"
123+
local commit=$(git rev-parse HEAD)
124+
local author=$(git log -1 --format=format:%an)
125+
local author_date=$(git log -1 --format=format:%ai)
126+
127+
jq --arg commit "$commit" \
128+
--arg author "$author" \
129+
--arg author_date "$author_date" \
130+
'. + [
131+
{name: "commit", value: $commit},
132+
{name: "author", value: $author},
133+
{name: "author_date", value: $author_date, type: "time"}
134+
]'
132135
}
133136

134137
add_git_metadata_committer() {
135-
local author=$(git log -1 --format=format:%an | jq -s -R .)
136-
local author_date=$(git log -1 --format=format:%ai | jq -R .)
137-
local committer=$(git log -1 --format=format:%cn | jq -s -R .)
138-
local committer_date=$(git log -1 --format=format:%ci | jq -R .)
138+
local author=$(git log -1 --format=format:%an)
139+
local author_date=$(git log -1 --format=format:%ai)
140+
local committer=$(git log -1 --format=format:%cn)
141+
local committer_date=$(git log -1 --format=format:%ci)
139142

140143
if [ "$author" = "$committer" ] && [ "$author_date" = "$committer_date" ]; then
141-
jq ". + [
142-
{name: \"committer\", value: ${committer}},
143-
{name: \"committer_date\", value: ${committer_date}, type: \"time\"}
144-
]"
144+
jq --arg committer "$committer" --arg committer_date "$committer_date" '. + [
145+
{name: "committer", value: $committer},
146+
{name: "committer_date", value: $committer_date, type: "time"}
147+
]'
145148
else
146149
cat
147150
fi
@@ -153,9 +156,9 @@ add_git_metadata_branch() {
153156
jq -R ". | select(. != \"\")" | jq -r -s "map(.) | join (\",\")")
154157

155158
if [ -n "${branch}" ]; then
156-
jq ". + [
157-
{name: \"branch\", value: \"${branch}\"}
158-
]"
159+
jq --arg branch "$branch" '. + [
160+
{name: "branch", value: $branch}
161+
]'
159162
else
160163
cat
161164
fi
@@ -167,20 +170,32 @@ add_git_metadata_tags() {
167170
jq -r -s "map(.) | join(\",\")")
168171

169172
if [ -n "${tags}" ]; then
170-
jq ". + [
171-
{name: \"tags\", value: \"${tags}\"}
172-
]"
173+
jq --arg tags "$tags" '. + [
174+
{name: "tags", value: $tags}
175+
]'
176+
else
177+
cat
178+
fi
179+
}
180+
181+
add_git_metadata_tag() {
182+
local tag=$(git tag --points-at HEAD)
183+
184+
if [ -n "${tag}" ]; then
185+
jq --arg tag "$tag" '. + [
186+
{name: "tag", value: $tag}
187+
]'
173188
else
174189
cat
175190
fi
176191
}
177192

178193
add_git_metadata_message() {
179-
local message=$(git log -1 --format=format:%B | head -c 10240 | jq -s -R .)
194+
local message=$(git log -1 --format=format:%B | head -c 10240)
180195

181-
jq ". + [
182-
{name: \"message\", value: ${message}, type: \"message\"}
183-
]"
196+
jq --arg message "$message" '. + [
197+
{name: "message", value: $message, type: "message"}
198+
]'
184199
}
185200

186201
add_git_metadata_url() {
@@ -213,9 +228,9 @@ add_git_metadata_url() {
213228
esac
214229

215230
if [ -n "$url" ]; then
216-
jq ". + [
217-
{name: \"url\", value: \"${url}\"}
218-
]"
231+
jq --arg url "$url" '. + [
232+
{name: "url", value: $url}
233+
]'
219234
else
220235
jq ". + []"
221236
fi
@@ -232,6 +247,15 @@ git_metadata() {
232247
add_git_metadata_url
233248
}
234249

250+
git_tag_metadata() {
251+
jq -n "[]" | \
252+
add_git_metadata_basic | \
253+
add_git_metadata_committer | \
254+
add_git_metadata_tag | \
255+
add_git_metadata_message | \
256+
add_git_metadata_url
257+
}
258+
235259
configure_submodule_credentials() {
236260
local username
237261
local password

assets/in_tags.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ git --no-pager log -1 --pretty=format:"%an" > .git/committer_name
128128
# pulled ref in following tasks and resources.
129129
echo "${return_ref}" > .git/ref
130130

131-
metadata=$(git_metadata)
131+
metadata=$(git_tag_metadata)
132132
echo "${metadata}" | jq '.' > .git/metadata.json
133133

134134
# Store short ref with templating. Useful to build Docker images with
@@ -138,7 +138,7 @@ echo "${return_ref}" | cut -c1-7 | awk "{ printf \"${short_ref_format}\", \$1 }"
138138
# Write individual metadata fields to separate files
139139

140140
# .git/commit - full SHA hash
141-
echo "${metadata}" | jq -r '.[] | select(.name == "commit") | .value' > .git/tag
141+
echo "${metadata}" | jq -r '.[] | select(.name == "tag") | .value' > .git/tag
142142
# .git/author - commit author name
143143
echo "${metadata}" | jq -r '.[] | select(.name == "author") | .value' > .git/author
144144
# .git/author_date - timestamp when the author originally created the commit

0 commit comments

Comments
 (0)