使用过滤器smudge / clean的git属性通过shell脚本处理.cpp文件,但未找到文件名 [英] git attribute using filter smudge/clean to process .cpp files through shell script not finding filename

查看:174
本文介绍了使用过滤器smudge / clean的git属性通过shell脚本处理.cpp文件,但未找到文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用小脚本(名为codeformat)来执行此操作,(也尝试用$ 1重新设置%f)

 #! / bin / bash 
clang-format -style = file%f |差异%f - **
如果[$? -ne 0];然后
回显错误:代码格式不正确
出口1
fi



使用* .cpp filter = codeformat,

  git config  - 全局过滤器设置了git config并更新了.gitattributes。 codeformat.clean codeformat 
git config --global filter.codeformat.smudge codeformat

看起来像脚本正在运行,但它没有获取文件名。为什么会这样?

解决方案

您有%f 错误的地方。



gitattributes文档(搜索%f ):


过滤器命令行上的序列%f被替换为过滤器正在处理的文件的名称。过滤器可能会在关键字替换中使用它。例如:

  [filterp4] 
clean = git-p4-filter --clean%f
smudge = git-p4-filter --smudge%f




<因此,要获取文件的路径名,您需要将 filter.codeformat.clean 设置为 codeformat%f



此时,您还需要修改bash脚本,因为参数替换的语法确实是 $ 1 。但是,请阅读gitattributes文档中的下一段:


请注意,%f是正在工作的路径的名称上。 根据正在过滤的版本,磁盘上的相应文件可能不存在,或者可能有不同的内容。因此,smudge和clean命令不应尝试访问磁盘上的文件,而只会执行作为标准输入提供给他们的内容的过滤器。

这里强调的是我的,但这是告诉你,你不能打开磁盘文件并读取它。您必须只过滤标准输入,并提供标准输出。



事实证明,clang-format的设计目的就是为了达到这个目的。然而,差异不是。)






编辑添加实例:

  $ cat〜/ scripts / dotest 
#! / bin / sh
echodotest run with $#arguments:>> / tmp / dotest_log
for i do
printf'%s\\\
'$ i
完成>> / tmp / dotest_log
cat
$这些dotest
[路径编辑] / scripts / dotest
$ cat .git / config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[filtertestfilter]
clean = dotest%f
smudge = dotest%f
$ cat .gitattributes
* .test filter = testfilter
$ echo'bar.test'> bar.test
$ git add bar.test
$ cat / tmp / dotest_log
使用1个参数运行:
bar.test


I wanted to setup code style formatting for all .cpp files before they are committed to git.

I made small script (named codeformat) to do this, (also tried replaing %f with $1)

#! /bin/bash
clang-format -style=file %f | diff %f -**
if [ $? -ne 0 ]; then
   echo "ERROR: codeformat not correct"
   exit 1
fi

did setup git config and updated .gitattributes with *.cpp filter=codeformat,

git config --global filter.codeformat.clean codeformat
git config --global filter.codeformat.smudge codeformat

looks like script is being run, but it is not getting file name. Why is this?

解决方案

You have the %f directive in the wrong place.

As shown in the gitattributes documentation (search for %f):

Sequence "%f" on the filter command line is replaced with the name of the file the filter is working on. A filter might use this in keyword substitution. For example:

[filter "p4"]
        clean = git-p4-filter --clean %f
        smudge = git-p4-filter --smudge %f

Hence, to get the path name of the file, you would need to set, e.g., filter.codeformat.clean to codeformat %f.

At this point you will also need to modify your bash script, since its syntax for argument substitution is indeed $1. However, read the immediate next paragraph from the gitattributes documentation:

Note that "%f" is the name of the path that is being worked on. Depending on the version that is being filtered, the corresponding file on disk may not exist, or may have different contents. So, smudge and clean commands should not try to access the file on disk, but only act as filters on the content provided to them on standard input.

The emphasis here is mine, but this is telling you that you cannot simply open the disk file and read it. You must filter only the standard input, providing standard output.

(As it turns out, clang-format is designed to do just that. Diff, however, is not.)


Edit to add working example:

$ cat ~/scripts/dotest
#! /bin/sh
echo "dotest run with $# arguments:" >>/tmp/dotest_log
for i do
    printf '%s\n' "$i"
done >>/tmp/dotest_log
cat
$ which dotest
[path edited]/scripts/dotest
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[filter "testfilter"]
        clean = dotest %f
        smudge = dotest %f
$ cat .gitattributes
*.test  filter=testfilter
$ echo 'bar.test' > bar.test
$ git add bar.test
$ cat /tmp/dotest_log
dotest run with 1 arguments:
bar.test

这篇关于使用过滤器smudge / clean的git属性通过shell脚本处理.cpp文件,但未找到文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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