Skip to content

Commit

Permalink
fix: when retrieving logs against a gittest repository using dot retu…
Browse files Browse the repository at this point in the history
…rns no logs (#174)
  • Loading branch information
purpleclay authored Dec 21, 2024
1 parent 5f71f61 commit 21f4788
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 16 deletions.
7 changes: 4 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";

# Lock to version: 0.14.0
dagger = {
url = "github:dagger/nix";
url = "github:dagger/nix?rev=9852fdddcdcb52841275ffb6a39fa1524d538d5a";
inputs = {
nixpkgs.follows = "nixpkgs";
};
Expand Down
37 changes: 32 additions & 5 deletions gittest/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ const (
// options. Grabbed from: https://loremipsum.io/
FileContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

// ReadmeContent is written to the README.md file when initializing the repository
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
Expand Down Expand Up @@ -290,9 +293,13 @@ func WithCloneDepth(depth int) RepositoryOption {
// Repository creation consists of two phases. First, a bare repository
// is initialized, before being cloned locally. This ensures a fully
// working remote. Without customization (options), the test repository
// will consist of single commit:
// will consist of a README.md and a single commit:
//
// > git log --oneline
// <HASH> initialized repository
//
// initialized repository
// > git ls-files
// README.md
func InitRepository(t *testing.T, opts ...RepositoryOption) {
t.Helper()

Expand Down Expand Up @@ -386,9 +393,12 @@ func cloneRemoteAndInit(t *testing.T, cloneName string, options ...string) {
setConfig(t, "user.name", DefaultAuthorName)
setConfig(t, "user.email", DefaultAuthorEmail)

// Check if there any any commits, if not, initialize and push back first commit
// Check if there any any commits, if not, initialize with readme and push back first commit
if out := MustExec(t, "git rev-list -n1 --all"); out == "" {
MustExec(t, fmt.Sprintf(`git commit --allow-empty -m "%s"`, InitialCommit))
TempFile(t, "README.md", ReadmeContent)
StageFile(t, "README.md")

MustExec(t, fmt.Sprintf(`git commit -m "%s"`, InitialCommit))
MustExec(t, fmt.Sprintf(gitPushTemplate, DefaultBranch))
}

Expand Down Expand Up @@ -754,6 +764,23 @@ func Log(t *testing.T) []LogEntry {
return ParseLog(log)
}

// 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 --pretty='format:> %%H %%d %%s%%+b%%-N' -- '<path>' '<path>'
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 --pretty='format:> %%H %%d %%s%%+b%%-N' -- %s", strings.Join(quotedPaths, " ")))
return ParseLog(log)
}

// LogBetween returns the log history of a repository (working directory)
// between two references. Raw output is parsed from this command:
//
Expand Down
10 changes: 10 additions & 0 deletions gittest/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ func TestLogBetween(t *testing.T) {
assert.Equal(t, "chore: tagged 0.2.0", diffLog[0].Message)
}

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.LogFor(t, ".")
require.Len(t, localLog, 1)
assert.Equal(t, gittest.InitialCommit, localLog[0].Message)
}

func TestTag(t *testing.T) {
gittest.InitRepository(t)

Expand Down
4 changes: 2 additions & 2 deletions restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

func TestRestoreUsingForUntrackedFiles(t *testing.T) {
gittest.InitRepository(t, gittest.WithFiles("README.md", ".github/ci.yaml", "go.mod"))
gittest.InitRepository(t, gittest.WithFiles("main.go", ".github/ci.yaml", "go.mod"))

untracked := [2]git.FileStatusIndicator{git.Untracked, git.Untracked}

client, _ := git.NewClient()
err := client.RestoreUsing([]git.FileStatus{
{Indicators: untracked, Path: "README.md"},
{Indicators: untracked, Path: "main.go"},
{Indicators: untracked, Path: ".github/"},
{Indicators: untracked, Path: "go.mod"},
})
Expand Down
8 changes: 4 additions & 4 deletions status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
)

func TestPorcelainStatus(t *testing.T) {
gittest.InitRepository(t, gittest.WithFiles("README.md"), gittest.WithStagedFiles("go.mod"))
gittest.InitRepository(t, gittest.WithFiles("main.go"), gittest.WithStagedFiles("go.mod"))

client, _ := git.NewClient()
statuses, err := client.PorcelainStatus()
require.NoError(t, err)

require.Len(t, statuses, 2)
assert.ElementsMatch(t,
[]string{"?? README.md", "A go.mod"},
[]string{"?? main.go", "A go.mod"},
[]string{statuses[0].String(), statuses[1].String()},
)
}

func TestPorcelainStatusWithIgnoreUntracked(t *testing.T) {
gittest.InitRepository(t, gittest.WithFiles("README.md"), gittest.WithStagedFiles("go.mod"))
gittest.InitRepository(t, gittest.WithFiles("main.go"), gittest.WithStagedFiles("go.mod"))

client, _ := git.NewClient()
statuses, err := client.PorcelainStatus(git.WithIgnoreUntracked())
Expand All @@ -36,7 +36,7 @@ func TestPorcelainStatusWithIgnoreUntracked(t *testing.T) {
}

func TestPorcelainStatusWithIgnoreRenames(t *testing.T) {
gittest.InitRepository(t, gittest.WithFiles("go.mod"), gittest.WithCommittedFiles("README.md"))
gittest.InitRepository(t, gittest.WithFiles("go.mod"))
gittest.Move(t, "README.md", "CONTRIBUTING.md")

client, _ := git.NewClient()
Expand Down
2 changes: 1 addition & 1 deletion taskfile.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"

vars:
GOLANG_DAGGER: "github.com/purpleclay/daggerverse/golang@v0.3.0"
GOLANG_DAGGER: "github.com/purpleclay/daggerverse/golang"

tasks:
default:
Expand Down

0 comments on commit 21f4788

Please sign in to comment.