Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This git hook requires `ruby` to be installed _(already pre-installed by defaul
To install simply run the following command. This requires `curl` to be installed.

```bash
sh <(curl -s https://raw.githubusercontent.com/janniks/prepare-commit-msg/master/scripts/install.sh)
sh <(curl -s https://raw.githubusercontent.com/yrave/prepare-commit-msg/master/scripts/install.sh)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced in PR?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sh <(curl -s https://raw.githubusercontent.com/yrave/prepare-commit-msg/master/scripts/install.sh)
sh <(curl -s https://raw.githubusercontent.com/janniks/prepare-commit-msg/master/scripts/install.sh)

```

> If you choose the option to install globally, you will have to reinitialize all your git repositories. The following helpful command reinitializes all repositories found in sub-directories of the current directory.
Expand Down
86 changes: 71 additions & 15 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/bin/sh
#!/bin/bash
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for the switch to bash ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe @schnappfuesch can help 😊

Copy link

@schnappfuesch schnappfuesch Mar 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The for-loop is using an invalid bracket syntax (((...))) for sh. The bash is a sh extension that understand this format. I'm also not sure if you can use the counter in the for-loop at all (using sh).
On most linuxe the sh is no longer directly installed but a replacement, which is linked to a different shell, so depending on the outcome of this link, the command might work (e.g. on ubuntu sh is linked to dash, which will fail).

If you pipe the script into sh using the curl command, you will also get this error:
Syntax error: Bad for loop variable.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more portable solution would be

Suggested change
#!/bin/bash
#!/usr/bin/env bash

which works on all systems (including the more obscure ones like NixOS, where /bin/bash does not exist)


should_use_file_from_github_url=true
# defaults
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER="^\s*\[[\-\w]*\d\]"
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER_ONE="^\s*\[[\-\w]*\d\]"
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER_TWO="^\s*[\-\w]*\d:"
PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER="[.]*\/([\-\w]*?\-\d+)"
PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES="(Merge\sbranch\s\'|\#\sRebase\s|This\sreverts\scommit\s)"
PLACEHOLDER_LOGGING_VERBOSE="true"

GITHUB_SCRIPT_URL="https://raw.githubusercontent.com/janniks/prepare-commit-msg/master/scripts/prepare-commit-msg"
PLACEHOLDER_NEW_COMMIT_MESSAGE="[#{issue_number.upcase}] #{original_commit_message.gsub(/(\s[[:punct:]])+$/, '')}"
PLACEHOLDER_NEW_COMMIT_MESSAGE_ONE="[#{issue_number.upcase}] #{original_commit_message.gsub(/(\s[[:punct:]])+$/, '')}"
PLACEHOLDER_NEW_COMMIT_MESSAGE_TWO="#{issue_number.upcase}: #{original_commit_message.gsub(/(\s[[:punct:]])+$/, '')}"
GITHUB_SCRIPT_URL="https://raw.githubusercontent.com/yrave/prepare-commit-msg/master/scripts/prepare-commit-msg"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GITHUB_SCRIPT_URL="https://raw.githubusercontent.com/yrave/prepare-commit-msg/master/scripts/prepare-commit-msg"
GITHUB_SCRIPT_URL="https://raw.githubusercontent.com/janniks/prepare-commit-msg/master/scripts/prepare-commit-msg"


PATH_GIT_GLOBAL="${HOME}/.git-template/"
PATH_GIT_LOCAL="./.git/"
Expand Down Expand Up @@ -75,16 +80,50 @@ if [ "$answer" != "${answer#[Yy]}" ]; then
PLACEHOLDER_LOGGING_VERBOSE=false
fi

printf -- "Set a custom regex for parsing issue numbers from commit messages:\n"
printf -- " - To use default value, leave empty and press return\n\n"
printf -- "Default: ${BLUE}${PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER}${RESET}\n${BLUE}>${RESET} "
printf -- "Set a custom message style where ISSUE_NUMBER is the issue number and COMMIT_MESSAGE is the commit message.\n"
printf -- " - To use default value, leave empty and press return\n"
printf -- "Default: ${BLUE}${PLACEHOLDER_NEW_COMMIT_MESSAGE}${RESET}\n\n"
printf -- " - To use ${GREEN}[ISSUE_NUMBER] COMMIT_MESSAGE${RESET}, enter 1\n"
printf -- "1: ${BLUE}${PLACEHOLDER_NEW_COMMIT_MESSAGE_ONE}${RESET}\n\n"
printf -- " - To use ${GREEN}ISSUE_NUMBER: COMMIT_MESSAGE${RESET}, enter 2\n"
printf -- "2: ${BLUE}${PLACEHOLDER_NEW_COMMIT_MESSAGE_TWO}${RESET}\n\n${BLUE}>${RESET} "
read answer

if [ ! -z "$answer" ]; then
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER=answer
if [ "$answer" == "1" ]; then
PLACEHOLDER_NEW_COMMIT_MESSAGE=$PLACEHOLDER_NEW_COMMIT_MESSAGE_ONE
elif [ "$answer" == "2" ]; then
PLACEHOLDER_NEW_COMMIT_MESSAGE=$PLACEHOLDER_NEW_COMMIT_MESSAGE_TWO
else
PLACEHOLDER_NEW_COMMIT_MESSAGE=answer
fi
fi

clear_n_lines 5
clear_n_lines 12
printf -- " - ${BLUE}Commit message style: ${PLACEHOLDER_NEW_COMMIT_MESSAGE} ${RESET}\n\n"

printf -- "Set a custom regex for parsing issue numbers from commit messages:\n\n"
printf -- " - To use default value, leave empty and press return\n"
printf -- "Default: ${BLUE}${PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER}${RESET}\n\n"
printf -- " - To use ${GREEN}[ISSUE_NUMBER] COMMIT_MESSAGE${RESET}, enter 1\n"
printf -- "1: ${BLUE}${PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER_ONE}${RESET}\n\n"
printf -- " - To use ${GREEN}ISSUE_NUMBER: COMMIT_MESSAGE${RESET}, enter 2\n"
printf -- "2: ${BLUE}${PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER_TWO}${RESET}\n\n${BLUE}>${RESET} "
read answer

if [ ! -z "$answer" ]; then
if [ "$answer" == "1" ]; then
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER=$PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER_ONE
elif [ "$answer" == "2" ]; then
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER=$PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER_TWO
else
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER=answer
fi
fi

clear_n_lines 13
printf -- " - ${BLUE}Regex for commit message: ${PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER} ${RESET}\n\n"

printf -- "Set a custom regex for parsing issue numbers from branch names:\n"
printf -- " - To use default value, leave empty and press return\n\n"
printf -- "Default: ${BLUE}${PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER}${RESET}\n${BLUE}>${RESET} "
Expand Down Expand Up @@ -125,7 +164,7 @@ if [ "$OPTION_GLOBAL_TEMPLATE" = true ]; then
printf -- " - A global \'${BLUE}${HOOK_NAME}${RESET}\' git hook already exists. ${RED}Aborting...${RESET}\n\n"
exit
fi

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👾

HOOK_FILE="${PATH_GIT_GLOBAL}${HOOK_DIR}${HOOK_NAME}"

printf -- " - Setting global git-template...\n"
Expand All @@ -140,8 +179,14 @@ else
HOOK_FILE="${PATH_GIT_LOCAL}${HOOK_DIR}${HOOK_NAME}"
fi

printf -- " - Downloading git hook...\n"
curl -s "$GITHUB_SCRIPT_URL" > "$HOOK_FILE"
if [ "$should_use_file_from_github_url" = true ] ; then
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a very nice feature 🙌 I might also like to add this to the install script

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :) I was too lazy to add it to the advanced option and I used it only for testing

printf -- " - Downloading git hook...\n"
curl -s "$GITHUB_SCRIPT_URL" > "$HOOK_FILE"
else
printf -- " - Creating git hook file from local file\n"
BASEDIR=$(dirname $0)
cat "${BASEDIR}/prepare-commit-msg" > "$HOOK_FILE"
fi

printf -- " - Replacing placeholders...\n"
PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER=$(printf -- "$PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER" | sed 's/\\/\\\\/g')
Expand All @@ -153,11 +198,22 @@ PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER=$(printf -- "$PLACEHOLDER_REGEX_BRANCH_ISS
PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES=$(printf -- "$PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES" | sed 's/\\/\\\\/g')
printf -v PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES "%q" "$PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES"
PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES=$(printf -- "$PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES" | sed 's/\//\\\//g')
PLACEHOLDER_NEW_COMMIT_MESSAGE=$(printf -- "$PLACEHOLDER_NEW_COMMIT_MESSAGE" | sed 's/\//\\\//g')

replace_placeholder_with_value () {
if [ $(uname -s) == "Linux" ]; then
sed -i "s/${1}/${2}/g" "$HOOK_FILE"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Is this important/relevant for all Linux?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve worked on other scripts that use the sed command and it’s been relevant for all Linux user.

return
fi

sed -i '' "s/${1}/${2}/g" "$HOOK_FILE"
}

sed -i '' "s/PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER/\/${PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER}\//g" "$HOOK_FILE"
sed -i '' "s/PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER/\/${PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER}\//g" "$HOOK_FILE"
sed -i '' "s/PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES/\/${PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES}\//g" "$HOOK_FILE"
sed -i '' "s/PLACEHOLDER_LOGGING_VERBOSE/${PLACEHOLDER_LOGGING_VERBOSE}/g" "$HOOK_FILE"
replace_placeholder_with_value PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER "\/${PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER}\/"
replace_placeholder_with_value PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER "\/${PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER}\/"
replace_placeholder_with_value PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES "\/${PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES}\/"
replace_placeholder_with_value PLACEHOLDER_LOGGING_VERBOSE "${PLACEHOLDER_LOGGING_VERBOSE}"
replace_placeholder_with_value PLACEHOLDER_NEW_COMMIT_MESSAGE "${PLACEHOLDER_NEW_COMMIT_MESSAGE}"

printf -- " - Requesting permission to execute git hook...\n"
chmod a+x "$HOOK_FILE"
Expand Down
3 changes: 1 addition & 2 deletions scripts/prepare-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ $verbose_prefix = "git hook: "
regex_commit_issue_number = PLACEHOLDER_REGEX_COMMIT_ISSUE_NUMBER
regex_branch_issue_number = PLACEHOLDER_REGEX_BRANCH_ISSUE_NUMBER
regex_git_commit_messages = PLACEHOLDER_REGEX_GIT_COMMIT_MESSAGES

# colorize strings in console output
class String
def error; "\e[31m#{self}\e[0m" end
Expand Down Expand Up @@ -62,7 +61,7 @@ if !issue_number
end

# combine to new message
new_commit_message = "[#{issue_number.upcase}] #{original_commit_message.gsub(/(\s[[:punct:]])+$/, '')}"
new_commit_message = "PLACEHOLDER_NEW_COMMIT_MESSAGE"

# write new commit message to file
File.open(commit_file, 'w') do |f|
Expand Down