Skip to content

Commit

Permalink
fix: when initialising a repository with a log, ensure commits are as…
Browse files Browse the repository at this point in the history
…sociated with a file (#176)
  • Loading branch information
purpleclay authored Dec 22, 2024
1 parent 21f4788 commit 9edf04f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 18 additions & 1 deletion gittest/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,12 @@ process:
}

func importLogEntry(t *testing.T, entry LogEntry) {
commitCmd := fmt.Sprintf(`git commit --allow-empty -m "%s"`, entry.Message)
// HACK:
// Flip the executable bit allowing the commit to be associated to the file
// without altering its contents
flipExecutableBit(t, "README.md")
StageFile(t, "README.md")
commitCmd := fmt.Sprintf(`git commit -m "%s"`, entry.Message)
MustExec(t, commitCmd)

// Grab the commit hash and use it when creating branches and tags
Expand Down Expand Up @@ -532,6 +537,18 @@ func importTagsAtRef(t *testing.T, tags []string, ref string) {
MustExec(t, "git push --tags")
}

func flipExecutableBit(t *testing.T, path string) {
fi, err := os.Stat(path)
require.NoError(t, err, "README.md should exist")

perms := fi.Mode()
if perms&0o100 != 0 {
require.NoError(t, os.Chmod(path, perms&^0o100), "failed to turn on executable bit")
} else {
require.NoError(t, os.Chmod(path, perms|0o100), "failed to turn off executable bit")
}
}

func setConfig(t *testing.T, key, value string) {
configCmd := fmt.Sprintf(`git config %s "%s"`, key, value)
_, err := Exec(t, configCmd)
Expand Down
8 changes: 5 additions & 3 deletions gittest/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,14 @@ func TestLogBetween(t *testing.T) {

func TestLogFor(t *testing.T) {
log := `(main) chore: this should also appear in log
chore: this should 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)
require.Len(t, localLog, 3)
assert.Equal(t, "chore: this should also appear in log", localLog[0].Message)
assert.Equal(t, "chore: this should appear in log", localLog[1].Message)
assert.Equal(t, gittest.InitialCommit, localLog[2].Message)
}

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

0 comments on commit 9edf04f

Please sign in to comment.