Skip to content

Commit 3ddbda0

Browse files
authored
Merge pull request atom#23446 from M3TIOR/fix--git-diff--nested-repositories-redux
Fix `git-diff` nested repositories REDUX
2 parents 7033a5a + 8b2e42e commit 3ddbda0

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// Initialize the repository contents.
28+
fs.copySync(path.join(__dirname, 'fixtures', 'working-dir'), nestedPath);
29+
fs.moveSync(
30+
path.join(nestedPath, 'git.git'),
31+
path.join(nestedPath, '.git')
32+
);
33+
34+
atom.project.setPaths([projectPath]);
35+
36+
jasmine.attachToDOM(atom.workspace.getElement());
37+
38+
waitsForPromise(async () => {
39+
await atom.workspace.open(path.join(nestedPath, 'sample.js'));
40+
await atom.packages.activatePackage('git-diff');
41+
});
42+
43+
runs(() => {
44+
editor = atom.workspace.getActiveTextEditor();
45+
editorElement = atom.views.getView(editor);
46+
});
47+
});
48+
49+
afterEach(() => {
50+
temp.cleanup();
51+
});
52+
53+
describe('When git-diff targets a file in a nested git-repository', () => {
54+
/***
55+
* Non-hack regression prevention for nested repositories. If we know
56+
* that our project path contains two repositories, we can ensure that
57+
* git-diff is targeting the correct one by creating an artificial change
58+
* in the ancestor repository, which is percieved differently within the
59+
* child. In this case, creating a new file will not generate markers in
60+
* the ancestor repo, even if there are changes; but changes will be
61+
* marked within the child repo. So all we have to do is check if
62+
* markers exist and we know we're targeting the proper repository,
63+
* If no markers exist, we're targeting an ancestor repo.
64+
*/
65+
it('uses the innermost repository', () => {
66+
editor.insertText('a');
67+
waitsFor(() => screenUpdates > 0);
68+
runs(() => {
69+
expect(
70+
editorElement.querySelectorAll('.git-line-modified').length
71+
).toBe(1);
72+
});
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)