Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return this.server.getPromise('/diff', this.getDiffArguments()).then((diffs) => {
if (typeof diffs !== 'string') {
// Invalid value means there is no changes, show dummy diff withotu any changes
diffs = `diff --git a/${this.filename} b/${this.filename}
index aaaaaaaa..bbbbbbbb 111111
--- a/${this.filename}
+++ b/${this.filename}`;
}
this.diffJson = diff2html.getJsonFromDiff(diffs);
}).catch(err => {
// The file existed before but has been removed, but we're trying to get a diff for it
export function parseDiff(diff: string) {
// generate PR files API-like object https://developer.github.com/v3/pulls/#list-pull-requests-files
const parsed = Diff2Html.getJsonFromDiff(diff) as DiffFile[];
parsed.forEach(file => {
file.filename = file.isDeleted ? file.oldName : file.newName;
// TODO: GitHub doesn't provide checksum for content unchanged file
file.sha = file.isDeleted ? file.checksumBefore : file.checksumAfter;
if (file.isDeleted) {
file.status = 'removed';
} else if (file.isNew || file.isCopy) {
file.status = 'added';
} else if (file.isRename) {
file.status = 'renamed';
file.previous_filename = file.oldName;
}
let position = 0;
file.blocks.forEach(block => {
position++;
block.lines.forEach(line => {
export function makeSharedDiff(raw_diff: string, date: Date = new Date()): SharedDiff {
let expire_date = new Date();
expire_date.setDate(date.getDate() + 1);
return {
created: date,
expiresAt: expire_date,
diff: Diff2Html.getJsonFromDiff(raw_diff),
rawDiff: raw_diff,
};
}
function createdHtml (oldString, newString, context, outputFormat){
function hljs (html) {
return html.replace(/<span class="d2h-code-line-ctn">(.+?)<\/span>/g, '<span class="d2h-code-line-ctn"><code>$1</code></span>')
}
let args = ['', oldString || '', newString || '', '', '', {context: context}]
let dd = createPatch(...args)
let outStr = Diff2Html.getJsonFromDiff(dd, {inputFormat: 'diff', outputFormat: outputFormat, showFiles: false, matching: 'lines'})
let html = Diff2Html.getPrettyHtml(outStr, {inputFormat: 'json', outputFormat: outputFormat, showFiles: false, matching: 'lines'})
return hljs(html)
}
</span>
self.server.get('/diff', this.getDiffArguments() , function(err, diffs) {
if (typeof diffs === "string") {
self.diffJson = diff2html.getJsonFromDiff(diffs);
self.render();
}
if (callback) callback();
});
}
newStr = YAML.dump(this.formatJson(this.diffTpl.newStr));
}
} catch {
oldstr = '';
newStr = '';
this.messageService.showError('SHARED.DIFF.CHECK_DATA');
}
try {
const dd = createPatch(
this.diffTpl.fileName,
oldstr,
newStr,
this.diffTpl.newHeader,
this.diffTpl.oldHeader
);
const outStr = Diff2Html.getJsonFromDiff(
dd,
{ inputFormat: this.inputType, outputFormat: this.outStyle, showFiles: false, matching: 'lines' }
);
const html = Diff2Html.getPrettyHtml(
outStr, { inputFormat: 'json', outputFormat: this.outStyle, showFiles: false, matching: 'lines' }
);
this.html = html;
} catch {
this.messageService.showError('SHARED.DIFF.TRANS_ERROR');
this.html = null;
}
}
getDiffJson (oldContent, newContent, context, outputFormat) {
const args = ['', oldContent, newContent, '', '', { context: context }]
const patch = createPatch(...args)
const outStr = Diff2Html.getJsonFromDiff(patch, {
inputFormat: 'diff',
outputFormat: outputFormat,
showFiles: true,
matching: 'lines'
})
const addedLines = outStr[0].addedLines
const deleteLines = outStr[0].deletedLines
const changeLines = Math.max(addedLines, deleteLines)
outStr.changeLines = changeLines
return outStr
},
createdHtml (oldContent, newContent, context, outputFormat) {
export function makeSharedDiff(raw_diff: string, date: Date = new Date()): SharedDiff {
let expire_date = new Date();
expire_date.setDate(date.getDate() + 1);
return {
created: date,
expiresAt: expire_date,
diff: Diff2Html.getJsonFromDiff(raw_diff),
rawDiff: raw_diff,
};
}