Skip to content

Commit

Permalink
Add source timestamp result file for Git step tool
Browse files Browse the repository at this point in the history
Extend Git step tool to write a result file container the source
timestamp of the commit that is checked out.
  • Loading branch information
HeavyWombat committed Jan 22, 2024
1 parent 8021369 commit c4b8b59
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
resultFileCommitTimestamp 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.resultFileCommitTimestamp, "result-file-commit-timestamp", "", "A file to write the commit 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.resultFileCommitTimestamp != "" {
output, err := git(ctx, "-C", flagValues.target, "show", "--no-patch", "--format=%ct")
if err != nil {
return err
}

if err = os.WriteFile(flagValues.resultFileCommitTimestamp, []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 commit-timestamp into file specified in --result-file-commit-timestamp flag", func() {
withTempFile("commit-timestamp", func(filename string) {
withTempDir(func(target string) {
Expect(run(withArgs(
"--url", exampleRepo,
"--target", target,
"--revision", "v0.1.0",
"--result-file-commit-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 c4b8b59

Please sign in to comment.