Skip to content
This repository was archived by the owner on Nov 22, 2025. It is now read-only.

Commit 06fb248

Browse files
committed
Add capability to clone and/or fork Git repositories
1 parent 25bf688 commit 06fb248

4 files changed

Lines changed: 362 additions & 4 deletions

File tree

CLAUDE.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ This is a complete remote AI-assisted development environment setup running Clau
3131
- `claude` - Authenticate Claude Code
3232
- `npx claude-flow@alpha init --force` - Initialize Claude Flow in project
3333

34+
### Project Management
35+
- `new-project <name> [--type <type>]` - Create new project with Claude enhancements
36+
- `clone-project <url> [options]` - Clone or fork existing repository with enhancements
37+
- `--fork` - Fork the repository using GitHub CLI before cloning
38+
- `--branch <name>` - Clone specific branch
39+
- `--feature <name>` - Create feature branch after clone/fork
40+
- `--git-name/--git-email` - Configure Git for this project
41+
- `--no-enhance` - Skip Claude enhancements (just clone/fork)
42+
3443
## Architecture Overview
3544

3645
### Infrastructure Components
@@ -233,9 +242,16 @@ The configuration files are copied to `/workspace/config/` and `/workspace/.agen
233242
new-project.sh my-app --type node
234243
new-project.sh my-app --git-name "John Doe" --git-email "john@example.com"
235244
```
236-
- Supports project types: node, python, go, rust, web
237-
- Automatically initializes Git with appropriate .gitignore
238-
- Creates CLAUDE.md template for project context
245+
- Use `clone-project.sh` to clone and enhance existing repositories:
246+
```bash
247+
clone-project https://github.com/user/repo
248+
clone-project https://github.com/original/repo --fork --feature my-feature
249+
clone-project https://github.com/company/app --git-name "John" --git-email "john@company.com"
250+
```
251+
- New projects support types: node, python, go, rust, web
252+
- Clone projects automatically detect dependencies and install them
253+
- Both create CLAUDE.md context and initialize Claude Flow
254+
- Fork mode sets up upstream remotes and helpful Git aliases
239255

240256
### Testing and Validation
241257
- No specific test framework - varies by project

docker/config/turbo-flow-aliases

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Project helpers
44
alias new-project="/workspace/scripts/lib/new-project.sh"
5+
alias clone-project="/workspace/scripts/lib/clone-project.sh"
56
alias project-status="/workspace/scripts/lib/system-status.sh"
67

78
# Monitoring

docker/lib/clone-project.sh

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
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

docker/lib/git.sh

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,47 @@ clone_and_setup() {
468468
print_success "Repository cloned and configured"
469469
}
470470

471+
# Function to setup fork remotes
472+
setup_fork_remotes() {
473+
local upstream_url=""
474+
475+
# The upstream remote should already be set by gh repo fork
476+
# But we'll verify and configure if needed
477+
if ! git remote get-url upstream >/dev/null 2>&1; then
478+
print_warning "Upstream remote not configured. Fork may not have been set up correctly."
479+
else
480+
upstream_url=$(git remote get-url upstream)
481+
print_success "Fork configured with upstream: $upstream_url"
482+
fi
483+
}
484+
485+
# Function to setup fork-specific Git aliases
486+
setup_fork_aliases() {
487+
print_status "Setting up fork management aliases..."
488+
489+
# Sync with upstream
490+
git config alias.sync-upstream '!git fetch upstream && git checkout main && git merge upstream/main'
491+
492+
# Push to fork's origin
493+
git config alias.push-fork 'push origin HEAD'
494+
495+
# Update all branches from upstream
496+
git config alias.update-from-upstream '!git fetch upstream && git rebase upstream/main'
497+
498+
# Create PR-ready branch
499+
git config alias.pr-branch '!f() { git checkout -b "$1" upstream/main; }; f'
500+
501+
# Show fork status
502+
git config alias.fork-status '!echo "=== Remotes ===" && git remote -v && echo && echo "=== Branch Tracking ===" && git branch -vv'
503+
504+
print_success "Fork aliases configured:"
505+
echo " • git sync-upstream - Fetch and merge upstream changes"
506+
echo " • git push-fork - Push current branch to your fork"
507+
echo " • git update-from-upstream - Rebase current branch on upstream/main"
508+
echo " • git pr-branch <name> - Create new branch from upstream/main"
509+
echo " • git fork-status - Show fork remotes and branch tracking"
510+
}
511+
471512
# Export functions
472513
export -f setup_git setup_github_auth setup_git_aliases setup_git_hooks create_gitignore
473-
export -f init_git_repo clone_and_setup
514+
export -f init_git_repo clone_and_setup setup_fork_remotes setup_fork_aliases

0 commit comments

Comments
 (0)