Skip to content

Commit

Permalink
fix: fix Git bug 2.46.x where reference-transaction runs on `git in…
Browse files Browse the repository at this point in the history
…it` ⚓

- `git init` runs `reference-transaction` where Githooks fails on different commands as the Git repo seems not correctly initialized which is a bug.
  • Loading branch information
gabyx committed Sep 20, 2024
1 parent 64803ef commit 3f63c0d
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 58 deletions.
9 changes: 6 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
default: "test-alpine"
type: string
machine:
image: ubuntu-2004:202010-01
image: ubuntu-2204:current
steps:
- checkout
- run: bash tests/<<parameters.test>>.sh
Expand All @@ -30,12 +30,15 @@ jobs:
executor:
size: medium
name: win/server-2022
version: 2023.11.1
version: current
steps:
- checkout
- run:
no_output_timeout: 30m
command: "& 'C:/Program Files/Git/bin/bash.exe' -c 'tests/<<parameters.test>>.sh --seq <<parameters.seq>>'"
command: <
& 'C:/Program Files/Git/bin/bash.exe'
-c 'tests/<<parameters.test>>.sh
--seq <<parameters.seq>>'

workflows:
version: 2
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 29 additions & 26 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,44 @@
flake-utils.url = "github:numtide/flake-utils";
};

outputs = {
self,
nixpkgs,
nixpkgsStable,
flake-utils,
...
} @ inputs:
outputs =
{
self,
nixpkgs,
nixpkgsStable,
flake-utils,
...
}@inputs:
flake-utils.lib.eachDefaultSystem
# Creates an attribute map `{ devShells.<system>.default = ...}`
# by calling this function:
(
system: let
overlays = [];
# Creates an attribute map `{ devShells.<system>.default = ...}`
# by calling this function:
(
system:
let
overlays = [ ];

# Import nixpkgs and load it into pkgs.
pkgs = import nixpkgs {
inherit system overlays;
};
# Import nixpkgs and load it into pkgs.
pkgs = import nixpkgs {
inherit system overlays;
};

# Things needed only at compile-time.
nativeBuildInputs = with pkgs; [
go_1_21
];
# Things needed only at compile-time.
nativeBuildInputs = with pkgs; [
go_1_22
];

# Things needed at runtime.
buildInputs = with pkgs; [];
in
with pkgs; {
# Things needed at runtime.
buildInputs = with pkgs; [ ];
in
with pkgs;
{
devShells.default = mkShell {
# To make CGO and the debugger delve work.
# https://nixos.wiki/wiki/Go#Using_cgo_on_NixOS
hardeningDisable = ["fortify"];
hardeningDisable = [ "fortify" ];

inherit buildInputs nativeBuildInputs;
};
}
);
);
}
11 changes: 10 additions & 1 deletion githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func mainRun() (exitCode int) {
cwd = filepath.ToSlash(cwd)

settings, uiSettings := setupSettings(cwd)
assertRegistered(settings.GitX, settings.InstallDir)

if settings.HookName != "reference-transaction" {
// Git 2.46.x apparently runs `reference-transaction` on `git init`
// where different commands fail like `git config ...` since
// the repo is not yet properly initialized (?).
assertRegistered(settings.GitX, settings.InstallDir)
}

checksums, err := hooks.GetChecksumStorage(settings.GitDirWorktree)
log.AssertNoErrorF(err, "Errors while loading checksum store.")
Expand Down Expand Up @@ -140,6 +146,9 @@ func setupSettings(repoPath string) (HookSettings, UISettings) {
// General execution context, in currenct working dir.
execx := cm.ExecContext{Env: os.Environ()}

log.DebugF("Arguments: '%q'", os.Args)
log.DebugF("Env: '%q'", os.Environ())

// Current git context, in current working dir.
gitx := git.NewCtxAt(repoPath)
log.AssertNoErrorF(gitx.InitConfigCache(nil),
Expand Down
29 changes: 22 additions & 7 deletions githooks/git/gitcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,22 @@ func (c *Context) GetMainWorktree() (string, error) {
// GetGitDirCommon returns the common Git directory.
// For normal repos this points to the `.git` directory.
// For worktrees this points to the main worktrees git dir.
// The env. variable GIT_COMMON_DIR has especiall
// The env. variable GIT_COMMON_DIR has especially
// be introduced for multiple worktrees, see:
// https://github.com/git/git/commit/c7b3a3d2fe2688a30ddb8d516ed000eeda13c24e
func (c *Context) GetGitDirCommon() (gitDir string, err error) {
gitDir, err = c.Get("rev-parse", "--git-common-dir")
if err != nil {
return
// Git 2.46.x apparently runs `reference-transaction` on `git init`
// where `rev-parse` fails despite the documentation saying it reports `$GIT_COMMON_DIR` if set
// (it is set!) -> Bug was reported.
gitDir = os.Getenv("GIT_COMMON_DIR")
if strs.IsEmpty(gitDir) {
gitDir = os.Getenv("GIT_DIR")
if strs.IsEmpty(gitDir) {
gitDir, err = c.Get("rev-parse", "--git-common-dir")
if err != nil {
return
}
}
}

if !filepath.IsAbs(gitDir) {
Expand All @@ -111,9 +120,15 @@ func (c *Context) GetGitDirCommon() (gitDir string, err error) {
// For normal repos this points to the `.git` directory.
// For worktrees this points to the actual worktrees git dir `.git/worktrees/<....>/`.
func (c *Context) GetGitDirWorktree() (gitDir string, err error) {
gitDir, err = c.Get("rev-parse", "--absolute-git-dir")
if err != nil {
return
// Git 2.46.x apparently runs `reference-transaction` on `git init`
// where `rev-parse` fails despite the documentation saying it reports `$GIT_DIR` if set
// (it is set!) -> Bug was reported.
gitDir = os.Getenv("GIT_DIR")
if strs.IsEmpty(gitDir) {
gitDir, err = c.Get("rev-parse", "--absolute-git-dir")
if err != nil {
return
}
}

gitDir = filepath.ToSlash(gitDir)
Expand Down
2 changes: 1 addition & 1 deletion githooks/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gabyx/githooks/githooks

go 1.21
go 1.22

require (
code.gitea.io/sdk/gitea v0.15.0
Expand Down
5 changes: 4 additions & 1 deletion tests/steps/step-006.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@ mkdir -p "$GH_TEST_TMP/test6" &&
check_local_install_run_wrappers

# Reinstall and check again.
"$GH_INSTALL_BIN_DIR/githooks-cli" install
"$GH_INSTALL_BIN_DIR/githooks-cli" install || {
echo "! Reinstall failed."
exit 1
}
check_local_install_run_wrappers
2 changes: 1 addition & 1 deletion tests/test-alpine-nolfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST_DIR="$ROOT_DIR/tests"
cd "$ROOT_DIR"

cat <<EOF | docker build --force-rm -t githooks:alpine-nolfs-base -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git
RUN apk add bash jq curl docker
Expand Down
2 changes: 1 addition & 1 deletion tests/test-alpine-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cd "$ROOT_DIR"

cat <<EOF | docker build \
--force-rm -t githooks:alpine-user-base -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git git-lfs
RUN apk add bash jq curl
Expand Down
2 changes: 1 addition & 1 deletion tests/test-alpine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST_DIR="$ROOT_DIR/tests"
cd "$ROOT_DIR"

cat <<EOF | docker build --force-rm -t githooks:alpine-lfs-base -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git git-lfs
RUN apk add bash jq curl docker
Expand Down
2 changes: 1 addition & 1 deletion tests/test-centralized.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST_DIR="$ROOT_DIR/tests"
cd "$ROOT_DIR"

cat <<EOF | docker build --force-rm -t githooks:alpine-lfs-centralized-base -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git git-lfs
RUN apk add bash jq curl docker
Expand Down
2 changes: 1 addition & 1 deletion tests/test-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ EOF

# Build test container.
cat <<EOF | docker build --force-rm -t githooks:test-rules -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git git-lfs
RUN apk add bash jq curl docker just
Expand Down
2 changes: 1 addition & 1 deletion tests/test-unittests-podman.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trap clean_up EXIT
cd "$ROOT_DIR"

cat <<EOF | docker build --force-rm -t githooks:unittests -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git git-lfs
RUN apk add bash jq curl
Expand Down
2 changes: 1 addition & 1 deletion tests/test-unittests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trap clean_up EXIT
cd "$ROOT_DIR"

cat <<EOF | docker build --force-rm -t githooks:unittests -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git git-lfs
RUN apk add bash jq curl docker
Expand Down
2 changes: 1 addition & 1 deletion tests/test-whitespace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST_DIR="$ROOT_DIR/tests"
cd "$ROOT_DIR"

cat <<EOF | docker build --force-rm -t githooks:alpine-lfs-whitespace-base -
FROM golang:1.21-alpine
FROM golang:1.22-alpine
RUN apk update && apk add git git-lfs
RUN apk add bash jq curl docker
Expand Down
4 changes: 2 additions & 2 deletions tests/test-windows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ RUN $newPath = ('{0}\bin;C:\go\bin;{1}' -f $env:GOPATH, $env:PATH); \
# doing this first to share cache across versions more aggressively
# Check hash below for download.
ENV GOLANG_VERSION 1.21.0
ENV GOLANG_VERSION 1.22.7
RUN $url = ('https://go.dev/dl/go{0}.windows-amd64.zip' -f $env:GOLANG_VERSION); \
Write-Host ('Downloading {0} ...' -f $url); \
$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri $url -OutFile 'go.zip'; \
\
$sha256 = '732121e64e0ecb07c77fdf6cc1bc5ce7b242c2d40d4ac29021ad4c64a08731f6'; \
$sha256 = 'efbc30520601f4d91d9f3f46af03aafb2e1428388c5ff6a40eb88489f7212e85'; \
Write-Host ('Verifying sha256 ({0}) ...' -f $sha256); \
if ((Get-FileHash go.zip -Algorithm sha256).Hash -ne $sha256) { \
Write-Host 'FAILED!'; \
Expand Down

0 comments on commit 3f63c0d

Please sign in to comment.