-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathgitAddWorktreeFromGitLogLineData.sh
More file actions
executable file
Β·47 lines (37 loc) Β· 1.58 KB
/
gitAddWorktreeFromGitLogLineData.sh
File metadata and controls
executable file
Β·47 lines (37 loc) Β· 1.58 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
#!/bin/bash
# Usage: ./gitAddWorktreeFromGitLogLineData.sh <commit-hash> <YYYY-MM-DD> <HH:MM>
# Example: ./gitAddWorktreeFromGitLogLineData.sh cb9b06026 2025-08-18 12:21
# Use git log --graph --decorate master --pretty=format:'%C(bold yellow)%h%Creset %C(bold green)%ad%Creset %C(bold blue)%an%Creset %C(bold cyan)%d%Creset %C(white)%s%Creset' --date=format:'%Y-%m-%d %H:%M'
# to get one-line git logs from branch master, wer you can snip <commit-hash> <YYYY-MM-DD> <HH:MM>
# Check argument count
if [ $# -ne 3 ]; then
echo "Usage: $0 <commit-hash> <YYYY-MM-DD> <HH:MM>"
exit 1
fi
HASH=$1
DATE=$2
TIME=$3
SHORT_HASH=$(git rev-parse --short "$HASH")
# Format date and time: YYYY-MM-DD β MM-DD, HH:MM β HH-MM
DATE_PART=$(echo "$DATE" | cut -d'-' -f2-3)
TIME_PART=$(echo "$TIME" | tr ':' '-')
# Try to find the original branch name for this commit
RAW_NAME=$(git name-rev --name-only "$HASH" 2>/dev/null)
if [ -n "$RAW_NAME" ]; then
# Clean up: remove 'remotes/origin/', 'remotes/', 'heads/' prefixes and trailing '~<n>'
BRANCH=$(echo "$RAW_NAME" | sed -E 's#^remotes/origin/##' | sed -E 's#^remotes/##' | sed -E 's#^heads/##' | sed -E 's#~[0-9]+$##')
else
BRANCH="noBranch"
fi
# Construct branch and directory name
NAME="trice_${DATE_PART}-${TIME_PART}_${SHORT_HASH}_${BRANCH}"
DIR="../${NAME}"
# Create worktree and new branch
echo "β‘οΈ Creating worktree and branch '$NAME' from commit $HASH (branches: $BRANCHES_CLEAN)..."
git worktree add -b "$NAME" "$DIR" "$HASH"
# Report result
if [ $? -eq 0 ]; then
echo "β
Worktree created at $DIR"
else
echo "β Error creating worktree"
fi