Change Reclass model check's default branch to current local.
Also, make sure it exists on remote for desync check.
Fixes PROD-35116
Change-Id: Ic4a5ca66537f9db5f28e4444454d40608f36bae9
diff --git a/telegraf/files/script/reclass_monitor.py b/telegraf/files/script/reclass_monitor.py
index c209ae9..f514e63 100644
--- a/telegraf/files/script/reclass_monitor.py
+++ b/telegraf/files/script/reclass_monitor.py
@@ -10,8 +10,8 @@
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--git-dir", default="/srv/salt/reclass")
- parser.add_argument("--remote", default="origin")
- parser.add_argument("--branch", default="master")
+ parser.add_argument("--remote", default="")
+ parser.add_argument("--branch")
return parser.parse_args()
CHANGES_FORMAT = '''reclass_staged_changes,directory={dir} value={staged}
@@ -34,14 +34,44 @@
dir=git_dir, staged=len(staged), unstaged=len(unstaged)
))
- if args.remote.strip() in call_process(
- str.split('git remote show'), cwd=git_dir
- ).splitlines():
- # 0 if local branch is synced with remote, 1 otherwise
- print('reclass_remote_desync,directory={dir} value={val}'.format(
- dir=git_dir, val=int(bool(call_process(
- str.split("git diff {branch}..{remote}/{branch}".format(
- branch=args.branch.strip(), remote=args.remote.strip()
- )), cwd=git_dir
- )))
- ))
+ # if no branch provided, default to current local branch
+ if not args.branch:
+ args.branch = call_process(str.split(
+ 'git rev-parse --abbrev-ref HEAD'
+ ), cwd=git_dir).strip().lstrip('heads/') # branch and tag share name
+ if args.branch == 'HEAD': # detached head, any assumption is bad
+ args.branch = 'master'
+ local_branch = args.branch.strip()
+
+ # remote handling
+ remotes = call_process(str.split('git remote show'), cwd=git_dir).splitlines()
+ remote_ref = '{}/{}'.format(args.remote.strip() or 'origin', local_branch)
+
+ # if no remote provided, try figuring out upstream tracking branch
+ if not args.remote:
+ upstream_ref = call_process(str.split(
+ 'git for-each-ref --format=%(upstream:short) refs/heads/{}'.format(local_branch)
+ ), cwd=git_dir).strip()
+ if upstream_ref and upstream_ref.split('/')[0] in remotes:
+ args.remote = upstream_ref.split('/')[0] # breaks on remotes with slashes
+ remote_ref = upstream_ref
+ else:
+ args.remote = 'origin'
+
+ if args.remote.strip() in remotes:
+ known_refs = call_process(str.split(
+ 'git for-each-ref --format=%(refname:short)'
+ ), cwd=git_dir).splitlines()
+
+ if all([ # check whether we have the local and remote branch with the same name
+ local_branch in known_refs,
+ remote_ref in known_refs
+ ]):
+ # 0 if local branch is synced with remote, 1 otherwise
+ print('reclass_remote_desync,directory={dir} value={val}'.format(
+ dir=git_dir, val=int(bool(call_process(
+ str.split("git diff {local_ref}..{remote_ref}".format(
+ local_ref=local_branch, remote_ref=remote_ref
+ )), cwd=git_dir
+ )))
+ ))