Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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());
}
}
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.
const commonDirFile = directory.getSubdirectory('commondir');
let commonDir;
if (await commonDirFile.exists()) {
const commonDirPathBuff = await fs.readFile(commonDirFile.getPath());
const commonDirPathString = commonDirPathBuff.toString().trim();
commonDir = new Directory(directory.resolve(commonDirPathString));
if (!(await commonDir.exists())) {
return false;
}
} else {
commonDir = directory;
}
return (
(await directory.getFile('HEAD').exists()) &&
(await commonDir.getSubdirectory('objects').exists()) &&
commonDir.getSubdirectory('refs').exists()
);
}
function toggleBlame() {
var editor = atom.workspace.getActivePaneItem()
if (!editor) return;
// An unsaved file has no filePath
filePath = editor.getPath()
if (!filePath) return;
// blaming an empty file is useless
if (editor.isEmpty()) return;
return atom.project.repositoryForDirectory(new Directory(path.dirname(filePath))).then(
function(projectRepo) {
// Ensure this project is backed by a git repository
if (!projectRepo) {
errorController.showError('error-not-backed-by-git');
return;
}
if (!(projectRepo.path in projectBlamers)) {
projectBlamers[projectRepo.path] = new Blamer(projectRepo);
}
BlameViewController.toggleBlame(projectBlamers[projectRepo.path]);
});
}
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());
}
}
it('resolves with null', async () => {
const dirPath = temp.mkdirSync('dir');
fs.writeFileSync(path.join(dirPath, '.git', 'objects'), '');
fs.writeFileSync(path.join(dirPath, '.git', 'HEAD'), '');
fs.writeFileSync(path.join(dirPath, '.git', 'refs'), '');
const directory = new Directory(dirPath);
const repo = await provider.repositoryForDirectory(directory);
expect(repo).toBe(null);
});
});
it('returns a Promise that resolves to a GitRepository', () => {
const directory = new Directory(
path.join(
__dirname,
'fixtures',
'git',
'master.git',
'worktrees',
'worktree-dir'
)
);
const result = provider.repositoryForDirectorySync(directory);
expect(result).toBeInstanceOf(GitRepository);
expect(provider.pathToRepository[result.getPath()]).toBeTruthy();
expect(result.getType()).toBe('git');
});
});