Skip to content

Commit

Permalink
chore: allow "list" and "run" without commits.
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Natel <[email protected]>
  • Loading branch information
i4ki committed Dec 22, 2023
1 parent c7f2a37 commit 1262a10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
39 changes: 25 additions & 14 deletions cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ Please see https://terramate.io/docs/cli/configuration/project-setup for details
log.Fatal().Msg("flag --changed provided but no git repository found")
}

if parsedArgs.Changed && !prj.hasCommits() {
log.Fatal().Msg("flag --changed provided but repository has no commits")
}

uimode := HumanMode
if val := os.Getenv("CI"); envVarIsSet(val) {
uimode = AutomationMode
Expand Down Expand Up @@ -615,17 +619,18 @@ func (c *cli) run() {
}

func (c *cli) setupGit() {
if c.prj.isRepo && c.parsedArgs.Changed {
if !c.prj.isRepo || !c.prj.hasCommits() || !c.parsedArgs.Changed {
return
}

if err := c.prj.checkDefaultRemote(); err != nil {
fatal(err, "checking git default remote")
}
if err := c.prj.checkDefaultRemote(); err != nil {
fatal(err, "checking git default remote")
}

if c.parsedArgs.GitChangeBase != "" {
c.prj.baseRef = c.parsedArgs.GitChangeBase
} else {
c.prj.baseRef = c.prj.defaultBaseRef()
}
if c.parsedArgs.GitChangeBase != "" {
c.prj.baseRef = c.parsedArgs.GitChangeBase
} else {
c.prj.baseRef = c.prj.defaultBaseRef()
}
}

Expand Down Expand Up @@ -867,7 +872,11 @@ func (c *cli) gencodeWithVendor() (generate.Report, download.Report) {
}

func (c *cli) checkGitUntracked() bool {
if c.parsedArgs.DisableCheckGitUntracked {
if !c.prj.isRepo || c.parsedArgs.DisableCheckGitUntracked {
return false
}

if c.prj.isRepo && !c.prj.hasCommits() {
return false
}

Expand All @@ -888,7 +897,11 @@ func (c *cli) checkGitUntracked() bool {
}

func (c *cli) checkGitUncommited() bool {
if c.parsedArgs.DisableCheckGitUncommitted {
if !c.prj.isRepo || c.parsedArgs.DisableCheckGitUncommitted {
return false
}

if c.prj.isRepo && !c.prj.hasCommits() {
return false
}

Expand Down Expand Up @@ -949,7 +962,7 @@ func (c *cli) gitSafeguardDefaultBranchIsReachable() {
Bool("is_enabled", c.gitSafeguardRemoteEnabled()).
Logger()

if !c.prj.isRepo || !c.gitSafeguardRemoteEnabled() {
if !c.prj.isRepo || !c.prj.hasCommits() || !c.gitSafeguardRemoteEnabled() {
logger.Debug().Msg("Safeguard default-branch-is-reachable is disabled.")
return
}
Expand Down Expand Up @@ -2108,10 +2121,8 @@ func lookupProject(wd string) (prj project, found bool, err error) {

gw, err := newGit(wd, false)
if err == nil {

gitdir, err := gw.Root()
if err == nil {

gitabs := gitdir
if !filepath.IsAbs(gitabs) {
gitabs = filepath.Join(wd, gitdir)
Expand Down
5 changes: 5 additions & 0 deletions cmd/terramate/cli/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (p *project) localDefaultBranchCommit() string {
return val
}

func (p *project) hasCommits() bool {
_, err := p.git.wrapper.RevParse("HEAD")
return err == nil
}

func (p *project) headCommit() string {
if p.git.headCommit != "" {
return p.git.headCommit
Expand Down
13 changes: 9 additions & 4 deletions cmd/terramate/e2etests/core/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func TestFailsOnChangeDetectionIfRepoDoesntHaveOriginMain(t *testing.T) {
t.Parallel()

rootdir := test.TempDir(t)
assertFails := func(stderrRegex string) {
assertFailsWithChanged := func(stderrRegex string) {
t.Helper()

ts := NewCLI(t, rootdir)
Expand All @@ -335,19 +335,24 @@ func TestFailsOnChangeDetectionIfRepoDoesntHaveOriginMain(t *testing.T) {
git := sandbox.NewGit(t, rootdir)
git.InitLocalRepo()

assertFails("repository must have a configured")
// list and run works without commit.
ts := NewCLI(t, rootdir)
AssertRun(t, ts.ListStacks())
AssertRun(t, ts.Run(HelperPath, "exit", "0"))

assertFailsWithChanged("flag --changed provided but repository has no commits")

// the main branch only exists after first commit.
path := test.WriteFile(t, git.BaseDir(), "README.md", "# generated by terramate")
git.Add(path)
git.Commit("first commit")

git.SetupRemote("notorigin", "main", "main")
assertFails("repository must have a configured")
assertFailsWithChanged("repository must have a configured")

git.CheckoutNew("not-main")
git.SetupRemote("origin", "not-main", "main")
assertFails("has no default branch ")
assertFailsWithChanged("has no default branch ")
}

func TestNoArgsProvidesBasicHelp(t *testing.T) {
Expand Down

0 comments on commit 1262a10

Please sign in to comment.