From fd5b427fd15c887aacc4c3b3847f3d6872ea2210 Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Thu, 11 Jan 2024 16:36:42 +0100 Subject: [PATCH] Add source timestamp for Git step Extend Git step CLI to write a result file containing the source timestamp of the commit that is checked out. --- cmd/git/main.go | 41 +++++++++++++++++++++++++++-------------- cmd/git/main_test.go | 15 +++++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/cmd/git/main.go b/cmd/git/main.go index ff12f9107b..c1d0e6851b 100644 --- a/cmd/git/main.go +++ b/cmd/git/main.go @@ -46,20 +46,21 @@ func (e ExitError) Error() string { } type settings struct { - help bool - url string - revision string - depth uint - target string - resultFileCommitSha string - resultFileCommitAuthor string - resultFileBranchName string - secretPath string - skipValidation bool - gitURLRewrite bool - resultFileErrorMessage string - resultFileErrorReason string - verbose bool + help bool + url string + revision string + depth uint + target string + resultFileCommitSha string + resultFileCommitAuthor string + resultFileBranchName string + resultFileSourceTimestamp string + secretPath string + skipValidation bool + gitURLRewrite bool + resultFileErrorMessage string + resultFileErrorReason string + verbose bool } var flagValues settings @@ -81,6 +82,7 @@ func init() { pflag.StringVar(&flagValues.target, "target", "", "The target directory of the clone operation") pflag.StringVar(&flagValues.resultFileCommitSha, "result-file-commit-sha", "", "A file to write the commit sha to.") pflag.StringVar(&flagValues.resultFileCommitAuthor, "result-file-commit-author", "", "A file to write the commit author to.") + pflag.StringVar(&flagValues.resultFileSourceTimestamp, "result-file-source-timestamp", "", "A file to write the source timestamp to.") pflag.StringVar(&flagValues.resultFileBranchName, "result-file-branch-name", "", "A file to write the branch name to.") pflag.StringVar(&flagValues.secretPath, "secret-path", "", "A directory that contains a secret. Either username and password for basic authentication. Or a SSH private key and optionally a known hosts file. Optional.") @@ -180,6 +182,17 @@ func runGitClone(ctx context.Context) error { } } + if flagValues.resultFileSourceTimestamp != "" { + output, err := git(ctx, "-C", flagValues.target, "show", "--no-patch", "--format=%ct") + if err != nil { + return err + } + + if err = os.WriteFile(flagValues.resultFileSourceTimestamp, []byte(output), 0644); err != nil { + return err + } + } + if strings.TrimSpace(flagValues.revision) == "" && strings.TrimSpace(flagValues.resultFileBranchName) != "" { output, err := git(ctx, "-C", flagValues.target, "rev-parse", "--abbrev-ref", "HEAD") if err != nil { diff --git a/cmd/git/main_test.go b/cmd/git/main_test.go index 5c8de99fb0..d50543a05c 100644 --- a/cmd/git/main_test.go +++ b/cmd/git/main_test.go @@ -471,6 +471,21 @@ var _ = Describe("Git Resource", func() { }) }) }) + + It("should store source-timestamp into file specified in --result-file-source-timestamp flag", func() { + withTempFile("source-timestamp", func(filename string) { + withTempDir(func(target string) { + Expect(run(withArgs( + "--url", exampleRepo, + "--target", target, + "--revision", "v0.1.0", + "--result-file-source-timestamp", filename, + ))).ToNot(HaveOccurred()) + + Expect(filecontent(filename)).To(Equal("1619426578")) + }) + }) + }) }) Context("Some tests mutate or depend on git configurations. They must run sequentially to avoid race-conditions.", Ordered, func() {