Repository navigation
Expand file tree
/
Copy pathgit-acommit
More file actions
executable file
·102 lines (85 loc) · 2.8 KB
/
Copy pathgit-acommit
File metadata and controls
executable file
·102 lines (85 loc) · 2.8 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
#!/bin/sh
# git-wcommit
# Purpose: Commit changes excluding the WIP files.
GIT_DIR=$(git rev-parse --git-dir) || { echo "Error: Not in a git repo." >&2; exit 1; }
BRANCH_RAW=$(git rev-parse --abbrev-ref HEAD)
[ "$BRANCH_RAW" = "HEAD" ] && BRANCH_RAW="detached-head"
BRANCH_SAFE=$(echo "$BRANCH_RAW" | tr ' /' '__')
TRACKER="$GIT_DIR/wip-$BRANCH_SAFE"
cleanup_and_exit() {
CODE=$?
if [ -n "$TEMP_STASH" ]; then
echo "Restoring WIP files..."
# Capture output (stderr + stdout) to variable
OUTPUT=$(git stash apply --index "$TEMP_STASH" 2>&1)
APPLY_STATUS=$?
if [ $APPLY_STATUS -eq 0 ]; then
# Success: Silence warnings, drop stash
git stash drop --quiet "$TEMP_STASH"
else
# Failure: Print the captured output so user sees errors
echo "$OUTPUT" >&2
echo "Error: Failed to restore stash. Run 'git stash apply --index $TEMP_STASH' manually." >&2
fi
fi
if [ -n "$RESTAGE_FILES" ]; then
git add -- $RESTAGE_FILES
fi
exit $CODE
}
ARGS=()
ADD_ALL=false
for arg in "$@"; do
if [ "$arg" = "-a" ] || [ "$arg" = "--all" ]; then ADD_ALL=true; else ARGS+=("$arg"); fi
done
# If no WIP active, pass through
if [ ! -f "$TRACKER" ]; then
echo "Info: No WIP found. Committing normally."
[ "$ADD_ALL" = true ] && ARGS+=("-a")
git commit "${ARGS[@]}"
exit $?
fi
# CHECK STATUS
STATUS=$(head -n 1 "$TRACKER")
if [ "$STATUS" != "ACTIVE" ]; then
echo "Error: WIP for this branch is marked as STASHED ($STATUS)." >&2
echo "Try running 'git wcheckout $BRANCH_RAW' to reload it." >&2
exit 1
fi
# Parse Files (Line 3 onwards)
MSG=$(sed -n 2p "$TRACKER")
WIP_FILES=$(tail -n +3 "$TRACKER" | tr '\n' ' ')
# 1. Identify currently staged files
STAGED=$(git diff --name-only --cached | tr '\n' ' ')
# 2. Isolate: Find staged files that are NOT in WIP_FILES
TFILE=$(mktemp)
echo "$WIP_FILES" | tr ' ' '\n' > "$TFILE"
TO_UNSTAGE=$(echo "$STAGED" | tr ' ' '\n' | grep -v -F -x -f "$TFILE" | tr '\n' ' ')
rm -f "$TFILE"
# 3. Unstage non-WIP
if [ -n "$TO_UNSTAGE" ]; then
RESTAGE_FILES="$TO_UNSTAGE"
git reset -- $TO_UNSTAGE >/dev/null 2>&1
fi
# 4. Ensure WIP is staged
if [ -n "$WIP_FILES" ]; then
git add -- $WIP_FILES
fi
# 5. Temp Stash
trap cleanup_and_exit INT TERM EXIT
STASH_OUT=$(git stash push --staged -m "TEMP_WCOMMIT_$BRANCH_SAFE" 2>&1)
if [ $? -ne 0 ]; then
echo "Error: Stash failed. $STASH_OUT" >&2
exit 1
fi
# Capture stash ref reliably
TEMP_STASH=$(echo "$STASH_OUT" | awk '/Saved working directory/ {print $NF}' | head -n1)
[ -z "$TEMP_STASH" ] && TEMP_STASH="stash@{0}"
# 6. Re-handle non-WIP
if [ "$ADD_ALL" = true ]; then
git add -u
elif [ -n "$RESTAGE_FILES" ]; then
git add -- $RESTAGE_FILES
RESTAGE_FILES=""
fi
git commit "${ARGS[@]}"