Skip to content

Commit 2f52aa7

Browse files
authored
feat: allow "." as valid source_roots entry (#2396)
Towards #2218
1 parent 043cf65 commit 2f52aa7

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

internal/config/state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/googleapis/librarian/internal/conventionalcommits"
24+
"github.com/googleapis/librarian/internal/gitrepo"
2425
)
2526

2627
const (
@@ -241,6 +242,10 @@ func isValidRelativePath(pathString string) bool {
241242
return false
242243
}
243244

245+
if pathString == gitrepo.RootPath {
246+
return true
247+
}
248+
244249
// The paths are expected to be relative and use the OS-specific path separator.
245250
// We clean the path to resolve ".." and check that it doesn't try to
246251
// escape the root.

internal/config/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func TestIsValidDirPath(t *testing.T) {
392392
{"absolute", "/a/b", false},
393393
{"up traversal", "../a", false},
394394
{"double dot", "..", false},
395-
{"single dot", ".", false},
395+
{"single dot", ".", true},
396396
{"invalid chars", "a/b<c", false},
397397
{"invalid null byte", "a/b\x00c", false},
398398
} {

internal/gitrepo/gitrepo.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type Repository interface {
5151
pushRefSpec(refSpec string) error
5252
}
5353

54+
const RootPath = "."
55+
5456
// LocalRepository represents a git repository.
5557
type LocalRepository struct {
5658
Dir string
@@ -335,18 +337,13 @@ func (r *LocalRepository) GetCommitsForPathsSinceCommit(paths []string, sinceCom
335337
// In theory, we should be able to remember our "current" commit for each
336338
// path, but that's likely to be significantly more complex.
337339
for _, candidatePath := range paths {
338-
currentPathHash, err := getHashForPathOrEmpty(commit, candidatePath)
339-
if err != nil {
340-
return err
341-
}
342-
parentPathHash, err := getHashForPathOrEmpty(parentCommit, candidatePath)
340+
matching, err := commitMatchesPath(candidatePath, commit, parentCommit)
343341
if err != nil {
344342
return err
345343
}
346344
// If we've found a change (including a path being added or removed),
347345
// add it to our list of commits and proceed to the next commit.
348-
if currentPathHash != parentPathHash {
349-
346+
if matching {
350347
commits = append(commits, &Commit{
351348
Hash: commit.Hash,
352349
Message: commit.Message,
@@ -367,6 +364,21 @@ func (r *LocalRepository) GetCommitsForPathsSinceCommit(paths []string, sinceCom
367364
return commits, nil
368365
}
369366

367+
func commitMatchesPath(path string, commit *object.Commit, parentCommit *object.Commit) (bool, error) {
368+
if path == RootPath {
369+
return true, nil
370+
}
371+
currentPathHash, err := getHashForPathOrEmpty(commit, path)
372+
if err != nil {
373+
return false, err
374+
}
375+
parentPathHash, err := getHashForPathOrEmpty(parentCommit, path)
376+
if err != nil {
377+
return false, err
378+
}
379+
return currentPathHash != parentPathHash, nil
380+
}
381+
370382
// getHashForPathOrEmpty returns the hash for a path at a given commit, or an
371383
// empty string if the path (file or directory) did not exist.
372384
func getHashForPathOrEmpty(commit *object.Commit, path string) (string, error) {

internal/gitrepo/gitrepo_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,13 @@ func TestGetCommitsForPathsSinceCommit(t *testing.T) {
975975
wantErr: true,
976976
wantErrPhrase: "did not find commit",
977977
},
978+
{
979+
name: "root path matches all commits",
980+
paths: []string{"."},
981+
sinceCommit: "",
982+
// The current implementation skips the initial commit.
983+
wantCommits: []string{"feat: commit 3", "feat: commit 2"},
984+
},
978985
} {
979986

980987
t.Run(test.name, func(t *testing.T) {

0 commit comments

Comments
 (0)