Allow stubmaker to work outside of a git repository (#24678)

Co-authored-by: Violet Hynes <violet.hynes@hashicorp.com>
This commit is contained in:
DrDaveD
2024-05-30 12:19:10 -05:00
committed by GitHub
parent b8d482c2fa
commit d8c241838a

View File

@@ -50,16 +50,22 @@ func main() {
DetectDotGit: true, DetectDotGit: true,
}) })
if err != nil { if err != nil {
if err.Error() != "repository does not exist" {
fatal(err) fatal(err)
} }
repo = nil
}
wt, err := repo.Worktree() var wt *git.Worktree
if repo != nil {
wt, err = repo.Worktree()
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
if !isEnterprise(wt) { if !isEnterprise(wt) {
return return
} }
}
// Read the file and figure out if we need to do anything. // Read the file and figure out if we need to do anything.
inputFile := os.Getenv("GOFILE") inputFile := os.Getenv("GOFILE")
@@ -88,6 +94,7 @@ func main() {
// We'd like to write the file, but first make sure that we're not going // We'd like to write the file, but first make sure that we're not going
// to blow away anyone's work or overwrite a file already in git. // to blow away anyone's work or overwrite a file already in git.
if repo != nil {
head, err := repo.Head() head, err := repo.Head()
if err != nil { if err != nil {
fatal(err) fatal(err)
@@ -109,6 +116,7 @@ func main() {
if tracked { if tracked {
fatal(fmt.Errorf("output file %s exists in git, not overwriting", outputFile)) fatal(fmt.Errorf("output file %s exists in git, not overwriting", outputFile))
} }
}
// Now we can finally write the file // Now we can finally write the file
output, err := os.Create(outputFile + ".tmp") output, err := os.Create(outputFile + ".tmp")