@subcommand.usage('<patch url or issue id or issue url>')
def CMDpatch(parser, args):
    'Patches in a code review.'
    parser.add_option('-b', dest='newbranch', help='create a new branch off trunk for the patch')
    parser.add_option('-f', '--force', action='store_true', help='with -b, clobber any existing branch')
    parser.add_option('-d', '--directory', action='store', metavar='DIR', help='Change to the directory DIR immediately, before doing anything else. Rietveld only.')
    parser.add_option('--reject', action='store_true', help='failed patches spew .rej files rather than attempting a 3-way merge. Rietveld only.')
    parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit', help="don't commit after patch applies. Rietveld only.")
    group = optparse.OptionGroup(parser, 'Options for continuing work on the current issue uploaded from a different clone (e.g. different machine). Must be used independently from the other options. No issue number should be specified, and the branch must have an issue number associated with it')
    group.add_option('--reapply', action='store_true', dest='reapply', help='Reset the branch and reapply the issue.\nCAUTION: This will undo any local changes in this branch')
    group.add_option('--pull', action='store_true', dest='pull', help='Performs a pull before reapplying.')
    parser.add_option_group(group)
    auth.add_auth_options(parser)
    _add_codereview_select_options(parser)
    (options, args) = parser.parse_args(args)
    _process_codereview_select_options(parser, options)
    auth_config = auth.extract_auth_config_from_options(options)
    if options.reapply:
        if options.newbranch:
            parser.error('--reapply works on the current branch only')
        if (len(args) > 0):
            parser.error('--reapply implies no additional arguments')
        cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview)
        if (not cl.GetIssue()):
            parser.error('current branch must have an associated issue')
        upstream = cl.GetUpstreamBranch()
        if (upstream == None):
            parser.error('No upstream branch specified. Cannot reset branch')
        RunGit(['reset', '--hard', upstream])
        if options.pull:
            RunGit(['pull'])
        return cl.CMDPatchIssue(cl.GetIssue(), options.reject, options.nocommit, options.directory)
    if ((len(args) != 1) or (not args[0])):
        parser.error('Must specify issue number or url')
    if git_common.is_dirty_git_tree('patch'):
        return 1
    if options.newbranch:
        if options.force:
            RunGit(['branch', '-D', options.newbranch], stderr=subprocess2.PIPE, error_ok=True)
        RunGit(['new-branch', options.newbranch])
    cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview)
    if cl.IsGerrit():
        if options.reject:
            parser.error('--reject is not supported with Gerrit codereview.')
        if options.nocommit:
            parser.error('--nocommit is not supported with Gerrit codereview.')
        if options.directory:
            parser.error('--directory is not supported with Gerrit codereview.')
    return cl.CMDPatchIssue(args[0], options.reject, options.nocommit, options.directory)
Patches in a code review.
@subcommand.usage('<patch url or issue id or issue url>')
def CMDpatch(parser, args):
    
    parser.add_option('-b', dest='newbranch', help='create a new branch off trunk for the patch')
    parser.add_option('-f', '--force', action='store_true', help='with -b, clobber any existing branch')
    parser.add_option('-d', '--directory', action='store', metavar='DIR', help='Change to the directory DIR immediately, before doing anything else. Rietveld only.')
    parser.add_option('--reject', action='store_true', help='failed patches spew .rej files rather than attempting a 3-way merge. Rietveld only.')
    parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit', help="don't commit after patch applies. Rietveld only.")
    group = optparse.OptionGroup(parser, 'Options for continuing work on the current issue uploaded from a different clone (e.g. different machine). Must be used independently from the other options. No issue number should be specified, and the branch must have an issue number associated with it')
    group.add_option('--reapply', action='store_true', dest='reapply', help='Reset the branch and reapply the issue.\nCAUTION: This will undo any local changes in this branch')
    group.add_option('--pull', action='store_true', dest='pull', help='Performs a pull before reapplying.')
    parser.add_option_group(group)
    auth.add_auth_options(parser)
    _add_codereview_select_options(parser)
    (options, args) = parser.parse_args(args)
    _process_codereview_select_options(parser, options)
    auth_config = auth.extract_auth_config_from_options(options)
    if options.reapply:
        if options.newbranch:
            parser.error('--reapply works on the current branch only')
        if (len(args) > 0):
            parser.error('--reapply implies no additional arguments')
        cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview)
        if (not cl.GetIssue()):
            parser.error('current branch must have an associated issue')
        upstream = cl.GetUpstreamBranch()
        if (upstream == None):
            parser.error('No upstream branch specified. Cannot reset branch')
        RunGit(['reset', '--hard', upstream])
        if options.pull:
            RunGit(['pull'])
        return cl.CMDPatchIssue(cl.GetIssue(), options.reject, options.nocommit, options.directory)
    if ((len(args) != 1) or (not args[0])):
        parser.error('Must specify issue number or url')
    if git_common.is_dirty_git_tree('patch'):
        return 1
    if options.newbranch:
        if options.force:
            RunGit(['branch', '-D', options.newbranch], stderr=subprocess2.PIPE, error_ok=True)
        RunGit(['new-branch', options.newbranch])
    cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview)
    if cl.IsGerrit():
        if options.reject:
            parser.error('--reject is not supported with Gerrit codereview.')
        if options.nocommit:
            parser.error('--nocommit is not supported with Gerrit codereview.')
        if options.directory:
            parser.error('--directory is not supported with Gerrit codereview.')
    return cl.CMDPatchIssue(args[0], options.reject, options.nocommit, options.directory)