Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow recursing into submodules when using git walker #507

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
}

// create a new walker for traversing the paths
walker, err := walk.NewCompositeReader(walkType, cfg.TreeRoot, paths, db, statz)
walker, err := walk.NewCompositeReader(walkType, cfg.TreeRoot, paths, cfg.IncludeGitSubmodules, db, statz)
if err != nil {
return fmt.Errorf("failed to create walker: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
TreeRootFile string `mapstructure:"tree-root-file" toml:"tree-root-file,omitempty"`
Verbose uint8 `mapstructure:"verbose" toml:"verbose,omitempty"`
Walk string `mapstructure:"walk" toml:"walk,omitempty"`
IncludeGitSubmodules bool `mapstructure:"include-git-submodules toml:"include-git-subsmodules,omitempty"`
WorkingDirectory string `mapstructure:"working-dir" toml:"-"`
Stdin bool `mapstructure:"stdin" toml:"-"` // not allowed in config

Expand Down Expand Up @@ -115,6 +116,10 @@ func SetFlags(fs *pflag.FlagSet) {
"The method used to traverse the files within the tree root. Currently supports "+
"<auto|git|filesystem>. (env $TREEFMT_WALK)",
)
fs.Bool(
"include-git-submodules", false,
"Also format files in submodules",
)
fs.StringP(
"working-dir", "C", ".",
"Run as if treefmt was started in the specified working directory instead of the current working "+
Expand Down
11 changes: 10 additions & 1 deletion walk/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type GitReader struct {
root string
path string
includeSubmodules bool

log *log.Logger
stats *stats.Stats
Expand All @@ -38,7 +39,13 @@ func (g *GitReader) Read(ctx context.Context, files []*File) (n int, err error)
r, w := io.Pipe()

// create a command which will execute from the specified sub path within root
cmd := exec.Command("git", "ls-files")
var cmd *exec.Cmd
if g.includeSubmodules {
cmd = exec.Command("git", "ls-files")
} else {
cmd = exec.Command("git", "ls-files", "--recurse-submodules")
}

cmd.Dir = filepath.Join(g.root, g.path)
cmd.Stdout = w

Expand Down Expand Up @@ -133,6 +140,7 @@ func (g *GitReader) Close() error {
func NewGitReader(
root string,
path string,
includeSubmodules bool,
statz *stats.Stats,
) (*GitReader, error) {
// check if the root is a git repository
Expand All @@ -148,6 +156,7 @@ func NewGitReader(
return &GitReader{
root: root,
path: path,
includeSubmodules: includeSubmodules,
stats: statz,
eg: &errgroup.Group{},
log: log.WithPrefix("walk | git"),
Expand Down
4 changes: 2 additions & 2 deletions walk/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGitReader(t *testing.T) {

// read empty worktree
statz := stats.New()
reader, err := walk.NewGitReader(tempDir, "", &statz)
reader, err := walk.NewGitReader(tempDir, "", false, &statz)
as.NoError(err)

files := make([]*walk.File, 8)
Expand All @@ -42,7 +42,7 @@ func TestGitReader(t *testing.T) {
cmd.Dir = tempDir
as.NoError(cmd.Run(), "failed to add everything to the index")

reader, err = walk.NewGitReader(tempDir, "", &statz)
reader, err = walk.NewGitReader(tempDir, "", false, &statz)
as.NoError(err)

count := 0
Expand Down
14 changes: 8 additions & 6 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func NewReader(
walkType Type,
root string,
path string,
includeGitSubmodules bool,
db *bolt.DB,
statz *stats.Stats,
) (Reader, error) {
Expand All @@ -203,9 +204,9 @@ func NewReader(
switch walkType {
case Auto:
// for now, we keep it simple and try git first, filesystem second
reader, err = NewReader(Git, root, path, db, statz)
reader, err = NewReader(Git, root, path, includeGitSubmodules, db, statz)
if err != nil {
reader, err = NewReader(Filesystem, root, path, db, statz)
reader, err = NewReader(Filesystem, root, path, includeGitSubmodules, db, statz)
}

return reader, err
Expand All @@ -214,7 +215,7 @@ func NewReader(
case Filesystem:
reader = NewFilesystemReader(root, path, statz, BatchSize)
case Git:
reader, err = NewGitReader(root, path, statz)
reader, err = NewGitReader(root, path, includeGitSubmodules, statz)

default:
return nil, fmt.Errorf("unknown walk type: %v", walkType)
Expand All @@ -238,12 +239,13 @@ func NewCompositeReader(
walkType Type,
root string,
paths []string,
includeGitSubmodules bool,
db *bolt.DB,
statz *stats.Stats,
) (Reader, error) {
// if not paths are provided we default to processing the tree root
if len(paths) == 0 {
return NewReader(walkType, root, "", db, statz)
return NewReader(walkType, root, "", includeGitSubmodules, db, statz)
}

readers := make([]Reader, len(paths))
Expand Down Expand Up @@ -275,10 +277,10 @@ func NewCompositeReader(

if info.IsDir() {
// for directories, we honour the walk type as we traverse them
readers[idx], err = NewReader(walkType, root, relPath, db, statz)
readers[idx], err = NewReader(walkType, root, relPath, includeGitSubmodules, db, statz)
} else {
// for files, we enforce a simple filesystem read
readers[idx], err = NewReader(Filesystem, root, relPath, db, statz)
readers[idx], err = NewReader(Filesystem, root, relPath, includeGitSubmodules, db, statz)
}

if err != nil {
Expand Down