Beispiel #1
0
def restore_temp_commit(flags, restore_data):
    """
    Restores the initial state before a temp commit was created.
    - restore_data  -- The restore data returned from the
                        'create_temp_commit' function.
    """
    try:
        # Restore branch.
        if restore_data[0] != get_branch():
            log(flags, "Switching active branch to \'" + restore_data[0] +
                "\'")
            switch_branch(restore_data[0])

        # Check if changes have been stashed (and temporary commit created).
        if restore_data[2] is not None:
            log(flags, "Resetting branch \'" +
                restore_data[0] + "\'to commit \'" +
                restore_data[1] + "\'")
            reset_branch(flags, restore_data[0], restore_data[1])

            log(flags, "Restoring uncommitted changes from stash to " +
                "branch \'" + restore_data[0] + "\'")
            apply_stash(flags, restore_data[0], drop=True)
    except Error as err:
        raise OpError(err)
Beispiel #2
0
def create_temp_commit(flags):
    """
    Commits any uncommitted changes on the current
    branch to a temporary commit.
    - branch    -- The branch to tag.
    Returns the a tuple with the branch name, commit id and
    stash name, used to restore the initial state with the
    'restore_temp_commit' function.
    If no changes can be committed the stash name is set to 'None'.
    """
    try:
        # Save the current branch
        current_branch = get_branch()
        log(flags, "Saving current branch name \'{0}\' ".format(current_branch))

        # Try to get the HEAD commit id of the current branch.
        head_commit = get_head_commit(current_branch)

        # Check for uncommitted changes.
        if not is_working_dir_clean():
            log(flags, "Stashing uncommitted changes on branch \'{0}\'".
                format(current_branch))
            # Save changes to tmp stash.
            stash_name = "gbpx<{0}>".format(head_commit)
            stash_changes(flags, stash_name)

            # Apply stash and create a temporary commit.
            log(flags, "Creating temporary commit on branch \'{0}\'".
                format(current_branch))
            apply_stash(flags, current_branch, drop=False)
            commit_changes(flags, "Temp \'{0}\' commit.".format(current_branch))
        else:
            stash_name = None
            log(flags, "Working directory clean, no commit needed")

        return current_branch, head_commit, stash_name
    except Error as err:
        raise OpError(err)