Commit 18d1ac22 authored by Elouan Martinet's avatar Elouan Martinet
Browse files

Add upstream-fork support

parent fb80150ec1a6
......@@ -27,7 +27,31 @@ def cmd(description, *args, **kwargs):
def git_clone(path_url, destination):
cmd("Cloning Git repository", ["git", "clone", path_url, destination, "--bare"])
cmd(
"Cloning Git fork repository", ["git", "clone", path_url, destination, "--bare"]
)
def git_configure_upstream(git_repo, git_upstream_url):
cmd(
"Adding upstream remote",
["git", "remote", "add", "upstream", git_upstream_url],
cwd=git_repo,
)
git_pull_master(git_repo)
def git_pull_master(git_repo):
cmd(
"Fetching upstream remote",
["git", "fetch", "upstream", "master:master"],
cwd=git_repo,
)
cmd(
"Pushing master branch to Git fork repository",
["git", "push", "-f", "origin", "master"],
cwd=git_repo,
)
def hg_clone(path, destination):
......@@ -52,7 +76,7 @@ gitrepo = .hg/git.git
)
def _ghclone(path_url, destination):
def _ghclone(git_fork_url, git_upstream_url, destination):
working_dir, project_name = osp.split(destination)
git_repo = osp.abspath(osp.join(working_dir, ".%s.git" % project_name))
if osp.isdir(destination):
......@@ -63,7 +87,8 @@ def _ghclone(path_url, destination):
)
else:
os.mkdir(destination)
git_clone(path_url, git_repo)
git_clone(git_fork_url, git_repo)
git_configure_upstream(git_repo, git_upstream_url)
hg_clone(git_repo, destination)
final_git_repo = osp.abspath(osp.join(destination, ".hg", "git.git"))
move(git_repo, final_git_repo)
......@@ -74,12 +99,14 @@ def _ghpull(git_repo):
branches = check_output(
["git", "branch", "--format=%(refname:lstrip=2)"], cwd=git_repo
)
git_pull_master(git_repo)
for branch in branches.splitlines():
cmd(
"Fetching branch %s from remote Git repository" % branch,
["git", "fetch", "origin", "%s:%s" % (branch, branch)],
cwd=git_repo,
)
if branch != "master":
cmd(
"Fetching branch %s from remote Git fork repository" % branch,
["git", "fetch", "origin", "%s:%s" % (branch, branch)],
cwd=git_repo,
)
cmd(
"Updating Mercurial repository",
......@@ -89,23 +116,27 @@ def _ghpull(git_repo):
def _ghpush(git_repo, bookmark):
if bookmark == "master":
raise ValueError("Cannot push master branch")
cmd(
"Pushing bookmark to local Git repository",
["hg", "push", "-B", bookmark, "-f"],
)
# TODO use --force-with-lease if possible
cmd(
"Publish Git branch to remote Git repository",
"Pushing Git branch to Git fork repository",
["git", "push", "--set-upstream", "origin", bookmark, "-f"],
cwd=git_repo,
)
@command("ghclone", [], _("git_path working_directory"), norepo=True)
def ghclone(ui, git_url, working_directory, **opts):
@command(
"ghclone", [], _("git_fork_path git_upstream_path working_directory"), norepo=True
)
def ghclone(ui, git_fork_url, git_upstream_url, working_directory, **opts):
"""Prepare working directory to work with github"""
try:
_ghclone(git_url, working_directory)
_ghclone(git_fork_url, git_upstream_url, working_directory)
except CalledProcessError:
pass
except ValueError as ex:
......@@ -130,3 +161,5 @@ def ghpush(ui, repo, bookmark, **opts):
_ghpush(git_repo, bookmark)
except CalledProcessError:
pass
except ValueError as ex:
print(ex)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment