Exemple #1
0
def branch(args):
    repo=_get_repo()
    
    parser = argparse.ArgumentParser(prog='git branch'
                                     , description="List, create, or delete branches")
    #list
    list_grp=parser.add_mutually_exclusive_group(required= False)
    list_grp.add_argument('-r','--remotes',action='store_true',help='list or delete remotep tracking branches')
    list_grp.add_argument('-a','--all',action='store_true',help='list both remote and local branches') 
    
    # move type commands
    move_type=parser.add_mutually_exclusive_group(required=False)
    move_type.add_argument('-m','--move', nargs='+', metavar=('[oldbranch]','newbranch'), help='move/rename oldbranch or HEAD')
    move_type.add_argument('-M',nargs='+',metavar=('[oldbranch]','newbranch'),help='move/rename even if branch already exists')

    # delete type commands
    delete_flags=parser.add_mutually_exclusive_group(required=False)
    delete_flags.add_argument('-d','--delete', nargs=1, metavar=('branchname'), help='delete branchname,TODO: branch must be fully merged with upstream ')
    delete_flags.add_argument('-D',nargs=1,metavar=('branchname'),help='Delete a branch irrespective of its merged status.')

    # misc flags
    parser.add_argument('-v','--verbose',action='count', help='When in list mode, show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given twice, print the name of the upstream branch, as well (see also git remote show <remote>).')
    parser.add_argument('-f','--force',action='store_true', help='Reset <branchname> to <startpoint> if <branchname> exists already. Without -f git branch refuses to change an existing branch.')
    abbrevgrp=parser.add_mutually_exclusive_group()
    abbrevgrp.add_argument('--abbrev',action='store',nargs='?',help='set number of characters to display in sha',type=int,default=7)
    abbrevgrp.add_argument('--no-abbrev',action='store_const',help='do not abbreviate sha ',const=40,dest='abbrev')
    track_flags=parser.add_mutually_exclusive_group(required=False )

    track_flags.add_argument('--set-upstream',action='store', nargs=2, metavar=('branchname','upstream') ,help='set branchname to track upstream')
    track_flags.add_argument('--no-track', nargs='+',metavar=('branchname','startpoint'),help='set existing branch to not track, or create new branch that doesnt track')
    
    
    # add_branch 
    parser.add_argument('branchname',nargs='?')
    parser.add_argument('startpoint',nargs='?')

    
    parser.add_argument('--edit_description',action='store',nargs='?',metavar='branchname', const=repo.active_branch)
    
    
    result = parser.parse_args(args)

    # combine args
    edit_description=result.edit_description
    delete_branchname=result.delete or result.D
    move_branchname = result.move or result.M
    no_track=result.no_track
    add_branchname = (result.branchname, result.startpoint or repo.active_branch)
    set_upstream= result.set_upstream

    force = result.force or result.D or result.M
    mutual_exclusive_list=( delete_branchname, 
                           move_branchname, 
                           edit_description, 
                           result.branchname, 
                           set_upstream,
                           no_track)
    list_flag=not any_one(mutual_exclusive_list)
    
    if not any_one((list_flag,)+ mutual_exclusive_list):
        raise GitError('too many options specified.\n'+parser.print_help())
        
    
    if list_flag:
        branch_list(result) 
    elif delete_branchname:
        delete_branch(delete_branchname[0], force , result.remotes, result.verbose)
    elif move_branchname:
        move_branch(move_branchname, force, result.verbose)
    elif add_branchname[0]:
        create_branch(add_branchname[0],add_branchname[1],force,False )
    elif edit_description:
        edit_branch_description(edit_description)
    elif set_upstream:
        add_tracking(set_upstream[0], *( ['origin']+set_upstream[1].split('/'))[-2:])
        print set_upstream[0], format_tracking_branch_desc(repo,set_upstream[0])
    elif no_track:
        if len(no_track)==1:
            remove_tracking(no_track[0])
        else:
            create_branch(no_track[0],no_track[1],force,True)
Exemple #2
0
def branch(args):
    repo = _get_repo()

    parser = argparse.ArgumentParser(
        prog='git branch', description="List, create, or delete branches")
    #list
    list_grp = parser.add_mutually_exclusive_group(required=False)
    list_grp.add_argument('-r',
                          '--remotes',
                          action='store_true',
                          help='list or delete remotep tracking branches')
    list_grp.add_argument('-a',
                          '--all',
                          action='store_true',
                          help='list both remote and local branches')

    # move type commands
    move_type = parser.add_mutually_exclusive_group(required=False)
    move_type.add_argument('-m',
                           '--move',
                           nargs='+',
                           metavar=('[oldbranch]', 'newbranch'),
                           help='move/rename oldbranch or HEAD')
    move_type.add_argument('-M',
                           nargs='+',
                           metavar=('[oldbranch]', 'newbranch'),
                           help='move/rename even if branch already exists')

    # delete type commands
    delete_flags = parser.add_mutually_exclusive_group(required=False)
    delete_flags.add_argument(
        '-d',
        '--delete',
        nargs=1,
        metavar=('branchname'),
        help=
        'delete branchname,TODO: branch must be fully merged with upstream ')
    delete_flags.add_argument(
        '-D',
        nargs=1,
        metavar=('branchname'),
        help='Delete a branch irrespective of its merged status.')

    # misc flags
    parser.add_argument(
        '-v',
        '--verbose',
        action='count',
        help=
        'When in list mode, show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given twice, print the name of the upstream branch, as well (see also git remote show <remote>).'
    )
    parser.add_argument(
        '-f',
        '--force',
        action='store_true',
        help=
        'Reset <branchname> to <startpoint> if <branchname> exists already. Without -f git branch refuses to change an existing branch.'
    )
    abbrevgrp = parser.add_mutually_exclusive_group()
    abbrevgrp.add_argument('--abbrev',
                           action='store',
                           nargs='?',
                           help='set number of characters to display in sha',
                           type=int,
                           default=7)
    abbrevgrp.add_argument('--no-abbrev',
                           action='store_const',
                           help='do not abbreviate sha ',
                           const=40,
                           dest='abbrev')
    track_flags = parser.add_mutually_exclusive_group(required=False)

    track_flags.add_argument('--set-upstream',
                             action='store',
                             nargs=2,
                             metavar=('branchname', 'upstream'),
                             help='set branchname to track upstream')
    track_flags.add_argument(
        '--no-track',
        nargs='+',
        metavar=('branchname', 'startpoint'),
        help=
        'set existing branch to not track, or create new branch that doesnt track'
    )

    # add_branch
    parser.add_argument('branchname', nargs='?')
    parser.add_argument('startpoint', nargs='?')

    parser.add_argument('--edit_description',
                        action='store',
                        nargs='?',
                        metavar='branchname',
                        const=repo.active_branch)

    result = parser.parse_args(args)

    # combine args
    edit_description = result.edit_description
    delete_branchname = result.delete or result.D
    move_branchname = result.move or result.M
    no_track = result.no_track
    add_branchname = (result.branchname, result.startpoint
                      or repo.active_branch)
    set_upstream = result.set_upstream

    force = result.force or result.D or result.M
    mutual_exclusive_list = (delete_branchname, move_branchname,
                             edit_description, result.branchname, set_upstream,
                             no_track)
    list_flag = not any_one(mutual_exclusive_list)

    if not any_one((list_flag, ) + mutual_exclusive_list):
        raise GitError('too many options specified.\n' + parser.print_help())

    if list_flag:
        branch_list(result)
    elif delete_branchname:
        delete_branch(delete_branchname[0], force, result.remotes,
                      result.verbose)
    elif move_branchname:
        move_branch(move_branchname, force, result.verbose)
    elif add_branchname[0]:
        create_branch(add_branchname[0], add_branchname[1], force, False)
    elif edit_description:
        edit_branch_description(edit_description)
    elif set_upstream:
        add_tracking(set_upstream[0],
                     *(['origin'] + set_upstream[1].split('/'))[-2:])
        print set_upstream[0], format_tracking_branch_desc(
            repo, set_upstream[0])
    elif no_track:
        if len(no_track) == 1:
            remove_tracking(no_track[0])
        else:
            create_branch(no_track[0], no_track[1], force, True)