diff --git a/flake.lock b/flake.lock index 4f50ba3..306b23f 100644 --- a/flake.lock +++ b/flake.lock @@ -7,16 +7,17 @@ ] }, "locked": { - "lastModified": 1718718080, - "narHash": "sha256-Obb+1wZtNziOXCa8F+PZ6+FmPVf6bFWHF9m5LxtJNpc=", + "lastModified": 1731102567, + "narHash": "sha256-A0xTZedeIwMceV86/BB3b6GgS+DVoqQwxywDgH68x7s=", "owner": "dagger", "repo": "nix", - "rev": "0e658d2f7079a823f7092825ec6700323b47e935", + "rev": "9852fdddcdcb52841275ffb6a39fa1524d538d5a", "type": "github" }, "original": { "owner": "dagger", "repo": "nix", + "rev": "9852fdddcdcb52841275ffb6a39fa1524d538d5a", "type": "github" } }, diff --git a/flake.nix b/flake.nix index 9ae270d..5bd5e64 100644 --- a/flake.nix +++ b/flake.nix @@ -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"; }; diff --git a/gittest/repository.go b/gittest/repository.go index 57ad8d0..dcf0b33 100644 --- a/gittest/repository.go +++ b/gittest/repository.go @@ -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 @@ -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 +// initialized repository // -// initialized repository +// > git ls-files +// README.md func InitRepository(t *testing.T, opts ...RepositoryOption) { t.Helper() @@ -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)) } @@ -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' -- '' '' +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: // diff --git a/gittest/repository_test.go b/gittest/repository_test.go index e311b2f..a438b6f 100644 --- a/gittest/repository_test.go +++ b/gittest/repository_test.go @@ -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) diff --git a/restore_test.go b/restore_test.go index 1a6f75d..a9ca261 100644 --- a/restore_test.go +++ b/restore_test.go @@ -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"}, }) diff --git a/status_test.go b/status_test.go index 4853b7e..2418002 100644 --- a/status_test.go +++ b/status_test.go @@ -11,7 +11,7 @@ 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() @@ -19,13 +19,13 @@ func TestPorcelainStatus(t *testing.T) { 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()) @@ -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() diff --git a/taskfile.yaml b/taskfile.yaml index abf0ccf..f96939f 100644 --- a/taskfile.yaml +++ b/taskfile.yaml @@ -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: