Skip to content

Commit a76d59d

Browse files
committed
support fetching yml config file from remote repository
1 parent 0d152c9 commit a76d59d

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

git_build_branch/branch_builder.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
from .gitutils import ( # noqa E402
1717
MissingRemote,
1818
OriginalBranch,
19+
ensure_repository_is_up_to_date,
1920
get_git,
2021
get_local_ref,
2122
git_recent_tags,
2223
has_local,
2324
has_merge_conflict,
2425
origin,
2526
print_merge_details,
27+
show_most_recent_change,
2628
)
2729

2830
from .sh_verbose import ShVerbose # noqa E402
@@ -304,22 +306,44 @@ def inner(text, bold=False):
304306
def main():
305307
parser = argparse.ArgumentParser(description='Rebuild the deploy branch for an environment')
306308
parser.add_argument("config_path", help="Path to the YAML configuration file")
309+
remote_repo_group = parser.add_argument_group("remote repo")
310+
remote_repo_group.add_argument("--remote-url", help="Remote url to clone repository from")
311+
remote_repo_group.add_argument("--repo-root", help="Path where repository is checked out")
312+
remote_repo_group.add_argument("--repo-filepath", help="Relative path to YAML configuration file")
307313
parser.add_argument("actions", nargs="*")
308314
parser.add_argument("-p", "--path", default=".", help="Path to the repository")
309315
parser.add_argument("-v", "--verbose", action="store_true")
310316
parser.add_argument("--push", action="store_true", help="Push the changes to remote git repository.")
311317
args = parser.parse_args()
312318

313319
git = get_git()
320+
321+
print(args.remote_url)
322+
323+
config_path = args.config_path
324+
remote_repo_args = [args.remote_url, args.repo_root, args.repo_filepath]
325+
if any(remote_repo_args) and not all(remote_repo_args):
326+
arg_names = [action.option_strings[0] for action in
327+
remote_repo_group._group_actions]
328+
print(
329+
red(f"All of the following arguments are required to use a remote "
330+
f"repository: {arg_names}"))
331+
exit(1)
332+
333+
if args.remote_url and args.repo_root and args.repo_filepath:
334+
ensure_repository_is_up_to_date(git, args.remote_url, args.repo_root)
335+
show_most_recent_change(git, args.repo_root, args.repo_filepath)
336+
config_path = f"{args.repo_root}/{args.repo_filepath}"
337+
314338
print("Fetching master")
315339
git.fetch("origin", "master")
316340
if args.push:
317341
print("Checking branch config for modifications")
318-
if git.diff("origin/master", "--", args.config_path):
319-
print(red("'{}' on this branch different from the one on master".format(args.config_path)))
342+
if git.diff("origin/master", "--", config_path):
343+
print(red("'{}' on this branch different from the one on master".format(config_path)))
320344
exit(1)
321345

322-
with open(args.config_path) as config_yaml:
346+
with open(config_path) as config_yaml:
323347
config = yaml.safe_load(config_yaml)
324348

325349
code_root = os.path.abspath(args.path)

git_build_branch/gitutils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33

44
import sh
5+
import sys
56

67
from .sh_verbose import ShVerbose
78

@@ -218,6 +219,29 @@ def print_merge_details(branch1, branch2, git, known_branches=None):
218219
known_branches=known_branches)
219220

220221

222+
def ensure_repository_is_up_to_date(git, remote_url, local_path):
223+
"""
224+
Clones repository to local_path if it does not already exist, otherwise
225+
pulls the latest changes from main
226+
:param git: sh command instance
227+
:param remote_url: url of repository to clone/update
228+
:param local_path: local path to directory to clone repository into
229+
"""
230+
if not os.path.exists(local_path):
231+
git.clone(remote_url, local_path)
232+
else:
233+
# -C repo needs to come right after git in shell, so cannot use C=repo
234+
git("-C", local_path, "pull", "origin", "main")
235+
236+
237+
def show_most_recent_change(git, repo, filepath):
238+
"""
239+
:param repo: local path to repository
240+
:param filepath: relative path to file from base of repository
241+
"""
242+
git("-C", repo, "show", "-n", 1, "--", filepath, _in=sys.stdin, _out=sys.stdout)
243+
244+
221245
if __name__ == '__main__':
222246
import sys
223247
args = sys.argv[1:]

0 commit comments

Comments
 (0)