blob: 4e779d89fde7ca519b1dd9ce9cb5fa70dcc744c6 [file] [log] [blame]
Diana Picus41f49b12017-12-22 11:49:03 +01001from subprocess import PIPE
2from subprocess import Popen
Diana Picus052b7d32017-11-24 16:19:41 +01003from subprocess import STDOUT
Diana Picus41f49b12017-12-22 11:49:03 +01004from subprocess import SubprocessError
Diana Picus052b7d32017-11-24 16:19:41 +01005
Diana Picus9a68e7b2017-12-19 18:00:02 +01006import re
7
8from linaropy.git.worktree import Worktree
Diana Picus052b7d32017-11-24 16:19:41 +01009
Diana Picus41f49b12017-12-22 11:49:03 +010010
Diana Picus052b7d32017-11-24 16:19:41 +010011class CommandPrinter(object):
12 """Command consumer that just prints the commands that it receives."""
13
14 def consume(self, command, directory):
15 print("{}$ {}".format(directory, ' '.join(command)))
16
17
18class CommandRunner(object):
19 """Command consumer that runs the commands that it receives."""
20
21 def consume(self, command, directory):
22 """
23 Run the given command in the given directory and print the stdout and
24 stderr. If an exception is thrown while running the command, it will be
25 rethrown as a RuntimeError.
26 """
Diana Picus052b7d32017-11-24 16:19:41 +010027 try:
Diana Picus41f49b12017-12-22 11:49:03 +010028 with Popen(command, stdout=PIPE, stderr=STDOUT, cwd=directory) as process:
29 for line in process.stdout:
30 print(str(line, 'utf-8'), end='')
31 if process.returncode != 0:
32 raise RuntimeError(
33 "Command failed with return code: {}\n".format(
34 process.returncode))
35 except SubprocessError as exc:
Diana Picus052b7d32017-11-24 16:19:41 +010036 raise RuntimeError(
37 "Error while running command\n{}".format(str(exc.output, 'utf-8'))) from exc
Diana Picus5ad55422017-12-14 17:57:10 +010038
39
40# FIXME: repo.pushToBranch doesn't work, because it doesn't handle remote
41# branches properly. Furthermore, there's no support for getting the remote URL,
42# so we need to resort to raw git commands. We may also consider moving the
43# functionality for parsing info out of the remote URL into the repo object.
44from sh import git
45from linaropy.cd import cd
46
47
48def get_user_from_remote(url):
49 """Get the username used as part of the remote URL, or None.
50
51 The remote URLs that we expect to see look like $protocol://$user@$location.
52 If they look any different, we won't be able to parse them.
53 """
54 pattern = re.compile("(.*://)?(?P<user>.*)@(.*)\n?")
55 match = pattern.match(str(url))
56 if match is None:
57 return None
58 return match.group('user')
59
60
61def get_remote_branch(repo, local_branch):
62 """
63 Get the name of the remote branch corresponding to the given local branch.
64 """
65 with cd(repo.repodir):
66 remote = git("remote", "get-url", "--push", "origin").strip()
67 user = get_user_from_remote(remote)
68 if not user:
69 raise EnvironmentError("Couldn't parse user from {}.".format(remote))
70
71 remote_branch = "linaro-local/{}/{}".format(user, local_branch)
72 if not repo.is_valid_branch_name(remote_branch):
73 raise EnvironmentError(
74 "{} is not a valid branch name.".format(remote_branch))
75
76 return remote_branch
77
78
79def push_branch(proj, local_branch, remote_branch, pathToRepo):
80 """Push the given local branch into origin's given remote branch."""
81 repo = Worktree(proj, pathToRepo)
82 with cd(repo.repodir):
83 git("push", "-u", "origin", "+{}:{}".format(local_branch, remote_branch))