经过一番修改,我选择了这种格式(这里是 .m 文件):
%% ------------------------------------------------
%
% Created on: <date_of_creation>
% Author: <author>
%
% Last modifier: <modifier>
% Last modified: <date_of_last_mod>
% On Branch: <branch>
%
%%-------------------------------------------------
...并意识到我不需要提交元数据来实现我的目标。我明白了:
- 日期来自
$(date)
- 来自
$(git config user.name)的作者/修改者
- 来自
$(git rev-parse --abbrev-ref HEAD)的当前分支
我仍然遵循 @LeGEC 的 方法,即拥有一个执行两件事的脚本,如下所示:
- insert_changelog.sh:这可确保每个具有特定扩展名的未跟踪文件都会收到更改日志。进一步填充
<date_of_creation>和<author>的静态信息。
- update_changelog.sh:此脚本更新每个跟踪和修改文件的后 3 个字段。
目前,我在运行git add <modified files>之前手动运行它。
我在下面附加代码。这是我的第一次 bash 脚本编写经验,请随时指出可以改进的地方
insert_changelog.sh:
#!/bin/bash
#If there are no .m files for which this would apply, suppress the this notification (If i get that right)
shopt -s nullglob
#Act on untracked files
files=($(git ls-files --others --exclude-standard))
for item in ${files[*]}
do
#For time being, only consider matlab files
if [[ $item == *.m ]]
then
#Check whether the header already exists
read -r first_line < $item
first_cl_line="%% ------------------------------------------------"
if [ "$first_line" = "$first_cl_line" ]
then
continue
else
#Include Changelog into file
cat changelog_template.txt > tempfile
cat $item >> tempfile
mv tempfile $item
#Fill in static fields of inception date and author
sed -i "3,4d" $item
sed -i "2 a % Created on: $(date)" $item
sed -i "3 a % Author: $(git config user.name)" $item
#Update current changelog
./update_changelog.sh $item
fi
fi
done;
update_changelog.sh:
#!/bin/bash
USER=$(git config user.name)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
#Remove outdated lines and replace with updated ones.
for item in $(git ls-files -m)
do
sed -i "5,8d" $item
sed -i "5 a % Last Modifier: $USER" $item
sed -i "6 a % Last Modified: $(date)" $item
sed -i "7 a % On Branch: $BRANCH" $item
sed -i "8 a %" $item
done;