你可以在git commit期间更改文件内容吗? [英] Can you change a file content during git commit?

查看:150
本文介绍了你可以在git commit期间更改文件内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在GitHub上打开小说中保留的内容之一是单词列表我想自动设置第一行,这是多少词典中的词。我的第一个选择是编写一个预先提交的钩子来读取文件,计算单词,重写第一行并将其重新写回。这是代码

  PRE_COMMIT {
my($ git)= @_;
my $ branch = $ git->命令(qw / rev-parse --abbrev-ref HEAD /);
表示$ branch中的预提交钩子;
if($ branch =〜/ master /){
my $ changed = $ git->命令(qw / show --name-status /);
my @changed_files =($ changed =〜/ \s\w\s +(\S +)/ g);
if($ words ~~ @changed_files){
my @words_content = read_file($ words);
说我有$#words_content words;
$ words_content [0] =$#words_content\\\
;
write_file($ words,@words_content);
}
}
};

但是,由于档案已经上架,我得到这个错误


错误:您对本地文件的更改将被
覆盖:
text / words.dic
请提交您的更改或储存
,然后才能切换分支机构。
正在中止

不妨将它作为post-commit挂钩并更改为下一次提交?或者做一些完全不同的事情?一般的问题是:如果您想在提交期间处理和更改文件的内容,那么执行该操作的正确方法是什么? 解决方案

实际提交被 git commit 所困住的是一旦预先提交钩子完成后,索引中的任何内容。这意味着只要 git add 也可以在预提交钩子中更​​改文件。



下面是我的示例pre-commit hook,从.sample修改过来:

 #!/ bin / sh 

#一个用于验证即将提交的示例钩子脚本。
#[先前剪下的大部分内容,添加了这个 -
#确保你删除了git diff-index的exec!]

num = $ (cat zorg)
num = $(expr 0 $ num + 1)
echo $ num> zorg
git add zorg
echoupdated zorg to $ num
exit 0

然后:

  $ git commit -m dink 
更新zorg为3
[master 76eeefc] dink
1个文件已更改,1个插入(+),1个删除( - )

但请注意一个小缺陷(不适用于您的案例):

  $ git commit 
git提交
更新zorg到4
#在分支大师
#未经记录的文件:
[snip]
没有添加提交但未跟踪的文件存在(使用git add跟踪)
$ git commit
更新zorg为5
#请输入提交信息以进行更改。从
开始的行[snip - 我不用改变任何东西就退出编辑器]
由于空提交消息而中止提交。
$ git commit
更新zorg为6
#请输入提交信息以进行更改。从
开始的行

基本上,因为预提交钩子更新和 git add s,即使我没有实际执行提交,文件也会不断增加,在这里。


One of the things I keep in my open novel in GitHub is a list of words I would like to set automatically the first line, which is the number of words in the dictionary. My first option is to write a pre-commit hook that reads the file, counts the words, rewrites the first line and writes it back again. Here's the code

PRE_COMMIT {
  my ($git) = @_;
  my $branch =  $git->command(qw/rev-parse --abbrev-ref HEAD/);
  say "Pre-commit hook in $branch";
  if ( $branch =~ /master/ ) {
     my $changed = $git->command(qw/show --name-status/);
     my @changed_files = ($changed =~ /\s\w\s+(\S+)/g);
     if ( $words ~~ @changed_files ) {
       my @words_content = read_file( $words );
       say "I have $#words_content words";
       $words_content[0] = "$#words_content\n";
       write_file( $words, @words_content );
     }
   }
};

However, since the file has already been staged, I get this error

error: Your local changes to the following files would be overwritten by checkout: text/words.dic Please, commit your changes or stash them before you can switch branches. Aborting

Might it be better to do it as a post-commit hook and have it changed for the next commit? Or do something completely different altogether? The general question is: if you want to process and change the contents of a file during commit, what's the proper way of doing it?

解决方案

The actual commit stuck in by git commit is whatever is in the index once the pre-commit hook finishes. This means that you can change files in the pre-commit hook, as long as you git add them too.

Here's my example pre-commit hook, modified from the .sample:

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# [snipped much of what used to be in it, added this --
#  make sure you take out the exec of git diff-index!]

num=$(cat zorg)
num=$(expr 0$num + 1)
echo $num > zorg
git add zorg
echo "updated zorg to $num"
exit 0

and then:

$ git commit -m dink
updated zorg to 3
[master 76eeefc] dink
 1 file changed, 1 insertion(+), 1 deletion(-)

But note a minor flaw (won't apply to your case):

$ git commit
git commit
updated zorg to 4
# On branch master
# Untracked files:
[snip]
nothing added to commit but untracked files present (use "git add" to track)
$ git commit
updated zorg to 5
# Please enter the commit message for your changes. Lines starting
[snip - I quit editor without changing anything]
Aborting commit due to empty commit message.
$ git commit
updated zorg to 6
# Please enter the commit message for your changes. Lines starting

Basically, because the pre-commit hook updates and git adds, the file keeps incrementing even though I'm not actually doing the commit, here.

这篇关于你可以在git commit期间更改文件内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆