Skip to content
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

Return failure code to the system when commands fail #75

Merged
merged 1 commit into from
Feb 8, 2024
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
6 changes: 5 additions & 1 deletion mu
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python
if __name__ == '__main__':
import sys
import mu_repo
mu_repo.main()

status = mu_repo.main()
success = status is None or status.succeeded
sys.exit(0 if success else 1)
6 changes: 5 additions & 1 deletion mu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python
if __name__ == '__main__':
import sys
import mu_repo
mu_repo.main()

status = mu_repo.main()
success = status is None or status.succeeded
sys.exit(0 if success else 1)
10 changes: 10 additions & 0 deletions mu_repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ def __init__(self, status_message, succeeded, config=None):
self.succeeded = succeeded
self.config = config

def __repr__(self):
return f"Status(status_message={self.status_message!r}, succeeded={self.succeeded!r})"

def __eq__(self, other):
return (
self.status_message == other.status_message
and self.succeeded == other.succeeded
and self.config == other.config
)

#===================================================================================================
# Params
#===================================================================================================
Expand Down
2 changes: 2 additions & 0 deletions mu_repo/action_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from mu_repo.backwards import iteritems
from mu_repo.get_repos_and_local_branches import GetReposAndLocalBranches
from mu_repo.print_ import Print
from mu_repo import Status

#===================================================================================================
# Run
Expand Down Expand Up @@ -33,4 +34,5 @@ def Run(params):
Print('Found more than one branch that matches ${START_COLOR}%s${RESET_COLOR}:\n' % params.args[1])
PrintBranchToRepos(branch_to_repos, params)
Print('\n${START_COLOR}ERROR${RESET_COLOR}: unable to decide branch to work on.', __color__='RED')
return Status("ERROR", succeeded=False)

11 changes: 8 additions & 3 deletions mu_repo/action_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def Run(params, on_output=None):
else:
commands.append(ParallelCmd(repo, [config.git] + args))

ExecuteInParallel(commands, on_output, serial=config.serial)

return Status('Finished', True)
cmd_return_codes = ExecuteInParallel(commands, on_output, serial=config.serial)
failed_cmds = [cmd for (cmd, returncode) in cmd_return_codes if returncode != 0]
if failed_cmds:
message = "Failed:\n" + "\n".join(f' {x.repo}' for x in failed_cmds)
Print('\n${START_COLOR}' + message + '${RESET_COLOR}', __color__="RED")
return Status(message, False)
else:
return Status('Finished', True)

3 changes: 3 additions & 0 deletions mu_repo/execute_git_command_in_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self, repo, cmd, output_queue):
self.repo = repo
self.cmd = cmd
self.output_queue = output_queue
self.returncode = None


class ReaderThread(threading.Thread):
Expand Down Expand Up @@ -97,6 +98,7 @@ def run(self, serial=False):
PrintError('Error executing: ' + ' '.join(cmd) + ' on: ' + repo)
if p is not None:
p.wait()
self.returncode = p.returncode

else:
try:
Expand All @@ -123,6 +125,7 @@ def run(self, serial=False):
stderr = AsStr(self.stderr_thread.GetFullOutput())

self._HandleOutput(msg, stdout, stderr)
self.returncode = p.returncode

def GetPartialStderrOutput(self):
stderr_thread = getattr(self, 'stderr_thread', None)
Expand Down
1 change: 1 addition & 0 deletions mu_repo/execute_parallel_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def ExecuteInParallel(commands, on_output=None, serial=False):
else:
from mu_repo.execute_git_command_in_thread import ExecuteThreadsHandlingOutputQueue
ExecuteThreadsHandlingOutputQueue(threads, output_queue, on_output=on_output)
return [(commands[i], t.returncode) for i, t in enumerate(threads)]



26 changes: 26 additions & 0 deletions mu_repo/tests/test_action_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import subprocess
from pathlib import Path


import mu_repo


def test_action_default(workdir, monkeypatch):
workdir = Path(workdir)
monkeypatch.chdir(workdir)
subprocess.check_call(f'git init core')
subprocess.check_call(f'git init app')
(workdir/ 'app/.mu_repo').write_text("repo=.\nrepo=../core\n", encoding="utf-8")

monkeypatch.chdir(workdir/ 'app')
subprocess.check_call(f'git config --add foo.bar foo-value')

subprocess.check_call(f'git config --get foo.bar')

status = mu_repo.main(args=['config', '--get', 'core.bare'], config_file='.mu_repo')
assert status == mu_repo.Status("Finished", succeeded=True)

status = mu_repo.main(args=['config', '--get', 'foo.bar'], config_file='.mu_repo')
assert status == mu_repo.Status("Failed:\n ../core", succeeded=False)


10 changes: 7 additions & 3 deletions mu_repo/tests/test_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ def test_checkout_partial_names(workdir):
mu_repo.main(config_file='.mu_repo', args=['branch', 'rb-scissors'])

# Checkout fb-rock on both projects
mu_repo.main(config_file='.mu_repo', args=['checkout', 'fb-rock'])
status = mu_repo.main(config_file='.mu_repo', args=['checkout', 'fb-rock'])
assert status == mu_repo.Status("Finished", True)
assert 'fb-rock' == get_current_branch('app')
assert 'fb-rock' == get_current_branch('lib')

# Only one possible mathc, switch to rb-scissors
mu_repo.main(config_file='.mu_repo', args=['checkout', 'rb-'])
status = mu_repo.main(config_file='.mu_repo', args=['checkout', 'rb-'])
assert status == mu_repo.Status("Finished", True)
assert 'rb-scissors' == get_current_branch('app')
assert 'rb-scissors' == get_current_branch('lib')

# Couldn't guess branch name, do not checkout
mu_repo.main(config_file='.mu_repo', args=['checkout', 'fb-'])
status = mu_repo.main(config_file='.mu_repo', args=['checkout', 'fb-'])
assert status == mu_repo.Status("ERROR", False)
assert 'rb-scissors' == get_current_branch('app')
assert 'rb-scissors' == get_current_branch('lib')


Expand Down
Loading