Last active
February 9, 2023 17:17
-
-
Save nunogrl/ea8f1d04e869d0e56c3e37e462b958d7 to your computer and use it in GitHub Desktop.
gitconfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
| |
import os | |
import re | |
import subprocess | |
import sys | |
| |
| |
def clean(lines, strip_empty_line=False): | |
cleaned_lines = [] | |
| |
for line in lines: | |
if line.startswith("# Please enter the commit message for your changes"): | |
break | |
| |
if add_ticket_number_to_body and line.startswith(body_ticket_section_name): | |
break | |
| |
cleaned_lines.append(line.strip("\n")) | |
| |
if ( | |
strip_empty_line and len(cleaned_lines) and cleaned_lines[-1] in ("\n", "") | |
): # NOQA | |
cleaned_lines.pop(-1) | |
| |
return "\n".join(cleaned_lines) | |
| |
| |
def to_bool(val): | |
return str(val).lower() in ("1", "t", "true") | |
| |
| |
add_ticket_number_to_subject = to_bool( | |
os.environ.get("GIT_ADD_TICKET_NUMBER_TO_SUBJECT", False)) | |
add_ticket_number_to_body = to_bool( | |
os.environ.get("GIT_ADD_TICKET_NUMBER_TO_BODY", False)) | |
| |
if not add_ticket_number_to_body and not add_ticket_number_to_subject: | |
sys.exit(0) | |
| |
tickets_pattern = [ | |
"AP-[0-9]+", | |
"WEB-[0-9]+", | |
"IR-[0-9]+", | |
"IRCR-[0-9]+", | |
"OP-[0-9]+", | |
] | |
body_ticket_section_name = "JiraTickets" | |
branch = subprocess.check_output( | |
"git rev-parse --abbrev-ref HEAD 2>/dev/null", shell=True).decode() | |
# use for debugging: | |
# branch = "ircr-2233-web-123-testing-my-git-hook" | |
commit_message_file = sys.argv[1] | |
# use for debugging: | |
# commit_message_file = "my-commit-message" | |
| |
with open(commit_message_file) as fpl: | |
raw_msg = fpl.readlines() | |
| |
subject, separator, body = clean(raw_msg[:1]), clean(raw_msg[1:2]), clean( | |
raw_msg[2:], True) | |
| |
if not branch or not commit_message_file: | |
sys.exit(1) | |
| |
if not subject and not separator and not body: | |
sys.exit(1) | |
| |
branch_pattern = r"|".join(tickets_pattern) | |
subject_pattern = r"(\[(" + r"|".join(tickets_pattern) + r")\])" | |
| |
tickets = re.findall(branch_pattern, branch, re.IGNORECASE) | |
subject = re.sub(subject_pattern, "", subject, re.IGNORECASE).strip() | |
| |
if add_ticket_number_to_subject: | |
new_subject = "" | |
| |
for ticket in tickets: | |
new_subject += f"[{ticket.upper()}]" | |
| |
subject = f"{new_subject} {subject}" | |
| |
if add_ticket_number_to_body: | |
body += f"\n\n{body_ticket_section_name}:" | |
| |
for ticket in tickets: | |
body += f"\n - {ticket.upper()}" | |
| |
with open(commit_message_file, "w") as fpl: | |
fpl.write(f"{subject}\n") | |
fpl.write(f"{separator}\n") | |
fpl.write(f"{body}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# DEBUG=1 | |
echo "Parsing with myo hooks" | |
if [ ${DEBUG:-} ] ; then | |
echo "[DEBUG MODE]" | |
fi | |
if [ "$(uname -s)"="Linux" ]; then | |
SED=$(which sed) | |
else | |
hash gsed &> /dev/null || echo "gnu-sed is missing (🍺)" | |
SED=$(which gsed) | |
fi | |
# Get current branch | |
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
if [ "$?" -gt "0" ]; then | |
exit 0 | |
fi | |
# Get file with current commit message | |
COMMIT_MESSAGE_FILE=$1 | |
if [ "$#" -ne "1" ] && [ ! -f "$COMMIT_MESSAGE_FILE" ] ; then | |
exit 1 | |
fi | |
COMMIT_SUBJECT=$(head -n1 $COMMIT_MESSAGE_FILE) | |
[ ${#COMMIT_SUBJECT} -eq 0 ] && exit 1 | |
# find tickets on the body | |
tickets=$(( | |
cat $COMMIT_MESSAGE_FILE | egrep -ioE "dops-[0-9]+|gdw-[0-9]+|op-[0-9]+|web-[0-9]+|ir-[0-9]+" | |
echo $BRANCH | awk -F/ '{print $NF}' | grep -ioE "((ir|web|op|dops|gdw)-[0-9]+)" | |
) | awk '{print toupper($1)}' | sort -u | sed "s/^./ - &/g" ) | |
# delete tickets section | |
${SED} -i -- '/.*JIRATickets.*/{s///;q;}' $COMMIT_MESSAGE_FILE | |
${SED} -i -- '${/^$/d;}' $COMMIT_MESSAGE_FILE | |
if [ ! -z "$tickets" ] ; then | |
cat <<- EOF >> $COMMIT_MESSAGE_FILE | |
JIRATickets: | |
$tickets | |
EOF | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[includeIf "gitdir:~/src/"] | |
path = ~/.gitconfig-work | |
[includeIf "gitdir:~/.password-store/*"] | |
path = ~/.gitconfig-work | |
[includeIf "gitdir:~/src/sandbox/*"] | |
path = ~/.gitconfig-personal | |
[includeIf "gitdir:~/sandbox/*"] | |
path = ~/.gitconfig-personal | |
[includeIf "gitdir:~/Documents/"] | |
path = ~/.gitconfig-personal | |
[includeIf "gitdir:~/Documents/personal/publish-instagram/*"] | |
path = ~/.gitconfig-personal | |
[commit] | |
gpgsign = true | |
[alias] | |
lg1 = log \ | |
--graph \ | |
--abbrev-commit \ | |
--decorate \ | |
--format=format:'%C(bold blue)%h%C(reset) - \ | |
%C(bold green)(%ar)%C(reset) \ | |
%C(white)%s%C(reset) %C(dim white)- \ | |
%an%C(reset)%C(bold yellow)%d%C(reset)' \ | |
--all | |
lg2 = log \ | |
--graph \ | |
--abbrev-commit \ | |
--decorate \ | |
--format=format:'%C(bold blue)%h%C(reset) - \ | |
%C(bold cyan)%aD%C(reset) \ | |
%C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''\ | |
%C(white)%s%C(reset) %C(dim white)- %an%C(reset)' \ | |
--all | |
lg = !"git lg1" | |
lgs = "!f() { \ | |
git log --all --color --graph --pretty=format:'%C(bold yellow)<sig>%G?</sig>%C(reset) %C(red)%h%C(reset) -%C(yellow)%d%C(reset) %s %C(green)(%cr) %C(blue)<%an>%C(reset)' | \ | |
sed \ | |
-e 's#<sig>G</sig>#Good#' \ | |
-e 's#<sig>B</sig>#\\nBAD \\nBAD \\nBAD \\nBAD \\nBAD#' \ | |
-e 's#<sig>U</sig>#Unknown#' \ | |
-e 's#<sig>X</sig>#Expired#' \ | |
-e 's#<sig>Y</sig>#Expired Key#' \ | |
-e 's#<sig>R</sig>#Revoked#' \ | |
-e 's#<sig>E</sig>#Missing Key#' \ | |
-e 's#<sig>N</sig>#None#' | \ | |
less -r; \ | |
}; f" | |
diffc = diff --color-words=. | |
# diff tools (prior to merge) | |
meld = difftool --tool=meld -y | |
meldd = difftool --dir-diff --tool=meld | |
meldbase = !git meld $(git merge-base origin/master HEAD) | |
review = !git fetch $1 $2 && git checkout FETCH_HEAD && git meldbase && true | |
[core] | |
editor = vim | |
[merge] | |
tool = meld | |
[filter "lfs"] | |
required = true | |
clean = git-lfs clean -- %f | |
smudge = git-lfs smudge -- %f | |
process = git-lfs filter-process |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[user] | |
name = Nuno Leitao | |
email = [email protected] | |
signingkey = A528ACE22DF6A908 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[user] | |
name = Nuno Leitao | |
email = [email protected] | |
signingkey = A528ACE22DF6A908 | |
[core] | |
hooksPath = ~/work/git-utils/hooks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment