How to use the diff.applyPatch function in diff

To help you get started, we’ve selected a few diff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github atom / github / lib / git / git-service.js View on Github external
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,
github jimmyleray / Emendare / old-server / src / models / text / Text.ts View on Github external
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 })
    }
github arestov / deklarota / dev / gulp-patch.js View on Github external
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);
    });
  });
});
github jimmyleray / Emendare / client / src / components / 1_molecules / Amend.tsx View on Github external
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 })
  }
}
github jimmyleray / Emendare / client / src / components / 0_atoms / diffPreview / diffPreview.tsx View on Github external
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
  }
}
github ember-cli / ember-cli / lib / models / edit-file-diff.js View on Github external
}).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);
    });
  }
github jimmyleray / Emendare / server / src / services / text / text.service.ts View on Github external
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())
github Coding / WebIDE-Frontend / app / components / Git / modals / diffFile.jsx View on Github external
.then((res) => {
              const content = res.content
              const newContent = jsdiff.applyPatch(content, diffPatch)
              this.initDiff(newContent, content)
            })
        }
github jimmyleray / Emendare / client / src / components / 0_atoms / diffPreview / diffPreview.tsx View on Github external
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
  }
}