Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async stagePatches (fileDiff: FileDiff, patches: Array): Promise {
const oldPath = fileDiff.getOldPathName() || ''
const newPath = fileDiff.getNewPathName()
const repo = await Git.Repository.open(this.repoPath)
const index = await repo.index()
const content = await (!fileDiff.isUntracked() ? this._indexBlob(oldPath) : null)
let newContent = content || ''
for (let patchText of patches) {
newContent = applyPatch(newContent, patchText)
}
const buffer = new Buffer(newContent)
const oid = repo.createBlobFromBuffer(buffer)
if (fileDiff.isDeleted()) {
// If we the file was deleted then we're guaranteed that `oldPath`
// isn't null.
await index.removeByPath(oldPath)
} else {
const entry = this._createIndexEntry({
oid: oid,
// If we're not deleted then we know it has a new path name.
// $FlowSilence
path: newPath,
fileSize: buffer.length,
public static async updateTextWithAmend(amend: any, io?: SocketIO.Server) {
amend.accepted = true
const newText = applyPatch(amend.text.actual, amend.patch)
if (newText) {
amend.version = amend.text.patches.length
amend.text.patches.push(amend.patch)
amend.text.actual = newText
} else {
amend.conflicted = true
}
await amend.text.save()
const text = await Text.model.findById(amend.text._id)
if (io) {
io.emit('text/' + text._id, { data: text })
}
fs.readFile(patch_path, function (err, patch) {
if (err) {return console.error(err);}
patch = patch.toString();
var result = jsdiff.applyPatch(source, patch.toString(), diff_options);
if (!result) {
return cb(patch.toString());
}
file.contents = new Buffer(result);
setImmediate(function () {
cb(null, file);
});
});
});
private computeDiff() {
let previousText = ''
for (let index = 0; index < this.props.amend.version; index++) {
previousText = JsDiff.applyPatch(
previousText,
this.props.text.patches[index]
)
}
const newText = JsDiff.applyPatch(previousText, this.props.amend.patch)
const diffs = JsDiff.diffLines(previousText, newText)
this.setState({ diffs })
}
}
const computeDiff = (amend: Partial, text: IText) => {
if (amend && text) {
let previousText = ''
for (let index = 0; index < (amend.version || 0); index++) {
previousText = JsDiff.applyPatch(previousText, text.patches[index])
}
const newText = JsDiff.applyPatch(previousText, amend.patch || '')
const diffs = JsDiff.diffLines(previousText, newText)
return diffs
}
}
}).then(result => {
let appliedDiff = jsdiff.applyPatch(result.currentString.toString(), result.diffString.toString());
if (!appliedDiff) {
let message = 'Patch was not cleanly applied.';
this.info.ui.writeLine(`${message} Please choose another action.`);
throw new SilentError(message);
}
return writeFile(resultHash.outputPath, appliedDiff);
});
}
othersAmends.forEach(async (otherAmend: Amend) => {
const isPatchable = JsDiff.applyPatch(text.actual, otherAmend.patch)
let event: Event
let oldEvent: Event
if (isPatchable) {
otherAmend.version = text.patches.length
} else {
otherAmend.conflicted = true
otherAmend.closed = true
otherAmend.finished = new Date()
otherAmend.totalPotentialVotesCount = text.followersCount
oldEvent = await Event.findOne({
where: {
target: { id: otherAmend.id.toString() }
}
})
event = new Event('result', otherAmend.id.toString())
.then((res) => {
const content = res.content
const newContent = jsdiff.applyPatch(content, diffPatch)
this.initDiff(newContent, content)
})
}
const computeDiff = (amend: Partial, text: IText) => {
if (amend && text) {
let previousText = ''
for (let index = 0; index < (amend.version || 0); index++) {
previousText = JsDiff.applyPatch(previousText, text.patches[index])
}
const newText = JsDiff.applyPatch(previousText, amend.patch || '')
const diffs = JsDiff.diffLines(previousText, newText)
return diffs
}
}