简体   繁体   中英

clang-format-diff.py without -i option generates patch file in format I can't apply

I made a script to run clang-format-diff.py on a diff of all uncommitted changes and unpushed commits.

File called run-clang-format.sh in base of git directory and contains:

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

When I add the -i option to clang-format-diff.py it works fine but I want to avoid this because it introduces some wacky reordering that I want to bypass. So I currently run the above shell script like so in the base of the git directory (so I get a .diff that I can review and/or edit manually before applying):

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

format.diff looks like this (I've never seen this "before formatting / after formatting" format):

--- 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)
 {

I tried several ways to apply this patch (from the base of the git directory):

$ 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: 

EDIT The patch above could be applied if the file paths had a leading dot.

How can I either apply this patch as is or get clang-format-patch.py to create a diff in a format like this:

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));

which is easily applied using git apply format.diff

Hopefully this isn't the best answer but I solved this using Pure Python way to apply a unified diff to a file? after reading clang-format-diff.py

I suppose the issue is clang-format-diff.py is using difflib.unified_diff yet https://docs.python.org/3/library/difflib.html doesn't explain how to apply the output manually.

So I created a new script apply-clang-format-diff.py in the base of the git directory that looks like:

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

and run it like python apply-clang-format-diff.py format.diff and it works.

Here's another fix. Apply this patch to clang-format-diff.py so that the unified diff output contains a leading dot.

--- 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:

Now I can git apply the patches (or use unix patch). But notice that the patch above (produced using diff -u ) has no leading dots and it applies just fine using patch . So what gives with the default output of clang-format-diff.py and difflib.unified_diff ???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM