-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-commit
More file actions
executable file
·56 lines (41 loc) · 1.41 KB
/
Copy pathmarkdown-commit
File metadata and controls
executable file
·56 lines (41 loc) · 1.41 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
#!/usr/bin/env bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
FILE_NAME="$1"
function echoerr {
printf "%s\n" "$*" >&2;
}
# Get list of changed files (exclude deleted files, remove status and quotes)
function get_changed_files {
git status --porcelain | grep -v "^ D" | sed 's/^...//' | sed 's/"//g'
}
# If no file specified, select from changed files using fzf
if [[ -z "$FILE_NAME" ]]; then
echoerr "Changing directory to \"$REPO_ROOT\""
cd "$REPO_ROOT"
CHANGED_FILES=$(get_changed_files)
if [[ -z "$CHANGED_FILES" ]]; then
echoerr "No changed files found"
exit 100
fi
FILE_NAME=$(echo "$CHANGED_FILES" | fzf --prompt="Select file to commit: ")
if [[ -z "$FILE_NAME" ]]; then
echoerr "No file selected"
exit 150
fi
fi
# Get file name without path and extension
FILE_NAME_SHORT=$(basename "$FILE_NAME" | sed 's/\.[^.]*$//')
# Get headers from markdown-headers-added script
HEADERS=$("$SCRIPT_DIR/markdown-headers-added" --short "$FILE_NAME")
if [[ -z "$HEADERS" ]]; then
echoerr "No added headers found in $FILE_NAME"
exit 200
fi
# Convert newlines to ", " to create comma-separated list
HEADERS_ONELINE=$(echo "$HEADERS" | paste -sd ';' | sed 's/;/; /g')
# Create commit message
COMMIT_MSG="${FILE_NAME_SHORT}: ${HEADERS_ONELINE}"
# Stage and commit
git add "$FILE_NAME"
git commit -m "$COMMIT_MSG"