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

Commit

Permalink
Merge pull request #38 from imikushin/fix-messing-up-project-dir
Browse files Browse the repository at this point in the history
Fix messing up project dir
  • Loading branch information
imikushin authored Jul 19, 2016
2 parents be4b321 + 40e4a6a commit e8ff70d
Show file tree
Hide file tree
Showing 74 changed files with 5,058 additions and 1,622 deletions.
8 changes: 4 additions & 4 deletions trash.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# package
github.com/rancher/trash

github.com/Sirupsen/logrus v0.9.0
github.com/codegangsta/cli v1.2.0
github.com/cloudfoundry-incubator/candiedyaml 4e924c7
github.com/Sirupsen/logrus v0.10.0
github.com/urfave/cli v1.18.0
github.com/cloudfoundry-incubator/candiedyaml 99c3df8
github.com/stretchr/testify v1.1.3
github.com/davecgh/go-spew 5215b55
github.com/pmezard/go-difflib 792786c
golang.org/x/sys 076b546
golang.org/x/sys a408501
36 changes: 26 additions & 10 deletions trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"strings"

"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/urfave/cli"

"github.com/rancher/trash/conf"
"github.com/rancher/trash/util"
)
Expand Down Expand Up @@ -61,11 +62,9 @@ func main() {
EnvVar: "TRASH_CACHE",
},
}
app.Action = func(c *cli.Context) {
exit(run(c))
}
app.Action = run

exit(app.Run(os.Args))
app.Run(os.Args)
}

func run(c *cli.Context) error {
Expand All @@ -79,10 +78,15 @@ func run(c *cli.Context) error {
update := c.Bool("update")
trashDir := c.String("cache")

trashDir, err := filepath.Abs(trashDir)
if err != nil {
return err
}

if err := os.Chdir(dir); err != nil {
return err
}
dir, err := os.Getwd()
dir, err = os.Getwd()
if err != nil {
return err
}
Expand Down Expand Up @@ -331,8 +335,7 @@ func checkGitRepo(trashDir, repoDir string, i conf.Import) error {
return err
}
}
if bytes, err := exec.Command("git", "status").CombinedOutput(); err != nil {
logrus.WithFields(logrus.Fields{"err": err}).Warnf("`git status` failed:\n%s", bytes)
if !isCurrentDirARepo(trashDir) {
os.Chdir(trashDir)
return cloneGitRepo(trashDir, repoDir, i)
}
Expand All @@ -344,6 +347,19 @@ func checkGitRepo(trashDir, repoDir string, i conf.Import) error {
return nil
}

func isCurrentDirARepo(trashDir string) bool {
d, err := os.Getwd()
if err != nil {
logrus.Fatalf("Error getting current dir: %s", err)
}
bytes, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
logrus.Debugf("Not in a git repo: `git rev-parse --show-toplevel` in dir %s failed: %s", d, err)
return false
}
return strings.HasPrefix(string(bytes), trashDir+"/src/")
}

func remoteExists(remoteName string) bool {
lines := util.CmdOutLines(exec.Command("git", "remote"))
for line := range lines {
Expand Down Expand Up @@ -389,8 +405,8 @@ func cloneGitRepo(trashDir, repoDir string, i conf.Import) error {
return err
}
os.Chdir(repoDir)
if err := exec.Command("git", "status").Run(); err != nil {
logrus.WithFields(logrus.Fields{"err": err, "repoDir": repoDir}).Debug("not a git repo, creating one")
if !isCurrentDirARepo(trashDir) {
logrus.WithFields(logrus.Fields{"repoDir": repoDir}).Debug("not a git repo, creating one")
exec.Command("git", "init", "-q").Run()
}
if i.Repo != "" {
Expand Down
7 changes: 7 additions & 0 deletions vendor/github.com/Sirupsen/logrus/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions vendor/github.com/Sirupsen/logrus/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/Sirupsen/logrus/entry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 46 additions & 1 deletion vendor/github.com/Sirupsen/logrus/logrus.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 0 additions & 27 deletions vendor/github.com/cloudfoundry-incubator/candiedyaml/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions vendor/github.com/codegangsta/cli/.travis.yml

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/codegangsta/cli/LICENSE

This file was deleted.

Loading

0 comments on commit e8ff70d

Please sign in to comment.