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`.
85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.membo.co.uk/dtomlinson/gitlocal/internal/config"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var statusCmd = &cobra.Command{
|
|
Use: "status",
|
|
Short: "Show all converted repositories",
|
|
Long: `Display a list of all repositories that have been converted from .git to .gitlocal.`,
|
|
RunE: runStatus,
|
|
}
|
|
|
|
func runStatus(cmd *cobra.Command, args []string) error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
|
|
if len(cfg.Repos) == 0 {
|
|
fmt.Println("No converted repositories")
|
|
return nil
|
|
}
|
|
|
|
fmt.Printf("Converted Repositories (%d):\n\n", len(cfg.Repos))
|
|
|
|
for _, repo := range cfg.Repos {
|
|
fmt.Printf(" %s\n", repo.Path)
|
|
fmt.Printf(" Converted: %s\n", formatTime(repo.ConvertedAt))
|
|
if repo.OriginalRemote != "" {
|
|
fmt.Printf(" Remote: %s\n", repo.OriginalRemote)
|
|
}
|
|
if repo.OriginalBranch != "" {
|
|
fmt.Printf(" Branch: %s\n", repo.OriginalBranch)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func formatTime(t time.Time) string {
|
|
now := time.Now()
|
|
diff := now.Sub(t)
|
|
|
|
switch {
|
|
case diff < time.Minute:
|
|
return "just now"
|
|
case diff < time.Hour:
|
|
mins := int(diff.Minutes())
|
|
if mins == 1 {
|
|
return "1 minute ago"
|
|
}
|
|
return fmt.Sprintf("%d minutes ago", mins)
|
|
case diff < 24*time.Hour:
|
|
hours := int(diff.Hours())
|
|
if hours == 1 {
|
|
return "1 hour ago"
|
|
}
|
|
return fmt.Sprintf("%d hours ago", hours)
|
|
case diff < 7*24*time.Hour:
|
|
days := int(diff.Hours() / 24)
|
|
if days == 1 {
|
|
return "1 day ago"
|
|
}
|
|
return fmt.Sprintf("%d days ago", days)
|
|
case diff < 30*24*time.Hour:
|
|
weeks := int(diff.Hours() / 24 / 7)
|
|
if weeks == 1 {
|
|
return "1 week ago"
|
|
}
|
|
return fmt.Sprintf("%d weeks ago", weeks)
|
|
default:
|
|
months := int(diff.Hours() / 24 / 30)
|
|
if months == 1 {
|
|
return "1 month ago"
|
|
}
|
|
return fmt.Sprintf("%d months ago", months)
|
|
}
|
|
}
|