Skip to content

Commit d22fd97

Browse files
committed
change npm run update_patches to generate one file per Modified file
and one big file for Deleted files for issue brave/brave-browser-snap#36
1 parent 5ab2540 commit d22fd97

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

lib/updatePatches.js

+54-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,60 @@ const util = require('../lib/util')
66
const updatePatches = (options) => {
77
config.update(options)
88

9-
const diffArgs = ['diff', '--full-index', '--ignore-space-at-eol']
10-
let diff = util.run('git', diffArgs, { cwd: config.projects.chrome.dir })
11-
fs.writeFileSync(path.join(config.projects.antimuon.dir, 'patches', 'master_patch.patch'), diff.stdout)
9+
const runOptions = { cwd: config.projects.chrome.dir }
10+
const patchDir = path.join(config.projects.antimuon.dir, 'patches')
11+
12+
console.log('updatePatches writing files to: ' + patchDir)
13+
14+
// grab Modified (and later Deleted) files but not Created (since we copy those)
15+
const modifiedDiffArgs = ['diff', '--diff-filter=M', '--name-only', '--ignore-space-at-eol']
16+
let modifiedDiff = util.run('git', modifiedDiffArgs, runOptions)
17+
let moddedFileList = modifiedDiff.stdout.toString().split('\n').filter(s => s.length > 0)
18+
19+
let n = moddedFileList.length
20+
21+
// When splitting one large diff into a per-file diff, there are a few ways
22+
// you can go about it. Because different files can have the same name
23+
// (by being located in different directories), you need to avoid collisions.
24+
// Mirroring the directory structure seems undesirable.
25+
// Prefixing with numbers works but is O(n) volatile for O(1) additions
26+
// We choose here to flatten the directory structure by replacing separators
27+
// In practice this will avoid collisions. Should a pathological case ever
28+
// appear, you can quickly patch this by changing the separator, even
29+
// to something longer
30+
const replacingForwardSlashes = true
31+
const replacingBackslashes = true
32+
33+
const desiredReplacementString = '.'
34+
const patchExtension = '.patch'
35+
36+
for (var i = 0; i < n; i++) {
37+
const old = moddedFileList[i]
38+
let revised = old
39+
40+
if (replacingForwardSlashes) {
41+
revised = revised.replace(/\//g, desiredReplacementString)
42+
}
43+
44+
if (replacingBackslashes) {
45+
revised = revised.replace(/\\/g, desiredReplacementString)
46+
}
47+
48+
const singleDiffArgs = ['diff', '--full-index', old]
49+
let singleDiff = util.run('git', singleDiffArgs, runOptions)
50+
51+
const contents = singleDiff.stdout.toString()
52+
const filename = revised + patchExtension
53+
54+
fs.writeFileSync(path.join(patchDir, filename), contents)
55+
56+
console.log('updatePatches wrote ' + (1 + i) + '/' + n + ': ' + filename)
57+
}
58+
59+
// finish off by creating one big patch for deleted files
60+
const deletedDiffArgs = ['diff', '--diff-filter=D', '--full-index', '--ignore-space-at-eol']
61+
let deletedDiff = util.run('git', deletedDiffArgs, runOptions)
62+
fs.writeFileSync(path.join(patchDir, 'master_deleted_patch.patch'), deletedDiff.stdout)
1263
}
1364

1465
module.exports = updatePatches

0 commit comments

Comments
 (0)