Skip to content

Commit

Permalink
Add source timestamp for Git step
Browse files Browse the repository at this point in the history
Extend Git step CLI to write a result file containing the source
timestamp of the commit that is checked out.
  • Loading branch information
HeavyWombat committed Jan 24, 2024
1 parent 7331e73 commit fd5b427
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
41 changes: 27 additions & 14 deletions cmd/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.")

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions cmd/git/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit fd5b427

Please sign in to comment.