1- #!/usr/bin/env node
1+ #!/usr/bin/env ts- node-dev
22
33/* eslint-disable */
44
5- const assert = require ( "assert" )
6- const fs = require ( "fs" )
7- const { execSync } = require ( "child_process" )
5+ import assert from "assert"
6+ import fs from "fs"
7+ import { execSync } from "child_process"
88
9- const obj1 = {
10- "a" : "b" ,
11- "b" : "c" ,
12- "c" : "d" ,
13- "d" : "e" ,
9+ export type StringFromToMap = { [ key : string ] : string }
1410
15- "g" : "h" ,
16-
17- "x" : "x" ,
18-
19- "y" : "z" ,
20- "z" : "z" ,
21-
22- /**
23- * this might mean that we need to go backwards
24- * rather than forwards
25- * (multiple commits can be reported as rewritten into one,
26- * but i don't think the opposite is possible)
27- *
28- * ~~and/or we might need another phase,
29- * because currently, A -> F,
30- * and both B and C stay at D.~~
31- * done
32- *
33- */
34- "A" : "D" ,
35- "B" : "D" ,
36- "C" : "D" ,
37- "D" : "E" ,
38- "E" : "F" ,
39- }
40-
41- reducePath ( obj1 )
42- console . log ( obj1 )
43- assert . deepStrictEqual ( obj1 , {
44- "a" : "e" ,
45-
46- "g" : "h" ,
47-
48- "x" : "x" ,
49-
50- "y" : "z" ,
51-
52- "A" : "F" ,
53- "B" : "F" ,
54- "C" : "F" ,
55- } )
56-
57- function reducePath ( obj ) {
58- let prevSize = - Infinity
59- let entries
60- let keysMarkedForDeletion = new Set ( )
11+ /**
12+ * mutates `obj` and returns it too
13+ */
14+ export function reducePath ( obj : StringFromToMap ) : StringFromToMap {
15+ let prevSize : number = - Infinity
16+ let entries : [ string , string ] [ ]
17+ let keysMarkedForDeletion : Set < string > = new Set < string > ( )
6118
6219 // as long as it continues to improve
6320 while ( keysMarkedForDeletion . size > prevSize ) {
@@ -100,30 +57,61 @@ function reducePath(obj) {
10057 return obj
10158}
10259
103- function combineRewrittenLists ( rewrittenListFile ) {
60+ export type RewrittenListBlockBase = {
61+ mapping : StringFromToMap
62+ }
63+ export type RewrittenListBlockAmend = RewrittenListBlockBase & {
64+ type : "amend"
65+ }
66+ export type RewrittenListBlockRebase = RewrittenListBlockBase & {
67+ type : "rebase"
68+ }
69+ export type RewrittenListBlock = RewrittenListBlockAmend | RewrittenListBlockRebase
70+
71+ export type CombineRewrittenListsRet = {
72+ /**
73+ * notice that this only includes rebases, no amends --
74+ * that's the whole point.
75+ *
76+ * further, probably only the 1st one is necessary,
77+ * because it's likely that we'll start creating separate files for new rebases,
78+ * or that might not be needed at all, because we might be able to
79+ * --apply after every rebase, no matter if the user exited or not,
80+ * thus we'd always have only 1 "rebase" block in the rewritten list.
81+ */
82+ mergedReducedRewrittenLists : RewrittenListBlockRebase [ ] ,
83+
84+ /**
85+ * the git's standard represantation of the rewritten-list
86+ * (no extras of ours)
87+ */
88+ combinedRewrittenList : string ,
89+ }
90+ export function combineRewrittenLists ( rewrittenListFileContent : string ) : CombineRewrittenListsRet {
10491 /**
10592 * $1 (amend/rebase)
10693 */
107- const extraOperatorLineCount = 1
94+ const extraOperatorLineCount = 1 as const
10895
109- const rewrittenLists = rewrittenListFile
96+ const rewrittenLists : RewrittenListBlock [ ] = rewrittenListFileContent
11097 . split ( "\n\n" )
11198 . map ( lists => lists . split ( "\n" ) )
11299 . map ( list => list [ list . length - 1 ] === "" ? list . slice ( 0 , - 1 ) : list )
113100 // .slice(0, -1)
114101 . filter ( list => list . length > extraOperatorLineCount )
115- . map ( list => ( {
116- type : list [ 0 ] ,
117- mapping : Object . fromEntries (
118- list . slice ( 1 ) . map ( line => line . split ( " " ) )
102+ . map ( ( list ) : RewrittenListBlock => ( {
103+ type : list [ 0 ] as RewrittenListBlock [ "type" ] ,
104+ mapping : Object . fromEntries < string > (
105+ list . slice ( 1 ) . map ( line => line . split ( " " ) as [ string , string ] )
119106 )
120107 } )
121108 )
122109 // .map(list => Object.fromEntries(list))
123110 console . log ( "rewrittenLists" , rewrittenLists )
124111
125- let prev = [ ]
126- let mergedReducedRewrittenLists = [ ]
112+ let prev : RewrittenListBlockAmend [ ] = [ ]
113+ let mergedReducedRewrittenLists : RewrittenListBlockRebase [ ] = [ ]
114+
127115 for ( const list of rewrittenLists ) {
128116 if ( list . type === "amend" ) {
129117 prev . push ( list )
@@ -169,7 +157,7 @@ function combineRewrittenLists(rewrittenListFile) {
169157 reducePath ( list . mapping )
170158 mergedReducedRewrittenLists . push ( list )
171159 } else {
172- throw new Error ( `invalid list type (got "${ list . type } ")` )
160+ throw new Error ( `invalid list type (got "${ ( list as any ) . type } ")` )
173161 }
174162 }
175163 /**
@@ -181,7 +169,7 @@ function combineRewrittenLists(rewrittenListFile) {
181169 console . log ( "mergedReducedRewrittenLists" , mergedReducedRewrittenLists )
182170
183171 const combinedRewrittenList = Object . entries ( mergedReducedRewrittenLists [ 0 ] . mapping ) . map ( ( [ k , v ] ) => k + " " + v ) . join ( "\n" ) + "\n"
184- fs . writeFileSync ( "rewritten-list" , combinedRewrittenList )
172+ // fs.writeFileSync("rewritten-list", combinedRewrittenList)
185173
186174 return {
187175 mergedReducedRewrittenLists,
@@ -198,13 +186,21 @@ if (!module.parent) {
198186
199187 const b4 = Object . keys ( mergedReducedRewrittenLists [ 0 ] . mapping )
200188 const after = Object . values ( mergedReducedRewrittenLists [ 0 ] . mapping )
189+
190+ const path = require ( "path" )
191+ const os = require ( "os" )
192+ const dir = path . join ( os . tmpdir ( ) , "gsr-reduce-path" )
193+ fs . mkdirSync ( dir , { recursive : true } )
201194
202- fs . writeFileSync ( "b4" , b4 . join ( "\n" ) + "\n" )
203- fs . writeFileSync ( "after" , after . join ( "\n" ) + "\n" )
195+ const b4path = path . join ( dir , "b4" )
196+ const afterpath = path . join ( dir , "after" )
197+ fs . writeFileSync ( b4path , b4 . join ( "\n" ) + "\n" )
198+ fs . writeFileSync ( afterpath , after . join ( "\n" ) + "\n" )
204199
205200 const N = after . length
206201 console . log ( { N } )
207202
208- execSync ( `git log --pretty=format:"%H" | head -n ${ N } | tac - > curr` )
209- execSync ( `diff -us curr after` , { stdio : "inherit" } )
203+ const currpath = path . join ( dir , "curr" )
204+ execSync ( `git log --pretty=format:"%H" | head -n ${ N } | tac - > ${ currpath } ` )
205+ execSync ( `diff -us ${ currpath } ${ afterpath } ` , { stdio : "inherit" } )
210206}
0 commit comments