@@ -6,9 +6,49 @@ const util = require('../lib/util')
6
6
const updatePatches = ( options ) => {
7
7
config . update ( options )
8
8
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
+ }
12
52
}
13
53
14
54
module . exports = updatePatches
0 commit comments