Skip to content

Commit 239bc14

Browse files
committed
warn patch maintainer to remove patch
brave/brave#49 rm cruft patches in addition to notifying
1 parent 17afdbe commit 239bc14

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

lib/updatePatches.js

+46-15
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)
35+
36+
// Add files here we specifically want to keep around regardless
37+
const exclusionList = []
1838

19-
let n = moddedFileList.length
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 cruftList = 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,33 @@ 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'
33-
34-
for (var i = 0; i < n; i++) {
35-
const old = moddedFileList[i]
36-
let revised = old
56+
let n = modifiedFileList.length
3757

38-
//replacing forward slashes
39-
//since git on Windows doesn't use backslashes, this is sufficient
40-
revised = revised.replace(/\//g, desiredReplacementSeparator)
58+
for (let i = 0; i < n; i++) {
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+
// regular rm patchfiles whose target is no longer modified
74+
let m = cruftList.length
75+
for (let i = 0; i < m; i++) {
76+
let filename = cruftList[i]
77+
let fullpath = path.join(patchDir, filename)
78+
79+
fs.removeSync(fullpath)
80+
81+
console.log('updatePatches *REMOVED* ' + (1 + i) + '/' + m + ': ' + filename)
82+
}
5283
}
5384

5485
module.exports = updatePatches

0 commit comments

Comments
 (0)