Skip to content

Commit f5d36b3

Browse files
iton0iton0
iton0
authored andcommitted
fix(internal/root): make Root function more robust
- updated the logic for checking if cloned git repository is bare - updated logic to account for custom git aliases allowing for broader use of the 'hkup --' functionality
1 parent 03df20a commit f5d36b3

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

internal/logic/root.go

+39-29
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,18 @@ import (
1313
//
1414
// Returns error if issue with cloning repository or initializing HkUp.
1515
func Root(cmd *cobra.Command, args []string) error {
16-
root := args[0]
17-
1816
// Tries to run git command in the terminal
19-
err := util.RunCommandInTerminal(root, args[1:]...)
17+
err := util.RunCommandInTerminal(args[0], args[1:]...)
2018
if err != nil {
2119
return err
2220
}
2321

24-
secondLast := args[len(args)-2]
25-
dir := args[len(args)-1]
26-
var isBare bool
27-
28-
// Checks if the cloned repository is bare
29-
for _, v := range args[1:] {
30-
if v == "--bare" {
31-
isBare = true
32-
}
33-
}
22+
secondLast := args[len(args)-2] // Second last arg that user provided
23+
dir := args[len(args)-1] // Last arg; assume it is the directory
24+
var isBare bool // Boolean whether directory is bare
3425

35-
if err = cdLogic(root, secondLast, dir, isBare); err != nil {
26+
// Tries to cd into the created directory and updates the isBare variable
27+
if isBare, err = cdLogic(secondLast, dir); err != nil {
3628
return err
3729
}
3830

@@ -66,28 +58,46 @@ func Root(cmd *cobra.Command, args []string) error {
6658
}
6759

6860
// cdLogic implements the HkUp wrapper logic around git-related clone command.
69-
// Returns error if issue with changing directory.
70-
func cdLogic(root, secondLast, dir string, isBare bool) error {
61+
// Returns:
62+
// - boolean of if directory is bare
63+
// - error if issue with changing directory
64+
func cdLogic(secondLast, dir string) (bool, error) {
7165
// Gets the remote repository name if no custom clone name is used
7266
// ex). Not Custom: git clone <url>
7367
// Custom: git clone <url> foo
74-
switch root {
75-
case "git", "/usr/bin/git":
76-
if isBare && strings.HasSuffix(dir, ".git") {
77-
start := strings.LastIndex(dir, "/") + 1
78-
dir = dir[start:]
79-
} else if strings.HasSuffix(dir, ".git") {
80-
start := strings.LastIndex(dir, "/") + 1
68+
69+
// Holds the boolean whether directory is bare or not
70+
var isBare bool
71+
72+
// Using the regular 'git' command
73+
if strings.HasSuffix(dir, ".git") { // bare
74+
start := strings.LastIndex(dir, "/") + 1
75+
dir = dir[start:]
76+
77+
// If the repo is not bare then updates the directory name ie regular clone
78+
if isBare = isBareRepo(dir); !isBare {
8179
end := strings.LastIndex(dir, ".git")
8280
dir = dir[start:end]
8381
}
84-
case "gh", "usr/bin/gh":
85-
if strings.Count(secondLast, "/") != 1 {
86-
start := strings.LastIndex(dir, "/") + 1
87-
dir = dir[start:]
88-
}
82+
} else if strings.Count(secondLast, "/") != 1 { // When using Github CLI
83+
start := strings.LastIndex(dir, "/") + 1
84+
dir = dir[start:]
8985
}
9086

9187
// Either successful or returns error if issue with changing directory
92-
return os.Chdir(dir)
88+
return isBare, os.Chdir(dir)
89+
}
90+
91+
// isBareRepo reports if given directory (dir) is a bare git repository.
92+
func isBareRepo(dir string) bool {
93+
// Checks if current working directory is git bare repo
94+
out, err := exec.Command("git", "-C", dir, "rev-parse", "--is-bare-repository").Output()
95+
if err != nil {
96+
return false
97+
}
98+
99+
// Returns error if current working directory is not a worktree
100+
result := strings.TrimSpace(string(out))
101+
102+
return result == "true"
93103
}

0 commit comments

Comments
 (0)