Skip to content

Commit 72e2108

Browse files
committed
Автоматическая генерация сообщений к коммитам с информацией о затронутых файлах.
1 parent 35c46ac commit 72e2108

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

REFERENCES.md

+25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Git
22

3+
- [Git - Viewing the Commit History](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History)
34
- [Git - git-status Documentation](https://git-scm.com/docs/git-status)
45
- [Git - git-remote Documentation](https://git-scm.com/docs/git-remote)
56
- [Git - git-push Documentation](https://git-scm.com/docs/git-push)
67
- [Git - git-rev-parse Documentation](https://git-scm.com/docs/git-rev-parse)
78
- [Git - git-merge-base Documentation](https://git-scm.com/docs/git-merge-base)
9+
- [Git - git-filter-branch Documentation](https://git-scm.com/docs/git-filter-branch)
810

911
- [version control - git and empty folders - Stack Overflow](https://stackoverflow.com/questions/1767165/git-and-empty-folders)
1012
- [What does the term "porcelain" mean in Git? - Stack Overflow](https://stackoverflow.com/questions/6976473/what-does-the-term-porcelain-mean-in-git)
@@ -19,6 +21,13 @@
1921
- [What does git rev-parse do? - Stack Overflow](https://stackoverflow.com/questions/15798862/what-does-git-rev-parse-do)
2022
- [Make an existing Git branch track a remote branch? - Stack Overflow](https://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch)
2123
- [git branch - Git: How do I list only local branches? - Stack Overflow](https://stackoverflow.com/questions/12370714/git-how-do-i-list-only-local-branches)
24+
- [Git: want to iterate through all commits on branch, and list files in each commit - Stack Overflow](https://stackoverflow.com/questions/5953767/git-want-to-iterate-through-all-commits-on-branch-and-list-files-in-each-commi)
25+
- [Git: How to find a deleted file in the project commit history? - Stack Overflow](https://stackoverflow.com/questions/7203515/git-how-to-find-a-deleted-file-in-the-project-commit-history)
26+
- [git - How to list all the files in a commit? - Stack Overflow](https://stackoverflow.com/questions/424071/how-to-list-all-the-files-in-a-commit)
27+
- [How to Batch Update Git Commit Messages](https://davidwalsh.name/update-git-commit-messages)
28+
- [Rewriting Git Commit Messages](http://www.burtonini.com/blog/2018/03/06/rewriting-git-commit-messages/)
29+
- [Edit an old commit message with git filter-branch - Stack Overflow](https://stackoverflow.com/questions/43603080/edit-an-old-commit-message-with-git-filter-branch)
30+
- [git commit - Purging file from Git repo failed, unable to create new backup - Stack Overflow](https://stackoverflow.com/questions/6403601/purging-file-from-git-repo-failed-unable-to-create-new-backup)
2231

2332
# bash
2433

@@ -36,3 +45,19 @@
3645
- [Bash Shell Find Out If a Variable Is Empty Or Not - nixCraft](https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/)
3746
- [bash - Test if a command outputs an empty string - Stack Overflow](https://stackoverflow.com/questions/12137431/test-if-a-command-outputs-an-empty-string)
3847
- [linux - How to compare strings in Bash - Stack Overflow](https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash)
48+
- [awk - print columns with text and spaces | Unix Linux Forums | Shell Programming and Scripting](https://www.unix.com/shell-programming-and-scripting/245398-awk-print-columns-text-spaces.html)
49+
- [bash case statements evaluate to strings - Stack Overflow](https://stackoverflow.com/questions/2283640/bash-case-statements-evaluate-to-strings)
50+
- [Introduction to Bash arrays | Opensource.com](https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays)
51+
- [shell - How to concatenate string variables in Bash - Stack Overflow](https://stackoverflow.com/questions/4181703/how-to-concatenate-string-variables-in-bash)
52+
- [Bash array usage cheatsheet.](https://gist.github.com/magnetikonline/0ca47c893de6a380c87e4bdad6ae5cf7)
53+
- [bash - Create array in loop from number of arguments - Stack Overflow](https://stackoverflow.com/questions/15420790/create-array-in-loop-from-number-of-arguments)
54+
- [Is there a way of reading the last element of an array with bash? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/198787/is-there-a-way-of-reading-the-last-element-of-an-array-with-bash)
55+
- [The weird, wondrous world of Bash arrays – Robert Aboukhalil – Medium](https://medium.com/@robaboukhalil/the-weird-wondrous-world-of-bash-arrays-a86e5adf2c69)
56+
- [bash - readarray (or pipe) issue - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/136206/readarray-or-pipe-issue)
57+
- [Reading output of a command into an array in Bash - Stack Overflow](https://stackoverflow.com/questions/11426529/reading-output-of-a-command-into-an-array-in-bash/32931403)
58+
- [How do you process the output of a command in the shell line-by-line? - Stack Overflow](https://stackoverflow.com/questions/3113005/how-do-you-process-the-output-of-a-command-in-the-shell-line-by-line)
59+
- [bash: convert '\n' delimited strings into array - Stack Overflow](https://stackoverflow.com/questions/24628076/bash-convert-n-delimited-strings-into-array)
60+
- [bash - Are <=,>=, <,>, ==, !=, &&, and || used for arithmetic expressions or conditional expressions? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/270591/are-and-used-for-arithmetic-expressions-or-conditiona)
61+
- [A bash function that takes argument like other languages? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/122632/a-bash-function-that-takes-argument-like-other-languages)
62+
- [Checking for empty string in Bash « timmurphy.org](https://timmurphy.org/2010/05/19/checking-for-empty-string-in-bash/)
63+
- [How to use arithmetic in an if statement for Bash - Stack Overflow](https://stackoverflow.com/questions/43443159/how-to-use-arithmetic-in-an-if-statement-for-bash)

commit-msg-generator.sh

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/bin/bash
2+
3+
#set -x # echo on
4+
5+
# Usage:
6+
# $ ./commit-msg-generator.sh -p=~/repo
7+
# $ ./commit-msg-generator.sh --projects=~/repo
8+
# $ ./commit-msg-generator.sh --default
9+
10+
function buildFilesListString {
11+
local -n files_list=$1
12+
local len=${#files_list[@]}
13+
for (( i=0; i<len; i++ )); do
14+
file_name="${files_list[$i]}"
15+
if (( i < len-1 )); then
16+
echo -n "'$file_name', "
17+
else
18+
echo -n "'$file_name'"
19+
fi
20+
done
21+
}
22+
23+
projects_dir=.
24+
25+
for i in "$@"; do
26+
case $i in
27+
-p=*|--projects=*)
28+
projects_dir="${i#*=}"
29+
eval projects_dir=$projects_dir
30+
shift # past argument=value
31+
;;
32+
--default)
33+
projects_dir=.
34+
shift # past argument with no value
35+
;;
36+
*)
37+
# unknown option
38+
;;
39+
esac
40+
done
41+
42+
cd "$projects_dir"
43+
pwd
44+
45+
git for-each-ref --format='%(refname:short)' refs/heads/ | while read branch_name; do
46+
#echo "branch_name = $branch_name"
47+
48+
for commit_id in $(git rev-list "$branch_name"); do
49+
#echo "commit_id = $commit_id"
50+
51+
declare -a all_files=()
52+
53+
SAVEIFS=$IFS # Save current IFS
54+
IFS=$'\n' # Change IFS to new line.
55+
all_files=( $(git show --pretty="" --name-status "$commit_id") )
56+
IFS=$SAVEIFS # Restore IFS
57+
58+
59+
declare -a all=()
60+
declare -a added=()
61+
declare -a modified=()
62+
declare -a deleted=()
63+
64+
for (( i=0; i<${#all_files[@]}; i++ )); do
65+
file_name_and_status="${all_files[$i]}"
66+
67+
#echo "file_name_and_status = $file_name_and_status"
68+
file_status=$(echo "$file_name_and_status" | awk -F"\t" '{ print $1 }')
69+
file_name=$(echo "$file_name_and_status" | awk -F"\t" '{ print $2 }')
70+
#echo "file_name = $file_name, file_status = $file_status"
71+
72+
all+=( "$file_name" )
73+
74+
case "$file_status" in
75+
('A')
76+
added+=( "$file_name" )
77+
#echo "added[-1] = ${added[-1]}"
78+
#echo "added = ${added[@]}"
79+
;;
80+
('M')
81+
modified+=( "$file_name" )
82+
#echo "modified[-1] = ${modified[-1]}"
83+
#echo "modified = ${modified[@]}"
84+
;;
85+
('D')
86+
deleted+=( "$file_name" )
87+
#echo "deleted[-1] = ${deleted[-1]}"
88+
#echo "deleted = ${deleted[@]}"
89+
;;
90+
esac
91+
92+
done
93+
94+
commit_message=""
95+
if [ -n "$all" ]; then
96+
commit_message="FILES: $(buildFilesListString all).\n"
97+
fi
98+
99+
if [ -n "$added" ]; then
100+
if [ -n "$commit_message" ]; then commit_message="${commit_message}\n"; fi
101+
commit_message="${commit_message}ADDED: $(buildFilesListString added)."
102+
fi
103+
104+
if [ -n "$modified" ]; then
105+
if [ -n "$commit_message" ]; then commit_message="${commit_message}\n"; fi
106+
commit_message="${commit_message}MODIFIED: $(buildFilesListString modified)."
107+
fi
108+
109+
if [ -n "$deleted" ]; then
110+
if [ -n "$commit_message" ]; then commit_message="${commit_message}\n"; fi
111+
commit_message="${commit_message}DELETED: $(buildFilesListString deleted)."
112+
fi
113+
114+
#echo "$commit_message"
115+
#echo "//---------------------------------------------------------------------"
116+
117+
git filter-branch -f --msg-filter \
118+
"if test \$GIT_COMMIT = '$commit_id'
119+
then
120+
echo \"$commit_message\"; else cat
121+
fi"
122+
123+
done
124+
125+
done

0 commit comments

Comments
 (0)