我试图在多对目录之间获得统一的差异,这样我就可以确保对之间的比较是一致的,并且我想知道是否有办法让diff
到使用相对路径而不是绝对路径格式化输出。
现在,如果我使用diff -r -u PATH1 PATH2
,那么我会得到这种输出:
diff -r -u PATH1/some/subfile.txt PATH2/some/subfile.txt
--- PATH1/some/subfile.txt Tue Feb 07 09:16:31 2017
+++ PATH2/some/subfile.txt Tue Feb 07 09:16:32 2017
@@ -70,7 +70,7 @@
*
* some stuff
*
- * I am Tweedledee and you are not
+ * I am Tweedledum and you are not
*/
void twiddle(void)
{
@@ -88,7 +88,7 @@
* check whether we should destroy everything
* and then destroy everything in either case
*/
-inline static void Tweedledee(void)
+inline static void Tweedledum(void)
{
if (should_destroy_everything())
{
我宁愿只获得相对路径......有没有办法让diff
这样做?例如:
diff -r -u PATH1/some/subfile.txt PATH2/some/subfile.txt
--- some/subfile.txt
+++ some/subfile.txt
@@ -70,7 +70,7 @@
*
* some stuff
*
- * I am Tweedledee and you are not
+ * I am Tweedledum and you are not
*/
void twiddle(void)
{
@@ -88,7 +88,7 @@
* check whether we should destroy everything
* and then destroy everything in either case
*/
-inline static void Tweedledee(void)
+inline static void Tweedledum(void)
{
if (should_destroy_everything())
{
这样可以更容易地比较预期相同的差异报告。 (在我的情况下PATH1
和PATH2
在每种情况下都不同,而文件的相对路径和确切的内容差异是相同的)
否则我必须过滤掉这些信息(手动或用脚本)
答案 0 :(得分:1)
我会将diff命令的输出传递给sed脚本,如下所示: $ diff -r -u PATH1 / some / subfile.txt PATH2 / some / subfile.txt | sed' 1s / PATH1 \ ///' | sed' 2s / PATH2 \ ///' 脚本说":在第1行,替换" PATH1",然后是单个正斜杠,什么都没有,然后,在第2行,替换" PATH2",然后是没有任何东西,单一的正斜线。我必须创建一些内容来测试它,所以我还没有对它进行测试。
答案 1 :(得分:0)
我咬了一下子弹并用Python进行了解析;它删除diff blah blah blah
语句并将路径重新激活到一对指定的根目录,同时删除时间戳:
udiff_re = re.compile(r'^@@ -(\d+),(\d+) \+(\d+),(\d+) @@$')
diffitem_re = re.compile(r'^(\+\+\+|---) (.*)\s+.{7} \d\d \d\d:\d\d:\d\d \d{4}$')
def process_diff_output(output, dir1, dir2):
state = 0
lines_pending = [0,0]
result = []
for line in output.splitlines():
if state == 0:
if line.startswith('@@'):
m = udiff_re.search(line)
if m:
nums = [int(n) for n in m.groups()]
else:
raise ValueError('Huh?\n' + line)
result.append(line)
lines_pending = [nums[1],nums[3]]
state = 1
elif line.startswith('--- ') or line.startswith('+++ '):
m = diffitem_re.search(line)
whichone = m.group(1)
filename = m.group(2)
dirx = dir1 if whichone == '---' else dir2
result.append('%s %s' % (whichone, os.path.relpath(filename, dirx)))
elif line.startswith('diff '):
pass # leave the diff cmd out
else:
raise ValueError('unknown header line\n'+line)
elif state == 1:
result.append(line)
if line.startswith('+'):
lines_pending[1] -= 1
elif line.startswith('-'):
lines_pending[0] -= 1
else:
lines_pending[0] -= 1
lines_pending[1] -= 1
if lines_pending == [0,0]:
state = 0
return '\n'.join(result)