Git - add extension API to get a patch for a commit range (#286159)

* Git - add extension API to get a patch for a commit range

* Turns out `git diff` does not support the `--root` flag
This commit is contained in:
Ladislau Szomoru
2026-01-06 15:18:53 +00:00
committed by GitHub
parent 67c15fe860
commit d0576dab05
4 changed files with 21 additions and 0 deletions

View File

@@ -200,6 +200,10 @@ export class ApiRepository implements Repository {
return this.#repository.diffBetween(ref1, ref2, path);
}
diffBetweenPatch(ref1: string, ref2: string, path?: string): Promise<string> {
return this.#repository.diffBetweenPatch(ref1, ref2, path);
}
diffBetweenWithStats(ref1: string, ref2: string, path?: string): Promise<DiffChange[]> {
return this.#repository.diffBetweenWithStats(ref1, ref2, path);
}

View File

@@ -261,6 +261,7 @@ export interface Repository {
diffBlobs(object1: string, object2: string): Promise<string>;
diffBetween(ref1: string, ref2: string): Promise<Change[]>;
diffBetween(ref1: string, ref2: string, path: string): Promise<string>;
diffBetweenPatch(ref1: string, ref2: string, path?: string): Promise<string>;
diffBetweenWithStats(ref1: string, ref2: string, path?: string): Promise<DiffChange[]>;
hashObject(data: string): Promise<string>;

View File

@@ -1772,6 +1772,17 @@ export class Repository {
return result.stdout.trim();
}
async diffBetweenPatch(ref: string, options: { path?: string }): Promise<string> {
const args = ['diff', ref, '--'];
if (options.path) {
args.push(this.sanitizeRelativePath(options.path));
}
const result = await this.exec(args);
return result.stdout;
}
async diffBetweenWithStats(ref: string, options: { path?: string; similarityThreshold?: number }): Promise<DiffChange[]> {
const args = ['diff', '--raw', '--numstat', '--diff-filter=ADMR', '-z',];

View File

@@ -1253,6 +1253,11 @@ export class Repository implements Disposable {
return this.run(Operation.Diff, () => this.repository.diffBetween(ref1, ref2, path));
}
diffBetweenPatch(ref1: string, ref2: string, path?: string): Promise<string> {
return this.run(Operation.Diff, () =>
this.repository.diffBetweenPatch(`${ref1}...${ref2}`, { path }));
}
diffBetweenWithStats(ref1: string, ref2: string, path?: string): Promise<DiffChange[]> {
if (ref1 === this._EMPTY_TREE) {
// Use git diff-tree to get the