From 9efb7073d32cbb34a87d8d740894e399ebe63b98 Mon Sep 17 00:00:00 2001 From: kalium Date: Thu, 31 Dec 2020 20:14:52 +0100 Subject: [PATCH] feat: cd without arg sets cwd to repository root --- cmd/tiger/main.go | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/cmd/tiger/main.go b/cmd/tiger/main.go index 32386f0..cfa8d81 100644 --- a/cmd/tiger/main.go +++ b/cmd/tiger/main.go @@ -470,29 +470,38 @@ everywhere: // reinventing coreutils poorly case "cd": + cd := "" if len(args) > 1 { - cd := strings.Join(args[1:], " ") + cd = strings.Join(args[1:], " ") if cd == "-" { cd = lastCwd } - lastCwd, err = os.Getwd() - checkErr(err) - err := os.Chdir(cd) + } else { + currentGwd, err := gitDir() if err != nil { - fmt.Println(Red, err, Reset) + println("", "", fmt.Errorf("cd without argument only works in a git repo!!")) + break } else { - difflast = "" - stdout, _, err := git("diff", "--numstat").Output() + cd = currentGwd + } + } + lastCwd, err = os.Getwd() + checkErr(err) + err := os.Chdir(cd) + if err != nil { + fmt.Println(Red, err, Reset) + } else { + difflast = "" + stdout, _, err := git("diff", "--numstat").Output() + if err == nil { + difflast = strings.TrimSpace(stdout) + } + currentGwd, err := gitDir() + if currentGwd != gwd { + watch.RemoveAll() if err == nil { - difflast = strings.TrimSpace(stdout) - } - currentGwd, err := gitDir() - if currentGwd != gwd { - watch.RemoveAll() - if err == nil { - gwd = currentGwd - go watch.AddWithSubdirs(gwd) - } + gwd = currentGwd + go watch.AddWithSubdirs(gwd) } } }