Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit 40e4a6a

Browse files
committed
Fix messing up project dir if .trash-cache is placed inside a git repo
e.g. when .trash-cache is inside the project dir
1 parent 3d2acb9 commit 40e4a6a

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

trash.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ func run(c *cli.Context) error {
7878
update := c.Bool("update")
7979
trashDir := c.String("cache")
8080

81+
trashDir, err := filepath.Abs(trashDir)
82+
if err != nil {
83+
return err
84+
}
85+
8186
if err := os.Chdir(dir); err != nil {
8287
return err
8388
}
84-
dir, err := os.Getwd()
89+
dir, err = os.Getwd()
8590
if err != nil {
8691
return err
8792
}
@@ -330,8 +335,7 @@ func checkGitRepo(trashDir, repoDir string, i conf.Import) error {
330335
return err
331336
}
332337
}
333-
if bytes, err := exec.Command("git", "status").CombinedOutput(); err != nil {
334-
logrus.WithFields(logrus.Fields{"err": err}).Warnf("`git status` failed:\n%s", bytes)
338+
if !isCurrentDirARepo(trashDir) {
335339
os.Chdir(trashDir)
336340
return cloneGitRepo(trashDir, repoDir, i)
337341
}
@@ -343,6 +347,19 @@ func checkGitRepo(trashDir, repoDir string, i conf.Import) error {
343347
return nil
344348
}
345349

350+
func isCurrentDirARepo(trashDir string) bool {
351+
d, err := os.Getwd()
352+
if err != nil {
353+
logrus.Fatalf("Error getting current dir: %s", err)
354+
}
355+
bytes, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
356+
if err != nil {
357+
logrus.Debugf("Not in a git repo: `git rev-parse --show-toplevel` in dir %s failed: %s", d, err)
358+
return false
359+
}
360+
return strings.HasPrefix(string(bytes), trashDir+"/src/")
361+
}
362+
346363
func remoteExists(remoteName string) bool {
347364
lines := util.CmdOutLines(exec.Command("git", "remote"))
348365
for line := range lines {
@@ -388,8 +405,8 @@ func cloneGitRepo(trashDir, repoDir string, i conf.Import) error {
388405
return err
389406
}
390407
os.Chdir(repoDir)
391-
if err := exec.Command("git", "status").Run(); err != nil {
392-
logrus.WithFields(logrus.Fields{"err": err, "repoDir": repoDir}).Debug("not a git repo, creating one")
408+
if !isCurrentDirARepo(trashDir) {
409+
logrus.WithFields(logrus.Fields{"repoDir": repoDir}).Debug("not a git repo, creating one")
393410
exec.Command("git", "init", "-q").Run()
394411
}
395412
if i.Repo != "" {

0 commit comments

Comments
 (0)