Skip to content

Commit 22f9fea

Browse files
committed
warn patch maintainer to remove patch
brave/brave#49
1 parent 17afdbe commit 22f9fea

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

lib/updatePatches.js

+46-14
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,42 @@ const util = require('../lib/util')
66
const updatePatches = (options) => {
77
config.update(options)
88

9-
const runOptions = { cwd: config.projects.chrome.dir }
109
const patchDir = path.join(config.projects.antimuon.dir, 'patches')
1110

11+
const runOptionsChrome = { cwd: config.projects.chrome.dir }
12+
const runOptionsPatch = { cwd: patchDir }
13+
14+
const desiredReplacementSeparator = '-'
15+
const patchExtension = '.patch'
16+
1217
console.log('updatePatches writing files to: ' + patchDir)
1318

1419
// grab Modified (and later Deleted) files but not Created (since we copy those)
1520
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)
21+
let modifiedDiff = util.run('git', modifiedDiffArgs, runOptionsChrome)
22+
let modifiedFileList = modifiedDiff.stdout.toString().split('\n').filter(s => s.length > 0)
23+
24+
// copy array
25+
let substitutedFileList = modifiedFileList.slice()
26+
27+
// replacing forward slashes and adding the patch extension to get nice filenames
28+
// since git on Windows doesn't use backslashes, this is sufficient
29+
substitutedFileList = substitutedFileList.map(s => s.replace(/\//g, desiredReplacementSeparator) + patchExtension)
30+
31+
// grab every existing patch file in the dir (at this point, patchfiles for now-unmodified files live on)
32+
const existingFileArgs = ['ls-files', '--exclude-standard']
33+
let existingFileOutput = util.run('git', existingFileArgs, runOptionsPatch)
34+
let existingFileList = existingFileOutput.stdout.toString().split('\n').filter(s => s.length > 0)
1835

19-
let n = moddedFileList.length
36+
// Add files here we specifically want to keep around regardless
37+
const exclusionList = []
38+
39+
// Subtract to find which patchfiles no longer have diffs, yet still exist
40+
const minuhend = existingFileList
41+
const subtrahend = substitutedFileList.concat(exclusionList)
42+
const difference = minuhend.filter(x => !subtrahend.includes(x))
43+
44+
const potentialCruftList = difference
2045

2146
// When splitting one large diff into a per-file diff, there are a few ways
2247
// you can go about it. Because different files can have the same name
@@ -28,27 +53,34 @@ const updatePatches = (options) => {
2853
// appear, you can quickly patch this by changing the separator, even
2954
// to something longer
3055

31-
const desiredReplacementSeparator = '-'
32-
const patchExtension = '.patch'
56+
let n = modifiedFileList.length
3357

3458
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)
59+
const old = modifiedFileList[i]
60+
const revised = substitutedFileList[i]
4161

4262
const singleDiffArgs = ['diff', '--src-prefix=a/', '--dst-prefix=b/', '--full-index', old]
43-
let singleDiff = util.run('git', singleDiffArgs, runOptions)
63+
let singleDiff = util.run('git', singleDiffArgs, runOptionsChrome)
4464

4565
const contents = singleDiff.stdout.toString()
46-
const filename = revised + patchExtension
66+
const filename = revised
4767

4868
fs.writeFileSync(path.join(patchDir, filename), contents)
4969

5070
console.log('updatePatches wrote ' + (1 + i) + '/' + n + ': ' + filename)
5171
}
72+
73+
// emit a warning to patch maintainers
74+
if (potentialCruftList.length > 0) {
75+
console.log('--------------------------------------------------------------')
76+
console.log('NOTICE: The following patchfiles have now-unmodified targets.')
77+
console.log(' Check to see if you need need to use `git rm`')
78+
console.log(' Alternatively, add them to the exclusion list.')
79+
console.log('Patch dir: ' + patchDir)
80+
console.log('--------------------------------------------------------------')
81+
potentialCruftList.filter(x => console.log(x))
82+
console.log('--------------------------------------------------------------')
83+
}
5284
}
5385

5486
module.exports = updatePatches

0 commit comments

Comments
 (0)