Skip to content

Commit 2445455

Browse files
authored
reimplement incoming check via git log (#3997)
fixes #3975
1 parent 37b4a1d commit 2445455

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

tools/src/main/python/opengrok_tools/scm/git.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,22 @@ def reposync(self):
5252
return self._run_custom_sync_command([self.command, 'pull', '--ff-only'])
5353

5454
def incoming_check(self):
55+
"""
56+
:return: True if there are any incoming changes present, False otherwise
57+
"""
5558
self._configure_git_pull()
56-
return self._run_custom_incoming_command([self.command, 'pull', '--dry-run'])
59+
self.fetch()
60+
branch = self.get_branch()
61+
status, out = self._run_command([self.command, 'log',
62+
'--pretty=tformat:%H', '..origin/' + branch])
63+
if status == 0:
64+
if len(out) == 0:
65+
return False
66+
else:
67+
raise RepositoryException("failed to check for incoming changes in {}: {}".
68+
format(self, status))
69+
70+
return True
5771

5872
def get_branch(self):
5973
status, out = self._run_command([self.command, 'branch', '--show-current'])

tools/src/test/python/test_mirror_incoming_retval.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,25 @@
3434

3535
import opengrok_tools.mirror
3636
from opengrok_tools.scm import get_repository
37-
from opengrok_tools.utils.exitvals import CONTINUE_EXITVAL
37+
from opengrok_tools.utils.exitvals import CONTINUE_EXITVAL, SUCCESS_EXITVAL
3838

3939

40+
@pytest.fixture(scope="module", autouse=True)
41+
def setup():
42+
# The default has changed for python 3.8 (see https://github.com/oracle/opengrok/issues/3296).
43+
# Because of the mocking we need to use the "fork" type to propagate all mocks to the
44+
# processes spawned by mirror command
45+
multiprocessing.set_start_method('fork')
46+
47+
48+
@pytest.mark.parametrize('do_changes', [True, False])
4049
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
41-
def test_incoming_retval(monkeypatch):
50+
def test_incoming_retval(monkeypatch, do_changes):
4251
"""
4352
Test that the special CONTINUE_EXITVAL value bubbles all the way up to
4453
the mirror.py return value.
4554
"""
4655

47-
# The default has changed for python 3.8 (see https://github.com/oracle/opengrok/issues/3296).
48-
# Because of the mocking we need to use the "fork" type to propagate all mocks to the
49-
# processes spawned by mirror command
50-
multiprocessing.set_start_method('fork')
51-
5256
class MockResponse:
5357

5458
# mock json() method always returns a specific testing dictionary
@@ -65,7 +69,9 @@ def raise_for_status():
6569
repo_path = os.path.join(source_root, repo_name)
6670
cloned_repo_name = "cloned_repo"
6771
cloned_repo_path = os.path.join(source_root, cloned_repo_name)
68-
project_name = "foo" # does not matter for this test
72+
# The project name does not matter for this test except for the lock file created by main()
73+
# so that both parametrized tests can run in parallel.
74+
project_name = "test_incoming_retval-" + str(do_changes)
6975

7076
os.mkdir(repo_path)
7177

@@ -85,15 +91,25 @@ def mock_get_config_value(*args, **kwargs):
8591
git_config.set_value('user', 'email', '[email protected]')
8692
git_config.set_value('user', 'name', 'John Doe')
8793

88-
new_file_path = os.path.join(repo_path, 'foo')
94+
# Add a file.
95+
new_file_path = os.path.join(repo_path, 'newfile')
8996
with open(new_file_path, 'w'):
9097
pass
9198
assert os.path.isfile(new_file_path)
9299
index = repo.index
93100
index.add([new_file_path])
94101
index.commit("add file")
102+
103+
# Clone the repository first so that any subsequent changes in the repo
104+
# will not be reflected in the clone.
95105
repo.clone(cloned_repo_path)
96106

107+
if do_changes:
108+
with open(new_file_path, 'w') as fp:
109+
fp.write("foo")
110+
index.add([new_file_path])
111+
index.commit("change file")
112+
97113
with monkeypatch.context() as m:
98114
m.setattr(sys, 'argv', ['prog', "-I", project_name])
99115

@@ -103,4 +119,9 @@ def mock_get_config_value(*args, **kwargs):
103119
m.setattr("opengrok_tools.utils.mirror.get_repos_for_project", mock_get_repos)
104120
m.setattr("opengrok_tools.utils.mirror.do_api_call", mock_get)
105121

106-
assert opengrok_tools.mirror.main() == CONTINUE_EXITVAL
122+
if do_changes:
123+
expected_retval = SUCCESS_EXITVAL
124+
else:
125+
expected_retval = CONTINUE_EXITVAL
126+
127+
assert opengrok_tools.mirror.main() == expected_retval

0 commit comments

Comments
 (0)