Skip to content

Commit a5e97ab

Browse files
committed
feat: Update scripts in shell
1 parent 4b5c542 commit a5e97ab

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

shell/count-files.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#!/usr/bin/env bash
22
# Proof of concept script to use Bash to produce report of file counts from git status.
33
#
4-
# Run this as a standalone report, not as a hook. See simple-hook.sh script for how CHANGES
4+
# Run this as a standalone report, not as a hook. If you use this as a hook, it will
5+
# print output in the console only not add to commit message
6+
#
7+
# See simple-hook.sh script for how CHANGES
58
# would get written out to a commit message file.
69
#
7-
# This will get count of files added, modified, deleted and renamed/moved and output it.
10+
# This script will get count of files added, modified, deleted and renamed/moved and output it.
811
# Note that modified and renamed/moved can both apply to the same file.
912
# There also other states like C for copied and ?? for untracked.
1013

@@ -16,7 +19,7 @@
1619
# Status summary excluding untracked files using a flag.
1720
CHANGES=$(git status -s -uno --porcelain)
1821

19-
# Note quotes and -n to preserve newlines so line count or grep for start of lines works.
22+
# Note quotes AND -n to preserve newlines so line count for grep, for start of lines works.
2023
FILES=$(echo -n "$CHANGES" | wc -l)
2124

2225
echo "All files changed: $FILES"

shell/sample.sh

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
#!/bin/sh
22

3-
# This script is based on the prepare-commit-msg.sample file in a git repo.
3+
# This script is based on the prepare-commit-msg.sample file that comes by default in any git repo when you run git init or git clone (you can also run git init later on to restore hooks).
4+
# Note everything here is uncommented but you're only meant to run one of the 3 sections after setting up vars.
45

6+
# e.g. '.git/COMMIT_EDITMSG'
57
COMMIT_MSG_FILE=$1
8+
# e.g. 'message' or 'template'
69
COMMIT_SOURCE=$2
10+
# A hash, if relevant.
711
SHA1=$3
812

13+
###
14+
915
# Remove line.
1016
/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"
1117

12-
LINES=$(git status -s -uno --porcelain | wc -l)
13-
# Write over
14-
# echo "Files changed: $LINES" > $COMMIT_MSG_FILE
18+
###
1519

1620
# Show files change.
17-
# (Uncomments files changed in the message)
21+
# (It will uncomment files changed in the message - which is maybe less efficient than just running git status but this is what git comes with.)
1822
#
19-
# case "$COMMIT_SOURCE,$SHA1" in
20-
# ,|template,)
21-
# /usr/bin/perl -i.bak -pe '
22-
# print "\n" . `git diff --cached --name-status -r`
23-
# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;;
24-
# *) ;;
25-
# esac
23+
case "$COMMIT_SOURCE,$SHA1" in
24+
, | template,)
25+
/usr/bin/perl -i.bak -pe '
26+
print "\n" . `git diff --cached --name-status -r`
27+
if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE"
28+
;;
29+
*) ;;
30+
esac

shell/simple-hook.sh

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/bin/sh
22
# A minimal git commit hook.
3+
# Usage:
4+
# ./simple-hook.sh MSG SRC SHA [-d]
5+
# The last part is an optional non-standard flag, allowing this to be run directly without writing out.
36

7+
set -e
8+
9+
# These are the 3 standard prepare-commit-msg args as per the hooks example and git hook docs.
410
COMMIT_MSG_FILE=$1
511
COMMIT_SOURCE=$2
612
SHA1=$3
@@ -12,4 +18,26 @@ echo "COMMIT_MSG_FILE = $COMMIT_MSG_FILE"
1218
echo "COMMIT_SOURCE = $COMMIT_SOURCE"
1319
echo "SHA1 = $SHA1"
1420

15-
echo -n "This is a message inserted before your commit message\n$CHANGES" >$COMMIT_MSG_FILE
21+
MESSAGE="This is a message inserted before your commit message
22+
$CHANGES"
23+
24+
if [ "$4" = '-d' ]; then
25+
echo '\nDRY RUN'
26+
echo "$MESSAGE"
27+
else
28+
# Note that -n flag is not needed here on echo.
29+
echo "$MESSAGE" >$COMMIT_MSG_FILE
30+
fi
31+
32+
# There is a weird bug where the space before the first line goes missing, but not in the dry run output.
33+
#
34+
# This is a message inserted before your commit message
35+
# M docs/installation.md
36+
# M shell/count-files.sh
37+
# M shell/simple-hook.sh
38+
39+
# Even weird in the logs
40+
# This is a message inserted before your commit message
41+
# M docs/installation.md
42+
# M shell/count-files.sh
43+
# M shell/simple-hook.sh

0 commit comments

Comments
 (0)