-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskfile.yaml
More file actions
538 lines (469 loc) · 18.6 KB
/
Copy pathTaskfile.yaml
File metadata and controls
538 lines (469 loc) · 18.6 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
---
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: "3"
tasks:
add-git-submodules:
desc: Add submodules from GitHub based on a naming pattern
cmds:
- |
# Determine the submodule path
if [ -n "{{.TARGET_DIR}}" ]; then
SUBMODULE_PATH="{{.TARGET_DIR}}/{{.REGEXP}}"
else
SUBMODULE_PATH="{{.REGEXP}}"
fi
# Clean up any existing repository or staged changes
if [ -d "$SUBMODULE_PATH" ]; then
echo "Removing existing directory: $SUBMODULE_PATH"
rm -rf "$SUBMODULE_PATH"
fi
# Remove from git index if it exists
git rm -f "$SUBMODULE_PATH" 2>/dev/null || true
git rm --cached "$SUBMODULE_PATH" 2>/dev/null || true
# Remove from .gitmodules if it exists
git config --file=.gitmodules --remove-section "submodule.$SUBMODULE_PATH" 2>/dev/null || true
# Get the repository URL
repo_url=$(gh repo view "{{.ORG}}/{{.REGEXP}}" --json url -q .url)
if [ -n "$repo_url" ]; then
echo "Adding submodule from: $repo_url to $SUBMODULE_PATH"
git submodule add "$repo_url" "$SUBMODULE_PATH"
else
echo "Error: Repository not found"
exit 1
fi
# Update and sync submodules
git submodule update --init --recursive
git submodule sync --recursive
requires:
vars: ['ORG', 'REGEXP']
silent: false
check-gh-cli:
desc: Validate that the GitHub CLI (gh) is installed
cmds:
- |
if ! command -v gh &> /dev/null; then
echo "'gh' command not found. Please install GitHub CLI: https://cli.github.com"
echo ""
echo " Installation instructions:"
echo " - macOS: brew install gh"
echo " - Ubuntu: sudo apt install gh"
echo " - Fedora: sudo dnf install gh"
echo " - Arch: sudo pacman -S github-cli"
echo " - Windows: winget install --id GitHub.cli"
echo ""
echo " For other systems, see: https://github.com/cli/cli#installation"
exit 1
fi
silent: true
create-release:
desc: Create a release on GitHub
deps: [check-gh-cli]
vars:
CHANGELOG_FILE: '{{.CHANGELOG_FILE | default "changelogs/CHANGELOG.rst"}}'
USE_CHANGELOG:
sh: '[ -f "{{.CHANGELOG_FILE}}" ] && echo "true" || echo "false"'
cmds:
- |
# Check if NEXT_VERSION is set
if [ -z "$NEXT_VERSION" ]; then
echo "'NEXT_VERSION' environment variable not set. Example: NEXT_VERSION=1.0.0"
exit 1
fi
# Create GitHub release
if [ "{{.USE_CHANGELOG}}" = "true" ] && [ -z "{{.AUTO_NOTES}}" ]; then
echo "Creating release $NEXT_VERSION from {{.CHANGELOG_FILE}}"
gh release create "$NEXT_VERSION" -F "{{.CHANGELOG_FILE}}"
else
echo "Creating release $NEXT_VERSION with generated notes"
gh release create "$NEXT_VERSION" --generate-notes
fi
requires:
vars: ['NEXT_VERSION']
silent: true
diff-changes:
desc: Show git diff excluding specified patterns (comma-separated)
cmds:
- |
# Disable pagination for empty diffs
export GIT_PAGER='less -F -X'
if [ -n "{{.EXCLUDES}}" ]; then
command="git diff {{.BASE_BRANCH}} --"
# Process each pattern and add it to the command
echo "{{.EXCLUDES}}" | tr ',' '\n' | while read -r pattern; do
# Trim whitespace from pattern
pattern=$(echo "$pattern" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [ -n "$pattern" ]; then
command="$command \":(exclude)$pattern\""
fi
done
# Capture the output of the git diff command
output=$(eval "$command")
# Check if there's any output
if [ -z "$output" ]; then
echo "No changes found when excluding patterns: {{.EXCLUDES}}"
else
echo "$output"
fi
else
# Capture output for regular diff
output=$(git diff {{.BASE_BRANCH}})
if [ -z "$output" ]; then
echo "No changes found in diff with {{.BASE_BRANCH}}"
else
echo "$output"
fi
fi
requires:
vars: ['BASE_BRANCH']
silent: true
ghcr-check-auth:
desc: Check GitHub authentication status
internal: true
deps: [check-gh-cli]
vars:
AUTH_STATUS:
sh: gh auth status &>/dev/null && echo "authenticated" || echo "unauthenticated"
HAS_TOKEN:
sh: '[ -n "$(gh auth token 2>/dev/null)" ] && echo "yes" || echo "no"'
status:
- test "{{.AUTH_STATUS}}" = "authenticated"
- test "{{.HAS_TOKEN}}" = "yes"
cmds:
- echo "Not authenticated with GitHub or missing token"
- echo " To use GHCR images, run:"
- echo " task ghcr-setup"
silent: true
ghcr-setup:
desc: Setup GitHub authentication for GHCR access
deps: [check-gh-cli]
cmds:
- |
# Check current auth status
if gh auth status &>/dev/null && [ -n "$(gh auth token 2>/dev/null)" ]; then
echo "Already authenticated with GitHub"
exit 0
fi
# Authenticate with required scopes
echo "Setting up GitHub authentication..."
gh auth login --scopes "read:packages"
# Verify token is available
if [ -z "$(gh auth token 2>/dev/null)" ]; then
echo "Failed to get GitHub token after authentication"
exit 1
fi
echo "GitHub authentication configured"
silent: true
ghcr-login:
desc: Login to GitHub Container Registry
internal: true
deps: [ghcr-check-auth]
vars:
TOKEN:
sh: gh auth token 2>/dev/null || true
preconditions:
- sh: '[ -n "{{.TOKEN}}" ]'
msg: "GitHub token not available. Run 'task ghcr-setup' first."
cmds:
- echo "{{.TOKEN}}" | docker login {{.GHCR_REGISTRY}} -u {{.GHCR_NAMESPACE}} --password-stdin
- echo "Logged in to GHCR"
silent: true
ghcr-pull-image:
desc: Pull and tag Docker image from registry
vars:
# Allow override of registry settings
REGISTRY: '{{.REGISTRY | default .GHCR_REGISTRY}}'
NAMESPACE: '{{.NAMESPACE | default .GHCR_NAMESPACE}}'
TAG: '{{.TAG | default .IMAGE_TAG}}'
# Build full image path
REMOTE_IMAGE: '{{.REMOTE_IMAGE | default (printf "%s/%s/%s:%s" .REGISTRY .NAMESPACE .IMAGE .TAG)}}'
LOCAL_IMAGE: '{{.LOCAL_IMAGE | default .IMAGE}}'
LOCAL_TAG: '{{.LOCAL_TAG | default .TAG}}'
requires:
vars: ['IMAGE']
cmds:
- |
echo "Pulling image: {{.REMOTE_IMAGE}}"
if ! docker pull {{.REMOTE_IMAGE}}; then
echo "Failed to pull {{.REMOTE_IMAGE}}"
echo " Image may not exist or you may not have access"
exit 1
fi
# Tag for local use if different
if [ "{{.REMOTE_IMAGE}}" != "{{.LOCAL_IMAGE}}:{{.LOCAL_TAG}}" ]; then
docker tag {{.REMOTE_IMAGE}} {{.LOCAL_IMAGE}}:{{.LOCAL_TAG}}
[ "{{.LOCAL_TAG}}" != "latest" ] && docker tag {{.REMOTE_IMAGE}} {{.LOCAL_IMAGE}}:latest
fi
echo "Image pulled and tagged as {{.LOCAL_IMAGE}}:{{.LOCAL_TAG}}"
silent: true
ghcr-push-image:
desc: Tag and push Docker image to registry
vars:
# Allow override of registry settings
REGISTRY: '{{.REGISTRY | default .GHCR_REGISTRY}}'
NAMESPACE: '{{.NAMESPACE | default .GHCR_NAMESPACE}}'
TAG: '{{.TAG | default .IMAGE_TAG}}'
# Build full image path
REMOTE_IMAGE: '{{.REMOTE_IMAGE | default (printf "%s/%s/%s:%s" .REGISTRY .NAMESPACE .IMAGE .TAG)}}'
LOCAL_IMAGE: '{{.LOCAL_IMAGE | default .IMAGE}}'
LOCAL_TAG: '{{.LOCAL_TAG | default "latest"}}'
# Additional tags to push (comma-separated)
ADDITIONAL_TAGS: '{{.ADDITIONAL_TAGS | default ""}}'
requires:
vars: ['IMAGE']
deps: [ghcr-login]
cmds:
- |
echo "Pushing image: {{.LOCAL_IMAGE}}:{{.LOCAL_TAG}} -> {{.REMOTE_IMAGE}}"
# Check if local image exists
if ! docker image inspect {{.LOCAL_IMAGE}}:{{.LOCAL_TAG}} >/dev/null 2>&1; then
echo "Local image {{.LOCAL_IMAGE}}:{{.LOCAL_TAG}} not found"
exit 1
fi
# Tag for remote if different
if [ "{{.LOCAL_IMAGE}}:{{.LOCAL_TAG}}" != "{{.REMOTE_IMAGE}}" ]; then
docker tag {{.LOCAL_IMAGE}}:{{.LOCAL_TAG}} {{.REMOTE_IMAGE}}
fi
# Push main tag
if ! docker push {{.REMOTE_IMAGE}}; then
echo "Failed to push {{.REMOTE_IMAGE}}"
exit 1
fi
# Push additional tags if specified
if [ -n "{{.ADDITIONAL_TAGS}}" ]; then
IFS=',' read -ra TAGS <<< "{{.ADDITIONAL_TAGS}}"
for tag in "${TAGS[@]}"; do
tag=$(echo "$tag" | xargs) # trim whitespace
REMOTE_TAG="{{.REGISTRY}}/{{.NAMESPACE}}/{{.IMAGE}}:${tag}"
echo "Pushing additional tag: ${REMOTE_TAG}"
docker tag {{.LOCAL_IMAGE}}:{{.LOCAL_TAG}} ${REMOTE_TAG}
docker push ${REMOTE_TAG}
done
fi
echo "Image pushed successfully to {{.REMOTE_IMAGE}}"
silent: true
pr-merge:
desc: "Merge the current branch's PR with squash and cleanup"
deps:
- check-gh-cli
cmds:
- |
# Get PR number for current branch
PR_NUMBER=$(gh pr view --json number -q .number 2>/dev/null)
if [ -z "$PR_NUMBER" ]; then
echo "No PR found for current branch"
exit 1
fi
# Check CI status
echo "Checking CI status..."
if ! gh pr checks "$PR_NUMBER" | grep -q "pass"; then
echo "Warning: Some checks may not have passed"
read -p "Continue with merge? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Merge PR
echo "Merging PR #$PR_NUMBER with squash..."
gh pr merge "$PR_NUMBER" --squash --delete-branch
# Detect default branch (main or master)
DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch" | sed 's/.*: //')
if [ -z "$DEFAULT_BRANCH" ]; then
# Fallback to main if detection fails
DEFAULT_BRANCH="main"
fi
# Switch to default branch and update
echo "Switching to $DEFAULT_BRANCH and pulling latest..."
git checkout "$DEFAULT_BRANCH"
git pull
# Cleanup
echo "Cleaning up local branches..."
git fetch --prune
echo "Done! You're now on $DEFAULT_BRANCH with the latest changes."
silent: false
pull-git-repos:
desc: Pull updates for all git repositories in the current directory
silent: true
cmds:
- |
find . -maxdepth 1 -type d ! -name ".git" -exec sh -c '
cd "{}" &&
echo "Processing: {}" &&
branch=$(git branch --show-current 2>/dev/null) &&
if [ -z "$branch" ]; then
echo "Failed in {}: Not on a branch. Skipping."
elif ! git diff --quiet || ! git diff --cached --quiet; then
echo "Failed in {}: Uncommitted or unstaged changes. Skipping."
else
git pull || echo "Failed in {}: Git pull failed."
fi
' \;
remove-git-submodules:
desc: Remove one or more submodules and clean up related files
cmds:
- |
# Set variables
submodule_path="{{.TARGET_DIR}}/{{.REGEXP}}"
# Find matching submodules
matching_submodules=$(git config --file .gitmodules --get-regexp path | grep "{{.REGEXP}}" | cut -d ' ' -f2 || true)
if [ -z "$matching_submodules" ]; then
echo "No matching submodules found for pattern: {{.REGEXP}}"
exit 0
fi
echo "Found matching submodules:"
echo "$matching_submodules"
echo "Proceeding with removal..."
for submodule in $matching_submodules; do
echo "Removing submodule: $submodule"
# Stage 1: Deinitialize the submodule
echo "Deinitializing submodule..."
git submodule deinit -f "$submodule" || {
echo "Warning: Failed to deinitialize $submodule, continuing anyway..."
}
# Stage 2: Remove the submodule entry from .git/config
git config --remove-section "submodule.$submodule" 2>/dev/null || true
# Stage 3: Remove the submodule from the working tree and .git/modules
echo "Removing from git..."
git rm -f "$submodule" || {
echo "Warning: Failed to git rm $submodule, trying manual removal..."
rm -rf "$submodule"
}
# Stage 4: Clean up .git/modules
echo "Cleaning up .git/modules..."
rm -rf ".git/modules/$submodule"
# Stage 5: Remove from .gitmodules file
echo "Updating .gitmodules..."
if git config -f .gitmodules --get-regexp "submodule\.$submodule" > /dev/null 2>&1; then
git config -f .gitmodules --remove-section "submodule.$submodule"
git add .gitmodules
fi
done
# Final cleanup and commit
echo "Performing final cleanup..."
git submodule sync --recursive
# Check if there are changes to commit
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "Committing changes..."
git commit -m "refactor: Remove submodules matching pattern {{.REGEXP}}" || true
fi
echo "Submodule removal complete"
requires:
vars: ['TARGET_DIR', 'REGEXP']
silent: true
recent-branches:
desc: Show branches you worked on most recently in a project
vars:
PROJECT_PATH: '{{.PROJECT_PATH | default "."}}'
LIMIT: '{{.LIMIT | default "10"}}'
cmds:
- |
# Change to project directory
cd "{{.PROJECT_PATH}}"
# Check if it's a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "Error: {{.PROJECT_PATH}} is not a git repository"
exit 1
fi
echo "Recent branches in $(pwd):"
echo ""
# Show recent branches sorted by last commit date
# Format: branch name, last commit date, last commit message
git for-each-ref --sort=-committerdate refs/heads/ \
--format='%(refname:short)|%(committerdate:relative)|%(contents:subject)' \
--count={{.LIMIT}} | while IFS='|' read -r branch date message; do
# Highlight current branch
current=$(git branch --show-current)
if [ "$branch" = "$current" ]; then
printf "* \033[32m%-30s\033[0m %-20s %s\n" "$branch" "$date" "$message"
else
printf " %-30s %-20s %s\n" "$branch" "$date" "$message"
fi
done
silent: true
recent-projects:
desc: Show the 5 most recently worked on git projects
vars:
SEARCH_PATH: '{{.SEARCH_PATH | default "$HOME"}}'
LIMIT: '{{.LIMIT | default "5"}}'
MAX_DEPTH: '{{.MAX_DEPTH | default "3"}}'
cmds:
- |
echo "Searching for recent projects in {{.SEARCH_PATH}}..."
echo ""
# Find all git repositories and get their last commit date
find "{{.SEARCH_PATH}}" -maxdepth {{.MAX_DEPTH}} -name ".git" -type d 2>/dev/null | while read -r git_dir; do
project_dir=$(dirname "$git_dir")
# Get last commit timestamp and branch
cd "$project_dir"
last_commit=$(git log -1 --format=%ct 2>/dev/null)
branch=$(git branch --show-current 2>/dev/null || echo "detached")
if [ -n "$last_commit" ]; then
echo "$last_commit|$project_dir|$branch"
fi
done | sort -rn | head -n {{.LIMIT}} | while IFS='|' read -r timestamp path branch; do
# Convert timestamp to human-readable date
if date -r "$timestamp" "+%Y-%m-%d %H:%M:%S" >/dev/null 2>&1; then
# macOS/BSD date
date_str=$(date -r "$timestamp" "+%Y-%m-%d %H:%M:%S")
else
# GNU date
date_str=$(date -d "@$timestamp" "+%Y-%m-%d %H:%M:%S")
fi
printf "%-25s %-20s %s\n" "$date_str" "[$branch]" "$path"
done
echo ""
echo "Tip: Use PROJECT_PATH=/path/to/project with recent-branches to see branches in a specific project"
silent: true
sync-submodules:
desc: "Initialize and update submodules idempotently"
cmds:
- |
echo "Synchronizing all submodules..."
# STEP 0: Ensure submodules are initialized before running any 'foreach' or update
git submodule update --init --recursive
# Check if any submodules exist in .gitmodules
if [ ! -f .gitmodules ] || [ ! -s .gitmodules ]; then
echo "No submodules defined in this repository. Nothing to synchronize."
exit 0
fi
# Step 1: Fix any detached HEAD in submodules by checking out proper branch
echo "Ensuring submodules are on the correct branch..."
git submodule foreach '
# Get the branch submodule is supposed to track from .gitmodules
configured_branch=$(git config -f $toplevel/.gitmodules submodule.$name.branch)
if [ -n "$configured_branch" ]; then
branch="$configured_branch"
else
if git show-ref --verify --quiet refs/remotes/origin/main; then
branch="main"
elif git show-ref --verify --quiet refs/remotes/origin/master; then
branch="master"
else
remote_head=$(git remote show origin | grep "HEAD branch" | sed "s/.*: //")
if [ -n "$remote_head" ]; then
branch="$remote_head"
else
echo "[$name] Could not determine default branch, using main"
branch="main"
fi
fi
fi
if git symbolic-ref -q HEAD >/dev/null; then
echo "[$name] Already on a branch"
else
echo "[$name] Currently in detached HEAD state, checking out $branch branch"
git checkout $branch || git checkout -b $branch || echo "[$name] Failed to checkout branch"
fi
'
# Step 2: Pull latest changes in each submodule if it's on a branch
echo "Pulling latest changes for each submodule..."
git submodule foreach '
if git symbolic-ref -q HEAD >/dev/null; then
git pull --ff-only origin $(git symbolic-ref --short HEAD) || echo "[$name] Failed to pull latest changes"
else
echo "[$name] Not on a branch, skipping pull"
fi
'
# Step 3: Final sync to ensure .git/config is up-to-date
git submodule sync --recursive
echo "All submodules have been successfully synchronized."