Skip to content

Commit

Permalink
fix(internal/logic/root): resolve gh command issue
Browse files Browse the repository at this point in the history
- updated cdLogic function to properly handle use of gh command
- updated main Root function to check specifically for flags when gh
  command is used

This commit should make the 'hkup --' functionality more robust to user
args for both git and gh commands. For example this should work when
running 'gh repo clone <url> [<customdir>] -- --bare'.
  • Loading branch information
iton0 committed Dec 21, 2024
1 parent df43c3e commit bb210d1
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions internal/logic/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,66 @@ import (
// Returns error if issue with cloning repository or initializing HkUp.
func Root(cmd *cobra.Command, args []string) error {
// Tries to run git command in the terminal
err := util.RunCommandInTerminal(args[0], args[1:]...)
if err != nil {
if err := util.RunCommandInTerminal(args[0], args[1:]...); err != nil {
return err
}

possibleRepoUrl := args[len(args)-2]
possibleCustomDir := args[len(args)-1]

// Loop through the args to see if "--" is used.
// Gets the two previous args and sets them to the repoUrl and
// possibleCustomDir variables, respectively.
for i, v := range args {
if v == "--" {
possibleRepoUrl = args[i-2]
possibleCustomDir = args[i-1]
break
}
}

// Tries to cd into the created directory
if err = cdLogic(args[len(args)-2], args[len(args)-1]); err != nil {
if err := cdLogic(possibleRepoUrl, possibleCustomDir); err != nil {
return err
}

// Tries to initialize HkUp
return Init(cmd, nil)
}

// cdLogic implements the HkUp wrapper logic around git-related clone command.
// cdLogic implements the HkUp wrapper logic around cloning for both 'git' and
// 'gh' command.
// Returns error if issue with changing directory
func cdLogic(secondLast, dir string) error {
// Using the regular 'git' command
if strings.HasSuffix(dir, ".git") { // bare
start := strings.LastIndex(dir, "/") + 1
dir = dir[start:]
func cdLogic(possibleRepoUrl, possibleCustomDir string) error {
// No custom directory name provided when using git command
usedDefaultGit := strings.HasSuffix(possibleCustomDir, ".git")

// No custom directory name provided when using gh command
usedDefaultGh := strings.Count(possibleRepoUrl, "/") != 1 && !usedDefaultGit

// If the repo is not bare then updates the directory name ie regular clone
if isBare, err := isBareRepo(dir); err != nil || !isBare {
end := strings.LastIndex(dir, ".git")
dir = dir[start:end]
// Starting index of the remote repo name
start := strings.LastIndex(possibleCustomDir, "/") + 1

// Checks if user did not provide a custom directory name
if usedDefaultGit { // git command
// If the repo is bare then just take the remote repo name
if isBare, _ := isBareRepo(possibleCustomDir[start:]); isBare {
possibleCustomDir = possibleCustomDir[start:]
} else { // Repo is not bare ie regular clone
end := strings.LastIndex(possibleCustomDir, ".git")
possibleCustomDir = possibleCustomDir[start:end]
}
} else if usedDefaultGh { // gh command
// If the repo is bare then just take the remote repo name
if isBare, _ := isBareRepo(possibleCustomDir[start:] + ".git"); isBare {
possibleCustomDir = possibleCustomDir[start:] + ".git"
} else { // Repo is not bare ie regular clone
possibleCustomDir = possibleCustomDir[start:]
}
} else if strings.Count(secondLast, "/") != 1 { // When using Github CLI
start := strings.LastIndex(dir, "/") + 1
dir = dir[start:]
}

// Either successful or returns error if issue with changing directory
return os.Chdir(dir)
return os.Chdir(possibleCustomDir)
}

// isBareRepo reports if given directory (dir) is a bare git repository.
Expand Down

0 comments on commit bb210d1

Please sign in to comment.