Commit ace00b4
Changed files (1)
001_init.md
@@ -0,0 +1,137 @@
+---
+id: 001
+title: Implement init command
+status: completed
+type: feature
+created: 2025-09-24
+completed: 2025-09-24
+assignee: crash
+---
+
+# Implement init command
+
+## Description
+
+Implement the `tissue init` command to initialize issue tracking in a git repository. This command creates an orphan "issues" branch, sets up the proper directory structure, and optionally configures a git worktree for easy access.
+
+## Implementation Tasks
+
+- [x] Update go.mod with go-git dependencies
+ - Add `github.com/go-git/go-git/v5`
+ - Add `github.com/go-git/go-billy/v5`
+
+- [x] Implement repository validation
+ - Open repository with `git.PlainOpen(".")`
+ - Verify it's a valid git repository
+ - Check if issues branch already exists
+
+- [x] Create orphan "issues" branch
+ - Store current branch reference to return later
+ - Create orphan branch using `plumbing.NewSymbolicReference`
+ - Set HEAD to point to new orphan branch
+ - **FIXED**: Clear index and working directory to ensure true orphan
+
+- [x] Initialize branch structure
+ - Create README.md with issue tracker description
+ - Create .gitignore (ignore everything except markdown files)
+ - Stage files with `worktree.Add()`
+ - Commit as first commit on orphan branch
+
+- [x] Handle remote operations
+ - Check for origin remote
+ - Push issues branch if remote exists
+ - Handle push failures gracefully
+
+- [x] Return to original branch
+ - Checkout back to the branch stored at start
+
+- [x] **UPDATED**: Implement automatic worktree setup with --issues-dir flag
+ - Always create worktree (no longer optional)
+ - Replace `--setup-worktree` with `--issues-dir <dir>` flag
+ - Default to `./issues` if flag not provided
+ - Use os/exec to run `git worktree add <dir> issues`
+ - Add `<dir>/` to main branch .gitignore
+ - Commit .gitignore update
+
+- [x] Error handling
+ - Clear error messages for each failure case
+ - Handle bare repositories appropriately
+ - Validate operations at each step
+
+- [x] User feedback
+ - Progress messages for each operation
+ - Success confirmation
+ - Instructions for next steps
+
+## Technical Notes
+
+### Orphan Branch Creation
+```go
+orphanRef := plumbing.NewSymbolicReference(
+ plumbing.HEAD,
+ plumbing.ReferenceName("refs/heads/issues"),
+)
+repo.Storer.SetReference(orphanRef)
+```
+
+### Worktree Limitation
+go-git v5 doesn't support `git worktree add` natively. Using os/exec as temporary solution:
+```go
+// TODO: Remove dependency on git CLI when go-git supports worktrees
+// Track progress: https://github.com/go-git/go-git/pull/396
+cmd := exec.Command("git", "worktree", "add", "issues", "issues")
+```
+
+## Acceptance Criteria
+
+1. Command creates orphan "issues" branch successfully
+2. Branch contains README.md and .gitignore
+3. **UPDATED**: Worktree is automatically created (no longer optional)
+4. **UPDATED**: --issues-dir flag allows custom directory name (defaults to ./issues)
+5. Proper error handling for edge cases
+6. Clear user feedback throughout process
+7. Returns to original branch after completion
+
+## Problems Encountered
+
+### Problem 1: Orphan branch not truly orphan
+**Date:** 2025-09-24
+**Description:** The created "issues" branch was not truly orphan - it had parent commits and shared history with the main branch.
+
+**Root Cause:**
+- Setting `plumbing.NewSymbolicReference` to point HEAD to a new branch name doesn't clear the index or working directory
+- When committing without clearing the index first, the commit inherits the parent from the current HEAD
+- The working directory still contains all files from the previous branch
+
+**Expected Behavior:**
+- `git checkout --orphan issues` creates a new branch with no history
+- `git rm -rf .` clears the index and working directory
+- First commit has no parent
+
+**Fix:**
+After creating the symbolic reference, must:
+1. Clear the entire git index using `worktree.RemoveGlob("*")`
+2. Clean the working directory of all tracked files
+3. Only then add the new files (README.md, .gitignore) and commit
+
+This ensures the branch starts with zero commits and empty directory, creating truly divergent history.
+
+## Summary
+
+The `tissue init` command has been successfully implemented with all requirements:
+
+1. ✅ Creates a truly orphan branch (no parent commits)
+2. ✅ Initializes branch with README.md and .gitignore
+3. ✅ Automatically sets up git worktree
+4. ✅ `--issues-dir` flag allows custom directory (defaults to ./issues)
+5. ✅ `--issues-branch` flag allows custom branch name (defaults to issues)
+6. ✅ Uses constants `_issuesBranchDefault` and `_issuesDirDefault` for defaults
+7. ✅ Pushes to remote if available
+8. ✅ Returns to original branch after completion
+9. ✅ Clear error messages and user feedback
+
+The command is now fully functional and ready for use.
+
+## Related Issues
+
+- Future: Remove os/exec dependency when go-git adds worktree support (PR #396)