gitDirAtPath finds the .git directory corresponding to the given path, which may be the .git directory itself, the working tree, or the root of a bare repository. We filter out the GIT_DIR environment variable to ensure we get the expected result, and we change directories to ensure that we can mak
(path string)
| 98 | // filepath.Abs. Using --absolute-git-dir instead of --git-dir is not an option |
| 99 | // because we support Git versions that don't have --absolute-git-dir. |
| 100 | func gitDirAtPath(path string) (string, error) { |
| 101 | // Filter out all the GIT_* environment variables. |
| 102 | env := os.Environ() |
| 103 | n := 0 |
| 104 | for _, val := range env { |
| 105 | if !strings.HasPrefix(val, "GIT_") { |
| 106 | env[n] = val |
| 107 | n++ |
| 108 | } |
| 109 | } |
| 110 | env = env[:n] |
| 111 | |
| 112 | // Trim any trailing .git path segment. |
| 113 | if filepath.Base(path) == ".git" { |
| 114 | path = filepath.Dir(path) |
| 115 | } |
| 116 | |
| 117 | curdir, err := os.Getwd() |
| 118 | if err != nil { |
| 119 | return "", err |
| 120 | } |
| 121 | |
| 122 | err = os.Chdir(path) |
| 123 | if err != nil { |
| 124 | return "", err |
| 125 | } |
| 126 | |
| 127 | cmd, err := subprocess.ExecCommand("git", "rev-parse", "--git-dir") |
| 128 | if err != nil { |
| 129 | return "", errors.Wrap(err, tr.Tr.Get("failed to find `git rev-parse --git-dir`")) |
| 130 | } |
| 131 | cmd.Cmd.Env = env |
| 132 | out, err := cmd.Output() |
| 133 | if err != nil { |
| 134 | if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 { |
| 135 | return "", errors.New(tr.Tr.Get("failed to call `git rev-parse --git-dir`: %s", string(err.Stderr))) |
| 136 | } |
| 137 | return "", errors.Wrap(err, tr.Tr.Get("failed to call `git rev-parse --git-dir`")) |
| 138 | } |
| 139 | |
| 140 | gitdir, err := tools.TranslateCygwinPath(strings.TrimRight(string(out), "\n")) |
| 141 | if err != nil { |
| 142 | return "", errors.Wrap(err, tr.Tr.Get("unable to translate path")) |
| 143 | } |
| 144 | |
| 145 | gitdir, err = filepath.Abs(gitdir) |
| 146 | if err != nil { |
| 147 | return "", errors.Wrap(err, tr.Tr.Get("unable to canonicalize path")) |
| 148 | } |
| 149 | |
| 150 | err = os.Chdir(curdir) |
| 151 | if err != nil { |
| 152 | return "", err |
| 153 | } |
| 154 | return tools.CanonicalizeSystemPath(gitdir) |
| 155 | } |
| 156 | |
| 157 | func fixUrlPath(path string) string { |
no test coverage detected