Skip to content

Commit a7a8ddf

Browse files
author
Jaakko Kangasharju
committed
Ignore submodules when diffing
By default, git diff includes entries for submodules in its output. This breaks when trying to extract information from such a file since the submodule directory is not in fact a file known to git. This may be related to a GitPython issue: gitpython-developers/GitPython#891 But for git-hammer, the sensible course of action seems to be ignoring submodules. They are not code in the main repository, and if their code should be in a project, the actual repository can be included. So add to diff the option ignore-submodules that causes it to skip submodules. Also add a unit test that creates a submodule in the test repository and verifies that git-hammer works correctly on it.
1 parent 0cad7c6 commit a7a8ddf

12 files changed

+45
-5
lines changed

githammer/hammer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _make_full_commit_stats(self, repository, commit):
146146

147147
def _make_diffed_commit_stats(self, repository, commit, previous_commit, previous_commit_line_counts,
148148
previous_commit_test_counts):
149-
diff_index = previous_commit.diff(commit, w=True)
149+
diff_index = previous_commit.diff(commit, w=True, ignore_submodules=True)
150150
current_files = set()
151151
previous_files = set()
152152
for add_diff in diff_index.iter_change_type('A'):
@@ -202,7 +202,7 @@ def _add_commit_object(self, repository, commit, session):
202202
if len(commit.parents) <= 1:
203203
if len(commit.parents) == 1:
204204
diff_stat = repository.git_repository.git.diff(
205-
commit.parents[0], commit, numstat=True)
205+
commit.parents[0], commit, numstat=True, ignore_submodules=True)
206206
else:
207207
diff_stat = repository.git_repository.git.show(commit, numstat=True, format='')
208208
added_lines = 0

tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .test_init import HammerInitTest
22
from .test_single_repository import HammerRepositoryTest
3+
from .test_submodule import HammerSubmoduleTest

tests/data/.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# When working on the test repository, clone it to the directory working-tree
2-
# here, so git will ignore it.
3-
working-tree
1+
# When working on the test repository, clone it to a directory starting with
2+
# worktree here, so git will ignore it.
3+
worktree*

tests/data/subrepository/HEAD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

tests/data/subrepository/config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
5+
ignorecase = true
6+
precomposeunicode = true

tests/data/subrepository/description

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.

tests/data/subrepository/info/exclude

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x=��
2+
�0�a�y�ً�I��B��N}�I:��HL�Ƿ �:�/��ò ��]��`��ulO����c��)��'UtdZS)+(�>D���
3+
�k�C�k���DUjT���,�[ݞG�MS��Oz�� �8��t9�8��`���E{�� n���G�/yt:
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
303804d461da9cdfef86f6053d0ad2d0545adae1

tests/test_submodule.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import git
3+
4+
from .hammer_test import HammerTest
5+
6+
7+
class HammerSubmoduleTest(HammerTest):
8+
9+
def setUp(self):
10+
super().setUp()
11+
git.Repo.clone_from(os.path.join(self.current_directory, 'data', 'repository'),
12+
os.path.join(self.working_directory.name, 'worktree'))
13+
self.repository = git.Repo(os.path.join(self.working_directory.name, 'worktree'))
14+
git.Submodule.add(self.repository, 'subrepo', 'subrepo',
15+
os.path.join(self.current_directory, 'data', 'subrepository'))
16+
author = git.Actor('Author A', '[email protected]')
17+
self.repository.index.commit('Add subrepo', author=author)
18+
19+
def test_repository_with_submodule_is_understood(self):
20+
self.hammer.add_repository(os.path.join(self.working_directory.name, 'worktree'))
21+
self.assertIsNotNone(self.hammer.head_commit())

0 commit comments

Comments
 (0)