def get_commit_list(self, old_commit, new_commit): """Gets the list of commits(inclusive) between the old and new commits. Args: old_commit: The oldest commit to be in the list new_commit: The newest commit to be in the list Returns: The list of commit SHAs from newest to oldest Raises: RepoManagerError when commits dont exist """ if not self.commit_exists(old_commit): raise RepoManagerError('The old commit %s does not exist' % old_commit) if not self.commit_exists(new_commit): raise RepoManagerError('The new commit %s does not exist' % new_commit) if old_commit == new_commit: return [old_commit] out, err_code = build_specified_commit.execute( ['git', 'rev-list', old_commit + '..' + new_commit], self.repo_dir) commits = out.split('\n') commits = [commit for commit in commits if commit] if err_code or not commits: raise RepoManagerError( 'Error getting commit list between %s and %s ' % (old_commit, new_commit)) # Make sure result is inclusive commits.append(old_commit) return commits
def get_current_commit(self): """Gets the current commit SHA of the repo. Returns: The current active commit SHA """ out, _ = build_specified_commit.execute(['git', 'rev-parse', 'HEAD'], self.repo_dir, check_result=True) return out.strip('\n')
def checkout_commit(self, commit): """Checks out a specific commit from the repo. Args: commit: The commit SHA to be checked out Raises: RepoManagerError when checkout is not successful """ if not self.commit_exists(commit): raise RepoManagerError( 'Commit %s does not exist in current branch' % commit) git_path = os.path.join(self.repo_dir, '.git', 'shallow') if os.path.exists(git_path): build_specified_commit.execute(['git', 'fetch', '--unshallow'], self.repo_dir, check_result=True) build_specified_commit.execute(['git', 'checkout', '-f', commit], self.repo_dir, check_result=True) build_specified_commit.execute(['git', 'clean', '-fxd'], self.repo_dir, check_result=True) if self.get_current_commit() != commit: raise RepoManagerError('Error checking out commit %s' % commit)
def _clone(self): """Creates a clone of the repo in the specified directory. Raises: RepoManagerError if the repo was not able to be cloned """ if not os.path.exists(self.base_dir): os.makedirs(self.base_dir) self.remove_repo() out, err = build_specified_commit.execute( ['git', 'clone', self.repo_url], location=self.base_dir) if not self._is_git_repo(): raise RepoManagerError('%s is not a git repo' % self.repo_url)
def commit_exists(self, commit): """Checks to see if a commit exists in the project repo. Args: commit: The commit SHA you are checking Returns: True if the commit exits in the project Raises: ValueException: an empty string was passed in as a commit """ # Handle the exception case, if empty string is passed execute will # raise a ValueError if not commit.rstrip(): raise RepoManagerError('An empty string is not a valid commit SHA') _, err_code = build_specified_commit.execute( ['git', 'cat-file', '-e', commit], self.repo_dir) return not err_code