|
1 | 1 | package dir |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
| 5 | + |
4 | 6 | "github.com/joshmedeski/sesh/v2/git" |
5 | 7 | "github.com/joshmedeski/sesh/v2/oswrap" |
6 | 8 | "github.com/joshmedeski/sesh/v2/pathwrap" |
@@ -39,12 +41,38 @@ func (d *RealDir) Dir(path string) (isDir bool, absPath string) { |
39 | 41 | } |
40 | 42 |
|
41 | 43 | func (d *RealDir) RootDir(path string) (hasRootDir bool, absPath string) { |
42 | | - isGit, absPath, err := d.git.GitRoot(path) |
43 | | - if err != nil { |
44 | | - return false, "" |
| 44 | + isGitBare, absPath := gitBareRootDir(d, path) |
| 45 | + if isGitBare { |
| 46 | + return true, absPath |
45 | 47 | } |
| 48 | + isGit, absPath := gitRootDir(d, path) |
46 | 49 | if isGit { |
47 | 50 | return true, absPath |
48 | 51 | } |
49 | 52 | return false, "" |
50 | 53 | } |
| 54 | + |
| 55 | +func gitBareRootDir(d *RealDir, path string) (hasRootDir bool, absPath string) { |
| 56 | + isGitBare, commonDir, _ := d.git.GitCommonDir(path) |
| 57 | + if isGitBare && strings.HasSuffix(commonDir, "/.bare") { |
| 58 | + topLevelDir := strings.TrimSuffix(commonDir, "/.bare") |
| 59 | + relativePath := strings.TrimPrefix(path, topLevelDir) |
| 60 | + firstDir := strings.Split(relativePath, string("/"))[1] |
| 61 | + name, err := d.path.Abs(topLevelDir + "/" + firstDir) |
| 62 | + if err != nil { |
| 63 | + return false, "" |
| 64 | + } |
| 65 | + return true, name |
| 66 | + } else { |
| 67 | + return false, "" |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func gitRootDir(d *RealDir, path string) (hasDir bool, absPath string) { |
| 72 | + isGit, topLevelDir, _ := d.git.ShowTopLevel(path) |
| 73 | + if isGit && topLevelDir != "" { |
| 74 | + return true, topLevelDir |
| 75 | + } else { |
| 76 | + return false, "" |
| 77 | + } |
| 78 | +} |
0 commit comments