def copy_p2g(ctx, start):
    """Fill git with content from Perforce."""

    view_name = ctx.config.view_name
    view_dirs = ctx.view_dirs
    git_dir = view_dirs.GIT_DIR
    if not os.path.exists(git_dir):
        LOG.warn("mirror Git repository {} missing, recreating...".format(git_dir))
        # it's not the end of the world if the git repo disappears, just recreate it
        create_git_repo(git_dir)

    # If Perforce client view is empty and git repo is empty, someone is
    # probably trying to push into an empty repo/perforce tree. Let them.
    if _p4_empty(ctx) and _git_empty():
        LOG.info("Nothing to copy from empty view {}".format(view_name))
        return

    # We're not empty anymore, we no longer need this to avoid
    # git push rejection of push to empty repo refs/heads/master.
    delete_empty_repo_branch(view_dirs.GIT_DIR)

    start_at = p4gf_util.git_ref_master()
    if start and start_at:
        raise RuntimeError((  "Cannot use --start={start} when repo already has commits."
                            + " master={start_at}")
                           .format(start=start, start_at=start_at))
    if start:
        start_at = "@{}".format(start)
    elif start_at is None:
        start_at = "@1"

    p4gf_copy_to_git.copy_p4_changes_to_git(ctx, start_at, "#head")

    # Want to exit this function with HEAD and master both pointing to
    # the end of history. If we just copied anything from Perforce to
    # Git, point to the end of Perforce history.
    #
    # Want to leave this function with our temp branch gone. Otherwise
    # pullers will see "origin/git_fusion_temp_branch" and wonder "if
    # it's temp, why does it seem to live forever?"

    # Common: We have a temp branch that has added zero or more
    # commits ahead of master. Move HEAD and master to the temp branch's
    # commit. Move HEAD, detached (~0), first, just in case someone left it on master.
    temp_branch = p4gf_const.P4GF_BRANCH_TEMP + '~0'
    p1 = p4gf_util.popen_no_throw(['git', 'checkout', temp_branch])
    detached_head = (p1['Popen'].returncode == 0)
    if detached_head:
        p4gf_util.popen_no_throw(['git', 'branch', '-f', 'master', temp_branch])

    # Rare: If there are zero p4 changes in this view (yet), our temp
    # branch either does not exist or points nowhere and we were unable
    # to detach head from that temp branch. In that case switch to
    # (empty) branch master, creating it. We really want a master
    # branch, even if empty, so that we can delete the temp branch.
    if not detached_head:
        p4gf_util.popen_no_throw(['git', 'checkout', '-b', 'master'])

    p4gf_util.popen_no_throw(['git', 'branch', '-d', p4gf_const.P4GF_BRANCH_TEMP])
Example #2
0
 def attempt_resync(self):
     """Attempts to sync -k the Git Fusion client to the change that
     corresponds to the HEAD of the Git mirror repository. This prevents
     the obscure "file(s) not on client" error.
     """
     # we assume we are in the GIT_WORK_TREE, which seems to be a safe
     # assumption at this point
     try:
         last_commit = p4gf_util.git_ref_master()
         if last_commit:
             last_changelist_number = self.ctx.mirror.get_change_for_commit(
                 last_commit, self.ctx)
             if last_changelist_number:
                 filerev = "//...@{}".format(last_changelist_number)
                 self._p4run(['sync', '-k', filerev])
     except P4.P4Exception:
         # don't stop the world if we have an error above
         LOG.warn("resync failed with exception", exc_info=True)
 def attempt_resync(self):
     """Attempts to sync -k the Git Fusion client to the change that
     corresponds to the HEAD of the Git mirror repository. This prevents
     the obscure "file(s) not on client" error.
     """
     # we assume we are in the GIT_WORK_TREE, which seems to be a safe
     # assumption at this point
     try:
         last_commit = p4gf_util.git_ref_master()
         if last_commit:
             last_changelist_number = self.ctx.mirror.get_change_for_commit(
                 last_commit, self.ctx)
             if last_changelist_number:
                 filerev = "//...@{}".format(last_changelist_number)
                 self._p4run(['sync', '-k', filerev])
     except P4.P4Exception:
         # don't stop the world if we have an error above
         LOG.warn("resync failed with exception", exc_info=True)
Example #4
0
def copy_p2g(ctx, start):
    """Fill git with content from Perforce."""

    view_name = ctx.config.view_name
    view_dirs = ctx.view_dirs
    git_dir = view_dirs.GIT_DIR
    if not os.path.exists(git_dir):
        LOG.warn(
            "mirror Git repository {} missing, recreating...".format(git_dir))
        # it's not the end of the world if the git repo disappears, just recreate it
        create_git_repo(git_dir)

    # If Perforce client view is empty and git repo is empty, someone is
    # probably trying to push into an empty repo/perforce tree. Let them.
    if _p4_empty(ctx) and _git_empty():
        LOG.info("Nothing to copy from empty view {}".format(view_name))
        return

    # We're not empty anymore, we no longer need this to avoid
    # git push rejection of push to empty repo refs/heads/master.
    delete_empty_repo_branch(view_dirs.GIT_DIR)

    start_at = p4gf_util.git_ref_master()
    if start and start_at:
        raise RuntimeError(
            ("Cannot use --start={start} when repo already has commits." +
             " master={start_at}").format(start=start, start_at=start_at))
    if start:
        start_at = "@{}".format(start)
    elif start_at is None:
        start_at = "@1"

    p4gf_copy_to_git.copy_p4_changes_to_git(ctx, start_at, "#head")

    # Want to exit this function with HEAD and master both pointing to
    # the end of history. If we just copied anything from Perforce to
    # Git, point to the end of Perforce history.
    #
    # Want to leave this function with our temp branch gone. Otherwise
    # pullers will see "origin/git_fusion_temp_branch" and wonder "if
    # it's temp, why does it seem to live forever?"

    # Common: We have a temp branch that has added zero or more
    # commits ahead of master. Move HEAD and master to the temp branch's
    # commit. Move HEAD, detached (~0), first, just in case someone left it on master.
    temp_branch = p4gf_const.P4GF_BRANCH_TEMP + '~0'
    p1 = p4gf_util.popen_no_throw(['git', 'checkout', temp_branch])
    detached_head = (p1['Popen'].returncode == 0)
    if detached_head:
        p4gf_util.popen_no_throw(
            ['git', 'branch', '-f', 'master', temp_branch])

    # Rare: If there are zero p4 changes in this view (yet), our temp
    # branch either does not exist or points nowhere and we were unable
    # to detach head from that temp branch. In that case switch to
    # (empty) branch master, creating it. We really want a master
    # branch, even if empty, so that we can delete the temp branch.
    if not detached_head:
        p4gf_util.popen_no_throw(['git', 'checkout', '-b', 'master'])

    p4gf_util.popen_no_throw(
        ['git', 'branch', '-d', p4gf_const.P4GF_BRANCH_TEMP])