Skip to content

Commit e83b069

Browse files
authored
Add configurable rename detection threshold (#115)
Added renameThreshold setting to control git diff rename similarity index.
1 parent aa166f1 commit e83b069

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@
361361
"description": "Whether to detect renames. Does not affect untracked files. May have a performance impact.",
362362
"default": true
363363
},
364+
"gitTreeCompare.renameThreshold": {
365+
"type": "number",
366+
"minimum": 0,
367+
"maximum": 100,
368+
"description": "The similarity index (in percent) for rename detection. A value of 100 means that only identical files are detected as renames, a value of 0 means that any file with any similarity is detected as a rename. Has no effect if gitTreeCompare.findRenames is false.",
369+
"default": 50
370+
},
364371
"gitTreeCompare.openChanges": {
365372
"type": "boolean",
366373
"description": "When selecting a modified file in the tree, whether to show its changes or just open the workspace file.",

src/gitHelper.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function parseDiffIndexOutput(repoRoot: string, out: string): IDiffStatus[] {
207207
return entries;
208208
}
209209

210-
export async function diffIndex(repo: Repository, ref: string, refreshIndex: boolean, findRenames: boolean): Promise<IDiffStatus[]> {
210+
export async function diffIndex(repo: Repository, ref: string, refreshIndex: boolean, findRenames: boolean, renameThreshold: number): Promise<IDiffStatus[]> {
211211
if (refreshIndex) {
212212
// avoid superfluous diff entries if files only got touched
213213
// (see https://github.com/letmaik/vscode-git-tree-compare/issues/37)
@@ -219,8 +219,7 @@ export async function diffIndex(repo: Repository, ref: string, refreshIndex: boo
219219
}
220220

221221
// exceptions can happen with newly initialized repos without commits, or when git is busy
222-
223-
const renamesFlag = findRenames ? '--find-renames' : '--no-renames';
222+
const renamesFlag = findRenames ? `--find-renames=${renameThreshold}%` : '--no-renames';
224223
let diffIndexResult = await repo.exec(['diff-index', '-z', renamesFlag, ref, '--']);
225224
let untrackedResult = await repo.exec(['ls-files', '-z', '--others', '--exclude-standard']);
226225

src/treeProvider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
100100
private iconsMinimal: boolean;
101101
private fullDiff: boolean;
102102
private findRenames: boolean;
103+
private renameThreshold: number;
103104
private showCollapsed: boolean;
104105
private compactFolders: boolean;
105106
private showCheckboxes: boolean;
@@ -342,6 +343,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
342343
this.iconsMinimal = config.get<boolean>('iconsMinimal', false);
343344
this.fullDiff = config.get<string>('diffMode') === 'full';
344345
this.findRenames = config.get<boolean>('findRenames', true);
346+
this.renameThreshold = config.get<number>('renameThreshold', 50);
345347
this.showCollapsed = config.get<boolean>('collapsed', false);
346348
this.compactFolders = config.get<boolean>('compactFolders', false);
347349
this.showCheckboxes = config.get<boolean>('showCheckboxes', false);
@@ -507,7 +509,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
507509
const filesInsideTreeRoot = new Map<FolderAbsPath, IDiffStatus[]>();
508510
const filesOutsideTreeRoot = new Map<FolderAbsPath, IDiffStatus[]>();
509511

510-
const diff = await diffIndex(this.repository!, this.mergeBase, this.refreshIndex, this.findRenames);
512+
const diff = await diffIndex(this.repository!, this.mergeBase, this.refreshIndex, this.findRenames, this.renameThreshold);
511513
const untrackedCount = diff.reduce((prev, cur, _) => prev + (cur.status === 'U' ? 1 : 0), 0);
512514
this.log(`${diff.length} diff entries (${untrackedCount} untracked)`);
513515

@@ -662,6 +664,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
662664
const oldIconsMinimal = this.iconsMinimal;
663665
const oldFullDiff = this.fullDiff;
664666
const oldFindRenames = this.findRenames;
667+
const oldRenameThreshold = this.renameThreshold;
665668
const oldCompactFolders = this.compactFolders;
666669
const oldshowCheckboxes = this.showCheckboxes;
667670
this.readConfig();
@@ -673,6 +676,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
673676
(!oldRefreshIndex && this.refreshIndex) ||
674677
oldFullDiff != this.fullDiff ||
675678
oldFindRenames != this.findRenames ||
679+
oldRenameThreshold != this.renameThreshold ||
676680
oldCompactFolders != this.compactFolders ||
677681
oldshowCheckboxes != this.showCheckboxes) {
678682

@@ -687,6 +691,7 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
687691

688692
if (oldFullDiff != this.fullDiff ||
689693
oldFindRenames != this.findRenames ||
694+
oldRenameThreshold != this.renameThreshold ||
690695
oldTreeRoot != this.treeRoot ||
691696
(!oldAutoRefresh && this.autoRefresh) ||
692697
(!oldRefreshIndex && this.refreshIndex)) {

0 commit comments

Comments
 (0)