6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-15 12:50:06 +00:00

Fix issues found by wsl linter

This commit is contained in:
Grzegorz Dlugoszewski
2025-08-24 14:54:02 +02:00
parent 59aaaffe35
commit 0bf2765349
16 changed files with 59 additions and 15 deletions

View File

@@ -15,6 +15,7 @@ linters:
disable: disable:
- depguard # We don't have any packages we need to block - depguard # We don't have any packages we need to block
- paralleltest # Tests are fast already and paralellizing them adds complexity - paralleltest # Tests are fast already and paralellizing them adds complexity
- wsl # We use wsl_v5 instead
exclusions: exclusions:
rules: rules:

View File

@@ -62,6 +62,7 @@ func runGetCommand(cmd *cobra.Command, args []string) error {
Root: viper.GetString(cfg.KeyReposRoot), Root: viper.GetString(cfg.KeyReposRoot),
URL: url, URL: url,
} }
return pkg.Get(config) return pkg.Get(config)
} }

View File

@@ -33,6 +33,7 @@ func handleGitGetInvocation() (string, []string) {
if len(os.Args) > 1 && (os.Args[1] == "get" || os.Args[1] == "list") { if len(os.Args) > 1 && (os.Args[1] == "get" || os.Args[1] == "list") {
return os.Args[1], os.Args[2:] return os.Args[1], os.Args[2:]
} }
return "get", os.Args[1:] return "get", os.Args[1:]
} }
@@ -44,6 +45,7 @@ func handleDefaultInvocation() (string, []string) {
if len(os.Args) > 1 { if len(os.Args) > 1 {
return os.Args[1], os.Args[2:] return os.Args[1], os.Args[2:]
} }
return "get", []string{} return "get", []string{}
} }

View File

@@ -83,6 +83,7 @@ func TestDetermineCommand(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Save original os.Args // Save original os.Args
oldArgs := os.Args oldArgs := os.Args
defer func() { os.Args = oldArgs }() defer func() { os.Args = oldArgs }()
// Set test args // Set test args
@@ -144,6 +145,7 @@ func TestHandleGitGetInvocation(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Save original os.Args // Save original os.Args
oldArgs := os.Args oldArgs := os.Args
defer func() { os.Args = oldArgs }() defer func() { os.Args = oldArgs }()
// Set test args // Set test args
@@ -187,6 +189,7 @@ func TestHandleGitListInvocation(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Save original os.Args // Save original os.Args
oldArgs := os.Args oldArgs := os.Args
defer func() { os.Args = oldArgs }() defer func() { os.Args = oldArgs }()
// Set test args // Set test args
@@ -236,6 +239,7 @@ func TestHandleDefaultInvocation(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Save original os.Args // Save original os.Args
oldArgs := os.Args oldArgs := os.Args
defer func() { os.Args = oldArgs }() defer func() { os.Args = oldArgs }()
// Set test args // Set test args

View File

@@ -96,7 +96,6 @@ func testConfigOnlyInGitconfig(t *testing.T) {
func testConfigOnlyInEnvVar(t *testing.T) { func testConfigOnlyInEnvVar(t *testing.T) {
Init(&gitconfigEmpty{}) Init(&gitconfigEmpty{})
os.Setenv(envVarName, fromEnv) os.Setenv(envVarName, fromEnv)
} }
func testConfigInGitconfigAndEnvVar(t *testing.T) { func testConfigInGitconfigAndEnvVar(t *testing.T) {

View File

@@ -28,10 +28,14 @@ func parseDumpFile(path string) ([]parsedLine, error) {
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
var parsedLines []parsedLine var (
var line int parsedLines []parsedLine
line int
)
for scanner.Scan() { for scanner.Scan() {
line++ line++
parsed, err := parseLine(scanner.Text()) parsed, err := parseLine(scanner.Text())
if err != nil && !errors.Is(errEmptyLine, err) { if err != nil && !errors.Is(errEmptyLine, err) {
return nil, fmt.Errorf("failed parsing dump file line %d: %w", line, err) return nil, fmt.Errorf("failed parsing dump file line %d: %w", line, err)

View File

@@ -30,6 +30,7 @@ func Get(c *GetCfg) error {
if c.Dump != "" { if c.Dump != "" {
return cloneDumpFile(c) return cloneDumpFile(c)
} }
return nil return nil
} }
@@ -74,10 +75,12 @@ func cloneDumpFile(c *GetCfg) error {
} }
fmt.Printf("Cloning %s...\n", opts.URL.String()) fmt.Printf("Cloning %s...\n", opts.URL.String())
_, err = git.Clone(opts) _, err = git.Clone(opts)
if err != nil { if err != nil {
return err return err
} }
} }
return nil return nil
} }

View File

@@ -18,7 +18,6 @@ var errDirNotExist = fmt.Errorf("directory doesn't exist")
// Exists returns true if a directory exists. If it doesn't or the directory can't be accessed it returns an error. // Exists returns true if a directory exists. If it doesn't or the directory can't be accessed it returns an error.
func Exists(path string) (bool, error) { func Exists(path string) (bool, error) {
_, err := os.Stat(path) _, err := os.Stat(path)
if err == nil { if err == nil {
return true, nil return true, nil
} }
@@ -61,6 +60,7 @@ func (f *RepoFinder) Find() error {
if os.IsPermission(err) { if os.IsPermission(err) {
return nil // Skip this path but continue return nil // Skip this path but continue
} }
return fmt.Errorf("failed to walk %s: %w", path, err) return fmt.Errorf("failed to walk %s: %w", path, err)
} }
@@ -73,6 +73,7 @@ func (f *RepoFinder) Find() error {
if d.Name() == dotgit { if d.Name() == dotgit {
parentPath := filepath.Dir(path) parentPath := filepath.Dir(path)
f.addIfOk(parentPath) f.addIfOk(parentPath)
return fs.SkipDir // Skip the .git directory contents return fs.SkipDir // Skip the .git directory contents
} }
@@ -85,7 +86,6 @@ func (f *RepoFinder) Find() error {
return nil // Continue walking return nil // Continue walking
}) })
if err != nil { if err != nil {
return fmt.Errorf("failed to walk directory tree: %w", err) return fmt.Errorf("failed to walk directory tree: %w", err)
} }

View File

@@ -61,6 +61,7 @@ func Clone(opts *CloneOpts) (*Repo, error) {
} }
Repo, err := Open(opts.Path) Repo, err := Open(opts.Path)
return Repo, err return Repo, err
} }
@@ -79,6 +80,7 @@ func (r *Repo) Uncommitted() (int, error) {
} }
count := 0 count := 0
for _, line := range out { for _, line := range out {
// Don't count lines with untracked files and empty lines. // Don't count lines with untracked files and empty lines.
if !strings.HasPrefix(line, untracked) && strings.TrimSpace(line) != "" { if !strings.HasPrefix(line, untracked) && strings.TrimSpace(line) != "" {
@@ -97,6 +99,7 @@ func (r *Repo) Untracked() (int, error) {
} }
count := 0 count := 0
for _, line := range out { for _, line := range out {
if strings.HasPrefix(line, untracked) { if strings.HasPrefix(line, untracked) {
count++ count++
@@ -122,6 +125,7 @@ func (r *Repo) CurrentBranch() (string, error) {
// Fall back to "main" as the modern default // Fall back to "main" as the modern default
return "main", nil return "main", nil
} }
return "", err return "", err
} }
@@ -190,6 +194,7 @@ func (r *Repo) Remote() (string, error) {
if strings.Contains(err.Error(), "No remote configured to list refs from") { if strings.Contains(err.Error(), "No remote configured to list refs from") {
return "", nil // Return empty string instead of error for missing remote return "", nil // Return empty string instead of error for missing remote
} }
return "", err return "", err
} }

View File

@@ -47,8 +47,8 @@ func TestUncommitted(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
r, _ := Open(test.repoMaker(t).Path()) r, _ := Open(test.repoMaker(t).Path())
got, err := r.Uncommitted()
got, err := r.Uncommitted()
if err != nil { if err != nil {
t.Errorf("got error %q", err) t.Errorf("got error %q", err)
} }
@@ -95,8 +95,8 @@ func TestUntracked(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
r, _ := Open(test.repoMaker(t).Path()) r, _ := Open(test.repoMaker(t).Path())
got, err := r.Uncommitted()
got, err := r.Uncommitted()
if err != nil { if err != nil {
t.Errorf("got error %q", err) t.Errorf("got error %q", err)
} }
@@ -139,8 +139,8 @@ func TestCurrentBranch(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
r, _ := Open(test.repoMaker(t).Path()) r, _ := Open(test.repoMaker(t).Path())
got, err := r.CurrentBranch()
got, err := r.CurrentBranch()
if err != nil { if err != nil {
t.Errorf("got error %q", err) t.Errorf("got error %q", err)
} }
@@ -182,8 +182,8 @@ func TestBranches(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
r, _ := Open(test.repoMaker(t).Path()) r, _ := Open(test.repoMaker(t).Path())
got, err := r.Branches()
got, err := r.Branches()
if err != nil { if err != nil {
t.Errorf("got error %q", err) t.Errorf("got error %q", err)
} }
@@ -287,6 +287,7 @@ func TestAheadBehind(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
r, _ := Open(test.repoMaker(t).Path()) r, _ := Open(test.repoMaker(t).Path())
upstream, err := r.Upstream(test.branch) upstream, err := r.Upstream(test.branch)
if err != nil { if err != nil {
t.Errorf("got error %q", err) t.Errorf("got error %q", err)
@@ -313,7 +314,6 @@ func TestCleanupFailedClone(t *testing.T) {
// └── x/ // └── x/
// └── y/ // └── y/
// └── file.txt // └── file.txt
tests := []struct { tests := []struct {
path string // path to cleanup path string // path to cleanup
wantGone string // this path should be deleted, if empty - nothing should be deleted wantGone string // this path should be deleted, if empty - nothing should be deleted
@@ -393,6 +393,7 @@ func TestRemote(t *testing.T) {
if test.wantErr && err == nil { if test.wantErr && err == nil {
t.Errorf("expected error but got none") t.Errorf("expected error but got none")
} }
if !test.wantErr && err != nil { if !test.wantErr && err != nil {
t.Errorf("unexpected error: %q", err) t.Errorf("unexpected error: %q", err)
} }
@@ -413,8 +414,8 @@ func createTestDirTree(t *testing.T) string {
root := test.TempDir(t, "") root := test.TempDir(t, "")
err := os.MkdirAll(filepath.Join(root, "a", "b", "c"), os.ModePerm) err := os.MkdirAll(filepath.Join(root, "a", "b", "c"), os.ModePerm)
err = os.MkdirAll(filepath.Join(root, "a", "x", "y"), os.ModePerm) err = os.MkdirAll(filepath.Join(root, "a", "x", "y"), os.ModePerm)
_, err = os.Create(filepath.Join(root, "a", "x", "y", "file.txt"))
_, err = os.Create(filepath.Join(root, "a", "x", "y", "file.txt"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -32,12 +32,14 @@ func (r *Repo) LoadStatus(fetch bool) *Status {
} }
var err error var err error
status.current, err = r.CurrentBranch() status.current, err = r.CurrentBranch()
if err != nil { if err != nil {
status.errors = append(status.errors, err.Error()) status.errors = append(status.errors, err.Error())
} }
var errs []error var errs []error
status.branches, errs = r.loadBranches() status.branches, errs = r.loadBranches()
for _, err := range errs { for _, err := range errs {
status.errors = append(status.errors, err.Error()) status.errors = append(status.errors, err.Error())
@@ -69,6 +71,7 @@ func (r *Repo) loadBranches() (map[string]string, []error) {
for _, branch := range branches { for _, branch := range branches {
status, err := r.loadBranchStatus(branch) status, err := r.loadBranchStatus(branch)
statuses[branch] = status statuses[branch] = status
if err != nil { if err != nil {
errors = append(errors, err) errors = append(errors, err)
} }
@@ -100,6 +103,7 @@ func (r *Repo) loadBranchStatus(branch string) (string, error) {
if ahead != 0 { if ahead != 0 {
res = append(res, fmt.Sprintf("%d ahead", ahead)) res = append(res, fmt.Sprintf("%d ahead", ahead))
} }
if behind != 0 { if behind != 0 {
res = append(res, fmt.Sprintf("%d behind", behind)) res = append(res, fmt.Sprintf("%d behind", behind))
} }
@@ -126,6 +130,7 @@ func (r *Repo) loadWorkTree() (string, error) {
if uncommitted != 0 { if uncommitted != 0 {
res = append(res, fmt.Sprintf("%d uncommitted", uncommitted)) res = append(res, fmt.Sprintf("%d uncommitted", uncommitted))
} }
if untracked != 0 { if untracked != 0 {
res = append(res, fmt.Sprintf("%d untracked", untracked)) res = append(res, fmt.Sprintf("%d untracked", untracked))
} }
@@ -151,6 +156,7 @@ func (s *Status) Branches() []string {
branches = append(branches, b) branches = append(branches, b)
} }
} }
return branches return branches
} }

View File

@@ -30,6 +30,7 @@ func RepoEmptyInDir(t *testing.T, parent string) *Repo {
} }
r.init() r.init()
return r return r
} }
@@ -97,6 +98,7 @@ func RepoWithBranchWithUpstream(t *testing.T) *Repo {
r := origin.clone() r := origin.clone()
r.checkout("feature/branch") r.checkout("feature/branch")
return r return r
} }
@@ -107,6 +109,7 @@ func RepoWithBranchWithoutUpstream(t *testing.T) *Repo {
r := origin.clone() r := origin.clone()
r.branch("feature/branch") r.branch("feature/branch")
r.checkout("feature/branch") r.checkout("feature/branch")
return r return r
} }

View File

@@ -23,6 +23,7 @@ func List(c *ListCfg) error {
} }
statuses := finder.LoadAll(c.Fetch) statuses := finder.LoadAll(c.Fetch)
printables := make([]print.Printable, len(statuses)) printables := make([]print.Printable, len(statuses))
for i := range statuses { for i := range statuses {
printables[i] = statuses[i] printables[i] = statuses[i]

View File

@@ -46,6 +46,7 @@ func Root(val string) *Node {
root := &Node{ root := &Node{
val: val, val: val,
} }
return root return root
} }
@@ -61,6 +62,7 @@ func (n *Node) Add(val string) *Node {
depth: n.depth + 1, depth: n.depth + 1,
} }
n.children = append(n.children, child) n.children = append(n.children, child)
return child return child
} }
@@ -108,9 +110,11 @@ func buildTree(root string, repos []Printable) *Node {
continue continue
} }
node = child node = child
} }
} }
return tree return tree
} }
@@ -180,8 +184,11 @@ func indentation(node *Node) string {
var indent strings.Builder var indent strings.Builder
const space = " " const (
const link = " " space = " "
link = "│ "
)
for _, y := range levels { for _, y := range levels {
if y { if y {
indent.WriteString(space) indent.WriteString(space)
@@ -203,12 +210,15 @@ func (n *Node) isYoungest() bool {
} }
sisters := n.parent.children sisters := n.parent.children
var myIndex int var myIndex int
for i, sis := range sisters { for i, sis := range sisters {
if sis.val == n.val { if sis.val == n.val {
myIndex = i myIndex = i
break break
} }
} }
return myIndex == len(sisters)-1 return myIndex == len(sisters)-1
} }

View File

@@ -78,6 +78,7 @@ func (c *Cmd) AndCaptureLine() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return lines[0], nil return lines[0], nil
} }
@@ -90,6 +91,7 @@ func (c *Cmd) AndShow() error {
if err != nil { if err != nil {
return &GitError{&bytes.Buffer{}, c.args, c.path, err} return &GitError{&bytes.Buffer{}, c.args, c.path, err}
} }
return nil return nil
} }
@@ -104,6 +106,7 @@ func (c *Cmd) AndShutUp() error {
if err != nil { if err != nil {
return &GitError{errStream, c.args, c.path, err} return &GitError{errStream, c.args, c.path, err}
} }
return nil return nil
} }
@@ -123,7 +126,6 @@ func (e GitError) Error() string {
} }
return fmt.Sprintf("git %s failed on %s: %s", e.Args, e.Path, msg) return fmt.Sprintf("git %s failed on %s: %s", e.Args, e.Path, msg)
} }
func lines(output []byte) []string { func lines(output []byte) []string {

View File

@@ -29,6 +29,7 @@ func ParseURL(rawURL string, defaultHost string, defaultScheme string) (url *url
} }
normalizeURL(url, defaultHost, defaultScheme) normalizeURL(url, defaultHost, defaultScheme)
return url, nil return url, nil
} }
@@ -49,6 +50,7 @@ func parseRawURL(rawURL string) (*urlpkg.URL, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("failed parsing URL %s: %w", rawURL, err) return nil, fmt.Errorf("failed parsing URL %s: %w", rawURL, err)
} }
return url, nil return url, nil
} }