def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit, directory):
    assert (not git_common.is_dirty_git_tree('apply'))
    assert parsed_issue_arg.valid
    self._changelist.issue = parsed_issue_arg.issue
    if parsed_issue_arg.hostname:
        self._rietveld_server = ('https://%s' % parsed_issue_arg.hostname)
    if (isinstance(parsed_issue_arg, _RietveldParsedIssueNumberArgument) and parsed_issue_arg.patch_url):
        assert parsed_issue_arg.patchset
        patchset = parsed_issue_arg.patchset
        patch_data = urllib2.urlopen(parsed_issue_arg.patch_url).read()
    else:
        patchset = (parsed_issue_arg.patchset or self.GetMostRecentPatchset())
        patch_data = self.GetPatchSetDiff(self.GetIssue(), patchset)
    top = settings.GetRelativeRoot()
    if top:
        os.chdir(top)
    try:
        patch_data = subprocess2.check_output(['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'], stdin=patch_data)
    except subprocess2.CalledProcessError:
        DieWithError('Git patch mungling failed.')
    logging.info(patch_data)
    cmd = ['git', 'apply', '--index', '-p0']
    if directory:
        cmd.extend(('--directory', directory))
    if reject:
        cmd.append('--reject')
    elif IsGitVersionAtLeast('1.7.12'):
        cmd.append('--3way')
    try:
        subprocess2.check_call(cmd, env=GetNoGitPagerEnv(), stdin=patch_data, stdout=subprocess2.VOID)
    except subprocess2.CalledProcessError:
        print('Failed to apply the patch')
        return 1
    if (not nocommit):
        RunGit(['commit', '-m', ((self.GetDescription() + '\n\n') + ('patch from issue %(i)s at patchset %(p)s (http://crrev.com/%(i)s#ps%(p)s)' % {'i': self.GetIssue(), 'p': patchset}))])
        self.SetIssue(self.GetIssue())
        self.SetPatchset(patchset)
        print('Committed patch locally.')
    else:
        print('Patch applied to index.')
    return 0
def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit, directory):
    assert (not git_common.is_dirty_git_tree('apply'))
    assert parsed_issue_arg.valid
    self._changelist.issue = parsed_issue_arg.issue
    if parsed_issue_arg.hostname:
        self._rietveld_server = ('https://%s' % parsed_issue_arg.hostname)
    if (isinstance(parsed_issue_arg, _RietveldParsedIssueNumberArgument) and parsed_issue_arg.patch_url):
        assert parsed_issue_arg.patchset
        patchset = parsed_issue_arg.patchset
        patch_data = urllib2.urlopen(parsed_issue_arg.patch_url).read()
    else:
        patchset = (parsed_issue_arg.patchset or self.GetMostRecentPatchset())
        patch_data = self.GetPatchSetDiff(self.GetIssue(), patchset)
    top = settings.GetRelativeRoot()
    if top:
        os.chdir(top)
    try:
        patch_data = subprocess2.check_output(['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'], stdin=patch_data)
    except subprocess2.CalledProcessError:
        DieWithError('Git patch mungling failed.')
    logging.info(patch_data)
    cmd = ['git', 'apply', '--index', '-p0']
    if directory:
        cmd.extend(('--directory', directory))
    if reject:
        cmd.append('--reject')
    elif IsGitVersionAtLeast('1.7.12'):
        cmd.append('--3way')
    try:
        subprocess2.check_call(cmd, env=GetNoGitPagerEnv(), stdin=patch_data, stdout=subprocess2.VOID)
    except subprocess2.CalledProcessError:
        print('Failed to apply the patch')
        return 1
    if (not nocommit):
        RunGit(['commit', '-m', ((self.GetDescription() + '\n\n') + ('patch from issue %(i)s at patchset %(p)s (http://crrev.com/%(i)s#ps%(p)s)' % {'i': self.GetIssue(), 'p': patchset}))])
        self.SetIssue(self.GetIssue())
        self.SetPatchset(patchset)
        print('Committed patch locally.')
    else:
        print('Patch applied to index.')
    return 0