def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) self.remote = self.get_opt('remote') validate_chars(self.remote, 'remote', r'A-Za-z0-9_\.-') try: repo = git.Repo(directory) except git.InvalidGitRepositoryError as _: raise CriticalError("directory '{}' does not contain a valid Git repository!".format(directory)) try: if not self.get_opt('no_fetch'): log.info('fetching from remote repo: {}'.format(self.remote)) repo.git.fetch(self.remote) branch = repo.active_branch log.info('active branch: %s', branch) commits_behind = repo.iter_commits('{branch}..{remote}/{branch}'.format(remote=self.remote, branch=branch)) commits_ahead = repo.iter_commits('{remote}/{branch}..{branch}'.format(remote=self.remote, branch=branch)) num_commits_behind = sum(1 for c in commits_behind) num_commits_ahead = sum(1 for c in commits_ahead) # happens with detached HEAD checkout like Travis CI does except TypeError as _: raise CriticalError(_) except git.GitCommandError as _: raise CriticalError(', '.join(str(_.stderr).split('\n'))) self.msg = "git checkout branch '{}' is ".format(branch) if num_commits_ahead + num_commits_behind == 0: self.ok() self.msg += 'up to date with' else: self.critical() self.msg += '{} commits behind, {} commits ahead of'.format(num_commits_behind, num_commits_ahead) self.msg += " remote '{}'".format(self.remote) self.msg += ' | commits_behind={};0;0 commits_ahead={};0;0'.format(num_commits_behind, num_commits_ahead)
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) expected_branch = self.get_opt('branch') if expected_branch is None: self.usage('expected branch not defined') if not re.match(r'^[\w\s-]+$', expected_branch): self.usage('Invalid branch name given, must be alphanumeric' + \ ', may contain dashes and spaces for detached HEADs') log_option('expected branch', expected_branch) try: repo = git.Repo(directory) except InvalidGitRepositoryError as _: raise CriticalError("directory '{}' does not contain a valid Git repository!".format(directory)) try: current_branch = repo.active_branch.name # happens with detached HEAD checkout like Travis CI does except TypeError as _: raise CriticalError(_) if current_branch == expected_branch: self.ok() self.msg = "git branch '{0}' currently checked out in directory '{1}'"\ .format(current_branch, directory) else: raise CriticalError("git branch '{0}' checked out, expecting '{1}' in directory '{2}'" .format(current_branch, expected_branch, directory))
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) expected_branch = self.get_opt('branch') if expected_branch is None: self.usage('expected branch not defined') if not re.match(r'^[\w-]+$', expected_branch): self.usage( 'Invalid branch name given, must be alphanumeric, may contain dashes' ) log_option('expected branch', expected_branch) repo = git.Repo(directory) try: current_branch = repo.active_branch.name # happens with detached HEAD checkout like Travis CI does except TypeError as _: raise CriticalError(_) if current_branch == expected_branch: self.ok() self.msg = "branch '{0}' currently checked out in directory '{1}'"\ .format(current_branch, directory) else: raise CriticalError( "branch '{0}' checked out, expecting '{1}' in directory '{2}'". format(current_branch, expected_branch, directory))
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) expected_branch = self.get_opt('branch') if expected_branch is None: self.usage('expected branch not defined') if not re.match(r'^[\w-]+$', expected_branch): self.usage( 'Invalid branch name given, must be alphanumeric, may contain dashes' ) log_option('expected branch', expected_branch) repo = git.Repo(directory) current_branch = repo.active_branch.name if current_branch == expected_branch: qquit( 'OK', "branch '{0}' currently checked out in directory '{1}'".format( current_branch, directory)) else: qquit( 'CRITICAL', "branch '{0}' checked out, expecting '{1}' in directory '{2}'". format(current_branch, expected_branch, directory))
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) expected_branch = self.get_opt('branch') if expected_branch is None: self.usage('expected branch not defined') if not re.match(r'^[\w\s-]+$', expected_branch): self.usage('Invalid branch name given, must be alphanumeric' + \ ', may contain dashes and spaces for detached HEADs') log_option('expected branch', expected_branch) try: repo = git.Repo(directory) except git.InvalidGitRepositoryError as _: raise CriticalError( "directory '{}' does not contain a valid Git repository!". format(directory)) try: current_branch = repo.active_branch.name # happens with detached HEAD checkout like Travis CI does except TypeError as _: raise CriticalError(_) if current_branch == expected_branch: self.ok() self.msg = "git branch '{0}' currently checked out in directory '{1}'"\ .format(current_branch, directory) else: raise CriticalError( "git branch '{current_branch}' checked out".format( current_branch=current_branch) + ", expecting branch '{expected_branch}' in directory '{directory}'" .format(expected_branch=expected_branch, directory=directory))
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) self.remote = self.get_opt('remote') validate_chars(self.remote, 'remote', r'A-Za-z0-9_\.-') try: repo = git.Repo(directory) except InvalidGitRepositoryError as _: raise CriticalError("directory '{}' does not contain a valid Git repository!".format(directory)) try: if not self.get_opt('no_fetch'): log.info('fetching from remote repo: {}'.format(self.remote)) repo.git.fetch(self.remote) branch = repo.active_branch log.info('active branch: %s', branch) commits_behind = repo.iter_commits('{branch}..{remote}/{branch}'.format(remote=self.remote, branch=branch)) commits_ahead = repo.iter_commits('{remote}/{branch}..{branch}'.format(remote=self.remote, branch=branch)) num_commits_behind = sum(1 for c in commits_behind) num_commits_ahead = sum(1 for c in commits_ahead) # happens with detached HEAD checkout like Travis CI does except TypeError as _: raise CriticalError(_) except GitCommandError as _: raise CriticalError(', '.join(str(_.stderr).split('\n'))) self.msg = "git checkout branch '{}' is ".format(branch) if num_commits_ahead + num_commits_behind == 0: self.ok() self.msg += 'up to date with' else: self.critical() self.msg += '{} commits behind, {} commits ahead of'.format(num_commits_behind, num_commits_ahead) self.msg += " remote '{}'".format(self.remote) self.msg += ' | commits_behind={};0;0 commits_ahead={};0;0'.format(num_commits_behind, num_commits_ahead)
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) try: repo = git.Repo(directory) except InvalidGitRepositoryError: raise CriticalError("directory '{}' does not contain a valid Git repository!".format(directory)) is_valid = repo.head.is_valid() self.msg = "git checkout valid = '{}' for directory '{}'".format(is_valid, directory) if not is_valid: self.critical()
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) try: repo = git.Repo(directory) except git.InvalidGitRepositoryError: raise CriticalError("directory '{}' does not contain a valid Git repository!".format(directory)) is_detached = repo.head.is_detached self.msg = "git checkout detached = '{}' for directory '{}'".format(is_detached, directory) if is_detached: self.critical()
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) try: repo = git.Repo(directory) except InvalidGitRepositoryError as _: raise CriticalError( "directory '{}' does not contain a valid Git repository!". format(directory)) try: untracked_files = repo.untracked_files num_untracked_files = len(untracked_files) changed_files = [item.a_path for item in repo.index.diff(None)] changed_files = [ filename for filename in changed_files if filename not in untracked_files ] num_changed_files = len(changed_files) except InvalidGitRepositoryError as _: raise CriticalError(_) except TypeError as _: raise CriticalError(_) self.msg = '{} changed file{}'.format(num_changed_files, plural(num_changed_files)) self.msg += ', {} untracked file{}'.format(num_untracked_files, plural(num_untracked_files)) self.msg += " in Git checkout at directory '{}'".format(directory) uncommitted_staged_changes = 0 if changed_files or untracked_files: self.critical() if self.verbose: if changed_files: self.msg += ' (changed files: {})'.format( ', '.join(changed_files)) if untracked_files: self.msg += ' (untracked files: {})'.format( ', '.join(untracked_files)) elif repo.is_dirty(): self.msg += ', uncommitted staged changes detected!' self.critical() uncommitted_staged_changes = 1 self.msg += ' | changed_files={};0;0 untracked_files={};0;0'.format( num_changed_files, num_untracked_files) self.msg += ' uncommitted_staged_changes={};0;0'.format( uncommitted_staged_changes)
def run(self): self.no_args() directory = self.get_opt('directory') directory = os.path.abspath(directory) expected_branch = self.get_opt('branch') validate_directory(directory) if expected_branch is None: self.usage('expected branch not defined') if not re.match(r'^[\w-]+$', expected_branch): self.usage('Invalid branch name given, must be alphanumeric') log_option('expected branch', expected_branch) repo = git.Repo(directory) current_branch = repo.active_branch.name if current_branch == expected_branch: qquit('OK', "branch '{0}' currently checked out in directory '{1}'" .format(current_branch, directory)) else: qquit('CRITICAL', "branch '{0}' checked out, expecting '{1}' in directory '{2}'" .format(current_branch, expected_branch, directory))
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) try: repo = git.Repo(directory) except git.InvalidGitRepositoryError: raise CriticalError( "directory '{}' does not contain a valid Git repository!". format(directory)) is_bare = repo.bare self.msg = "git checkout bare = '{}' for directory '{}'".format( is_bare, directory) if self.get_opt('not_bare'): if is_bare: self.critical() else: if not is_bare: self.critical()
def run(self): self.no_args() directory = self.get_opt('directory') validate_directory(directory) directory = os.path.abspath(directory) try: repo = git.Repo(directory) except InvalidGitRepositoryError as _: raise CriticalError("directory '{}' does not contain a valid Git repository!".format(directory)) try: untracked_files = repo.untracked_files num_untracked_files = len(untracked_files) changed_files = [item.a_path for item in repo.index.diff(None)] changed_files = [filename for filename in changed_files if filename not in untracked_files] num_changed_files = len(changed_files) except InvalidGitRepositoryError as _: raise CriticalError(_) except TypeError as _: raise CriticalError(_) self.msg = '{} changed file{}'.format(num_changed_files, plural(num_changed_files)) self.msg += ', {} untracked file{}'.format(num_untracked_files, plural(num_untracked_files)) self.msg += " in Git checkout at directory '{}'".format(directory) uncommitted_staged_changes = 0 if changed_files or untracked_files: self.critical() if self.verbose: if changed_files: self.msg += ' (changed files: {})'.format(', '.join(changed_files)) if untracked_files: self.msg += ' (untracked files: {})'.format(', '.join(untracked_files)) elif repo.is_dirty(): self.msg += ', uncommitted staged changes detected!' self.critical() uncommitted_staged_changes = 1 self.msg += ' | changed_files={};0;0 untracked_files={};0;0'.format(num_changed_files, num_untracked_files) self.msg += ' uncommitted_staged_changes={};0;0'.format(uncommitted_staged_changes)