def push_changes_to_fork(branch: str): """ Push changes into dist_git branch * Reset commit with the latest downstream origin * push changes back to origin :param branch: str: Name of branch to sync """ FramboGit.call_git_cmd(f"reset --hard upstream/{branch}") FramboGit.call_git_cmd(f"push origin {branch} --force")
def fetch_pr_origin(number: str, msg: str): """ Fetch specific Pull Request. :param number: PR number to fetch :param msg: message shown in log file """ # Download Upstream repo to temporary directory # 'git fetch origin pull/ID/head:BRANCHNAME or git checkout origin/pr/ID' FramboGit.call_git_cmd( "fetch origin pull/{n}/head:PR{n}".format(n=number), msg=msg)
def test_call_git_cmd_clone(self, tmpdir, url, ok): """Test Git.clone().""" tmpdir = str(tmpdir) if ok: assert Git.call_git_cmd( f"clone --depth=1 --single-branch {url} {tmpdir}") assert isdir(join(tmpdir, ".git")) assert isfile(join(tmpdir, "README.md")) else: with pytest.raises(CalledProcessError): Git.call_git_cmd(f"clone {url} {tmpdir}")
def git_add_all(upstream_msg: str, related_msg: str): """ Add and push all files into the fork. :param upstream_msg: :param related_msg: """ FramboGit.call_git_cmd("add *", msg="Add all") git_status = FramboGit.call_git_cmd("status", msg="Check git status") if "nothing to commit" in git_status: logger.info( "Downstream repository was NOT changed. NOTHING TO COMMIT.") return upstream_msg += f"\n{related_msg}\n" try: commit_msg = " ".join([ f"-m '{msg}'" for msg in upstream_msg.split("\n") if msg != "" ]) FramboGit.call_git_cmd(f"commit {commit_msg}", msg="Commit into distgit") except CalledProcessError: pass try: FramboGit.call_git_cmd("push -u origin", msg="Push changes into git") except CalledProcessError: pass
def clone_repo(clone_url: str, tempdir: str) -> Path: """ Clone git repository from url. :param clone_url: url to clone from, it can also be a local path (directory) :param tempdir: temporary directory, where the git is cloned :return: directory with cloned repo or None """ # clone_url can be url as well as path to directory, try to get the last part (strip .git) reponame = FramboGit.strip_dot_git(clone_url.split("/")[-1]) cloned_dir = Path(tempdir) / reponame clone_success = True try: FramboGit.call_git_cmd( f"clone --recurse-submodules {clone_url} {str(cloned_dir)}") except CalledProcessError: # clone git repository w/o submodule. In case submodules does not exist clone_success = False if not clone_success: try: FramboGit.call_git_cmd(f"clone {clone_url} {str(cloned_dir)}") except CalledProcessError: raise return cloned_dir
def get_changes_from_distgit(url: str): """ Sync fork with the latest changes from downstream origin. * Add downstream origin and upstream * fetch upstream :param url: Str: URL which is add upstream into origin """ FramboGit.call_git_cmd(f"remote -v") remote_defined: bool = False try: remote_defined = FramboGit.call_git_cmd( f"config remote.upstream.url") except subprocess.CalledProcessError: pass # add git remote upstream if it is not defined if not remote_defined: FramboGit.call_git_cmd(f"remote add upstream {url}") FramboGit.call_git_cmd(f"remote update upstream")
def test_create_dot_gitconfig(self, tmpdir): Git.call_git_cmd(f"init {tmpdir}") user_name = "Jara Cimrman" Git.create_dot_gitconfig(user_name=user_name, user_email="mail") assert Git.call_git_cmd(f"config --get user.name").strip() == user_name
def test_strip_dot_git(self, url, expected_result): """Test strip_dot_git().""" assert Git.strip_dot_git(url) == expected_result
def test_parse_gh_repo_nok(self, url): """Test parse_gh_repo().""" assert Git.parse_git_repo(url) is None
def test_parse_gh_repo_ok(self, url): """Test parse_gh_repo().""" assert Git.parse_git_repo(url) == ("foo", "bar") assert Git.get_username_from_git_url(url) == "foo" assert Git.get_reponame_from_git_url(url) == "bar"
def test_call_git_cmd(self): assert Git.call_git_cmd("version").startswith("git version")
def sync_fork_with_upstream(branches_to_sync): for brn in branches_to_sync: FramboGit.call_git_cmd(f"checkout -b {brn} upstream/{brn}") FramboGit.call_git_cmd(f"push origin {brn} --force")
def get_all_branches() -> str: """ Returns list of all branches as for origin as for upstream :return: List of all branches """ return FramboGit.call_git_cmd(f"branch -a", return_output=True)
def test_parse_gh_repo_ok(self, url): """Test parse_gh_repo().""" assert Git.parse_git_repo(url) == ('foo', 'bar') assert Git.get_username_from_git_url(url) == 'foo' assert Git.get_reponame_from_git_url(url) == 'bar'