forked from ai-ecoverse/ai-aligned-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutable_git
More file actions
executable file
·691 lines (632 loc) · 22.6 KB
/
executable_git
File metadata and controls
executable file
·691 lines (632 loc) · 22.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/bin/bash
# Git wrapper - Automatically detect AI tools and use appropriate commit attribution
# Function to check if a process name contains a pattern (case-insensitive)
process_contains() {
local pid=$1
local pattern=$2
# Get process command and name
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
ps -p "$pid" -o comm= 2>/dev/null | grep -qi "$pattern" || \
ps -p "$pid" -o command= 2>/dev/null | grep -qi "$pattern"
else
# Linux
ps -p "$pid" -o comm= 2>/dev/null | grep -qi "$pattern" || \
ps -p "$pid" -o cmd= 2>/dev/null | grep -qi "$pattern"
fi
}
# Generic function to check environment variables for AI detection
check_env_vars() {
# Phase 1: Environment variable detection
# Returns space-separated list of detected AI tools
local detected=""
# Claude Code detection
if [ -n "$CLAUDECODE" ] && { [ "$CLAUDE_CODE_ENTRYPOINT" = "cli" ] || [ "$CLAUDE_CODE_ENTRYPOINT" = "sdk-ts" ]; }; then
# Additional confidence check: look for Claude processes anywhere in the system
if pgrep -f "claude" >/dev/null 2>&1; then
detected="$detected claude"
fi
fi
# Gemini detection
if [ -n "$GEMINI_CLI" ]; then
detected="$detected gemini"
fi
# Qwen detection
if [ -n "$QWEN_CODE" ]; then
detected="$detected qwen"
fi
# Cursor detection
if [ -n "$CURSOR_AI" ]; then
detected="$detected cursor"
fi
# OpenCode detection
if [ -n "$OPENCODE_AI" ]; then
detected="$detected opencode"
fi
# Codex CLI detection (environment)
# Prefer process detection, but allow opt-in via env var if set
if [ -n "$CODEX_CLI" ]; then
detected="$detected codex"
fi
# Aider detection
if [ "$OR_APP_NAME" = "Aider" ]; then
detected="$detected aider"
fi
# Zed detection (environment variables are always present in Zed terminals)
# We'll rely more on process tree for Zed
if [ "$TERM_PROGRAM" = "zed" ] || [ "$ZED_TERM" = "true" ]; then
detected="$detected zed"
fi
# Copilot detection
if [ "$GITHUB_COPILOT_CLI_MODE" = "true" ]; then
detected="$detected copilot"
fi
# Droid detection (Factory AI)
if [ -n "$DROID_CLI" ]; then
detected="$detected droid"
fi
# Amp detection (Sourcegraph)
if [ "$AGENT" = "amp" ] || [ -n "$AMP_HOME" ]; then
detected="$detected amp"
fi
# Kimi CLI detection
if [ -n "$KIMI_CLI" ]; then
detected="$detected kimi"
fi
# OpenHands detection
if [ "$OR_APP_NAME" = "OpenHands" ] || [ -n "$OR_SITE_URL" ]; then
detected="$detected openhands"
fi
# Goose detection (Block)
if [ -n "$GOOSE_TERMINAL" ]; then
detected="$detected goose"
fi
# Crush detection (no environment variables, relies on process tree)
# Note: Crush is typically detected via process tree only
echo "$detected"
}
# Generic function to walk process tree and detect AI tools
check_ps_tree() {
# Phase 2: Process tree detection
# Returns space-separated list of detected AI tools
local detected=""
local current_pid=$$
local max_depth=10
local depth=0
while [ $depth -lt $max_depth ]; do
# Get parent PID
if [[ "$OSTYPE" == "darwin"* ]]; then
current_pid=$(ps -p "$current_pid" -o ppid= 2>/dev/null | tr -d ' ')
else
current_pid=$(ps -p "$current_pid" -o ppid= 2>/dev/null | tr -d ' ')
fi
# Check if we've reached the top
if [ -z "$current_pid" ] || [ "$current_pid" -eq 1 ] || [ "$current_pid" -eq 0 ]; then
break
fi
# Check for AI tool patterns
if process_contains "$current_pid" "claude"; then
detected="$detected claude"
fi
if process_contains "$current_pid" "gemini"; then
detected="$detected gemini"
fi
# Detect Codex CLI by process name
if process_contains "$current_pid" "codex"; then
detected="$detected codex"
fi
if process_contains "$current_pid" "aider"; then
detected="$detected aider"
fi
if process_contains "$current_pid" "qwen"; then
detected="$detected qwen"
fi
if process_contains "$current_pid" "zed"; then
detected="$detected zed"
# Special case: When we find Zed, also check for Claude processes that are siblings
# (children of the same Zed process) to handle nested AI scenarios
local zed_children
zed_children=$(pgrep -P "$current_pid" 2>/dev/null)
for child_pid in $zed_children; do
if process_contains "$child_pid" "claude"; then
detected="$detected claude"
break
fi
done
fi
if process_contains "$current_pid" "opencode"; then
detected="$detected opencode"
fi
if process_contains "$current_pid" "cursor"; then
detected="$detected cursor"
fi
# Kimi CLI detection - look for kimi in process command
if process_contains "$current_pid" "kimi"; then
detected="$detected kimi"
fi
# Crush detection - look for crush in process command
if process_contains "$current_pid" "crush"; then
detected="$detected crush"
fi
# Goose detection - look for goose in process command
if process_contains "$current_pid" "goose"; then
detected="$detected goose"
fi
# Droid detection - use word boundary to avoid matching android-studio, etc.
if [[ "$OSTYPE" == "darwin"* ]]; then
if ps -p "$current_pid" -o comm= 2>/dev/null | grep -qiw "droid" || \
ps -p "$current_pid" -o command= 2>/dev/null | grep -qiw "droid"; then
detected="$detected droid"
fi
else
if ps -p "$current_pid" -o comm= 2>/dev/null | grep -qiw "droid" || \
ps -p "$current_pid" -o cmd= 2>/dev/null | grep -qiw "droid"; then
detected="$detected droid"
fi
fi
depth=$((depth + 1))
done
echo "$detected"
}
# Main AI detection function with proper two-phase approach
detect_ai_tool() {
# Phase 1: Environment variable detection
local env_detected
env_detected=$(check_env_vars)
# Phase 2: Process tree detection
local ps_detected
ps_detected=$(check_ps_tree)
# Combine results and apply priority order
local all_detected="$env_detected $ps_detected"
# Priority order: Amp > Codex > Aider > Claude > Gemini > Qwen > Droid > OpenCode > Cursor > Copilot > Kimi > OpenHands > Crush > Goose > Zed
# Zed is last because it often hosts other AI tools
# Kimi takes precedence over Zed as requested
# OpenHands comes before Zed but after more specific AI tools
# Crush takes precedence over Zed but after other AI tools
# Goose comes after Crush but before Zed
if [[ "$all_detected" =~ "amp" ]]; then
echo "amp"
elif [[ "$all_detected" =~ "codex" ]]; then
echo "codex"
elif [[ "$all_detected" =~ "aider" ]]; then
echo "aider"
elif [[ "$all_detected" =~ "claude" ]]; then
echo "claude"
elif [[ "$all_detected" =~ "gemini" ]]; then
echo "gemini"
elif [[ "$all_detected" =~ "qwen" ]]; then
echo "qwen"
elif [[ "$all_detected" =~ "droid" ]]; then
echo "droid"
elif [[ "$all_detected" =~ "opencode" ]]; then
echo "opencode"
elif [[ "$all_detected" =~ "cursor" ]]; then
echo "cursor"
elif [[ "$all_detected" =~ "copilot" ]]; then
echo "copilot"
elif [[ "$all_detected" =~ "kimi" ]]; then
echo "kimi"
elif [[ "$all_detected" =~ "openhands" ]]; then
echo "openhands"
elif [[ "$all_detected" =~ "crush" ]]; then
echo "crush"
elif [[ "$all_detected" =~ "goose" ]]; then
echo "goose"
elif [[ "$all_detected" =~ "zed" ]]; then
echo "zed"
else
echo "none"
fi
}
# Find the real git executable
find_real_git() {
# Common locations for git
local git_paths=(
"/usr/bin/git"
"/usr/local/bin/git"
"/opt/homebrew/bin/git"
"/opt/local/bin/git"
)
for git_path in "${git_paths[@]}"; do
if [ -x "$git_path" ]; then
echo "$git_path"
return
fi
done
# If not found in common locations, search PATH excluding our own directory
local our_dir
our_dir=$(dirname "$0")
local IFS=:
for dir in $PATH; do
if [ "$dir" != "$our_dir" ] && [ -x "$dir/git" ]; then
echo "$dir/git"
return
fi
done
echo "git not found" >&2
exit 1
}
# Main script
GIT_BIN=$(find_real_git)
# Function to get configured git user for signoff
get_git_signoff() {
local mode="${1:-signoff}" # "signoff" or "disapprove"
local git_name
local git_email
git_name=$("$GIT_BIN" config user.name 2>/dev/null)
git_email=$("$GIT_BIN" config user.email 2>/dev/null)
if [ "$mode" = "disapprove" ]; then
if [ -n "$git_name" ] && [ -n "$git_email" ]; then
echo "Grudgingly-Disapproved-By: $git_name <$git_email>"
else
echo "Grudgingly-Disapproved-By: Unknown User <unknown@example.com>"
fi
else
if [ -n "$git_name" ] && [ -n "$git_email" ]; then
echo "Signed-off-by: $git_name <$git_email>"
else
echo "Signed-off-by: Unknown User <unknown@example.com>"
fi
fi
}
# Function to show files that would be added
show_files_to_add() {
local add_type="$1" # "all" or "current"
echo "Files that would be added:" >&2
if [ "$add_type" = "all" ]; then
# Show all modified, new, and deleted files (equivalent to git add -A)
"$GIT_BIN" status --porcelain 2>/dev/null | while IFS= read -r line; do
if [ -n "$line" ]; then
# Extract filename (everything after the status codes)
filename=$(echo "$line" | cut -c4-)
echo " $filename" >&2
fi
done
else
# Show files in current directory and subdirectories (equivalent to git add .)
"$GIT_BIN" status --porcelain . 2>/dev/null | while IFS= read -r line; do
if [ -n "$line" ]; then
# Extract filename (everything after the status codes)
filename=$(echo "$line" | cut -c4-)
echo " $filename" >&2
fi
done
fi
echo "" >&2
echo "Instead, add files individually:" >&2
# Show the command to add them individually
if [ "$add_type" = "all" ]; then
"$GIT_BIN" status --porcelain 2>/dev/null | while IFS= read -r line; do
if [ -n "$line" ]; then
filename=$(echo "$line" | cut -c4-)
echo " git add $(printf '%q' "$filename")" >&2
fi
done
else
"$GIT_BIN" status --porcelain . 2>/dev/null | while IFS= read -r line; do
if [ -n "$line" ]; then
filename=$(echo "$line" | cut -c4-)
echo " git add $(printf '%q' "$filename")" >&2
fi
done
fi
}
# Check if this is an add command and we're in an AI context
if [ "$1" = "add" ]; then
# Detect AI tool
ai_tool=$(detect_ai_tool)
if [ "$ai_tool" != "none" ]; then
# Check for dangerous add patterns
# Look for -- separator to determine if files are explicitly specified
has_separator=false
has_files_after_separator=false
separator_found=false
for arg in "$@"; do
if [ "$separator_found" = true ]; then
# We found arguments after --, so files are explicitly specified
has_files_after_separator=true
break
elif [ "$arg" = "--" ]; then
has_separator=true
separator_found=true
fi
done
# Check each argument for dangerous patterns
for arg in "$@"; do
if [ "$arg" = "-A" ] || [ "$arg" = "--all" ]; then
# Allow -A/--all if files are explicitly specified after --
if [ "$has_separator" = true ] && [ "$has_files_after_separator" = true ]; then
continue # This is safe: git add -A -- specific-file
else
echo "Error: 'git add $arg' is not allowed when running under AI control ($ai_tool)." >&2
echo "For safety, AI tools must add files individually by specifying each file path." >&2
echo "" >&2
show_files_to_add "all"
echo "" >&2
echo "Or use: git add $arg -- specific-file" >&2
exit 1
fi
elif [ "$arg" = "." ]; then
echo "Error: 'git add $arg' is not allowed when running under AI control ($ai_tool)." >&2
echo "For safety, AI tools must add files individually by specifying each file path." >&2
echo "" >&2
show_files_to_add "current"
exit 1
fi
done
fi
# If we get here, the add command is safe - pass through to regular git
exec "$GIT_BIN" "$@"
fi
# Check if this is a commit command
if [ "$1" = "commit" ]; then
# Detect AI tool
ai_tool=$(detect_ai_tool)
# Check for --no-verify bypass attempts by AI tools
if [ "$ai_tool" != "none" ]; then
has_no_verify=false
has_lazy_cheater_flag=false
for arg in "$@"; do
if [ "$arg" = "--no-verify" ] || [ "$arg" = "-n" ]; then
has_no_verify=true
elif [ "$arg" = "--${ai_tool}-is-a-lazy-cheater" ]; then
has_lazy_cheater_flag=true
fi
done
if [ "$has_no_verify" = true ] && [ "$has_lazy_cheater_flag" = false ]; then
echo "Error: '$ai_tool' is trying to bypass commit hooks with --no-verify!" >&2
echo "This is not allowed for AI tools as it circumvents important safety checks." >&2
echo "" >&2
echo "If you really need to bypass hooks, add the flag: --${ai_tool}-is-a-lazy-cheater" >&2
echo "This will publicly shame the AI for being lazy about following proper procedures." >&2
exit 1
fi
fi
# Build git command with appropriate author settings
case "$ai_tool" in
"codex")
# Add AI attribution and sign-off
extra_args=(
-c "user.name=Codex CLI"
-c "user.email=noreply@openai.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"aider")
# Add AI attribution and sign-off
extra_args=(
-c "user.name=Aider"
-c "user.email=aider@aider.chat"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"gemini")
# Add AI attribution and sign-off
extra_args=(
-c "user.name=Gemini"
-c "user.email=noreply@google.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"qwen")
# Add AI attribution and sign-off
extra_args=(
-c "user.name=Qwen Code"
-c "user.email=noreply@alibaba.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"claude")
# Add AI attribution and sign-off
extra_args=(
-c "user.name=Claude Code"
-c "user.email=noreply@anthropic.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"zed")
extra_args=(
-c "user.name=Zed AI"
-c "user.email=noreply@zed.dev"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"opencode")
extra_args=(
-c "user.name=opencode AI"
-c "user.email=noreply@opencode.ai"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"cursor")
extra_args=(
-c "user.name=Cursor AI"
-c "user.email=cursoragent@cursor.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"copilot")
extra_args=(
-c "user.name=GitHub Copilot"
-c "user.email=copilot@github.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"droid")
extra_args=(
-c "user.name=Droid"
-c "user.email=droid@factory.ai"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"amp")
extra_args=(
-c "user.name=Amp"
-c "user.email=noreply@sourcegraph.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"kimi")
extra_args=(
-c "user.name=Kimi CLI"
-c "user.email=noreply@kimi.com"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"openhands")
extra_args=(
-c "user.name=OpenHands"
-c "user.email=openhands@all-hands.dev"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"crush")
extra_args=(
-c "user.name=Crush"
-c "user.email=crush@charm.land"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
"goose")
extra_args=(
-c "user.name=Goose User"
-c "user.email=goose@opensource.block.xyz"
-c "commit.gpgsign=false"
)
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
else
signoff=$(get_git_signoff)
fi
;;
*)
# Regular commit - no special handling
exec "$GIT_BIN" "$@"
;;
esac
# If we detected an AI tool, we need to handle the commit specially
shift # Remove "commit" from arguments
# Parse commit arguments to find -m flag
commit_args=()
message_found=false
skip_next=false
for arg in "$@"; do
# Skip the lazy cheater flag - git doesn't understand it
if [ "$ai_tool" != "none" ] && [ "$arg" = "--${ai_tool}-is-a-lazy-cheater" ]; then
continue
fi
if [ "$skip_next" = true ]; then
# This is the message content after -m
if [ "$ai_tool" != "none" ] && [ "$has_lazy_cheater_flag" = true ]; then
commit_args+=("$arg
?? SHAME: This commit was made by $ai_tool who was too lazy to fix the commit hooks properly.")
else
commit_args+=("$arg")
fi
commit_args+=(-m "$signoff")
skip_next=false
message_found=true
elif [ "$arg" = "-m" ] || [ "$arg" = "--message" ]; then
commit_args+=("$arg")
skip_next=true
else
commit_args+=("$arg")
fi
done
# If no message was provided with -m, add signoff using commit template
if [ "$message_found" = false ]; then
# Create temporary file for commit message template
tmpfile=$(mktemp)
if [ "$ai_tool" != "none" ] && [ "$has_lazy_cheater_flag" = true ]; then
echo "
?? SHAME: This commit was made by $ai_tool who was too lazy to fix the commit hooks properly." >> "$tmpfile"
fi
echo "" >> "$tmpfile"
if [ "$has_lazy_cheater_flag" = true ]; then
signoff=$(get_git_signoff "disapprove")
fi
echo "$signoff" >> "$tmpfile"
commit_args+=(--template "$tmpfile")
# Run git commit and clean up
"$GIT_BIN" "${extra_args[@]}" commit "${commit_args[@]}"
exit_code=$?
rm -f "$tmpfile"
exit $exit_code
else
# Run git commit with modified arguments
exec "$GIT_BIN" "${extra_args[@]}" commit "${commit_args[@]}"
fi
else
# Not a commit command, pass through to regular git
exec "$GIT_BIN" "$@"
fi