@@ -13,26 +13,18 @@ import (
13
13
//
14
14
// Returns error if issue with cloning repository or initializing HkUp.
15
15
func Root (cmd * cobra.Command , args []string ) error {
16
- root := args [0 ]
17
-
18
16
// Tries to run git command in the terminal
19
- err := util .RunCommandInTerminal (root , args [1 :]... )
17
+ err := util .RunCommandInTerminal (args [ 0 ] , args [1 :]... )
20
18
if err != nil {
21
19
return err
22
20
}
23
21
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
34
25
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 {
36
28
return err
37
29
}
38
30
@@ -66,28 +58,46 @@ func Root(cmd *cobra.Command, args []string) error {
66
58
}
67
59
68
60
// 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 ) {
71
65
// Gets the remote repository name if no custom clone name is used
72
66
// ex). Not Custom: git clone <url>
73
67
// 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 {
81
79
end := strings .LastIndex (dir , ".git" )
82
80
dir = dir [start :end ]
83
81
}
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 :]
89
85
}
90
86
91
87
// 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"
93
103
}
0 commit comments