@@ -6,17 +6,42 @@ const util = require('../lib/util')
6
6
const updatePatches = ( options ) => {
7
7
config . update ( options )
8
8
9
- const runOptions = { cwd : config . projects . chrome . dir }
10
9
const patchDir = path . join ( config . projects . antimuon . dir , 'patches' )
11
10
11
+ const runOptionsChrome = { cwd : config . projects . chrome . dir }
12
+ const runOptionsPatch = { cwd : patchDir }
13
+
14
+ const desiredReplacementSeparator = '-'
15
+ const patchExtension = '.patch'
16
+
12
17
console . log ( 'updatePatches writing files to: ' + patchDir )
13
18
14
19
// grab Modified (and later Deleted) files but not Created (since we copy those)
15
20
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 = [ ]
18
38
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
20
45
21
46
// When splitting one large diff into a per-file diff, there are a few ways
22
47
// you can go about it. Because different files can have the same name
@@ -28,27 +53,33 @@ const updatePatches = (options) => {
28
53
// appear, you can quickly patch this by changing the separator, even
29
54
// to something longer
30
55
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
37
57
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 ]
41
61
42
62
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 )
44
64
45
65
const contents = singleDiff . stdout . toString ( )
46
- const filename = revised + patchExtension
66
+ const filename = revised
47
67
48
68
fs . writeFileSync ( path . join ( patchDir , filename ) , contents )
49
69
50
70
console . log ( 'updatePatches wrote ' + ( 1 + i ) + '/' + n + ': ' + filename )
51
71
}
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
+ }
52
83
}
53
84
54
85
module . exports = updatePatches
0 commit comments