Skip to content

reimplement incoming check via git log #3997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tools/src/main/python/opengrok_tools/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,22 @@ def reposync(self):
return self._run_custom_sync_command([self.command, 'pull', '--ff-only'])

def incoming_check(self):
"""
:return: True if there are any incoming changes present, False otherwise
"""
self._configure_git_pull()
return self._run_custom_incoming_command([self.command, 'pull', '--dry-run'])
self.fetch()
branch = self.get_branch()
status, out = self._run_command([self.command, 'log',
'--pretty=tformat:%H', '..origin/' + branch])
if status == 0:
if len(out) == 0:
return False
else:
raise RepositoryException("failed to check for incoming changes in {}: {}".
format(self, status))

return True

def get_branch(self):
status, out = self._run_command([self.command, 'branch', '--show-current'])
Expand Down
41 changes: 31 additions & 10 deletions tools/src/test/python/test_mirror_incoming_retval.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@

import opengrok_tools.mirror
from opengrok_tools.scm import get_repository
from opengrok_tools.utils.exitvals import CONTINUE_EXITVAL
from opengrok_tools.utils.exitvals import CONTINUE_EXITVAL, SUCCESS_EXITVAL


@pytest.fixture(scope="module", autouse=True)
def setup():
# The default has changed for python 3.8 (see https://github.com/oracle/opengrok/issues/3296).
# Because of the mocking we need to use the "fork" type to propagate all mocks to the
# processes spawned by mirror command
multiprocessing.set_start_method('fork')


@pytest.mark.parametrize('do_changes', [True, False])
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
def test_incoming_retval(monkeypatch):
def test_incoming_retval(monkeypatch, do_changes):
"""
Test that the special CONTINUE_EXITVAL value bubbles all the way up to
the mirror.py return value.
"""

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

class MockResponse:

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

os.mkdir(repo_path)

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

new_file_path = os.path.join(repo_path, 'foo')
# Add a file.
new_file_path = os.path.join(repo_path, 'newfile')
with open(new_file_path, 'w'):
pass
assert os.path.isfile(new_file_path)
index = repo.index
index.add([new_file_path])
index.commit("add file")

# Clone the repository first so that any subsequent changes in the repo
# will not be reflected in the clone.
repo.clone(cloned_repo_path)

if do_changes:
with open(new_file_path, 'w') as fp:
fp.write("foo")
index.add([new_file_path])
index.commit("change file")

with monkeypatch.context() as m:
m.setattr(sys, 'argv', ['prog', "-I", project_name])

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

assert opengrok_tools.mirror.main() == CONTINUE_EXITVAL
if do_changes:
expected_retval = SUCCESS_EXITVAL
else:
expected_retval = CONTINUE_EXITVAL

assert opengrok_tools.mirror.main() == expected_retval