Skip to content

Commit

Permalink
fix: add no build if image exists + fix Git 2.46.x git init problem…
Browse files Browse the repository at this point in the history
… ⚓ (#176)

* fix: add no build if image exists ⚓
* ci: add branches ⚓
* fix: fix Git bug 2.46.x where `reference-transaction` runs on `git init` ⚓
   - `git init` runs `reference-transaction` where Githooks fails on different 
      commands as the Git repo seems not correctly initialized which seems like a Git bug (?).
  • Loading branch information
gabyx authored Sep 20, 2024
1 parent ffb4404 commit 01fe593
Show file tree
Hide file tree
Showing 22 changed files with 150 additions and 78 deletions.
11 changes: 8 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 All @@ -59,7 +62,9 @@ workflows:
branches:
only: &task-branches
- /feature\/.*/
- /feat\/.*/
- /bugfix\/.*/
- /fix\/.*/
- linux:
matrix:
parameters:
Expand Down
1 change: 1 addition & 0 deletions docs/cli/git_hooks_images_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ git hooks images update
Useful to build images in shared repositories
`githooks/.images.yaml` directory.
Namespace is read from the current repository.
-b, --always-build Always build images, even if they already exist.
-h, --help help for update
```

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;
};
}
);
);
}
14 changes: 12 additions & 2 deletions 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 Expand Up @@ -583,7 +592,8 @@ func updateLocalHookImages(settings *HookSettings) {
settings.RepositoryDir,
settings.RepositoryHooksDir,
"",
settings.ContainerMgr)
settings.ContainerMgr,
false)

log.AssertNoErrorF(e, "Could not updating container images from '%s'.", settings.HookDir)
}
Expand Down
13 changes: 9 additions & 4 deletions githooks/cmd/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"github.com/spf13/cobra"
)

func runImagesUpdate(ctx *ccm.CmdContext, imagesFile string) {
func runImagesUpdate(ctx *ccm.CmdContext, imagesFile string, alwaysBuild bool) {
repoDir, _, _ := ccm.AssertRepoRoot(ctx)

containerMgr, err := hooks.NewContainerManager(ctx.GitX, false, nil)
ctx.Log.AssertNoErrorPanicF(err, "Could not create container manager.")

hooksDir := hooks.GetGithooksDir(repoDir)
err = hooks.UpdateImages(ctx.Log, hooksDir, repoDir, hooksDir, imagesFile, containerMgr)
err = hooks.UpdateImages(ctx.Log, hooksDir, repoDir, hooksDir, imagesFile, containerMgr, alwaysBuild)
ctx.Log.AssertNoErrorF(err, "Could not build images in '%s'.", imagesFile)

if strs.IsNotEmpty(imagesFile) {
Expand Down Expand Up @@ -55,7 +55,8 @@ func runImagesUpdate(ctx *ccm.CmdContext, imagesFile string) {
allRepos[rI].RepositoryDir,
hooksDir,
"",
containerMgr)
containerMgr,
alwaysBuild)
ctx.Log.AssertNoErrorF(err, "Could not build images in '%s'.", allRepos[rI].OriginalURL)
}
}
Expand All @@ -69,14 +70,15 @@ func NewCmd(ctx *ccm.CmdContext) *cobra.Command {
Long: "Manages container images used by Githooks repositories in the current repository."}

imagesFile := ""
alwaysBuild := false
imagesUpdateCmd := &cobra.Command{
Use: "update",
Short: `Build/pull container images.`,
Long: "Build/pull container images in the current\n" +
"repository and shared repositories which are needed for Githooks.",
PreRun: ccm.PanicIfNotExactArgs(ctx.Log, 0),
Run: func(c *cobra.Command, args []string) {
runImagesUpdate(ctx, imagesFile)
runImagesUpdate(ctx, imagesFile, alwaysBuild)
}}

imagesUpdateCmd.Flags().StringVar(&imagesFile,
Expand All @@ -86,6 +88,9 @@ func NewCmd(ctx *ccm.CmdContext) *cobra.Command {
"'githooks/.images.yaml' directory.\n"+
"Namespace is read from the current repository.")

imagesUpdateCmd.Flags().BoolVarP(&alwaysBuild,
"always-build", "b", false, "Always build images, even if they already exist.")

imagesCmd.AddCommand(ccm.SetCommandDefaults(ctx.Log, imagesUpdateCmd))

imagesCmd.PersistentPreRun = func(_ *cobra.Command, _ []string) {
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
2 changes: 0 additions & 2 deletions githooks/hooks/gitconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ const (
GitCKNumThreads = "githooks.numThreads"

GitCKAliasHooks = "alias.hooks"

GitCKBuildImagesOnSharedUpdate = "githooks.buildImagesOnSharedUpdate"
)

// Git config keys for local config.
Expand Down
22 changes: 19 additions & 3 deletions githooks/hooks/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func buildImage(
stage string,
imageRef string,
file string,
repositoryDir string) (err error) {
repositoryDir string,
alwaysBuild bool,
) (err error) {
// Do a build of the image because no `pull` but `build` specified.

if filepath.IsAbs(context) {
Expand All @@ -143,6 +145,17 @@ func buildImage(
dockerfile, file)
}

if !alwaysBuild {
exists, e := mgr.ImageExists(imageRef)
log.AssertNoError(e, "Could not check if images exists.")

if exists {
log.InfoF("Image '%v' already exists.", imageRef)

return nil
}
}

out, err := mgr.ImageBuild(
log,
path.Join(repositoryDir, dockerfile),
Expand Down Expand Up @@ -185,7 +198,9 @@ func UpdateImages(
repositoryDir string,
hooksDir string,
configFile string,
containerMgr container.IManager) (err error) {
containerMgr container.IManager,
alwaysBuild bool,
) (err error) {

if strs.IsEmpty(configFile) {
configFile = GetRepoImagesFile(hooksDir)
Expand Down Expand Up @@ -264,7 +279,8 @@ func UpdateImages(
img.Build.Stage,
imageRef,
configFile,
repositoryDir)
repositoryDir,
alwaysBuild)

if e != nil {
err = cm.CombineErrors(err, e)
Expand Down
2 changes: 1 addition & 1 deletion githooks/hooks/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ RUN apk add bash
mgr, err := container.NewManager("docker")
assert.Nil(t, err)
assert.NotNil(t, mgr)
err = UpdateImages(log, "test-repo", repo, path.Join(repo, ".githooks"), "", mgr)
err = UpdateImages(log, "test-repo", repo, path.Join(repo, ".githooks"), "", mgr, true)
assert.Nil(t, err, "Update images failed: %s", err)

mgr, err = container.NewManager("")
Expand Down
4 changes: 3 additions & 1 deletion githooks/hooks/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,9 @@ func UpdateSharedHooks(
hook.OriginalURL,
hook.RepositoryDir,
GetSharedGithooksDir(hook.RepositoryDir),
"", containerMgr)
"",
containerMgr,
false)
log.AssertNoErrorF(e, "Updating container images of '%s' failed.", hook.OriginalURL)
}
}
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
Loading

0 comments on commit 01fe593

Please sign in to comment.