Skip to content

Commit

Permalink
ChecksumsDir used to set absolute path to a centralized trust checksu…
Browse files Browse the repository at this point in the history
…ms directory
  • Loading branch information
victorciresica committed Oct 14, 2024
1 parent 1dc5cb5 commit bc79c9b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func setupSettings(repoPath string) (HookSettings, UISettings) {
nonInteractive := hooks.IsRunnerNonInteractive(gitx, git.Traverse)
skipNonExistingSharedHooks := hooks.SkipNonExistingSharedHooks(gitx, git.Traverse)
skipUntrustedHooks, _ := hooks.SkipUntrustedHooks(gitx, git.Traverse)
checksumsDir := hooks.GetChecksumDirectoryGitDir(gitDir)

isTrusted, hasTrustFile, trustAllSet := hooks.IsRepoTrusted(gitx, repoPath)
if !isTrusted && hasTrustFile && !trustAllSet && !nonInteractive && !isGithooksDisabled {
Expand All @@ -188,6 +189,7 @@ func setupSettings(repoPath string) (HookSettings, UISettings) {
RepositoryHooksDir: path.Join(repoPath, hooks.HooksDirName),
GitDirWorktree: gitDir,
InstallDir: installDir,
ChecksumsDir: checksumsDir,

HookPath: hookPath,
HookName: path.Base(hookPath),
Expand Down
1 change: 1 addition & 0 deletions githooks/apps/runner/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type HookSettings struct {
HookName string // Name of the hook.
HookDir string // Directory of the hook.
HookNamespace string // Namespace of this repositorie's Githooks.
ChecksumsDir string // Name of the directory with the trust checksums.

IsRepoTrusted bool // If the repository is a trusted repository.
SkipNonExistingSharedHooks bool // If Githooks should skip non-existing shared hooks.
Expand Down
4 changes: 2 additions & 2 deletions githooks/cmd/common/install/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func InstallIntoRepo(

func cleanArtefactsInRepo(log cm.ILogContext, gitDir string) {

// Remove checksum files...
cacheDir := hooks.GetChecksumDirectoryGitDir(gitDir)
// Remove local checksum files...
cacheDir := path.Join(gitDir, hooks.ChecksumsDir)
if cm.IsDirectory(cacheDir) {
log.AssertNoErrorF(os.RemoveAll(cacheDir),
"Could not delete checksum cache dir '%s'.", cacheDir)
Expand Down
5 changes: 5 additions & 0 deletions githooks/hooks/gitconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const (
GitCKContainerImageUpdateAutomatic = "githooks.containerImageUpdateAutomatic"

GitCKExportStagedFilesAsFile = "githooks.exportStagedFilesAsFile"

GitCKChecksumsDir = "githooks.checksumsDir"
)

// GetGlobalGitConfigKeys gets all global git config keys relevant for Githooks.
Expand Down Expand Up @@ -102,6 +104,8 @@ func GetGlobalGitConfigKeys() []string {
GitCKExportStagedFilesAsFile,

GitCKContainerizedHooksEnabled,

GitCKChecksumsDir,
}
}

Expand All @@ -126,6 +130,7 @@ func GetLocalGitConfigKeys() []string {
GitCKContainerizedHooksEnabled,

GitCKExportStagedFilesAsFile,
GitCKChecksumsDir,
}
}

Expand Down
3 changes: 3 additions & 0 deletions githooks/hooks/githooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const DialogExecutableName = "githooks-dialog"
const HooksDirName = ".githooks"
const HooksDirNameShared = "githooks"

// ChecksumsDir denotes the directory name used by default for storing checksums for trusted hooks.
const ChecksumsDir = ".githooks.checksums"

// GithooksWebpage is the main Githooks webpage.
const GithooksWebpage = "https://github.com/gabyx/githooks"

Expand Down
15 changes: 14 additions & 1 deletion githooks/hooks/trusted.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,20 @@ func (t *ChecksumStore) Summary() string {

// GetChecksumDirectoryGitDir gets the checksum file inside the Git directory.
func GetChecksumDirectoryGitDir(gitDir string) string {
return path.Join(gitDir, ".githooks.checksums")
var conf string
gitx := git.NewCtxAt(gitDir)
scope := git.Traverse
conf, set := os.LookupEnv("GITHOOKS_CHECKSUMS_DIR")
if !set {
conf = gitx.GetConfig(GitCKChecksumsDir, scope)
}

switch {
case !strs.IsEmpty(conf) && filepath.IsAbs(conf):
return conf
default:
return path.Join(gitDir, ChecksumsDir)
}
}

// GetChecksumStorage loads the checksum store from the
Expand Down

0 comments on commit bc79c9b

Please sign in to comment.