@@ -100,100 +100,111 @@ function reducePath(obj) {
100100 return obj
101101}
102102
103- const prefix = "" // "test/.tmp-described.off/"
104- const rewrittenListFile = fs . readFileSync ( prefix + ".git/stacked-rebase/rewritten-list" , { encoding : "utf-8" } )
105- console . log ( { rewrittenListFile } )
106-
107- /**
108- * $1 (amend/rebase)
109- */
110- const extraOperatorLineCount = 1
111-
112- const rewrittenLists = rewrittenListFile
113- . split ( "\n\n" )
114- . map ( lists => lists . split ( "\n" ) )
115- . map ( list => list [ list . length - 1 ] === "" ? list . slice ( 0 , - 1 ) : list )
116- // .slice(0, -1)
117- . filter ( list => list . length > extraOperatorLineCount )
118- . map ( list => ( {
119- type : list [ 0 ] ,
120- mapping : Object . fromEntries (
121- list . slice ( 1 ) . map ( line => line . split ( " " ) )
122- )
123- } )
124- )
125- // .map(list => Object.fromEntries(list))
126- console . log ( "rewrittenLists" , rewrittenLists )
127-
128- let prev = [ ]
129- let mergedReducedRewrittenLists = [ ]
130- for ( const list of rewrittenLists ) {
131- if ( list . type === "amend" ) {
132- prev . push ( list )
133- } else if ( list . type === "rebase" ) {
134- /**
135- * merging time
136- */
137- for ( const amend of prev ) {
138- assert . equal ( Object . keys ( amend . mapping ) . length , 1 )
139-
140- const [ key , value ] = Object . entries ( amend . mapping ) [ 0 ]
141-
103+ function combineRewrittenLists ( rewrittenListFile ) {
104+ /**
105+ * $1 (amend/rebase)
106+ */
107+ const extraOperatorLineCount = 1
108+
109+ const rewrittenLists = rewrittenListFile
110+ . split ( "\n\n" )
111+ . map ( lists => lists . split ( "\n" ) )
112+ . map ( list => list [ list . length - 1 ] === "" ? list . slice ( 0 , - 1 ) : list )
113+ // .slice(0, -1)
114+ . filter ( list => list . length > extraOperatorLineCount )
115+ . map ( list => ( {
116+ type : list [ 0 ] ,
117+ mapping : Object . fromEntries (
118+ list . slice ( 1 ) . map ( line => line . split ( " " ) )
119+ )
120+ } )
121+ )
122+ // .map(list => Object.fromEntries(list))
123+ console . log ( "rewrittenLists" , rewrittenLists )
124+
125+ let prev = [ ]
126+ let mergedReducedRewrittenLists = [ ]
127+ for ( const list of rewrittenLists ) {
128+ if ( list . type === "amend" ) {
129+ prev . push ( list )
130+ } else if ( list . type === "rebase" ) {
142131 /**
143- * (try to) merge
132+ * merging time
144133 */
145- if ( key in list . mapping ) {
146- if ( value === list . mapping [ key ] ) {
147- // pointless
148- continue
134+ for ( const amend of prev ) {
135+ assert . equal ( Object . keys ( amend . mapping ) . length , 1 )
136+
137+ const [ key , value ] = Object . entries ( amend . mapping ) [ 0 ]
138+
139+ /**
140+ * (try to) merge
141+ */
142+ if ( key in list . mapping ) {
143+ if ( value === list . mapping [ key ] ) {
144+ // pointless
145+ continue
146+ } else {
147+ throw new Error (
148+ `NOT IMPLEMENTED - identical key in 'amend' and 'rebase', but different values.`
149+ + `(key = "${ key } ", amend's value = "${ value } ", rebase's value = "${ list . mapping [ key ] } ")`
150+ )
151+ }
149152 } else {
150- throw new Error (
151- `NOT IMPLEMENTED - identical key in 'amend' and 'rebase', but different values.`
152- + `(key = "${ key } ", amend's value = "${ value } ", rebase's value = "${ list . mapping [ key ] } ")`
153- )
154- }
155- } else {
156- if ( Object . values ( list . mapping ) . includes ( key ) ) {
157- /**
158- * add the single new entry of amend's mapping into rebase's mapping.
159- * it will get `reducePath`'d later.
160- */
161- Object . assign ( list . mapping , amend . mapping )
162- } else {
163- throw new Error (
164- "NOT IMPLEMENTED - neither key nor value of 'amend' was included in the 'rebase'."
165- + "could be that we missed the ordering, or when we call 'reducePath', or something else."
166- )
153+ if ( Object . values ( list . mapping ) . includes ( key ) ) {
154+ /**
155+ * add the single new entry of amend's mapping into rebase's mapping.
156+ * it will get `reducePath`'d later.
157+ */
158+ Object . assign ( list . mapping , amend . mapping )
159+ } else {
160+ throw new Error (
161+ "NOT IMPLEMENTED - neither key nor value of 'amend' was included in the 'rebase'."
162+ + "could be that we missed the ordering, or when we call 'reducePath', or something else."
163+ )
164+ }
167165 }
168166 }
167+
168+ prev = [ ]
169+ reducePath ( list . mapping )
170+ mergedReducedRewrittenLists . push ( list )
171+ } else {
172+ throw new Error ( `invalid list type (got "${ list . type } ")` )
169173 }
174+ }
175+ /**
176+ * TODO handle multiple rebases
177+ * or, multiple separate files for each new rebase,
178+ * since could potentially lose some info if skipping partial steps?
179+ */
180+
181+ console . log ( "mergedReducedRewrittenLists" , mergedReducedRewrittenLists )
170182
171- prev = [ ]
172- reducePath ( list . mapping )
173- mergedReducedRewrittenLists . push ( list )
174- } else {
175- throw new Error ( `invalid list type (got "${ list . type } ")` )
183+ const combinedRewrittenList = Object . entries ( mergedReducedRewrittenLists [ 0 ] . mapping ) . map ( ( [ k , v ] ) => k + " " + v ) . join ( "\n" ) + "\n"
184+ fs . writeFileSync ( "rewritten-list" , combinedRewrittenList )
185+
186+ return {
187+ mergedReducedRewrittenLists,
188+ combinedRewrittenList,
176189 }
177190}
178- /**
179- * TODO handle multiple rebases
180- * or, multiple separate files for each new rebase,
181- * since could potentially lose some info if skipping partial steps?
182- */
183-
184- console . log ( "mergedReducedRewrittenLists" , mergedReducedRewrittenLists )
185191
186- const combinedRewrittenList = Object . entries ( mergedReducedRewrittenLists [ 0 ] . mapping ) . map ( ( [ k , v ] ) => k + " " + v ) . join ( "\n" ) + "\n"
187- fs . writeFileSync ( "rewritten-list" , combinedRewrittenList )
192+ if ( ! module . parent ) {
193+ const prefix = "" // "test/.tmp-described.off/"
194+ const rewrittenListFile = fs . readFileSync ( prefix + ".git/stacked-rebase/rewritten-list" , { encoding : "utf-8" } )
195+ console . log ( { rewrittenListFile } )
188196
189- const b4 = Object . keys ( mergedReducedRewrittenLists [ 0 ] . mapping )
190- const after = Object . values ( mergedReducedRewrittenLists [ 0 ] . mapping )
197+ const { mergedReducedRewrittenLists } = combineRewrittenLists ( rewrittenListFile )
191198
192- fs . writeFileSync ( "b4" , b4 . join ( "\n" ) + "\n" )
193- fs . writeFileSync ( "after" , after . join ( "\n" ) + "\n" )
199+ const b4 = Object . keys ( mergedReducedRewrittenLists [ 0 ] . mapping )
200+ const after = Object . values ( mergedReducedRewrittenLists [ 0 ] . mapping )
201+
202+ fs . writeFileSync ( "b4" , b4 . join ( "\n" ) + "\n" )
203+ fs . writeFileSync ( "after" , after . join ( "\n" ) + "\n" )
194204
195- const N = after . length
196- console . log ( { N } )
205+ const N = after . length
206+ console . log ( { N } )
197207
198- execSync ( `git log --pretty=format:"%H" | head -n ${ N } | tac - > curr` )
199- execSync ( `diff -us curr after` , { stdio : "inherit" } )
208+ execSync ( `git log --pretty=format:"%H" | head -n ${ N } | tac - > curr` )
209+ execSync ( `diff -us curr after` , { stdio : "inherit" } )
210+ }
0 commit comments