Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// in a headless repository (which is better than our previous behavior of
// crashing); we should fix it so that we can accurately reflect staged
// submodules in the index.
if (null !== headCommit) {
// Now we need to figure out which subs to list, and what paths to
// inspect in them.
const openArray = yield SubmoduleUtil.listOpenSubmodules(repo);
const openSet = new Set(openArray);
const index = yield repo.index();
Object.assign(args.staged, exports.readConflicts(index, options.paths));
const headTree = yield headCommit.getTree();
const diff = yield NodeGit.Diff.treeToIndex(repo, headTree, index);
const changes = yield SubmoduleUtil.getSubmoduleChangesFromDiff(diff,
true);
const indexUrls =
yield SubmoduleConfigUtil.getSubmodulesFromIndex(repo, index);
const headUrls =
yield SubmoduleConfigUtil.getSubmodulesFromCommit(repo, headCommit);
// No paths specified, so we'll do all submodules, restricting to open
// ones based on options.
let filterPaths; // map from sub name to paths to use
const filtering = 0 !== options.paths.length;
// Will look at submodules that are open or have changes. TODO: we're
// ignoring changes affecting only the `.gitmodules` file for now.
// `merged` records changes that were present in at least one parent
const merged = {};
// `changes` records what changes were actually made in this
// commit (as opposed to merged from a parent).
let changes = null;
if (parents.length === 0) {
changes = {};
}
for (const parent of parents) {
const parentTree = yield parent.getTree();
const parentSubmodules = yield getSubmodulesFromCommit(repo, parent);
// TODO: this doesn't really handle URL updates
Object.assign(oldUrls, parentSubmodules);
const diff =
yield NodeGit.Diff.treeToTree(repo, parentTree, headTree, null);
const ourChanges = yield SubmoduleUtil.getSubmoduleChangesFromDiff(
diff,
true);
if (changes === null) {
changes = ourChanges;
} else {
for (const path of Object.keys(changes)) {
if (ourChanges[path] === undefined) {
merged[path] = changes[path];
delete changes[path];
} else {
delete ourChanges[path];
}
}
for (const path of Object.keys(ourChanges)) {
merged[path] = ourChanges[path];
makeConflict(theirs));
}
}
// Now we handle the changes that Git was able to take care of by itself.
// First, we're going to need to write the index to a tree; this write
// requires that we clean the conflicts. Anything we've already diagnosed
// as either a conflict or a non-simple change will be ignored here.
yield index.conflictCleanup();
const simpleChanges = {};
const treeId = yield index.writeTreeTo(repo);
const tree = yield NodeGit.Tree.lookup(repo, treeId);
const srcTree = yield srcCommit.getTree();
const diff = yield NodeGit.Diff.treeToTree(repo, srcTree, tree, null);
const treeChanges =
yield SubmoduleUtil.getSubmoduleChangesFromDiff(diff, false);
for (let name in treeChanges) {
// Skip changes we've already taken into account and the `.gitmodules`
// file.
if (SubmoduleConfigUtil.modulesFileName === name ||
name in changes ||
name in conflicts) {
continue; // CONTINUE
}
const change = treeChanges[name];
if (null === change.newSha) {
simpleChanges[name] = null;
} else {
simpleChanges[name] = new Submodule(urls[name], change.newSha);
exports.getSubmoduleChangesFromDiff = function (diff, allowMetaChanges) {
assert.instanceOf(diff, NodeGit.Diff);
assert.isBoolean(allowMetaChanges);
const num = diff.numDeltas();
const result = {};
const DELTA = NodeGit.Diff.DELTA;
const COMMIT = NodeGit.TreeEntry.FILEMODE.COMMIT;
const modulesFileName = SubmoduleConfigUtil.modulesFileName;
for (let i = 0; i < num; ++i) {
const delta = diff.getDelta(i);
switch (delta.status()) {
case DELTA.COPIED:
case DELTA.RENAMED: {
if (COMMIT === delta.newFile.mode() ||
COMMIT === delta.oldFile.mode()) {
throw new Error(
"Not sure if these are possible. TODO: find out.");
}
} break;
case DELTA.MODIFIED:
case DELTA.CONFLICTED: {
const newFile = delta.newFile();
return Repo.index().then(ind => {
return NodeGit.Diff.indexToWorkdir(Repo, ind, {
flags: NodeGit.Diff.OPTION.SHOW_UNTRACKED_CONTENT | NodeGit.Diff.OPTION.RECURSE_UNTRACKED_DIRS
})
}).then(diff => {
return processDiff(diff, path, commit, fullFile);
}).then(tree => {
return NodeGit.Diff.treeToIndex(Repo, tree, index);
}).catch(err => {
return NodeGit.Diff.treeToIndex(Repo, null, index);
.then(repo => {
return Git.Diff.indexToWorkdir(repo);
})
.then(diff => {
.then(function(tree){
return Git.Diff.treeToWorkdir(repo, tree);
})
.then(function(diff){
function processDiff(diff, filePath, paths, fileDir, includeDiff, includeDiffs, query, scope, diffContents, diffs) {
var result = [];
if (includeDiff) {
result.push(diff.toBuf(git.Diff.FORMAT.PATCH)
.then(function(buf) {
diffContents.push(buf.toString());
}));
}
if (includeDiffs) {
var page = Number(query.page) || 1;
var pageSize = Number(query.pageSize) || Number.MAX_SAFE_INTEGER;
result.push(diff.patches()
.then(function(patches) {
var start = pageSize * (page - 1);
var end = Math.min(pageSize + start, patches.length);
var i = start;
patches.forEach(function(patch, pi) {
var newFile = patch.newFile();
var newFilePath = newFile.path();
var oldFile = patch.oldFile();
const options = {
ignoreSubmodules: 1,
flags: NodeGit.Diff.OPTION.INCLUDE_UNTRACKED |
NodeGit.Diff.OPTION.EXCLUDE_SUBMODULES,
};
if (0 !== paths.length) {
options.pathspec = paths;
}
if (allUntracked) {
options.flags = options.flags |
NodeGit.Diff.OPTION.RECURSE_UNTRACKED_DIRS;
}
if (ignoreIndex) {
const workdirToTreeDiff =
yield NodeGit.Diff.treeToWorkdir(repo, tree, options);
const workdirToTreeStatus = readDiff(workdirToTreeDiff);
return {
staged: {},
workdir: workdirToTreeStatus,
};
}
const index = yield repo.index();
const workdirToIndexDiff =
yield NodeGit.Diff.indexToWorkdir(repo, index, options);
const workdirToIndexStatus = readDiff(workdirToIndexDiff);
const indexToTreeDiff =
yield NodeGit.Diff.treeToIndex(repo, tree, null, options);
const indexToTreeStatus = readDiff(indexToTreeDiff);
return {
staged: indexToTreeStatus,
workdir: workdirToIndexStatus,