Skip to content

Commit 200f023

Browse files
authored
util/gitutil: Full/ShortCommit(): replace git show with git rev-parse
The FullCommit and ShortCommit methods are expected to simply get the hash of HEAD. Using `git show` for this purpose is overkill because `git show` can have side effects that are annoying when `docker build` runs in a CI environment: 1) CI environments (e.g. CircleCI) may use blobless clones, where the target repository is checked out using `git clone --filter=blob:none {url}`. 2) `git show` does more than just discover the hash of a specified revision: in general, it needs parent commits and blobs to compute the diff. When a blobless clone is used, `git show` may try to download these additional blobs from the remote. From my experiments, this happens even when `--quiet` or `--no-patch` is specified. 3) If the `docker build` step runs in an environment where Git is not configured properly to download from the remote, the step may hang indefinitely, as it did in my case with the following prompt: The authenticity of host 'github.com (140.82.112.3)' can't be established. ED25519 key fingerprint is SHA256:+.... This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])?
1 parent 55e8fa3 commit 200f023

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

util/gitutil/gitutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ func (c *Git) RemoteURL() (string, error) {
101101
}
102102

103103
func (c *Git) FullCommit() (string, error) {
104-
return c.clean(c.run("show", "--format=%H", "HEAD", "--quiet", "--"))
104+
return c.clean(c.run("rev-parse", "HEAD"))
105105
}
106106

107107
func (c *Git) ShortCommit() (string, error) {
108-
return c.clean(c.run("show", "--format=%h", "HEAD", "--quiet", "--"))
108+
return c.clean(c.run("rev-parse", "--short", "HEAD"))
109109
}
110110

111111
func (c *Git) Tag() (string, error) {

0 commit comments

Comments
 (0)