【问题标题】:clang-format-diff.py without -i option generates patch file in format I can't apply没有 -i 选项的 clang-format-diff.py 以我无法应用的格式生成补丁文件
【发布时间】:2021-01-12 20:07:17
【问题描述】:

我编写了一个脚本,在所有未提交的更改和未推送的提交的差异上运行 clang-format-diff.py

在 git 目录下名为 run-clang-format.sh 的文件包含:

git diff -U0 --no-color origin/master | clang-format-diff.py -p1

当我将-i 选项添加到clang-format-diff.py 时,它可以正常工作,但我想避免这种情况,因为它引入了一些我想绕过的古怪重新排序。所以我目前在 git 目录的基础上运行上面的 shell 脚本(所以我得到一个 .diff,我可以在应用之前手动查看和/或编辑):

./run-clang-format.sh > format.diff

format.diff 看起来像这样(我从未见过这种“格式化前/格式化后”的格式):

--- symengine/functions.cpp (before formatting)
+++ symengine/functions.cpp (after formatting)
@@ -11,7 +11,10 @@
 extern RCP<const Basic> im3;
 extern RCP<const Basic> im5;
 
-RCP<const Basic> sqrt(RCP<const Basic> &arg) { return pow(arg, div(one, i2)); }
+RCP<const Basic> sqrt(RCP<const Basic> &arg)
+{
+    return pow(arg, div(one, i2));
+}
 
 RCP<const Basic> cbrt(RCP<const Basic> &arg)
 {

我尝试了几种方法来应用这个补丁(从 gi​​t 目录的基础):

$ git apply format.diff 
error: functions.cpp: No such file or directory
$ git am format.diff 
Patch format detection failed.
$ patch -p1 < format.diff 
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- symengine/functions.cpp    (before formatting)
|+++ symengine/functions.cpp    (after formatting)
--------------------------
File to patch: 

编辑如果文件路径有一个前导点,则可以应用上面的补丁。

如何按原样应用此补丁或获取 clang-format-patch.py​​ 以创建如下格式的差异:

diff --git a/symengine/functions.cpp b/symengine/functions.cpp
index c2fbc01a..75ef7d63 100644
--- a/symengine/functions.cpp
+++ b/symengine/functions.cpp
@@ -11,10 +11,8 @@ extern RCP<const Basic> im2;
 extern RCP<const Basic> im3;
 extern RCP<const Basic> im5;
 
-RCP<const Basic> sqrt(RCP<const Basic> &arg)
-{
-    return pow(arg, div(one, i2));
-}
+RCP<const Basic> sqrt(RCP<const Basic> &arg) { return pow(arg, div(one, i2)); }
+
 RCP<const Basic> cbrt(RCP<const Basic> &arg)
 {
     return pow(arg, div(one, i3));

使用git apply format.diff很容易应用

【问题讨论】:

    标签: git diff patch clang-format difflib


    【解决方案1】:

    希望这不是最佳答案,但我在阅读 clang-format-diff.py 后使用 Pure Python way to apply a unified diff to a file? 解决了这个问题

    我想问题是 clang-format-diff.py 正在使用 difflib.unified_diffhttps://docs.python.org/3/library/difflib.html 没有解释如何手动应用输出。

    所以我在 git 目录的基础上创建了一个新脚本 apply-clang-format-diff.py,如下所示:

    import patch
    import sys
    pset = patch.fromfile(sys.argv[1])
    pset.apply()
    

    然后像python apply-clang-format-diff.py format.diff 一样运行它,它就可以工作了。

    【讨论】:

      【解决方案2】:

      这是另一个修复。将此补丁应用于clang-format-diff.py,以便统一差异输出包含一个前导点。

      --- clang-format-diff.py    2020-09-26 17:15:43.000000000 -0400
      +++ clang-format-diff.py    2020-09-26 20:42:29.000000000 -0400
      @@ -24,6 +24,7 @@
       import re
       import subprocess
       import sys
      +import os
       
       if sys.version_info.major >= 3:
           from io import StringIO
      @@ -111,9 +112,10 @@
           if not args.i:
             with open(filename) as f:
               code = f.readlines()
      +      filepath = os.path.join(os.path.curdir, filename)
             formatted_code = StringIO(stdout).readlines()
             diff = difflib.unified_diff(code, formatted_code,
      -                                  filename, filename,
      +                                  filepath, filepath,
                                         '(before formatting)', '(after formatting)')
             diff_string = ''.join(diff)
             if len(diff_string) > 0:
      
      

      现在我可以git apply 补丁(或使用 unix 补丁)。但请注意,上面的补丁(使用diff -u 生成)没有前导点,使用patch 可以很好地应用。那么clang-format-diff.pydifflib.unified_diff 的默认输出给出了什么????

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-16
        • 2018-07-28
        • 1970-01-01
        • 2014-04-07
        • 2022-12-31
        • 1970-01-01
        • 2017-03-15
        • 1970-01-01
        相关资源
        最近更新 更多