def check_git_branches_upstream(self, target):
     target = os.path.abspath(target)
     gitroot = find_git_root(target)
     if gitroot is None:
         die('Failed to find git root for target {0}'.format(target))
     log.debug("finding branches for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     branches = repo.branches
     if self.branch_prefix is not None:
         log.debug('restricting to branches matching branch prefix')
         branches = [x for x in branches if self.branch_prefix.match(str(x))]
         if not branches:
             log.error("No branches matching '%s' for target '%s'", self.get_opt('branch_prefix'), target)
             self.status = 'NO BRANCHES'
     #if log.isEnabledFor(logging.DEBUG):
     #log.debug('\n\nbranches for target %s:\n\n%s\n', target, '\n'.join(list(branches)))
     for branch in branches:
         expected = '{0}/{1}'.format(self.origin, branch)
         tracking_branch = str(branch.tracking_branch())
         if tracking_branch == expected:
             log.info("OK: repo '{0}' branch '{1}' is tracking '{2}'"
                      .format(gitroot, branch, tracking_branch))
         else:
             self.status = "ERROR"
             log.error("BAD: branch '{0}' is tracking '{1}' (expected '{2}')"
                       .format(branch, tracking_branch, expected))
 def check_git_tags_dockerfiles(self, target):
     target = os.path.abspath(target)
     gitroot = find_git_root(target)
     log.debug("finding tags for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     tags = [str(x).split('/')[-1] for x in repo.tags]
     if self.tag_prefix is not None:
         log.debug('restricting to tags matching tag prefix')
         tags = [x for x in tags if self.tag_prefix.match(x)]
     #if log.isEnabledFor(logging.DEBUG):
     log.debug('\n\ntags for target %s:\n\n%s\n', target, '\n'.join(tags))
     original_checkout = 'master'
     try:
         try:
             original_checkout = repo.active_branch.name
         except TypeError as _:
             pass
         for tag in tags:
             log.debug("checking tag '%s' Dockerfiles for target '%s'", tag,
                       target)
             try:
                 repo.git.checkout(tag)
             except git.GitError as _:
                 die(_)
             self.check_path(target, tag)
     except Exception as _:  # pylint: disable=broad-except
         die(_)
     finally:
         log.debug("returning to original checkout '%s'", original_checkout)
         repo.git.checkout(original_checkout)
 def check_git_tags_dockerfiles(self, target):
     target = os.path.abspath(target)
     gitroot = find_git_root(target)
     log.debug("finding tags for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     tags = [str(x).split("/")[-1] for x in repo.tags]
     if self.tag_prefix is not None:
         log.debug("restricting to tags matching tag prefix")
         tags = [x for x in tags if self.tag_prefix.match(x)]
     # if log.isEnabledFor(logging.DEBUG):
     log.debug("\n\ntags for target %s:\n\n%s\n", target, "\n".join(tags))
     original_checkout = "master"
     try:
         try:
             original_checkout = repo.active_branch.name
         except TypeError as _:
             pass
         for tag in tags:
             log.debug("checking tag '%s' Dockerfiles for target '%s'", tag, target)
             try:
                 repo.git.checkout(tag)
             except git.exc.GitCommandError as _:
                 die(_)
             self.check_path(target, tag)
     except Exception as _:  # pylint: disable=broad-except
         die(_)
     finally:
         log.debug("returning to original checkout '%s'", original_checkout)
         repo.git.checkout(original_checkout)
 def check_git_branches_dockerfiles(self, target):
     gitroot = find_git_root(target)
     if gitroot is None:
         die('Failed to find git root for target {0}'.format(target))
     log.debug("finding branches for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     branches = [str(x) for x in repo.refs if isinstance(x, git.refs.remote.RemoteReference)]
     branches = [x.split('/')[-1] for x in branches]
     branches = [x for x in branches if x not in ('HEAD', 'master')]
     if self.branch_prefix is not None:
         log.debug('restricting to branches matching branch prefix')
         branches = [x for x in branches if self.branch_prefix.match(x)]
     #if log.isEnabledFor(logging.DEBUG):
     log.debug('\n\nbranches for target %s:\n\n%s\n', target, '\n'.join(branches))
     original_checkout = 'master'
     try:
         try:
             original_checkout = repo.active_branch.name
         except TypeError as _:
             pass
         for branch in branches:
             log.debug("checking branch '%s' Dockerfiles for target '%s'", branch, target)
             self.branches_checked += 1
             try:
                 repo.git.checkout(branch)
             except git.exc.GitCommandError as _:
                 die(_)
             self.check_path(target, branch)
     except Exception as _:  # pylint: disable=broad-except
         traceback.print_exc()
         sys.exit(1)
     finally:
         log.debug("returning to original checkout '%s'", original_checkout)
         repo.git.checkout(original_checkout)
 def check_git_branches_upstream(self, target):
     target = os.path.abspath(target)
     gitroot = find_git_root(target)
     if gitroot is None:
         die('Failed to find git root for target {0}'.format(target))
     log.debug("finding branches for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     branches = repo.branches
     if self.branch_prefix is not None:
         log.debug('restricting to branches matching branch prefix')
         branches = [
             x for x in branches if self.branch_prefix.match(str(x))
         ]
         if not branches:
             log.error("No branches matching '%s' for target '%s'",
                       self.get_opt('branch_prefix'), target)
             self.status = 'NO BRANCHES'
     #if log.isEnabledFor(logging.DEBUG):
     #log.debug('\n\nbranches for target %s:\n\n%s\n', target, '\n'.join(list(branches)))
     for branch in branches:
         expected = '{0}/{1}'.format(self.origin, branch)
         tracking_branch = str(branch.tracking_branch())
         if tracking_branch == expected:
             log.info(
                 "OK: repo '{0}' branch '{1}' is tracking '{2}'".format(
                     gitroot, branch, tracking_branch))
         else:
             self.status = "ERROR"
             log.error(
                 "BAD: branch '{0}' is tracking '{1}' (expected '{2}')".
                 format(branch, tracking_branch, expected))
 def check_git_branches_upstream(self, target):
     target = os.path.abspath(target)
     gitroot = find_git_root(target)
     if gitroot is None:
         die('Failed to find git root for target {0}'.format(target))
     log.debug("finding branches for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     branches = repo.branches
     if self.branch_prefix is not None:
         log.debug('restricting to branches matching branch prefix')
         branches = [
             x for x in branches if self.branch_prefix.match(str(x))
         ]
         if not branches:
             log.error("No branches matching '%s' for target '%s'",
                       self.get_opt('branch_prefix'), target)
             self.status = 'NO BRANCHES'
     #if log.isEnabledFor(logging.DEBUG):
     #log.debug('\n\nbranches for target %s:\n\n%s\n', target, '\n'.join(list(branches)))
     for branch in branches:
         expected = '{0}/{1}'.format(self.origin, branch)
         # have to str() this as it returns an object that will fail equality match otherwise
         tracking_branch = str(branch.tracking_branch())
         if tracking_branch == expected:
             log.info(
                 "OK: repo '{0}' branch '{1}' is tracking '{2}'".format(
                     gitroot, branch, tracking_branch))
         elif self.get_opt('fix') and tracking_branch == 'None':
             log.warn(
                 "WARN: setting repo '{0}' unconfigured branch '{1}' to track '{2}'"
                 .format(gitroot, branch, expected))
             #print(list(repo.remotes.origin.refs))
             branch.set_tracking_branch(
                 git.refs.remote.RemoteReference(repo, 'refs/remotes/' +
                                                 expected))
         elif self.get_opt('force_fix'):
             log.warn(
                 "WARN: forcibly resetting repo '{0}' branch '{1}' to track '{2}'"
                 .format(gitroot, branch, expected))
             branch.set_tracking_branch(
                 git.refs.remote.RemoteReference(repo, 'refs/remotes/' +
                                                 expected))
         else:
             self.status = "ERROR"
             log.error(
                 "BAD: branch '{0}' is tracking '{1}' (expected '{2}')".
                 format(branch, tracking_branch, expected))
Exemple #7
0
 def check_git_branches_dockerfiles(self, target):
     gitroot = find_git_root(target)
     if gitroot is None:
         die('Failed to find git root for target {0}'.format(target))
     log.debug("finding branches for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     #branches = [str(x) for x in repo.refs if isinstance(x, git.refs.remote.RemoteReference)]
     branches = [str(x) for x in repo.refs if isinstance(x, git.Head)]
     branches = [x.split('/')[-1] for x in branches]
     branches = set(branches)
     branches = [x for x in branches if x not in ('HEAD', 'master')]
     self.branches = branches
     if self.branch_prefix is not None:
         log.debug('restricting to branches matching branch prefix')
         branches = [x for x in branches if self.branch_prefix.match(x)]
     self.selected_branches = branches
     #if log.isEnabledFor(logging.DEBUG):
     log.debug('\n\nbranches for target %s:\n\n%s\n', target,
               '\n'.join(branches))
     # in Travis CI there is no original branch and master branch does not exist, so falling back to assuming master
     # causes failure, better to not check out original branch if you don't know
     #original_branch = 'master'
     original_branch = None
     try:
         try:
             original_branch = repo.active_branch.name
         except TypeError as _:
             pass
         for branch in branches:
             log.debug("checking branch '%s' Dockerfiles for target '%s'",
                       branch, target)
             self.branches_checked += 1
             try:
                 repo.git.checkout(branch)
             except git.GitError as _:
                 die(_)
             self.check_path(target, branch)
     except Exception as _:  # pylint: disable=broad-except
         traceback.print_exc()
         sys.exit(1)
     finally:
         if original_branch != None:
             log.debug("checking out original branch '%s'", original_branch)
             repo.git.checkout(original_branch)
 def check_git_branches_dockerfiles(self, target):
     gitroot = find_git_root(target)
     if gitroot is None:
         die('Failed to find git root for target {0}'.format(target))
     log.debug("finding branches for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     #branches = [str(x) for x in repo.refs if isinstance(x, git.refs.remote.RemoteReference)]
     branches = [str(x) for x in repo.refs if isinstance(x, git.Head)]
     branches = [x.split('/')[-1] for x in branches]
     branches = set(branches)
     branches = [x for x in branches if x not in ('HEAD', 'master')]
     self.branches = branches
     if self.branch_prefix is not None:
         log.debug('restricting to branches matching branch prefix')
         branches = [x for x in branches if self.branch_prefix.match(x)]
     self.selected_branches = branches
     #if log.isEnabledFor(logging.DEBUG):
     log.debug('\n\nbranches for target %s:\n\n%s\n', target,
               '\n'.join(branches))
     original_checkout = 'master'
     try:
         try:
             original_checkout = repo.active_branch.name
         except TypeError as _:
             pass
         for branch in branches:
             log.debug("checking branch '%s' Dockerfiles for target '%s'",
                       branch, target)
             self.branches_checked += 1
             try:
                 repo.git.checkout(branch)
             except git.exc.GitCommandError as _:
                 die(_)
             self.check_path(target, branch)
     except Exception as _:  # pylint: disable=broad-except
         traceback.print_exc()
         sys.exit(1)
     finally:
         log.debug("returning to original checkout '%s'", original_checkout)
         repo.git.checkout(original_checkout)
 def check_git_branches_upstream(self, target):
     target = os.path.abspath(target)
     gitroot = find_git_root(target)
     if gitroot is None:
         die('Failed to find git root for target {0}'.format(target))
     log.debug("finding branches for target '{0}'".format(target))
     repo = git.Repo(gitroot)
     branches = repo.branches
     if self.branch_prefix is not None:
         log.debug('restricting to branches matching branch prefix')
         branches = [x for x in branches if self.branch_prefix.match(str(x))]
         if not branches:
             log.error("No branches matching '%s' for target '%s'", self.get_opt('branch_prefix'), target)
             self.status = 'NO BRANCHES'
     #if log.isEnabledFor(logging.DEBUG):
     #log.debug('\n\nbranches for target %s:\n\n%s\n', target, '\n'.join(list(branches)))
     for branch in branches:
         expected = '{0}/{1}'.format(self.origin, branch)
         # have to str() this as it returns an object that will fail equality match otherwise
         tracking_branch = str(branch.tracking_branch())
         if tracking_branch == expected:
             log.info("OK: repo '{0}' branch '{1}' is tracking '{2}'"
                      .format(gitroot, branch, tracking_branch))
         elif self.get_opt('fix') and tracking_branch == 'None':
             log.warn("WARN: setting repo '{0}' unconfigured branch '{1}' to track '{2}'"
                      .format(gitroot, branch, expected))
             #print(list(repo.remotes.origin.refs))
             branch.set_tracking_branch(git.refs.remote.RemoteReference(repo, 'refs/remotes/' + expected))
         elif self.get_opt('force_fix'):
             log.warn("WARN: forcibly resetting repo '{0}' branch '{1}' to track '{2}'"
                      .format(gitroot, branch, expected))
             branch.set_tracking_branch(git.refs.remote.RemoteReference(repo, 'refs/remotes/' + expected))
         else:
             self.status = "ERROR"
             log.error("BAD: branch '{0}' is tracking '{1}' (expected '{2}')"
                       .format(branch, tracking_branch, expected))