diff --git a/gittest/repository.go b/gittest/repository.go index 8a81174..dcf0b33 100644 --- a/gittest/repository.go +++ b/gittest/repository.go @@ -66,7 +66,7 @@ const ( ReadmeContent = "# Gitz Test Repository\n\n" + FileContent // an internal template for pushing changes back to a remote origin - gitPushTemplate = "git push origin %s" + gitPushTemplate = "git push -u origin %s" ) // RepositoryOption provides a utility for setting repository options during @@ -764,17 +764,20 @@ func Log(t *testing.T) []LogEntry { return ParseLog(log) } -// LogFrom ... +// LogFor returns the log history of a repository (working directory) +// at the given paths. Useful if you need to understand the history +// behind any number of files or directories. This will ignore any +// empty commits // -// git log -- '' '' -func LogFrom(t *testing.T, paths ...string) []LogEntry { +// git log --pretty='format:> %%H %%d %%s%%+b%%-N' -- '' '' +func LogFor(t *testing.T, paths ...string) []LogEntry { t.Helper() var quotedPaths []string for _, path := range paths { quotedPaths = append(quotedPaths, fmt.Sprintf("'%s'", path)) } - log := MustExec(t, fmt.Sprintf("git log -- %s", strings.Join(quotedPaths, " "))) + log := MustExec(t, fmt.Sprintf("git log --pretty='format:> %%H %%d %%s%%+b%%-N' -- %s", strings.Join(quotedPaths, " "))) return ParseLog(log) } diff --git a/gittest/repository_test.go b/gittest/repository_test.go index 293d188..a438b6f 100644 --- a/gittest/repository_test.go +++ b/gittest/repository_test.go @@ -467,13 +467,14 @@ func TestLogBetween(t *testing.T) { assert.Equal(t, "chore: tagged 0.2.0", diffLog[0].Message) } -func TestLogFrom(t *testing.T) { - // log := `(main, origin/main) chore: this should also appear in log - // chore: this should appear in log` - gittest.InitRepository(t) +func TestLogFor(t *testing.T) { + log := `(main) chore: this should also appear in log + chore: this should appear in log` + gittest.InitRepository(t, gittest.WithLog(log)) - localLog := gittest.LogFrom(t) - require.Len(t, localLog, 3) + localLog := gittest.LogFor(t, ".") + require.Len(t, localLog, 1) + assert.Equal(t, gittest.InitialCommit, localLog[0].Message) } func TestTag(t *testing.T) {