-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Git info into the merged vars (#16)
* Add Git info into the merged vars This change, if applied, adds Git information into the merged vars. The path is `__meta__.merge_info.git` and looks like: ``` __meta__: last_update: git: author: John Smith <[email protected]> committer: GitHub <[email protected]> hash: c6bbc9cae55bf40bed1de897a592583615e5a420 message: adds roles and vars for argo workshop (#6887) when_author: "2022-09-12T06:37:54Z" when_committer: "2022-09-12T06:37:54Z" ``` see GPTEINFRA-4542 * If the git binary is in PATH, use it to find the most recent change for a catalog item if not, fallback to pure-go implementation of git log (slower). To avoid schema errors, create a default Schema for built-in features that are added to the merged vars. * If a schema found in the repository has properties.__meta__ then merge default schema into it. This way, it works even when additional_properties: false is used. Create a default schema for built-in features that are added to the merged vars. * Update usage in readme.adoc
- Loading branch information
Showing
8 changed files
with
365 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"os/exec" | ||
"path/filepath" | ||
"bytes" | ||
|
||
git "github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
|
||
func isRepo(path string) bool { | ||
_, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{DetectDotGit: true}) | ||
return err == nil | ||
} | ||
|
||
|
||
func findMostRecentCommit(p string, related []Include) *object.Commit { | ||
repo, err := git.PlainOpenWithOptions(p, &git.PlainOpenOptions{DetectDotGit: true}) | ||
if err != nil { | ||
logErr.Fatal("Can't open repository", p, err) | ||
} | ||
|
||
wt, _ := repo.Worktree() | ||
|
||
cIter, err := repo.Log( | ||
&git.LogOptions{ | ||
Order: git.LogOrderCommitterTime, | ||
All: false, | ||
PathFilter: func(path string) bool { | ||
if filepath.Join(wt.Filesystem.Root(), path) == abs(p) { | ||
return true | ||
} | ||
for _, f := range related { | ||
if filepath.Join(wt.Filesystem.Root(), path) == abs(f.path) { | ||
return true | ||
} | ||
} | ||
return false | ||
}, | ||
}, | ||
) | ||
|
||
var commit *object.Commit | ||
cIter.ForEach(func(o *object.Commit) error { | ||
commit = o | ||
// Stop at first found, return EOF | ||
return io.EOF | ||
}) | ||
|
||
return commit | ||
} | ||
|
||
func findMostRecentCommitCmd(p string, related []Include) *object.Commit { | ||
// Use the git command | ||
// see https://github.com/go-git/go-git/issues/137 | ||
args := []string{ | ||
"log", | ||
"--max-count=1", | ||
"--pretty=format:%H", | ||
"--", | ||
p, | ||
} | ||
|
||
for _, r := range related { | ||
args = append(args, r.path) | ||
} | ||
|
||
cmd := exec.Command("git", args...) | ||
logDebug.Println(cmd) | ||
|
||
var out bytes.Buffer | ||
cmd.Stdout = &out | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
logErr.Fatal(err) | ||
} | ||
|
||
repo, err := git.PlainOpenWithOptions(p, &git.PlainOpenOptions{DetectDotGit: true}) | ||
if err != nil { | ||
logErr.Fatal("Can't open repository", p, err) | ||
} | ||
|
||
commit, err := repo.CommitObject(plumbing.NewHash(out.String())) | ||
return commit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
|
||
func TestIsRepo(t *testing.T) { | ||
if isRepo("/tmp") { | ||
t.Error("/tmp is a repo???") | ||
} | ||
|
||
if isRepo(".") == false { | ||
t.Error(". is not in a repo") | ||
} | ||
|
||
if isRepo("agnosticv.go") == false { | ||
t.Error("agnosticv.go is not in a repo") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.