Skip to content

Commit e4f975a

Browse files
authored
Merge pull request #44 from brave/split-patches
split patches w/ no "deleted" patch
2 parents 613d9e4 + 35f7cd1 commit e4f975a

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

lib/updatePatches.js

+43-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,49 @@ 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+
31+
const desiredReplacementSeparator = '-'
32+
const patchExtension = '.patch'
33+
34+
for (var i = 0; i < n; i++) {
35+
const old = moddedFileList[i]
36+
let revised = old
37+
38+
//replacing forward slashes
39+
//since git on Windows doesn't use backslashes, this is sufficient
40+
revised = revised.replace(/\//g, desiredReplacementSeparator)
41+
42+
const singleDiffArgs = ['diff', '--src-prefix=a/', '--dst-prefix=b/', '--full-index', old]
43+
let singleDiff = util.run('git', singleDiffArgs, runOptions)
44+
45+
const contents = singleDiff.stdout.toString()
46+
const filename = revised + patchExtension
47+
48+
fs.writeFileSync(path.join(patchDir, filename), contents)
49+
50+
console.log('updatePatches wrote ' + (1 + i) + '/' + n + ': ' + filename)
51+
}
1252
}
1353

1454
module.exports = updatePatches

0 commit comments

Comments
 (0)