1+ #! /bin/bash
2+ # Clone or fork an existing project with Claude enhancements
3+
4+ # Source common utilities and git functions
5+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
6+ source " ${SCRIPT_DIR} /common.sh"
7+ source " ${SCRIPT_DIR} /git.sh"
8+
9+ # Parse command line arguments
10+ REPO_URL=" "
11+ FORK_MODE=false
12+ BRANCH_NAME=" "
13+ CLONE_DEPTH=" "
14+ GIT_NAME=" "
15+ GIT_EMAIL=" "
16+ FEATURE_BRANCH=" "
17+ SKIP_DEPS=false
18+ SKIP_ENHANCE=false
19+ PROJECT_NAME=" "
20+
21+ # Function to show usage
22+ show_usage () {
23+ echo " Usage: $0 <repository-url> [options]"
24+ echo " "
25+ echo " Clone or fork a repository and enhance it with Claude tools"
26+ echo " "
27+ echo " Options:"
28+ echo " --fork Fork repo before cloning (requires gh CLI)"
29+ echo " --branch <name> Checkout specific branch after clone"
30+ echo " --depth <n> Shallow clone with n commits"
31+ echo " --git-name <name> Configure Git user name for this project"
32+ echo " --git-email <email> Configure Git user email for this project"
33+ echo " --feature <name> Create and checkout feature branch after clone"
34+ echo " --no-deps Skip dependency installation"
35+ echo " --no-enhance Skip all enhancements (just clone/fork)"
36+ echo " -h, --help Show this help message"
37+ echo " "
38+ echo " Examples:"
39+ echo " $0 https://github.com/user/my-app"
40+ echo " $0 https://github.com/original/project --fork"
41+ echo " $0 https://github.com/original/project --fork --feature add-new-feature"
42+ echo " $0 https://github.com/company/app --git-name \" John Doe\" --git-email \" john@company.com\" "
43+ exit 1
44+ }
45+
46+ # Parse arguments
47+ if [ $# -eq 0 ]; then
48+ show_usage
49+ fi
50+
51+ # Check for help flag first
52+ if [[ " $1 " == " -h" ]] || [[ " $1 " == " --help" ]]; then
53+ show_usage
54+ fi
55+
56+ REPO_URL=" $1 "
57+ shift
58+
59+ # Validate repository URL
60+ if [[ ! " $REPO_URL " =~ ^(https? ://| git@) ]]; then
61+ print_error " Invalid repository URL: $REPO_URL "
62+ exit 1
63+ fi
64+
65+ # Extract project name from URL
66+ PROJECT_NAME=$( basename " $REPO_URL " .git)
67+ if [[ -z " $PROJECT_NAME " ]]; then
68+ print_error " Could not determine project name from URL"
69+ exit 1
70+ fi
71+
72+ # Parse optional arguments
73+ while [[ $# -gt 0 ]]; do
74+ case $1 in
75+ --fork)
76+ FORK_MODE=true
77+ shift
78+ ;;
79+ --branch)
80+ BRANCH_NAME=" $2 "
81+ shift 2
82+ ;;
83+ --depth)
84+ CLONE_DEPTH=" $2 "
85+ shift 2
86+ ;;
87+ --git-name)
88+ GIT_NAME=" $2 "
89+ shift 2
90+ ;;
91+ --git-email)
92+ GIT_EMAIL=" $2 "
93+ shift 2
94+ ;;
95+ --feature)
96+ FEATURE_BRANCH=" $2 "
97+ shift 2
98+ ;;
99+ --no-deps)
100+ SKIP_DEPS=true
101+ shift
102+ ;;
103+ --no-enhance)
104+ SKIP_ENHANCE=true
105+ shift
106+ ;;
107+ -h|--help)
108+ show_usage
109+ ;;
110+ * )
111+ print_error " Unknown option: $1 "
112+ show_usage
113+ ;;
114+ esac
115+ done
116+
117+ PROJECT_DIR=" $PROJECTS_DIR /active/$PROJECT_NAME "
118+
119+ # Check if project already exists
120+ if [ -d " $PROJECT_DIR " ]; then
121+ print_error " Project $PROJECT_NAME already exists at $PROJECT_DIR "
122+ exit 1
123+ fi
124+
125+ # Fork mode handling
126+ if [[ " $FORK_MODE " == true ]]; then
127+ # Check for gh CLI
128+ if ! command_exists gh; then
129+ print_error " GitHub CLI (gh) is required for forking. Please install it first."
130+ exit 1
131+ fi
132+
133+ # Check gh authentication
134+ if ! gh auth status > /dev/null 2>&1 ; then
135+ print_error " GitHub CLI is not authenticated. Please run: gh auth login"
136+ exit 1
137+ fi
138+
139+ print_status " Forking repository: $REPO_URL "
140+
141+ # Fork and clone in one command
142+ cd " $PROJECTS_DIR /active" || exit 1
143+ if ! gh repo fork " $REPO_URL " --clone; then
144+ print_error " Failed to fork repository"
145+ exit 1
146+ fi
147+
148+ cd " $PROJECT_NAME " || exit 1
149+
150+ # Setup fork-specific configurations
151+ if [[ " $SKIP_ENHANCE " != true ]]; then
152+ print_status " Setting up fork remotes and aliases..."
153+ setup_fork_remotes
154+ setup_fork_aliases
155+ fi
156+ else
157+ # Regular clone mode
158+ print_status " Cloning repository: $REPO_URL "
159+
160+ # Build clone command
161+ CLONE_CMD=" git clone"
162+ if [[ -n " $CLONE_DEPTH " ]]; then
163+ CLONE_CMD=" $CLONE_CMD --depth $CLONE_DEPTH "
164+ fi
165+ if [[ -n " $BRANCH_NAME " ]]; then
166+ CLONE_CMD=" $CLONE_CMD --branch $BRANCH_NAME "
167+ fi
168+ CLONE_CMD=" $CLONE_CMD \" $REPO_URL \" \" $PROJECT_DIR \" "
169+
170+ # Execute clone
171+ eval $CLONE_CMD
172+ if [ $? -ne 0 ]; then
173+ print_error " Failed to clone repository"
174+ exit 1
175+ fi
176+
177+ cd " $PROJECT_DIR " || exit 1
178+ fi
179+
180+ # Checkout specific branch if requested (and not already done during clone)
181+ if [[ -n " $BRANCH_NAME " ]] && [[ " $FORK_MODE " == true ]]; then
182+ print_status " Checking out branch: $BRANCH_NAME "
183+ git checkout " $BRANCH_NAME " 2> /dev/null || {
184+ print_warning " Branch $BRANCH_NAME not found locally, trying to fetch from upstream"
185+ git fetch upstream " $BRANCH_NAME " 2> /dev/null && git checkout -b " $BRANCH_NAME " " upstream/$BRANCH_NAME "
186+ } || {
187+ print_error " Could not checkout branch: $BRANCH_NAME "
188+ }
189+ fi
190+
191+ # Configure Git user for this project if provided
192+ if [[ -n " $GIT_NAME " ]] || [[ -n " $GIT_EMAIL " ]]; then
193+ print_status " Configuring Git for this project..."
194+ if [[ -n " $GIT_NAME " ]]; then
195+ git config user.name " $GIT_NAME "
196+ print_success " Git user name set to: $GIT_NAME "
197+ fi
198+ if [[ -n " $GIT_EMAIL " ]]; then
199+ git config user.email " $GIT_EMAIL "
200+ print_success " Git user email set to: $GIT_EMAIL "
201+ fi
202+ fi
203+
204+ # Apply enhancements unless skipped
205+ if [[ " $SKIP_ENHANCE " != true ]]; then
206+ print_status " Applying Claude enhancements..."
207+
208+ # Setup Git hooks
209+ setup_git_hooks " $PROJECT_DIR "
210+
211+ # Install dependencies unless skipped
212+ if [[ " $SKIP_DEPS " != true ]]; then
213+ print_status " Installing project dependencies..."
214+
215+ # Use existing dependency installation logic from git.sh
216+ if [[ -f " package.json" ]] && command_exists npm; then
217+ print_status " Installing Node.js dependencies..."
218+ npm install
219+ fi
220+
221+ if [[ -f " requirements.txt" ]] && command_exists pip3; then
222+ print_status " Installing Python dependencies..."
223+ pip3 install -r requirements.txt
224+ fi
225+
226+ if [[ -f " go.mod" ]] && command_exists go; then
227+ print_status " Installing Go dependencies..."
228+ go mod download
229+ fi
230+
231+ if [[ -f " Cargo.toml" ]] && command_exists cargo; then
232+ print_status " Installing Rust dependencies..."
233+ cargo build
234+ fi
235+ fi
236+
237+ # Check for CLAUDE.md and create if missing
238+ if [[ ! -f " CLAUDE.md" ]]; then
239+ print_status " No CLAUDE.md found. Running claude /init to create one..."
240+ if command_exists claude; then
241+ claude /init
242+ else
243+ print_warning " Claude CLI not found. Creating basic CLAUDE.md template..."
244+ cat > CLAUDE.md << CLAUDE_EOF
245+ # $PROJECT_NAME
246+
247+ ## Project Overview
248+ This project was cloned from: $REPO_URL
249+
250+ ## Setup Instructions
251+ [Add setup instructions here]
252+
253+ ## Development Commands
254+ [Add common commands here]
255+
256+ ## Architecture Notes
257+ [Add architectural decisions and patterns]
258+
259+ ## Important Files
260+ [List key files and their purposes]
261+ CLAUDE_EOF
262+ print_success " Basic CLAUDE.md template created. Please update with project details."
263+ fi
264+ else
265+ print_success " CLAUDE.md already exists"
266+ fi
267+
268+ # Initialize Claude Flow if available
269+ if command_exists claude-flow || command_exists npx; then
270+ print_status " Initializing Claude Flow..."
271+ npx claude-flow@alpha init --force 2> /dev/null || true
272+ fi
273+ fi
274+
275+ # Create feature branch if requested
276+ if [[ -n " $FEATURE_BRANCH " ]]; then
277+ print_status " Creating feature branch: $FEATURE_BRANCH "
278+ git checkout -b " $FEATURE_BRANCH "
279+ print_success " Switched to new branch: $FEATURE_BRANCH "
280+ fi
281+
282+ # Final success message
283+ print_success " Project $PROJECT_NAME cloned successfully"
284+ echo " 📁 Location: $PROJECT_DIR "
285+ echo " 📝 Next steps:"
286+ echo " 1. cd $PROJECT_DIR "
287+ if [[ ! -f " CLAUDE.md" ]] || [[ " $SKIP_ENHANCE " == true ]]; then
288+ echo " 2. Run 'claude /init' to set up project context"
289+ fi
290+ echo " 3. Start coding with: claude"
291+
292+ # Show Git configuration for this project
293+ echo " "
294+ echo " Git Configuration:"
295+ echo " User: $( git config user.name) <$( git config user.email) >"
296+ echo " Branch: $( git branch --show-current) "
297+ if [[ " $FORK_MODE " == true ]]; then
298+ echo " Origin: $( git remote get-url origin 2> /dev/null || echo ' not set' ) "
299+ echo " Upstream: $( git remote get-url upstream 2> /dev/null || echo ' not set' ) "
300+ fi
0 commit comments