【问题标题】:How to obtain which lines changed for each commited file with git如何使用 git 获取每个提交文件更改的行
【发布时间】:2019-07-26 20:53:43
【问题描述】:

我知道 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 github git-diff


    【解决方案1】:

    不是一个完整的答案,但 git log -p 会显示每次提交的补丁。 如果 git 没有为您执行此操作的功能,则可以制作一个仅显示您想要的部分的脚本。

    【讨论】:

      【解决方案2】:

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

      首先,我使用'diff --git' 作为分隔符来拆分patchfile 字符串。这将为每个更改的文件返回一个单独的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
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-13
        • 2016-07-05
        • 2012-09-30
        • 1970-01-01
        • 2020-11-12
        • 2019-07-29
        • 2012-04-27
        • 1970-01-01
        相关资源
        最近更新 更多