Skip to content

Implement file name and line number formatting #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ Output in Git commit message
```
# Commit title goes here

- Added a parameter to helloWorld function
- Concatenated strings
- [foo.js#1] Added a parameter to helloWorld function
- [foo.js#3] Concatenated strings
# Changes to be committed:
# modified: foo.js
#
Expand Down Expand Up @@ -96,7 +96,7 @@ Contributions to improve simplicity/resolve compatibility would be preferred. If

**TODO**

- [ ] Add filename and line number to bulleted commit commets - [suggestion by
- [x] Add filename and line number to bulleted commit commets - [suggestion by
joncalhoun](https://news.ycombinator.com/item?id=10904142) on HN
- Create more robust regular expression for validating comment syntax
- [ ] Check for multiline block comments
Expand Down
13 changes: 12 additions & 1 deletion prepare-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# Output (in commit message):
# - Added new link parsing funciton

# Arg 1: SHOW_LINES (T/F)
SHOW_LINES=true

IGNORED=( ".ccignore" )

# If the script is deployed to the .git directory,
Expand All @@ -31,8 +34,10 @@ fi

if which pcregrep > /dev/null; then
grep_search="pcregrep -o2 '((?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?)([^\r\n*\/]+)'"
N_grep_search="pcregrep -o2 -n '((?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?)([^\r\n*\/]+)'"
else
grep_search="grep -Po '(?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?\K([^\r\n*\/]+)'"
N_grep_search="grep -Po -n '(?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?\K([^\r\n*\/]+)'"
fi

IFS=$'\n' ADDED_FILES=( $(git ls-files) )
Expand All @@ -48,13 +53,19 @@ done

comments=""
for fn in "${ADDED_FILES[@]}"; do
temp=$( cat "$fn" | eval "$grep_search" | tr -s '[:space:]' )
if [ "$SHOW_LINES" = true ]; then
temp=$(cat "$fn" | eval "$N_grep_search" | tr -s '[:space:]' | sed 's/[[:blank:]]*$//' | awk -v fname=$fn -F':' '{ $1 ="[" fname "#" $1 "]"; print}')
else
temp=$( cat "$fn" | eval "$grep_search" | tr -s '[:space:]' | sed 's/[[:blank:]]*$//' )
fi
if [[ "$temp" != "" ]]; then
comments+="$temp"
comments+="\n"
fi
done

comments=$(echo -en "$comments" | sed -e 's/^/- /')

grep -qs "^$comments" "$1"
if [ $? -eq 1 ]; then
if [[ "$comments" == "" ]]; then
Expand Down
21 changes: 10 additions & 11 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# Output (in commit message):
# - Added new link parsing funciton

# Arg 1: SHOW_LINES (T/F)
SHOW_LINES=$1

IGNORED=( ".ccignore" )

# If the script is deployed to the .git directory,
Expand All @@ -23,16 +26,12 @@ if echo "$DIR" | grep -qs 'git'; then
cd "$( echo "$DIR" | grep -Po '^.*(?=(\.git))' )"
fi

if [ -f ".ccignore" ]; then
while IFS='' read -r line || [[ -n "$line" ]]; do
IGNORED+=( "$line" )
done < ".ccignore"
fi

if which pcregrep > /dev/null; then
grep_search="pcregrep -o2 '((?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?)([^\r\n*\/]+)'"
N_grep_search="pcregrep -o2 -n '((?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?)([^\r\n*\/]+)'"
else
grep_search="grep -Po '(?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?\K([^\r\n*\/]+)'"
N_grep_search="grep -Po -n '(?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?\K([^\r\n*\/]+)'"
fi

IFS=$'\n' ADDED_FILES=( $(git ls-files) )
Expand All @@ -49,15 +48,15 @@ done

comments=""
for fn in "${ADDED_FILES[@]}"; do
temp=$( cat "$fn" | eval "$grep_search" | tr -s '[:space:]' )
if [ "$SHOW_LINES" = true ]; then
temp=$(cat "$fn" | eval "$N_grep_search" | tr -s '[:space:]' | sed 's/[[:blank:]]*$//' | awk -v fname=$fn -F':' '{ $1 ="[" fname "#" $1 "]"; print}')
else
temp=$( cat "$fn" | eval "$grep_search" | tr -s '[:space:]' | sed 's/[[:blank:]]*$//' )
fi
if [[ "$temp" != "" ]]; then
comments+="$temp"
comments+="\n"
fi
# Uncomment sed command to remove '// @commit' comments in all files
# sed -ri 's/(([#;*]|\/{1,2})?(\s+)?@commit([-:.\s]+)?)([^\r\n*\/]+)//' "$fn" # GNU sed compatible only
# Uncomment sed command to replace multiple empty lines with a single empty line
# sed '/^$/N;/^\n$/D' "$fn" # GNU sed compatible only
done
comments=$(echo -en "$comments" | sed -e 's/^/- /')
echo -n "$comments"