Skip to content

Commit 967d63b

Browse files
fix(git-diff): Now targets the closest parent repo to the open file.
Initial fix to `helpers` done by Utkarsh, Spec written by me. Co-authored-by: Utkarsh Gupta <[email protected]>
1 parent a0374af commit 967d63b

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

packages/git-diff/lib/helpers.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use babel';
2+
import { Directory } from 'atom';
23

34
export default async function(goalPath) {
4-
for (const directory of atom.project.getDirectories()) {
5-
if (goalPath === directory.getPath() || directory.contains(goalPath)) {
6-
return atom.project.repositoryForDirectory(directory);
7-
}
5+
if (goalPath) {
6+
return atom.project.repositoryForDirectory(new Directory(goalPath));
87
}
98
return null;
109
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const path = require('path');
2+
const fs = require('fs-plus');
3+
const temp = require('temp').track();
4+
5+
describe('GitDiff when targeting nested repository', () => {
6+
let editor, editorElement, projectPath, screenUpdates;
7+
8+
beforeEach(() => {
9+
screenUpdates = 0;
10+
spyOn(window, 'requestAnimationFrame').andCallFake(fn => {
11+
fn();
12+
screenUpdates++;
13+
});
14+
spyOn(window, 'cancelAnimationFrame').andCallFake(i => null);
15+
16+
projectPath = temp.mkdirSync('git-diff-spec-');
17+
18+
fs.copySync(path.join(__dirname, 'fixtures', 'working-dir'), projectPath);
19+
fs.moveSync(
20+
path.join(projectPath, 'git.git'),
21+
path.join(projectPath, '.git')
22+
);
23+
24+
// The nested repo doesn't need to be managed by the temp module because
25+
// it's a part of our test environment.
26+
const nestedPath = path.join(projectPath, 'nested-repository');
27+
// When instantiating a GitRepository, the repository will always point
28+
// to the .git folder in it's path.
29+
const targetRepositoryPath = path.join(nestedPath, '.git');
30+
// Initialize the repository contents.
31+
fs.copySync(path.join(__dirname, 'fixtures', 'working-dir'), nestedPath);
32+
fs.moveSync(
33+
path.join(nestedPath, 'git.git'),
34+
path.join(nestedPath, '.git')
35+
);
36+
37+
atom.project.setPaths([projectPath]);
38+
39+
jasmine.attachToDOM(atom.workspace.getElement());
40+
41+
waitsForPromise(async () => {
42+
await atom.workspace.open(path.join(nestedPath, 'sample.js'));
43+
await atom.packages.activatePackage('git-diff');
44+
});
45+
46+
runs(() => {
47+
editor = atom.workspace.getActiveTextEditor();
48+
editorElement = atom.views.getView(editor);
49+
});
50+
});
51+
52+
afterEach(() => {
53+
temp.cleanup();
54+
});
55+
56+
describe('When git-diff targets a file in a nested git-repository', () => {
57+
/***
58+
* Non-hack regression prevention for nested repositories. If we know
59+
* that our project path contains two repositories, we can ensure that
60+
* git-diff is targeting the correct one by creating an artificial change
61+
* in the ancestor repository, which doesn't effect the target repository.
62+
* If our diff shows any kind of change to our target file, we're targeting
63+
* the incorrect repository.
64+
*/
65+
it("uses the innermost repository", () => {
66+
//waitsForPromise(async () => await new Promise(resolve => setTimeout(resolve, 4000)));
67+
//waitsFor(() => !! atom.packages.isPackageLoaded("git-diff"));
68+
waitsFor(() => screenUpdates > 0);
69+
runs(() => expect(editor.getMarkers().length).toBe(0));
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)