Skip to content

Instantly share code, notes, and snippets.

@adamhjk
Created November 7, 2014 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamhjk/5a475b8dd45971a4e814 to your computer and use it in GitHub Desktop.
Save adamhjk/5a475b8dd45971a4e814 to your computer and use it in GitHub Desktop.
func dot_git_dir(dir string) string {
git_dir := path.Join(dir, ".git")
_, dir_error := os.Stat(git_dir)
if dir_error != nil {
if git_dir == "/.git" {
log.Fatal("Cannot find a .git directory")
}
return dot_git_dir(path.Dir(dir))
} else {
return git_dir
}
}
@dmitshur
Copy link

dmitshur commented Nov 8, 2014

It looks like you're dealing with file paths here, so the correct package to use would be path/filepath, not path. That way it's more readable and has a better chance of working on all 3 OSes.

Using path instead of path/filepath feels like seeing someone use text/template instead of html/template to generate HTML... It may work for simple cases, but it's better to use the correct package and be safe.

@anisse
Copy link

anisse commented Nov 8, 2014

Idiomatic golang uses the early return pattern, which means you can eliminate the else part of your code. See:
https://golang.org/doc/effective_go.html#if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment