|
1 | 1 | 'use strict';
|
2 | 2 | import { commands, Range, TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
|
3 | 3 | import { Container } from '../container';
|
4 |
| -import { GitLogCommit, GitService, GitStatusFile, GitUri } from '../git/gitService'; |
| 4 | +import { GitLogCommit, GitUri } from '../git/gitService'; |
5 | 5 | import { Logger } from '../logger';
|
6 | 6 | import { Messages } from '../messages';
|
7 |
| -import { Iterables } from '../system'; |
8 | 7 | import { ActiveEditorCommand, command, CommandContext, Commands, getCommandUri } from './common';
|
9 | 8 | import { DiffWithCommandArgs } from './diffWith';
|
10 | 9 | import { UriComparer } from '../comparers';
|
@@ -52,101 +51,34 @@ export class DiffWithNextCommand extends ActiveEditorCommand {
|
52 | 51 | args.line = editor == null ? 0 : editor.selection.active.line;
|
53 | 52 | }
|
54 | 53 |
|
55 |
| - const gitUri = await GitUri.fromUri(uri); |
56 |
| - let status: GitStatusFile | undefined; |
57 |
| - |
58 |
| - if (args.commit === undefined || !(args.commit instanceof GitLogCommit) || args.range !== undefined) { |
59 |
| - try { |
60 |
| - const sha = args.commit === undefined ? gitUri.sha! : args.commit.sha; |
61 |
| - |
62 |
| - if (GitService.isStagedUncommitted(sha)) { |
63 |
| - const diffArgs: DiffWithCommandArgs = { |
64 |
| - repoPath: gitUri.repoPath!, |
65 |
| - lhs: { |
66 |
| - sha: sha, |
67 |
| - uri: gitUri |
68 |
| - }, |
69 |
| - rhs: { |
70 |
| - sha: '', |
71 |
| - uri: gitUri |
72 |
| - }, |
73 |
| - line: args.line, |
74 |
| - showOptions: args.showOptions |
75 |
| - }; |
76 |
| - return commands.executeCommand(Commands.DiffWith, diffArgs); |
77 |
| - } |
78 |
| - |
79 |
| - let log = await Container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, { |
80 |
| - maxCount: sha !== undefined ? undefined : 2, |
81 |
| - range: args.range!, |
82 |
| - renames: true |
83 |
| - }); |
84 |
| - if (log === undefined) { |
85 |
| - const fileName = await Container.git.findNextFileName(gitUri.repoPath!, gitUri.fsPath); |
86 |
| - if (fileName !== undefined) { |
87 |
| - log = await Container.git.getLogForFile(gitUri.repoPath, fileName, { |
88 |
| - maxCount: sha !== undefined ? undefined : 2, |
89 |
| - range: args.range!, |
90 |
| - renames: true |
91 |
| - }); |
92 |
| - } |
93 |
| - |
94 |
| - if (log === undefined) { |
95 |
| - return Messages.showFileNotUnderSourceControlWarningMessage('Unable to open compare'); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - args.commit = (sha && log.commits.get(sha)) || Iterables.first(log.commits.values()); |
100 |
| - |
101 |
| - // If the sha is missing or the file is uncommitted, treat it as a DiffWithWorking |
102 |
| - if (gitUri.sha === undefined) { |
103 |
| - status = await Container.git.getStatusForFile(gitUri.repoPath!, gitUri.fsPath); |
104 |
| - if (status !== undefined) return commands.executeCommand(Commands.DiffWithWorking, uri); |
105 |
| - } |
106 |
| - } |
107 |
| - catch (ex) { |
108 |
| - Logger.error(ex, 'DiffWithNextCommand', `getLogForFile(${gitUri.repoPath}, ${gitUri.fsPath})`); |
109 |
| - return Messages.showGenericErrorMessage('Unable to open compare'); |
110 |
| - } |
| 54 | + const gitUri = args.commit !== undefined ? GitUri.fromCommit(args.commit) : await GitUri.fromUri(uri); |
| 55 | + try { |
| 56 | + const diffUris = await Container.git.getNextDiffUris(gitUri.repoPath!, gitUri, gitUri.sha); |
| 57 | + |
| 58 | + if (diffUris === undefined || diffUris.next === undefined) return undefined; |
| 59 | + |
| 60 | + const diffArgs: DiffWithCommandArgs = { |
| 61 | + repoPath: diffUris.current.repoPath, |
| 62 | + lhs: { |
| 63 | + sha: diffUris.current.sha || '', |
| 64 | + uri: diffUris.current.documentUri() |
| 65 | + }, |
| 66 | + rhs: { |
| 67 | + sha: diffUris.next.sha || '', |
| 68 | + uri: diffUris.next.documentUri() |
| 69 | + }, |
| 70 | + line: args.line, |
| 71 | + showOptions: args.showOptions |
| 72 | + }; |
| 73 | + return commands.executeCommand(Commands.DiffWith, diffArgs); |
111 | 74 | }
|
112 |
| - |
113 |
| - if (args.commit.nextSha === undefined) { |
114 |
| - // Check if the file is staged |
115 |
| - status = status || (await Container.git.getStatusForFile(gitUri.repoPath!, gitUri.fsPath)); |
116 |
| - if (status !== undefined && status.indexStatus === 'M') { |
117 |
| - const diffArgs: DiffWithCommandArgs = { |
118 |
| - repoPath: args.commit.repoPath, |
119 |
| - lhs: { |
120 |
| - sha: args.commit.sha, |
121 |
| - uri: args.commit.uri |
122 |
| - }, |
123 |
| - rhs: { |
124 |
| - sha: GitService.stagedUncommittedSha, |
125 |
| - uri: args.commit.uri |
126 |
| - }, |
127 |
| - line: args.line, |
128 |
| - showOptions: args.showOptions |
129 |
| - }; |
130 |
| - |
131 |
| - return commands.executeCommand(Commands.DiffWith, diffArgs); |
132 |
| - } |
133 |
| - |
134 |
| - return commands.executeCommand(Commands.DiffWithWorking, uri); |
| 75 | + catch (ex) { |
| 76 | + Logger.error( |
| 77 | + ex, |
| 78 | + 'DiffWithNextCommand', |
| 79 | + `getNextDiffUris(${gitUri.repoPath}, ${gitUri.fsPath}, ${gitUri.sha})` |
| 80 | + ); |
| 81 | + return Messages.showGenericErrorMessage('Unable to open compare'); |
135 | 82 | }
|
136 |
| - |
137 |
| - const diffArgs: DiffWithCommandArgs = { |
138 |
| - repoPath: args.commit.repoPath, |
139 |
| - lhs: { |
140 |
| - sha: args.commit.sha, |
141 |
| - uri: args.commit.uri |
142 |
| - }, |
143 |
| - rhs: { |
144 |
| - sha: args.commit.nextSha, |
145 |
| - uri: args.commit.nextUri |
146 |
| - }, |
147 |
| - line: args.line, |
148 |
| - showOptions: args.showOptions |
149 |
| - }; |
150 |
| - return commands.executeCommand(Commands.DiffWith, diffArgs); |
151 | 83 | }
|
152 | 84 | }
|
0 commit comments