MCPcopy
hub / github.com/desktop/desktop / getCommitRangeDiff

Function getCommitRangeDiff

app/src/lib/git/diff.ts:193–246  ·  view source on GitHub ↗
(
  repository: Repository,
  file: FileChange,
  commits: ReadonlyArray<string>,
  hideWhitespaceInDiff: boolean = false,
  useNullTreeSHA: boolean = false
)

Source from the content-addressed store, hash-verified

191 *
192 */
193export async function getCommitRangeDiff(
194 repository: Repository,
195 file: FileChange,
196 commits: ReadonlyArray<string>,
197 hideWhitespaceInDiff: boolean = false,
198 useNullTreeSHA: boolean = false
199): Promise<IDiff> {
200 if (commits.length === 0) {
201 throw new Error('No commits to diff...')
202 }
203
204 const oldestCommit = useNullTreeSHA ? NullTreeSHA : commits[0]
205 const oldestCommitRef = useNullTreeSHA ? NullTreeSHA : `${commits[0]}^`
206 const latestCommit = commits.at(-1) ?? '' // can't be undefined since commits.length > 0
207 const args = [
208 'diff',
209 oldestCommitRef,
210 latestCommit,
211 ...(hideWhitespaceInDiff ? ['-w'] : []),
212 '--patch-with-raw',
213 '--format=',
214 '-z',
215 '--no-color',
216 '--',
217 ensureRelativePath(file.path),
218 ]
219
220 if (
221 file.status.kind === AppFileStatusKind.Renamed ||
222 file.status.kind === AppFileStatusKind.Copied
223 ) {
224 args.push(ensureRelativePath(file.status.oldPath))
225 }
226
227 const result = await git(args, repository.path, 'getCommitsDiff', {
228 encoding: 'buffer',
229 expectedErrors: new Set([GitError.BadRevision]),
230 })
231
232 // This should only happen if the oldest commit does not have a parent (ex:
233 // initial commit of a branch) and therefore `SHA^` is not a valid reference.
234 // In which case, we will retry with the null tree sha.
235 if (result.gitError === GitError.BadRevision && useNullTreeSHA === false) {
236 return getCommitRangeDiff(
237 repository,
238 file,
239 commits,
240 hideWhitespaceInDiff,
241 true
242 )
243 }
244
245 return buildDiff(result.stdout, repository, file, latestCommit, oldestCommit)
246}
247
248/**
249 * Get the files that were changed for the merge base comparison of two branches.

Callers 1

_changeFileSelectionMethod · 0.90

Calls 4

gitFunction · 0.90
ensureRelativePathFunction · 0.85
buildDiffFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected