-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-repository-provider.js
183 lines (170 loc) · 6.41 KB
/
git-repository-provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const fs = require('fs');
const { Directory } = require('pathwatcher');
const GitRepository = require('./git-repository');
const GIT_FILE_REGEX = RegExp('^gitdir: (.+)');
// Returns the .gitdir path in the agnostic Git symlink .git file given, or
// null if the path is not a valid gitfile.
//
// * `gitFile` {String} path of gitfile to parse
function pathFromGitFileSync(gitFile) {
try {
const gitFileBuff = fs.readFileSync(gitFile, 'utf8');
return gitFileBuff != null ? gitFileBuff.match(GIT_FILE_REGEX)[1] : null;
} catch (error) {}
}
// Returns a {Promise} that resolves to the .gitdir path in the agnostic
// Git symlink .git file given, or null if the path is not a valid gitfile.
//
// * `gitFile` {String} path of gitfile to parse
function pathFromGitFile(gitFile) {
return new Promise(resolve => {
fs.readFile(gitFile, 'utf8', (err, gitFileBuff) => {
if (err == null && gitFileBuff != null) {
const result = gitFileBuff.toString().match(GIT_FILE_REGEX);
resolve(result != null ? result[1] : null);
} else {
resolve(null);
}
});
});
}
// Checks whether a valid `.git` directory is contained within the given
// directory or one of its ancestors. If so, a Directory that corresponds to the
// `.git` folder will be returned. Otherwise, returns `null`.
//
// * `directory` {Directory} to explore whether it is part of a Git repository.
function findGitDirectorySync(directory) {
// TODO: Fix node-pathwatcher/src/directory.coffee so the following methods
// can return cached values rather than always returning new objects:
// getParent(), getFile(), getSubdirectory().
let gitDir = directory.getSubdirectory('.git');
if (typeof gitDir.getPath === 'function') {
const gitDirPath = pathFromGitFileSync(gitDir.getPath());
if (gitDirPath) {
gitDir = new Directory(directory.resolve(gitDirPath));
}
}
if (
typeof gitDir.existsSync === 'function' &&
gitDir.existsSync() &&
isValidGitDirectorySync(gitDir)
) {
return gitDir;
} else if (directory.isRoot()) {
return null;
} else {
return findGitDirectorySync(directory.getParent());
}
}
// Checks whether a valid `.git` directory is contained within the given
// directory or one of its ancestors. If so, a Directory that corresponds to the
// `.git` folder will be returned. Otherwise, returns `null`.
//
// Returns a {Promise} that resolves to
// * `directory` {Directory} to explore whether it is part of a Git repository.
async function findGitDirectory(directory) {
// TODO: Fix node-pathwatcher/src/directory.coffee so the following methods
// can return cached values rather than always returning new objects:
// getParent(), getFile(), getSubdirectory().
let gitDir = directory.getSubdirectory('.git');
if (typeof gitDir.getPath === 'function') {
const gitDirPath = await pathFromGitFile(gitDir.getPath());
if (gitDirPath) {
gitDir = new Directory(directory.resolve(gitDirPath));
}
}
if (
typeof gitDir.exists === 'function' &&
(await gitDir.exists()) &&
isValidGitDirectory(gitDir)
) {
return gitDir;
} else if (directory.isRoot()) {
return null;
} else {
return findGitDirectory(directory.getParent());
}
}
// Returns a boolean indicating whether the specified directory represents a Git
// repository.
//
// * `directory` {Directory} whose base name is `.git`.
function isValidGitDirectorySync(directory) {
// To decide whether a directory has a valid .git folder, we use
// the heuristic adopted by the valid_repository_path() function defined in
// node_modules/git-utils/deps/libgit2/src/repository.c.
return (
directory.getSubdirectory('objects').existsSync() &&
directory.getFile('HEAD').existsSync() &&
directory.getSubdirectory('refs').existsSync()
);
}
// Returns a {Promise} that resolves to a {Boolean} indicating whether the
// specified directory represents a Git repository.
//
// * `directory` {Directory} whose base name is `.git`.
async function isValidGitDirectory(directory) {
// To decide whether a directory has a valid .git folder, we use
// the heuristic adopted by the valid_repository_path() function defined in
// node_modules/git-utils/deps/libgit2/src/repository.c.
return (
(await directory.getSubdirectory('objects').exists()) &&
(await directory.getFile('HEAD').exists()) &&
directory.getSubdirectory('refs').exists()
);
}
// Provider that conforms to the [email protected] service.
class GitRepositoryProvider {
constructor(project, config) {
// Keys are real paths that end in `.git`.
// Values are the corresponding GitRepository objects.
this.project = project;
this.config = config;
this.pathToRepository = {};
}
// Returns a {Promise} that resolves with either:
// * {GitRepository} if the given directory has a Git repository.
// * `null` if the given directory does not have a Git repository.
async repositoryForDirectory(directory) {
// Only one GitRepository should be created for each .git folder. Therefore,
// we must check directory and its parent directories to find the nearest
// .git folder.
const gitDir = await findGitDirectory(directory);
return this.repositoryForGitDirectory(gitDir);
}
// Returns either:
// * {GitRepository} if the given directory has a Git repository.
// * `null` if the given directory does not have a Git repository.
repositoryForDirectorySync(directory) {
// Only one GitRepository should be created for each .git folder. Therefore,
// we must check directory and its parent directories to find the nearest
// .git folder.
const gitDir = findGitDirectorySync(directory);
return this.repositoryForGitDirectory(gitDir);
}
// Returns either:
// * {GitRepository} if the given Git directory has a Git repository.
// * `null` if the given directory does not have a Git repository.
repositoryForGitDirectory(gitDir) {
if (!gitDir) {
return null;
}
const gitDirPath = gitDir.getPath();
let repo = this.pathToRepository[gitDirPath];
if (!repo) {
repo = GitRepository.open(gitDirPath, {
project: this.project,
config: this.config
});
if (!repo) {
return null;
}
repo.onDidDestroy(() => delete this.pathToRepository[gitDirPath]);
this.pathToRepository[gitDirPath] = repo;
repo.refreshIndex();
repo.refreshStatus();
}
return repo;
}
}
module.exports = GitRepositoryProvider;