summaryrefslogtreecommitdiff
path: root/doc/examples.md
blob: 4f8dba45a8f7bec856b626a9255d9be4e1779cb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# GitSyncer Usage Examples

This guide provides practical examples of using GitSyncer for various scenarios.

## Table of Contents

- [Basic Operations](#basic-operations)
- [Repository Discovery](#repository-discovery)
- [Advanced Synchronization](#advanced-synchronization)
- [Automation Examples](#automation-examples)
- [Troubleshooting Scenarios](#troubleshooting-scenarios)

## Basic Operations

### Sync a Single Repository

```bash
# Sync a specific repository
gitsyncer --sync my-project

# Sync with custom working directory
gitsyncer --sync my-project --work-dir /tmp/gitsyncer-work

# Dry run to preview changes
gitsyncer --sync my-project --dry-run
```

### Sync All Configured Repositories

```bash
# Sync all repositories in config
gitsyncer --sync-all

# Create missing GitHub repos automatically
gitsyncer --sync-all --create-github-repos
```

### List Operations

```bash
# List configured organizations
gitsyncer --list-orgs

# List configured repositories
gitsyncer --list-repos

# Show version
gitsyncer --version
```

## Repository Discovery

### Sync All Public Codeberg Repositories to GitHub

```bash
# Discover and sync all public repos from Codeberg
gitsyncer --sync-codeberg-public

# Also create repos on GitHub if they don't exist
gitsyncer --sync-codeberg-public --create-github-repos

# Dry run to see what would be synced
gitsyncer --sync-codeberg-public --dry-run
```

### Sync All Public GitHub Repositories to Codeberg

```bash
# Discover and sync all public repos from GitHub
gitsyncer --sync-github-public

# Note: Codeberg repos must already exist
gitsyncer --sync-github-public
```

### Full Bidirectional Sync

```bash
# Sync all public repos in both directions
gitsyncer --full

# Equivalent to:
# gitsyncer --sync-codeberg-public --sync-github-public --create-github-repos
```

## Advanced Synchronization

### Working with Branch Filters

Configuration with branch exclusions:
```json
{
  "organizations": [
    {"host": "git@github.com", "name": "myorg"},
    {"host": "git@codeberg.org", "name": "myorg"}
  ],
  "repositories": ["my-project"],
  "exclude_branches": [
    "^temp-",
    "^feature/experimental-",
    "-wip$"
  ]
}
```

Output shows excluded branches:
```bash
$ gitsyncer --sync my-project

🚫 Excluded 3 branches based on patterns:
   Patterns: '^temp-', '^feature/experimental-', '-wip$'
   Excluded branches:
   - temp-fix
   - feature/experimental-ai
   - feature-wip
```

### Handling Merge Conflicts

When conflicts occur:
```bash
$ gitsyncer --sync my-project

ERROR: repository has unresolved merge conflicts
Please resolve conflicts in: /home/user/.gitsyncer-work/my-project
Or delete the directory to start fresh: rm -rf /home/user/.gitsyncer-work/my-project
```

Resolution options:
```bash
# Option 1: Manually resolve conflicts
cd /home/user/.gitsyncer-work/my-project
git status
# Fix conflicts
git add .
git commit -m "Resolved conflicts"
cd -
gitsyncer --sync my-project

# Option 2: Start fresh
rm -rf /home/user/.gitsyncer-work/my-project
gitsyncer --sync my-project
```

### Abandoned Branch Detection

GitSyncer detects branches inactive for 6+ months:
```bash
$ gitsyncer --sync-all

[1/3] Syncing project1...
Repository project1 synchronized successfully!

🔍 Abandoned branches in project1:
   Main branch (main) is abandoned - last commit: 2023-01-15
   Other abandoned branches:
   - feature/old-feature (2023-02-01) - abandoned: main branch is abandoned
   - bugfix/old-fix (2023-03-15) - abandoned: main branch is abandoned

=== Summary of Abandoned Branches ===
Total repositories with abandoned branches: 1

Repository: project1
  - feature/old-feature
  - bugfix/old-fix
```

## Automation Examples

### Cron Job for Regular Sync

```bash
# Add to crontab (crontab -e)
# Sync all repos every 6 hours
0 */6 * * * /usr/local/bin/gitsyncer --sync-all --config /home/user/.gitsyncer.json >> /var/log/gitsyncer.log 2>&1

# Sync public repos daily at 2 AM
0 2 * * * /usr/local/bin/gitsyncer --full >> /var/log/gitsyncer-public.log 2>&1
```

### Shell Script Wrapper

```bash
#!/bin/bash
# sync-repos.sh

set -e

CONFIG_FILE="$HOME/.config/gitsyncer/config.json"
LOG_FILE="$HOME/.gitsyncer/sync.log"

echo "Starting sync at $(date)" >> "$LOG_FILE"

# Test GitHub token first
if ! gitsyncer --test-github-token; then
    echo "GitHub token test failed" >> "$LOG_FILE"
    exit 1
fi

# Sync all repos
if gitsyncer --sync-all --config "$CONFIG_FILE" >> "$LOG_FILE" 2>&1; then
    echo "Sync completed successfully at $(date)" >> "$LOG_FILE"
else
    echo "Sync failed at $(date)" >> "$LOG_FILE"
    exit 1
fi
```

### CI/CD Integration

GitHub Actions example:
```yaml
name: Sync Repositories

on:
  schedule:
    - cron: '0 */6 * * *'  # Every 6 hours
  workflow_dispatch:  # Manual trigger

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Install GitSyncer
        run: |
          wget https://github.com/yourusername/gitsyncer/releases/latest/download/gitsyncer-linux-amd64
          chmod +x gitsyncer-linux-amd64
          sudo mv gitsyncer-linux-amd64 /usr/local/bin/gitsyncer
      
      - name: Create config
        run: |
          cat > gitsyncer.json << EOF
          {
            "organizations": [
              {"host": "git@github.com", "name": "${{ github.repository_owner }}"},
              {"host": "git@codeberg.org", "name": "${{ secrets.CODEBERG_ORG }}"}
            ]
          }
          EOF
      
      - name: Sync repositories
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gitsyncer --sync-all --create-github-repos
```

## Troubleshooting Scenarios

### Testing GitHub Authentication

```bash
# Test token is valid
$ gitsyncer --test-github-token
Testing GitHub token authentication...
  Loaded token from env var (length: 40)
  Checking URL: https://api.github.com/repos/myorg/gitsyncer
  Token present: true (length: 40)
SUCCESS: Token is valid! Repository check returned: true
```

### Debugging Sync Issues

```bash
# Check git status in work directory
cd ~/.gitsyncer-work/my-project
git status
git remote -v
git branch -a

# Check for stashed changes
git stash list

# View recent operations
git reflog
```

### Repository Not Found

```bash
$ gitsyncer --sync nonexistent-repo
ERROR: Failed to clone from any organization
```

Solutions:
1. Verify repository exists on at least one platform
2. Check repository name spelling
3. For private repos, ensure proper authentication

### Working Directory Issues

```bash
# Permission denied
$ gitsyncer --sync my-project --work-dir /root/work
ERROR: failed to create work directory: permission denied

# Solution: Use accessible directory
gitsyncer --sync my-project --work-dir ~/gitsyncer-work

# Disk space issues
$ gitsyncer --sync large-repo
ERROR: write error: no space left on device

# Solution: Clean up or use different disk
df -h
rm -rf ~/.gitsyncer-work/old-repo
gitsyncer --sync large-repo --work-dir /mnt/storage/gitsyncer
```

### Network and Connectivity

```bash
# SSH key issues
$ gitsyncer --sync my-project
ERROR: git@github.com: Permission denied (publickey)

# Solution: Add SSH key to agent
ssh-add ~/.ssh/id_rsa

# Firewall/proxy issues
$ gitsyncer --sync my-project
ERROR: Failed to connect to github.com port 22: Connection timed out

# Solution: Use HTTPS URLs in config
{
  "organizations": [
    {"host": "https://github.com", "name": "myorg"},
    {"host": "https://codeberg.org", "name": "myorg"}
  ]
}
```

## Best Practices

### 1. Start with Dry Run
Always test with `--dry-run` first:
```bash
gitsyncer --sync-all --dry-run
```

### 2. Use Specific Working Directories
Organize syncs by project or purpose:
```bash
gitsyncer --sync personal-projects --work-dir ~/sync/personal
gitsyncer --sync work-projects --work-dir ~/sync/work
```

### 3. Monitor Sync Operations
Keep logs for troubleshooting:
```bash
gitsyncer --sync-all 2>&1 | tee -a ~/gitsyncer.log
```

### 4. Regular Maintenance
Clean up old working directories:
```bash
# Remove repos no longer in config
cd ~/.gitsyncer-work
ls -la
rm -rf old-project-name
```

### 5. Handle Secrets Securely
Never put tokens in scripts directly:
```bash
# Bad
gitsyncer --config config-with-token.json

# Good
export GITHUB_TOKEN="$(pass show github/token)"
gitsyncer --sync-all
```