Skip to content

Commit

Permalink
feat: add root git-wrapper functionality
Browse files Browse the repository at this point in the history
- added logic for running git-related (git & gh) cloning commands with
  HkUp.
  • Loading branch information
iton0 committed Dec 5, 2024
1 parent 677e32c commit 8313203
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/iton0/hkup-cli/cmd/config"
"github.com/iton0/hkup-cli/cmd/template"
"github.com/iton0/hkup-cli/internal/logic"
"github.com/spf13/cobra"
)

Expand All @@ -15,10 +16,12 @@ var (
version = "dev"

rootCmd = &cobra.Command{
Use: "hkup",
Use: "hkup [-- <git/gh clone command>]",
Short: "hkup CLI",
Long: `hkup is a management tool for git hooks`,
Args: cobra.MinimumNArgs(1),
Version: version,
RunE: logic.Root,
}
)

Expand Down
93 changes: 93 additions & 0 deletions internal/logic/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package logic

import (
"os"
"os/exec"
"strings"

"github.com/iton0/hkup-cli/internal/util"
"github.com/spf13/cobra"
)

// Root wraps git-related clone commands for easier initialization of HkUp.
//
// Returns error if issue with cloning repository or initializing HkUp.
func Root(cmd *cobra.Command, args []string) error {
root := args[0]

// Tries to run git command in the terminal
err := util.RunCommandInTerminal(root, args[1:]...)
if err != nil {
return err
}

secondLast := args[len(args)-2]
dir := args[len(args)-1]
var isBare bool

// Checks if the cloned repository is bare
for _, v := range args[1:] {
if v == "--bare" {
isBare = true
}
}

if err = cdLogic(root, secondLast, dir, isBare); err != nil {
return err
}

// Current working directory is a bare repository and
// prompts the user to create a worktree & cd into the worktree
if isBare {
// Asks for the worktree path
worktree, err := util.UserInputPrompt("Create Worktree Path:")
if err != nil {
return err
}

// Asks for the branch to checkout worktree into
branch, err := util.UserInputPrompt("Branch:")
if err != nil {
return err
}

// Tries to create worktree and cd into it
err = exec.Command("git", "worktree", "add", worktree, branch).Run()
if err != nil {
return err
}
if err = os.Chdir(worktree); err != nil {
return err
}
}

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

// cdLogic implements the HkUp wrapper logic around git-related clone command.
// Returns error if issue with changing directory.
func cdLogic(root, secondLast, dir string, isBare bool) error {
// Gets the remote repository name if no custom clone name is used
// ex). Not Custom: git clone <url>
// Custom: git clone <url> foo
switch root {
case "git", "/usr/bin/git":
if isBare && strings.HasSuffix(dir, ".git") {
start := strings.LastIndex(dir, "/") + 1
dir = dir[start:]
} else if strings.HasSuffix(dir, ".git") {
start := strings.LastIndex(dir, "/") + 1
end := strings.LastIndex(dir, ".git")
dir = dir[start:end]
}
case "gh", "usr/bin/gh":
if strings.Count(secondLast, "/") != 1 {
start := strings.LastIndex(dir, "/") + 1
dir = dir[start:]
}
}

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

0 comments on commit 8313203

Please sign in to comment.