Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何将最新的git提交元数据自动包含到提交的文件中?_Bash_Git_Logging_Header_Changelog - Fatal编程技术网

Bash 如何将最新的git提交元数据自动包含到提交的文件中?

Bash 如何将最新的git提交元数据自动包含到提交的文件中?,bash,git,logging,header,changelog,Bash,Git,Logging,Header,Changelog,我的目标是在每个文件的顶部添加一个基本的更改日志,例如: # Changelog: # Last Modified on: 31.2.2020 # Last Modified by: Arthur Dent # Last Modification: "After a Trillian tries, it works" # File created: 01.01.1970 def whale2pot(args) .... 现在,为了不必手动执行此操作,我想包括 git l

我的目标是在每个文件的顶部添加一个基本的更改日志,例如:

# Changelog: 
# Last Modified on: 31.2.2020
# Last Modified by: Arthur Dent
# Last Modification: "After a Trillian tries, it works"
# File created: 01.01.1970

def whale2pot(args) ....
现在,为了不必手动执行此操作,我想包括

git log -1
到与此提交相关的文件。(但不确定是否包含提交消息是智能的…)

实现这一点的一种方法是通过bash脚本,该脚本将上述内容的输出预先添加到文件中。 但这会修改文件,回购协议永远不会是最新的

因此:有没有一种方法可以“重载”
git commit
,或者在git没有注意到的情况下偷偷地将其加入


提前感谢=)

在回购协议中有如此明显的合并冲突来源,可能会令人惊讶地毛骨悚然,
在浏览回购协议时,从git中提取信息非常容易(
git log-1/file

在深入研究如何在文件内容中实际存储这些信息之前, 也许您可以满足于一个方便的shell快捷方式,或者一个集成到编辑器中的大纲<例如,code>vscode有
git lens
扩展,它提供了非常接近的内容(实际上是每行注释)


创建日期将是非常静态的,因此可以在文件创建时插入并保持这种状态

对于标题的其他部分:我认为
过滤器
是最接近正确的方法

官方文件如下:

请参阅此答案中的解释:

您将编写两个脚本:

  • 一个是
    clean
    脚本,它将用常量值替换标题行(例如:
    #上次修改:
    没有日期)
  • 第二个是
    smudge
    脚本,它将运行
    git log-1
    并用所需的值填充行
转移文件时将运行
clean
脚本,并确保git中存储的blob具有恒定的头,以避免合并、重定基址等问题

签出文件时将运行
smudge
脚本,并将在工作树版本中写入正确的内容,即您将在编辑器中实际打开的磁盘上的文件



这个答案中没有分类的要点是:smudge脚本在stdin上接收文件的内容,而不是文件名,因此我还看不到从该脚本运行
git log-1 file/name
的干净方法。

经过一些修改后,我选择了这种格式(这里是针对.m文件的):

更新_changelog.sh:
我想知道预提交钩子是否会起作用。相关:你不能使用上次提交的信息,因为这会改变文件的内容,需要创建一个新的修订来保存修改。这当然是可行的,我只是没有正确的想法来做,而不把回购变成一个合并的噩梦,不过。@GregBurghardt,谢谢你介绍我认识git hooks。我将使用一个预推钩子进行尝试,因为我希望预推上一次提交的输出。我猜如果我推动多个提交,这将是一个挑战。¯_(ツ)_/““[在此处插入相关@VonC-answer的链接]”链接仍然缺失!:-)@phd你的意思是?但是别忘了使用Git 2.27+:@VonC:我在考虑一个简单的解释:)关于内容过滤器:只有设置一个长时间运行的过滤器进程(
filter.[driver].process
),才能访问这些信息,对吗?没有办法将它们插入污迹脚本?@LeGEC我通常只使用污迹/脚本,而不是长时间运行的过滤器,如《感谢大家》中所述,互联网是一个很棒的地方!
%% ------------------------------------------------  
%  
% Created on:       <date_of_creation>  
% Author:           <author>  
%  
% Last modifier:    <modifier>  
% Last modified:    <date_of_last_mod>  
% On Branch:        <branch>  
%  
%%-------------------------------------------------
#!/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;
#!/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;