如何使用 git 获取每个提交的文件更改了哪些行

How to obtain which lines changed for each commited file with git

我知道git diff 可以显示两个提交之间的差异。但是,我不知道如何为每个提交的文件获取更改的行(例如@@ -12,6 +12,11 @@)。我已经使用正则表达式来获取数字,但我希望将它们分开用于每个文件。

换句话说,我想要这样的东西:

a/aten/src/ATen/cpu/vec256/vec256_int.h
@@ -12,6 +12,11 @@
@@ -95,25 +100,19 @@
@@ -190,25 +189,19 @@
@@ -380,25 +373,19 @@

diff --git a/test/test_torch.py b/test/test_torch.py
@@ -1388,6 +1388,14 @@

来自下面的输出:

diff --git a/aten/src/ATen/cpu/vec256/vec256_int.h b/aten/src/ATen/cpu/vec256/vec256_int.h
index 9d2581e18..5c1cf80d5 100644
--- a/aten/src/ATen/cpu/vec256/vec256_int.h
+++ b/aten/src/ATen/cpu/vec256/vec256_int.h
@@ -12,6 +12,11 @@ namespace {
 struct Vec256i {
 protected:
   __m256i values;
+
+  static inline __m256i invert(const __m256i& v) {
+    const auto ones = _mm256_set1_epi64x(-1);
+    return _mm256_xor_si256(ones, v);
+  }
 public:
   Vec256i() {}
   Vec256i(__m256i v) : values(v) {}
@@ -95,25 +100,19 @@ struct Vec256<int64_t> : public Vec256i {
     return _mm256_cmpeq_epi64(values, other.values);
   }

@@ -190,25 +189,19 @@ struct Vec256<int32_t> : public Vec256i {
     return _mm256_cmpeq_epi32(values, other.values);
   }

@@ -380,25 +373,19 @@ struct Vec256<int16_t> : public Vec256i {
     return _mm256_cmpeq_epi16(values, other.values);
   }


diff --git a/test/test_torch.py b/test/test_torch.py
index 0c30c1f1a..10f6085cf 100644
--- a/test/test_torch.py
+++ b/test/test_torch.py
@@ -1388,6 +1388,14 @@ class _TestTorchMixin(object):
     def test_neg(self):
         self._test_neg(self, lambda t: t)

+    def test_threshold(self):
+        for dtype in torch.testing.get_all_dtypes():
+            if dtype != torch.uint8 and dtype != torch.float16:
+                # 100 is wide enough to use AVX2 instructions for all types
+                x = torch.randn(100).sign().to(dtype=dtype)
+                y = torch.threshold(x, 0, 0)
+                self.assertTrue(y.le(0).any())
+
     def test_reciprocal(self):
         a = torch.randn(100, 89)
         res_div = 1 / a

注意:我使用的是 Python 语言。

不是一个完整的答案,但是 git log -p 会显示每个提交的补丁。 如果 git 没有适合您的功能,那么可以制作一个只显示您想要的部分的脚本。

不知道是否已经存在某些东西,我开发了一些东西来实现它。

首先,我使用 'diff --git' 作为分隔符拆分 patchfile 字符串。这将为每个更改的文件 return 一个单独的 patchfile(全部存储在 split_patchfile 中):

    def splitPatchfile(patchfile):
        split_patchfile = patchfile.split('diff --git')
        return split_patchfile

其次,我解析每个 patchfile 以找到更改的行。这将创建保存在 lines_numbers 中的 2Dimensional-ish list。每个 line_number 将包含每个补丁文件的更改行。

    def findChangedLinesPerFile(split_patchfile):
        lines_numbers = []
        for split_patch in split_patchfile:
            lines_numbers.append(findChangedLines(split_patch))
        return lines_numbers

    def findChangedLines(split_patch):
        regex = r"^@@ [-+](\d+)"
        matches = re.finditer(regex, split_patch, re.MULTILINE)
        line_numbers = []

        for matchNum, match in enumerate(matches, start=1):
            # print(int(match.group(1))) # debug which lines are modified
            line_numbers.append(int(match.group(1)))
        return line_numbers

第三,由于元素的顺序对于进一步的工作很重要,我清除了空元素以使每个 patchfile 与其 line_numbers 相对应。 由于 patchfile.split('diff --git') 出现空元素。它创建了 3 patchfile,因为我有 2 'diff --git'(第一个 patchfile 是空的)。

    def removeEmptyElements(split_patchfile, lines_numbers):
        split_patchfile = list(filter(None, split_patchfile))
        lines_numbers = list(filter(None, lines_numbers))
        return split_patchfile, lines_numbers