Adds initial gitlocal CLI and core functionality

Introduces the `gitlocal` command-line tool for managing nested Git repositories.

Includes the following main commands:
- `convert`: Renames `.git` to `.gitlocal`, allowing a parent repository to ignore the converted child repository. Supports recursive scanning and dry-run options. Tracks converted repositories in a global configuration.
- `revert`: Restores `.gitlocal` to `.git`. Includes an option to revert all tracked repositories.
- `status`: Displays a list of all repositories currently tracked by `gitlocal`, showing their path, conversion time, and original remote/branch.

Establishes internal modules for Git operations, configuration management, and recursive repository scanning.
Adds a comprehensive test suite covering core command logic and utility functions.
Initializes Go module and basic project `.gitignore`.
This commit is contained in:
2026-04-11 14:48:01 +01:00
parent c9867f410f
commit b5f1495680
16 changed files with 2263 additions and 0 deletions

111
internal/config/config.go Normal file
View File

@@ -0,0 +1,111 @@
package config
import (
"fmt"
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v3"
)
const (
ConfigVersion = "1"
ConfigFile = ".gitlocal.yml"
)
type Repo struct {
Path string `yaml:"path"`
ConvertedAt time.Time `yaml:"converted_at"`
OriginalRemote string `yaml:"original_remote,omitempty"`
OriginalBranch string `yaml:"original_branch,omitempty"`
}
type Config struct {
Version string `yaml:"version"`
Repos []Repo `yaml:"repos"`
}
// GetConfigPath returns the absolute path to the config file
func GetConfigPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get home directory: %w", err)
}
return filepath.Join(home, ConfigFile), nil
}
// Load reads the config file or creates a new empty config if it doesn't exist
func Load() (*Config, error) {
configPath, err := GetConfigPath()
if err != nil {
return nil, err
}
// If config doesn't exist, return empty config
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return &Config{
Version: ConfigVersion,
Repos: []Repo{},
}, nil
}
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
return &cfg, nil
}
// Save writes the config to disk
func (c *Config) Save() error {
configPath, err := GetConfigPath()
if err != nil {
return err
}
data, err := yaml.Marshal(c)
if err != nil {
return fmt.Errorf("failed to marshal config: %w", err)
}
if err := os.WriteFile(configPath, data, 0644); err != nil {
return fmt.Errorf("failed to write config file: %w", err)
}
return nil
}
// AddRepo adds a new repo to the config
func (c *Config) AddRepo(repo Repo) {
// Remove existing entry if it exists
c.RemoveRepo(repo.Path)
c.Repos = append(c.Repos, repo)
}
// RemoveRepo removes a repo from the config by path
func (c *Config) RemoveRepo(path string) {
filtered := []Repo{}
for _, r := range c.Repos {
if r.Path != path {
filtered = append(filtered, r)
}
}
c.Repos = filtered
}
// FindRepo returns a repo by path, or nil if not found
func (c *Config) FindRepo(path string) *Repo {
for _, r := range c.Repos {
if r.Path == path {
return &r
}
}
return nil
}

View File

@@ -0,0 +1,260 @@
package config
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestLoadConfigNotExists(t *testing.T) {
// Override config path to use temp file
originalHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", originalHome)
cfg, err := Load()
if err != nil {
t.Fatalf("expected no error when config doesn't exist, got: %v", err)
}
if cfg.Version != ConfigVersion {
t.Errorf("expected version %s, got %s", ConfigVersion, cfg.Version)
}
if len(cfg.Repos) != 0 {
t.Errorf("expected empty repos, got %d repos", len(cfg.Repos))
}
}
func TestLoadConfigExists(t *testing.T) {
originalHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", originalHome)
// Create a config file
configPath := filepath.Join(tempDir, ConfigFile)
configContent := `version: "1"
repos:
- path: /test/path/1
converted_at: 2026-04-07T14:30:00Z
original_remote: git@github.com:user/repo1.git
original_branch: main
- path: /test/path/2
converted_at: 2026-04-07T15:00:00Z
original_remote: git@github.com:user/repo2.git
original_branch: develop
`
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
t.Fatalf("failed to write test config: %v", err)
}
cfg, err := Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Version != "1" {
t.Errorf("expected version 1, got %s", cfg.Version)
}
if len(cfg.Repos) != 2 {
t.Fatalf("expected 2 repos, got %d", len(cfg.Repos))
}
if cfg.Repos[0].Path != "/test/path/1" {
t.Errorf("expected path /test/path/1, got %s", cfg.Repos[0].Path)
}
if cfg.Repos[0].OriginalRemote != "git@github.com:user/repo1.git" {
t.Errorf("expected remote git@github.com:user/repo1.git, got %s", cfg.Repos[0].OriginalRemote)
}
}
func TestSaveConfig(t *testing.T) {
originalHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", originalHome)
cfg := &Config{
Version: ConfigVersion,
Repos: []Repo{
{
Path: "/test/repo",
ConvertedAt: time.Now(),
OriginalRemote: "git@github.com:test/repo.git",
OriginalBranch: "main",
},
},
}
if err := cfg.Save(); err != nil {
t.Fatalf("failed to save config: %v", err)
}
configPath := filepath.Join(tempDir, ConfigFile)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
t.Fatalf("config file was not created")
}
// Load it back to verify
loadedCfg, err := Load()
if err != nil {
t.Fatalf("failed to load saved config: %v", err)
}
if len(loadedCfg.Repos) != 1 {
t.Fatalf("expected 1 repo, got %d", len(loadedCfg.Repos))
}
if loadedCfg.Repos[0].Path != "/test/repo" {
t.Errorf("expected path /test/repo, got %s", loadedCfg.Repos[0].Path)
}
}
func TestAddRepo(t *testing.T) {
cfg := &Config{
Version: ConfigVersion,
Repos: []Repo{},
}
repo1 := Repo{
Path: "/test/repo1",
ConvertedAt: time.Now(),
OriginalRemote: "git@github.com:test/repo1.git",
OriginalBranch: "main",
}
cfg.AddRepo(repo1)
if len(cfg.Repos) != 1 {
t.Fatalf("expected 1 repo, got %d", len(cfg.Repos))
}
if cfg.Repos[0].Path != "/test/repo1" {
t.Errorf("expected path /test/repo1, got %s", cfg.Repos[0].Path)
}
}
func TestAddRepoReplaceExisting(t *testing.T) {
now := time.Now()
later := now.Add(1 * time.Hour)
cfg := &Config{
Version: ConfigVersion,
Repos: []Repo{
{
Path: "/test/repo",
ConvertedAt: now,
OriginalRemote: "git@github.com:test/old.git",
OriginalBranch: "main",
},
},
}
// Add same path again with different data
repo := Repo{
Path: "/test/repo",
ConvertedAt: later,
OriginalRemote: "git@github.com:test/new.git",
OriginalBranch: "develop",
}
cfg.AddRepo(repo)
if len(cfg.Repos) != 1 {
t.Fatalf("expected 1 repo (replaced), got %d", len(cfg.Repos))
}
if cfg.Repos[0].OriginalRemote != "git@github.com:test/new.git" {
t.Errorf("expected new remote, got %s", cfg.Repos[0].OriginalRemote)
}
if cfg.Repos[0].OriginalBranch != "develop" {
t.Errorf("expected branch develop, got %s", cfg.Repos[0].OriginalBranch)
}
if !cfg.Repos[0].ConvertedAt.Equal(later) {
t.Errorf("expected later timestamp")
}
}
func TestRemoveRepo(t *testing.T) {
cfg := &Config{
Version: ConfigVersion,
Repos: []Repo{
{Path: "/test/repo1"},
{Path: "/test/repo2"},
{Path: "/test/repo3"},
},
}
cfg.RemoveRepo("/test/repo2")
if len(cfg.Repos) != 2 {
t.Fatalf("expected 2 repos, got %d", len(cfg.Repos))
}
for _, repo := range cfg.Repos {
if repo.Path == "/test/repo2" {
t.Errorf("repo2 should have been removed")
}
}
}
func TestRemoveRepoNotFound(t *testing.T) {
cfg := &Config{
Version: ConfigVersion,
Repos: []Repo{
{Path: "/test/repo1"},
},
}
cfg.RemoveRepo("/test/nonexistent")
if len(cfg.Repos) != 1 {
t.Fatalf("expected 1 repo, got %d", len(cfg.Repos))
}
}
func TestFindRepo(t *testing.T) {
cfg := &Config{
Version: ConfigVersion,
Repos: []Repo{
{
Path: "/test/repo1",
OriginalRemote: "git@github.com:test/repo1.git",
},
{
Path: "/test/repo2",
OriginalRemote: "git@github.com:test/repo2.git",
},
},
}
repo := cfg.FindRepo("/test/repo1")
if repo == nil {
t.Fatalf("expected to find repo1")
}
if repo.OriginalRemote != "git@github.com:test/repo1.git" {
t.Errorf("expected repo1 remote, got %s", repo.OriginalRemote)
}
}
func TestFindRepoNotFound(t *testing.T) {
cfg := &Config{
Version: ConfigVersion,
Repos: []Repo{
{Path: "/test/repo1"},
},
}
repo := cfg.FindRepo("/test/nonexistent")
if repo != nil {
t.Errorf("expected nil for nonexistent repo, got %v", repo)
}
}

94
internal/git/git.go Normal file
View File

@@ -0,0 +1,94 @@
package git
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
GitDir = ".git"
GitLocalDir = ".gitlocal"
)
// IsGitRepo checks if the path contains a .git directory
func IsGitRepo(path string) bool {
gitPath := filepath.Join(path, GitDir)
info, err := os.Stat(gitPath)
if err != nil {
return false
}
return info.IsDir()
}
// IsGitLocalRepo checks if the path contains a .gitlocal directory
func IsGitLocalRepo(path string) bool {
gitLocalPath := filepath.Join(path, GitLocalDir)
info, err := os.Stat(gitLocalPath)
if err != nil {
return false
}
return info.IsDir()
}
// GetRemoteURL returns the remote URL for the git repo
func GetRemoteURL(gitDir string) string {
cmd := exec.Command("git", "--git-dir", gitDir, "remote", "get-url", "origin")
output, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(output))
}
// GetCurrentBranch returns the current branch for the git repo
func GetCurrentBranch(gitDir string) string {
cmd := exec.Command("git", "--git-dir", gitDir, "branch", "--show-current")
output, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(output))
}
// Convert renames .git to .gitlocal
func Convert(path string) error {
gitPath := filepath.Join(path, GitDir)
gitLocalPath := filepath.Join(path, GitLocalDir)
if !IsGitRepo(path) {
return fmt.Errorf("no .git directory found at %s", path)
}
if IsGitLocalRepo(path) {
return fmt.Errorf(".gitlocal already exists at %s", path)
}
if err := os.Rename(gitPath, gitLocalPath); err != nil {
return fmt.Errorf("failed to rename .git to .gitlocal: %w", err)
}
return nil
}
// Revert renames .gitlocal back to .git
func Revert(path string) error {
gitPath := filepath.Join(path, GitDir)
gitLocalPath := filepath.Join(path, GitLocalDir)
if !IsGitLocalRepo(path) {
return fmt.Errorf("no .gitlocal directory found at %s", path)
}
if IsGitRepo(path) {
return fmt.Errorf(".git already exists at %s", path)
}
if err := os.Rename(gitLocalPath, gitPath); err != nil {
return fmt.Errorf("failed to rename .gitlocal to .git: %w", err)
}
return nil
}

372
internal/git/git_test.go Normal file
View File

@@ -0,0 +1,372 @@
package git
import (
"os"
"path/filepath"
"testing"
"git.membo.co.uk/dtomlinson/gitlocal/internal/testutil"
)
func TestIsGitRepo(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
expected bool
}{
{
name: "valid git repo",
setup: func(t *testing.T) string {
return testutil.CreateTempGitRepo(t)
},
expected: true,
},
{
name: "not a git repo",
setup: func(t *testing.T) string {
return t.TempDir()
},
expected: false,
},
{
name: "has .gitlocal but not .git",
setup: func(t *testing.T) string {
dir := t.TempDir()
gitLocalDir := filepath.Join(dir, GitLocalDir)
if err := os.Mkdir(gitLocalDir, 0755); err != nil {
t.Fatalf("failed to create .gitlocal: %v", err)
}
return dir
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.setup(t)
result := IsGitRepo(path)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
func TestIsGitLocalRepo(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
expected bool
}{
{
name: "has .gitlocal",
setup: func(t *testing.T) string {
dir := t.TempDir()
gitLocalDir := filepath.Join(dir, GitLocalDir)
if err := os.Mkdir(gitLocalDir, 0755); err != nil {
t.Fatalf("failed to create .gitlocal: %v", err)
}
return dir
},
expected: true,
},
{
name: "has .git but not .gitlocal",
setup: func(t *testing.T) string {
return testutil.CreateTempGitRepo(t)
},
expected: false,
},
{
name: "has neither",
setup: func(t *testing.T) string {
return t.TempDir()
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.setup(t)
result := IsGitLocalRepo(path)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
func TestGetRemoteURL(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
expected string
}{
{
name: "repo with remote",
setup: func(t *testing.T) string {
dir := testutil.CreateTempGitRepoWithRemote(t, "git@github.com:test/repo.git")
return filepath.Join(dir, GitDir)
},
expected: "git@github.com:test/repo.git",
},
{
name: "repo without remote",
setup: func(t *testing.T) string {
dir := testutil.CreateTempGitRepo(t)
return filepath.Join(dir, GitDir)
},
expected: "",
},
{
name: "invalid git dir",
setup: func(t *testing.T) string {
return "/nonexistent/.git"
},
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gitDir := tt.setup(t)
result := GetRemoteURL(gitDir)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestGetCurrentBranch(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
expected string
}{
{
name: "default branch (master or main)",
setup: func(t *testing.T) string {
dir := testutil.CreateTempGitRepo(t)
return filepath.Join(dir, GitDir)
},
// Git might use master or main depending on config
expected: "", // We'll check it's non-empty instead
},
{
name: "custom branch",
setup: func(t *testing.T) string {
dir := testutil.CreateTempGitRepoWithBranch(t, "develop")
return filepath.Join(dir, GitDir)
},
expected: "develop",
},
{
name: "invalid git dir",
setup: func(t *testing.T) string {
return "/nonexistent/.git"
},
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gitDir := tt.setup(t)
result := GetCurrentBranch(gitDir)
if tt.name == "default branch (master or main)" {
// Just check it returned something
if result == "" {
t.Errorf("expected non-empty branch name, got empty string")
}
} else if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestConvert(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
expectErr bool
}{
{
name: "valid git repo",
setup: func(t *testing.T) string {
return testutil.CreateTempGitRepo(t)
},
expectErr: false,
},
{
name: "no .git directory",
setup: func(t *testing.T) string {
return t.TempDir()
},
expectErr: true,
},
{
name: ".gitlocal already exists",
setup: func(t *testing.T) string {
dir := testutil.CreateTempGitRepo(t)
gitLocalDir := filepath.Join(dir, GitLocalDir)
if err := os.Mkdir(gitLocalDir, 0755); err != nil {
t.Fatalf("failed to create .gitlocal: %v", err)
}
return dir
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.setup(t)
err := Convert(path)
if tt.expectErr && err == nil {
t.Errorf("expected error, got nil")
}
if !tt.expectErr && err != nil {
t.Errorf("expected no error, got %v", err)
}
if !tt.expectErr {
// Verify .git was renamed to .gitlocal
gitPath := filepath.Join(path, GitDir)
gitLocalPath := filepath.Join(path, GitLocalDir)
if _, err := os.Stat(gitPath); !os.IsNotExist(err) {
t.Errorf(".git directory still exists")
}
if _, err := os.Stat(gitLocalPath); os.IsNotExist(err) {
t.Errorf(".gitlocal directory does not exist")
}
}
})
}
}
func TestRevert(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
expectErr bool
}{
{
name: "valid .gitlocal directory",
setup: func(t *testing.T) string {
dir := testutil.CreateTempGitRepo(t)
// Convert to .gitlocal
if err := Convert(dir); err != nil {
t.Fatalf("failed to convert: %v", err)
}
return dir
},
expectErr: false,
},
{
name: "no .gitlocal directory",
setup: func(t *testing.T) string {
return t.TempDir()
},
expectErr: true,
},
{
name: ".git already exists",
setup: func(t *testing.T) string {
dir := testutil.CreateTempGitRepo(t)
gitLocalDir := filepath.Join(dir, GitLocalDir)
if err := os.Mkdir(gitLocalDir, 0755); err != nil {
t.Fatalf("failed to create .gitlocal: %v", err)
}
return dir
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.setup(t)
err := Revert(path)
if tt.expectErr && err == nil {
t.Errorf("expected error, got nil")
}
if !tt.expectErr && err != nil {
t.Errorf("expected no error, got %v", err)
}
if !tt.expectErr {
// Verify .gitlocal was renamed to .git
gitPath := filepath.Join(path, GitDir)
gitLocalPath := filepath.Join(path, GitLocalDir)
if _, err := os.Stat(gitLocalPath); !os.IsNotExist(err) {
t.Errorf(".gitlocal directory still exists")
}
if _, err := os.Stat(gitPath); os.IsNotExist(err) {
t.Errorf(".git directory does not exist")
}
}
})
}
}
func TestConvertRevertRoundTrip(t *testing.T) {
dir := testutil.CreateTempGitRepoWithRemote(t, "git@github.com:test/repo.git")
// Initial state - should have .git
if !IsGitRepo(dir) {
t.Fatal("expected .git to exist initially")
}
// Convert
if err := Convert(dir); err != nil {
t.Fatalf("convert failed: %v", err)
}
// Should have .gitlocal, not .git
if IsGitRepo(dir) {
t.Error("expected .git to be gone after convert")
}
if !IsGitLocalRepo(dir) {
t.Error("expected .gitlocal to exist after convert")
}
// Remote should still be accessible via .gitlocal
gitLocalPath := filepath.Join(dir, GitLocalDir)
remote := GetRemoteURL(gitLocalPath)
if remote != "git@github.com:test/repo.git" {
t.Errorf("expected remote to be preserved, got %q", remote)
}
// Revert
if err := Revert(dir); err != nil {
t.Fatalf("revert failed: %v", err)
}
// Should have .git again, not .gitlocal
if !IsGitRepo(dir) {
t.Error("expected .git to exist after revert")
}
if IsGitLocalRepo(dir) {
t.Error("expected .gitlocal to be gone after revert")
}
// Remote should still be accessible
gitPath := filepath.Join(dir, GitDir)
remote = GetRemoteURL(gitPath)
if remote != "git@github.com:test/repo.git" {
t.Errorf("expected remote to be preserved after round trip, got %q", remote)
}
}

View File

@@ -0,0 +1,59 @@
package scanner
import (
"os"
"path/filepath"
"git.membo.co.uk/dtomlinson/gitlocal/internal/git"
)
// FindNestedGitRepos recursively scans rootPath for nested .git and .gitlocal directories
// and returns a list of absolute paths to directories containing .git or .gitlocal
// It skips the root repo's .git/.gitlocal directory if one exists
func FindNestedGitRepos(rootPath string) ([]string, error) {
var repos []string
// Get absolute path
absRoot, err := filepath.Abs(rootPath)
if err != nil {
return nil, err
}
// Check if root itself is a git repo (either .git or .gitlocal)
rootIsGitRepo := git.IsGitRepo(absRoot) || git.IsGitLocalRepo(absRoot)
rootGitPath := filepath.Join(absRoot, git.GitDir)
rootGitLocalPath := filepath.Join(absRoot, git.GitLocalDir)
err = filepath.Walk(absRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip if not a directory
if !info.IsDir() {
return nil
}
// Skip if this is the root's .git or .gitlocal directory
if rootIsGitRepo && (path == rootGitPath || path == rootGitLocalPath) {
return filepath.SkipDir
}
// Check if this directory is named .git or .gitlocal
if info.Name() == git.GitDir || info.Name() == git.GitLocalDir {
// Get the parent directory (the actual repo path)
repoPath := filepath.Dir(path)
repos = append(repos, repoPath)
// Skip descending into .git/.gitlocal directory
return filepath.SkipDir
}
return nil
})
if err != nil {
return nil, err
}
return repos, nil
}

View File

@@ -0,0 +1,225 @@
package scanner
import (
"os"
"path/filepath"
"sort"
"testing"
"git.membo.co.uk/dtomlinson/gitlocal/internal/git"
"git.membo.co.uk/dtomlinson/gitlocal/internal/testutil"
)
func TestFindNestedGitRepos(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) (rootDir string, expectedPaths []string)
expectedCount int
}{
{
name: "no nested repos",
setup: func(t *testing.T) (string, []string) {
dir := testutil.CreateTempGitRepo(t)
return dir, []string{}
},
expectedCount: 0,
},
{
name: "nested repos structure",
setup: func(t *testing.T) (string, []string) {
rootDir, nestedDirs := testutil.CreateNestedRepoStructure(t)
return rootDir, nestedDirs
},
expectedCount: 3,
},
{
name: "non-git directory",
setup: func(t *testing.T) (string, []string) {
dir := t.TempDir()
// Create subdirectories without .git
os.MkdirAll(filepath.Join(dir, "subdir1"), 0755)
os.MkdirAll(filepath.Join(dir, "subdir2", "deep"), 0755)
return dir, []string{}
},
expectedCount: 0,
},
{
name: "mixed nested repos and regular dirs",
setup: func(t *testing.T) (string, []string) {
dir := testutil.CreateTempGitRepo(t)
// Create one nested git repo
nested1 := filepath.Join(dir, "nested1")
os.MkdirAll(nested1, 0755)
testutil.CreateTempGitRepo(t) // This creates in temp dir, we need to manually init
// Let's use a helper to init git in nested1
initGitInDir(t, nested1)
// Create regular directories
os.MkdirAll(filepath.Join(dir, "regular"), 0755)
os.MkdirAll(filepath.Join(dir, "another", "deep"), 0755)
return dir, []string{nested1}
},
expectedCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rootDir, expectedPaths := tt.setup(t)
repos, err := FindNestedGitRepos(rootDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(repos) != tt.expectedCount {
t.Errorf("expected %d repos, got %d: %v", tt.expectedCount, len(repos), repos)
}
if tt.expectedCount > 0 {
// Sort both slices for comparison
sort.Strings(repos)
sort.Strings(expectedPaths)
for i, expected := range expectedPaths {
if i >= len(repos) {
t.Errorf("missing expected repo: %s", expected)
continue
}
if repos[i] != expected {
t.Errorf("expected repo %s, got %s", expected, repos[i])
}
}
}
})
}
}
func TestFindNestedGitReposSkipsRootGit(t *testing.T) {
rootDir := testutil.CreateTempGitRepo(t)
// Create nested repos
nested1 := filepath.Join(rootDir, "nested1")
os.MkdirAll(nested1, 0755)
initGitInDir(t, nested1)
repos, err := FindNestedGitRepos(rootDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should find nested1 but not the root
if len(repos) != 1 {
t.Fatalf("expected 1 nested repo, got %d", len(repos))
}
if repos[0] != nested1 {
t.Errorf("expected %s, got %s", nested1, repos[0])
}
}
func TestFindNestedGitReposWithGitLocal(t *testing.T) {
rootDir := testutil.CreateTempGitRepo(t)
// Create nested repo with .gitlocal
nested1 := filepath.Join(rootDir, "nested1")
os.MkdirAll(nested1, 0755)
initGitInDir(t, nested1)
// Convert to .gitlocal
if err := git.Convert(nested1); err != nil {
t.Fatalf("failed to convert: %v", err)
}
// Create nested repo with .git
nested2 := filepath.Join(rootDir, "nested2")
os.MkdirAll(nested2, 0755)
initGitInDir(t, nested2)
repos, err := FindNestedGitRepos(rootDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should find both
if len(repos) != 2 {
t.Fatalf("expected 2 nested repos, got %d", len(repos))
}
sort.Strings(repos)
expectedPaths := []string{nested1, nested2}
sort.Strings(expectedPaths)
for i, expected := range expectedPaths {
if repos[i] != expected {
t.Errorf("expected repo %s, got %s", expected, repos[i])
}
}
}
func TestFindNestedGitReposWithRootGitLocal(t *testing.T) {
rootDir := testutil.CreateTempGitRepo(t)
// Convert root to .gitlocal
if err := git.Convert(rootDir); err != nil {
t.Fatalf("failed to convert root: %v", err)
}
// Create nested repo
nested1 := filepath.Join(rootDir, "nested1")
os.MkdirAll(nested1, 0755)
initGitInDir(t, nested1)
repos, err := FindNestedGitRepos(rootDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should find nested1 but not the root .gitlocal
if len(repos) != 1 {
t.Fatalf("expected 1 nested repo, got %d", len(repos))
}
if repos[0] != nested1 {
t.Errorf("expected %s, got %s", nested1, repos[0])
}
}
func TestFindNestedGitReposDeepNesting(t *testing.T) {
rootDir := testutil.CreateTempGitRepo(t)
// Create deeply nested structure
deep := filepath.Join(rootDir, "a", "b", "c", "d", "nested")
os.MkdirAll(deep, 0755)
initGitInDir(t, deep)
repos, err := FindNestedGitRepos(rootDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(repos) != 1 {
t.Fatalf("expected 1 nested repo, got %d", len(repos))
}
if repos[0] != deep {
t.Errorf("expected %s, got %s", deep, repos[0])
}
}
// Helper function to initialize git in a directory
func initGitInDir(t *testing.T, dir string) {
t.Helper()
// We can't use testutil.CreateTempGitRepo since it creates a new temp dir
// So we manually init git here
gitDir := filepath.Join(dir, git.GitDir)
if err := os.MkdirAll(gitDir, 0755); err != nil {
t.Fatalf("failed to create .git directory: %v", err)
}
// Create minimal git structure
// For the scanner, we just need the .git directory to exist
// The scanner doesn't validate the git repo structure
}

View File

@@ -0,0 +1,158 @@
package testutil
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
// CreateTempGitRepo creates a temporary directory with a git repository
func CreateTempGitRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
// Initialize git repo
cmd := exec.Command("git", "init")
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Fatalf("failed to init git repo: %v", err)
}
// Configure git user for the test repo
configName := exec.Command("git", "config", "user.name", "Test User")
configName.Dir = dir
if err := configName.Run(); err != nil {
t.Fatalf("failed to configure git user.name: %v", err)
}
configEmail := exec.Command("git", "config", "user.email", "test@example.com")
configEmail.Dir = dir
if err := configEmail.Run(); err != nil {
t.Fatalf("failed to configure git user.email: %v", err)
}
// Create initial commit
readmePath := filepath.Join(dir, "README.md")
if err := os.WriteFile(readmePath, []byte("# Test Repo\n"), 0644); err != nil {
t.Fatalf("failed to create README: %v", err)
}
add := exec.Command("git", "add", "README.md")
add.Dir = dir
if err := add.Run(); err != nil {
t.Fatalf("failed to git add: %v", err)
}
commit := exec.Command("git", "commit", "-m", "Initial commit")
commit.Dir = dir
if err := commit.Run(); err != nil {
t.Fatalf("failed to git commit: %v", err)
}
return dir
}
// CreateTempGitRepoWithRemote creates a temporary git repo with a remote URL configured
func CreateTempGitRepoWithRemote(t *testing.T, remoteURL string) string {
t.Helper()
dir := CreateTempGitRepo(t)
// Add remote
cmd := exec.Command("git", "remote", "add", "origin", remoteURL)
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Fatalf("failed to add git remote: %v", err)
}
return dir
}
// CreateTempGitRepoWithBranch creates a temporary git repo on a specific branch
func CreateTempGitRepoWithBranch(t *testing.T, branch string) string {
t.Helper()
dir := CreateTempGitRepo(t)
// Create and checkout branch
cmd := exec.Command("git", "checkout", "-b", branch)
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Fatalf("failed to create branch: %v", err)
}
return dir
}
// CreateNestedRepoStructure creates a directory structure with nested git repos
// Returns the root directory path
func CreateNestedRepoStructure(t *testing.T) (rootDir string, nestedDirs []string) {
t.Helper()
rootDir = CreateTempGitRepo(t)
// Create nested repos
nested1 := filepath.Join(rootDir, "project1")
if err := os.MkdirAll(nested1, 0755); err != nil {
t.Fatalf("failed to create nested dir: %v", err)
}
initGit(t, nested1)
nested2 := filepath.Join(rootDir, "subdir", "project2")
if err := os.MkdirAll(nested2, 0755); err != nil {
t.Fatalf("failed to create nested dir: %v", err)
}
initGit(t, nested2)
nested3 := filepath.Join(rootDir, "subdir", "deep", "project3")
if err := os.MkdirAll(nested3, 0755); err != nil {
t.Fatalf("failed to create nested dir: %v", err)
}
initGit(t, nested3)
return rootDir, []string{nested1, nested2, nested3}
}
// initGit initializes a git repo in the given directory
func initGit(t *testing.T, dir string) {
t.Helper()
cmd := exec.Command("git", "init")
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Fatalf("failed to init git repo: %v", err)
}
// Configure git user
configName := exec.Command("git", "config", "user.name", "Test User")
configName.Dir = dir
if err := configName.Run(); err != nil {
t.Fatalf("failed to configure git user.name: %v", err)
}
configEmail := exec.Command("git", "config", "user.email", "test@example.com")
configEmail.Dir = dir
if err := configEmail.Run(); err != nil {
t.Fatalf("failed to configure git user.email: %v", err)
}
// Create initial commit
readmePath := filepath.Join(dir, "README.md")
if err := os.WriteFile(readmePath, []byte("# Test\n"), 0644); err != nil {
t.Fatalf("failed to create README: %v", err)
}
add := exec.Command("git", "add", "README.md")
add.Dir = dir
if err := add.Run(); err != nil {
t.Fatalf("failed to git add: %v", err)
}
commit := exec.Command("git", "commit", "-m", "Initial commit")
commit.Dir = dir
if err := commit.Run(); err != nil {
t.Fatalf("failed to git commit: %v", err)
}
}