Function bodies 56 total
treehouse.FindTreehouse function · go · L31-L86 (56 LOC)internal/treehouse/treehouse.go
func FindTreehouse(startPath string) (*Info, error) {
// Convert to absolute path
absPath, err := filepath.Abs(startPath)
if err != nil {
return nil, &TreehouseError{
Code: "INIT_PATH_ERROR",
Message: fmt.Sprintf("Failed to resolve path: %v", err),
}
}
// Check if we're in a git worktree (nook) - if so, find the base repo
baseRepoPath := findBaseRepo(absPath)
if baseRepoPath != "" {
// We're in a worktree, use the base repo path
treehousePath := filepath.Join(baseRepoPath, ".treehouse")
info, err := os.Stat(treehousePath)
if err == nil && info.IsDir() {
return &Info{
TreehousePath: treehousePath,
RepoRoot: baseRepoPath,
WorktreesPath: filepath.Join(treehousePath, "nooks"),
}, nil
}
}
// Walk up the directory tree from current path
currentPath := absPath
for {
treehousePath := filepath.Join(currentPath, ".treehouse")
info, err := os.Stat(treehousePath)
if err == nil && info.IsDir() {
// Check if this .treehouse has a detreehouse.findBaseRepo function · go · L89-L111 (23 LOC)internal/treehouse/treehouse.go
func findBaseRepo(path string) string {
// Run git rev-parse --git-dir to check if we're in a worktree
cmd := exec.Command("git", "rev-parse", "--git-dir")
cmd.Dir = path
output, err := cmd.Output()
if err != nil {
return ""
}
gitDir := strings.TrimSpace(string(output))
// If git-dir contains "worktrees", we're in a worktree
// Pattern: /path/to/base/.git/worktrees/nook-name
if strings.Contains(gitDir, string(filepath.Separator)+"worktrees"+string(filepath.Separator)) {
// Extract base repo: remove .git/worktrees/nook-name
parts := strings.Split(gitDir, string(filepath.Separator)+".git"+string(filepath.Separator)+"worktrees")
if len(parts) > 0 {
return parts[0]
}
}
return ""
}worktree.GetParentInfo function · go · L31-L109 (79 LOC)internal/worktree/worktree.go
func GetParentInfo(treehousePath string) (*ParentInfo, error) {
// Get current working directory
cwd, err := os.Getwd()
if err != nil {
return nil, &WorktreeError{
Code: "WORKTREE_CWD_ERROR",
Message: fmt.Sprintf("Failed to get current directory: %v", err),
}
}
// Check if we're in a git worktree
// git rev-parse --show-toplevel gives us the root
// git rev-parse --git-dir gives us the git dir (if worktree, it's .git/worktrees/<name>)
gitDirOut, err := exec.Command("git", "rev-parse", "--git-dir").Output()
if err != nil {
return nil, &WorktreeError{
Code: "GIT_DIR_ERROR",
Message: fmt.Sprintf("Failed to get git directory: %v", err),
}
}
gitDir := strings.TrimSpace(string(gitDirOut))
// Get worktree root
rootOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return nil, &WorktreeError{
Code: "GIT_ROOT_ERROR",
Message: fmt.Sprintf("Failed to get git root: %v", err),
}
}
worktreeRoot := stringsworktree.Create function · go · L113-L152 (40 LOC)internal/worktree/worktree.go
func Create(worktreesPath string, nookID string, parentBranch string) (string, error) {
// Ensure worktrees directory exists
if err := os.MkdirAll(worktreesPath, 0755); err != nil {
return "", &WorktreeError{
Code: "GIT_WORKTREE_FAILED",
Message: fmt.Sprintf("Failed to create worktrees directory: %v", err),
}
}
// Calculate worktree path
worktreePath := filepath.Join(worktreesPath, nookID)
// Check if worktree path already exists
if _, err := os.Stat(worktreePath); err == nil {
return "", &WorktreeError{
Code: "NOOK_ALREADY_EXISTS",
Message: fmt.Sprintf("Worktree path already exists: %s", worktreePath),
}
}
// Create the worktree with a new branch
// git worktree add -b <branch-name> <path> [<commit-ish>]
// We create a new branch named after the nook ID, based on parent branch
cmd := exec.Command("git", "worktree", "add", "-b", nookID, worktreePath, parentBranch)
output, err := cmd.CombinedOutput()
if err != nil {
return "", &WorktreeError{worktree.Exists function · go · L155-L164 (10 LOC)internal/worktree/worktree.go
func Exists(worktreePath string) (bool, error) {
_, err := os.Stat(worktreePath)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}worktree.Remove function · go · L167-L194 (28 LOC)internal/worktree/worktree.go
func Remove(worktreesPath string, nookID string) error {
worktreePath := filepath.Join(worktreesPath, nookID)
// Check if worktree exists
if _, err := os.Stat(worktreePath); os.IsNotExist(err) {
return &WorktreeError{
Code: "NOOK_NOT_FOUND",
Message: fmt.Sprintf("Worktree not found: %s", worktreePath),
}
}
// Remove the worktree
cmd := exec.Command("git", "worktree", "remove", worktreePath, "--force")
output, err := cmd.CombinedOutput()
if err != nil {
return &WorktreeError{
Code: "GIT_WORKTREE_FAILED",
Message: fmt.Sprintf("Failed to remove worktree: %s", strings.TrimSpace(string(output))),
}
}
// Delete the branch
cmd = exec.Command("git", "branch", "-D", nookID)
// Ignore branch deletion errors - branch might not exist
_ = cmd.Run()
return nil
}‹ prevpage 2 / 2