Skip to content

util/gitutil: Full/ShortCommit(): replace git show with git rev-parse#3747

Open
jegorbunov wants to merge 2 commits intodocker:masterfrom
jegorbunov:gitutil-use-rev-parse-instead-of-show
Open

util/gitutil: Full/ShortCommit(): replace git show with git rev-parse#3747
jegorbunov wants to merge 2 commits intodocker:masterfrom
jegorbunov:gitutil-use-rev-parse-instead-of-show

Conversation

@jegorbunov
Copy link
Copy Markdown

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])?

@jegorbunov jegorbunov force-pushed the gitutil-use-rev-parse-instead-of-show branch from 200f023 to ba6eae3 Compare March 27, 2026 10:50
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])?

Signed-off-by: Jegor Gorbunov <jegor.gorbunov@point-devel.com>
git rev-parse HEAD doesn't need any `--`, however, when it is called
on an empty repo git returns an error:

```
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
```

So, it is both IsUnknownRevision and IsAmbiguousArgument.

Lets simply fix the test by flipping the check: I don't see
any dependency on this behavior in non-test code.

Signed-off-by: Jegor Gorbunov <jegor.gorbunov@point-devel.com>
@jegorbunov jegorbunov force-pushed the gitutil-use-rev-parse-instead-of-show branch from 2f19caf to 758cdba Compare March 30, 2026 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant