@@ -23,7 +23,7 @@ export const apply: BranchSequencerBase = (args) =>
23
23
// callbackAfterDone: defaultApplyCallback,
24
24
delayMsBetweenCheckouts : 0 ,
25
25
} ) . then (
26
- ( ret ) => ( unmarkThatNeedsToApply ( args . pathToStackedRebaseDirInsideDotGit ) , ret ) //
26
+ ( ret ) => ( markThatApplied ( args . pathToStackedRebaseDirInsideDotGit ) , ret ) //
27
27
) ;
28
28
29
29
const defaultApplyAction : ActionInsideEachCheckedOutBranch = async ( {
@@ -82,44 +82,72 @@ const defaultApplyCallback__disabled: CallbackAfterDone = ({
82
82
} ;
83
83
noop ( defaultApplyCallback__disabled ) ;
84
84
85
- export type ReturnOfApplyIfNeedsToApply =
85
+ export type ReturnOfApplyIfNeedsToApply = {
86
+ markThatNeedsToApply : ( ) => void ;
87
+ } & (
86
88
| {
87
89
neededToApply : false ;
88
90
userAllowedToApplyAndWeApplied ?: never ;
89
- markThatNeedsToApply : ( ) => void ;
90
91
}
91
92
| {
92
93
neededToApply : true ;
93
94
userAllowedToApplyAndWeApplied : false ;
94
95
// markThatNeedsToApply?: never; // TODO TS infer auto - force code owner to exit
95
- markThatNeedsToApply : ( ) => void ;
96
96
}
97
97
| {
98
98
neededToApply : true ;
99
99
userAllowedToApplyAndWeApplied : true ;
100
- markThatNeedsToApply : ( ) => void ;
101
- } ;
100
+ }
101
+ ) ;
102
102
103
- const filenameOfNeedsToApply = "needs-to-apply" as const ;
103
+ const getPaths = (
104
+ pathToStackedRebaseDirInsideDotGit : string //
105
+ ) =>
106
+ ( {
107
+ rewrittenListPath : path . join ( pathToStackedRebaseDirInsideDotGit , filenames . rewrittenList ) ,
108
+ needsToApplyPath : path . join ( pathToStackedRebaseDirInsideDotGit , filenames . needsToApply ) ,
109
+ appliedPath : path . join ( pathToStackedRebaseDirInsideDotGit , filenames . applied ) ,
110
+ } as const ) ;
111
+
112
+ const markThatNeedsToApply = (
113
+ pathToStackedRebaseDirInsideDotGit : string //
114
+ ) : void =>
115
+ [ getPaths ( pathToStackedRebaseDirInsideDotGit ) ] . map (
116
+ ( { rewrittenListPath, needsToApplyPath, appliedPath } ) => (
117
+ fs . existsSync ( rewrittenListPath )
118
+ ? fs . copyFileSync ( rewrittenListPath , needsToApplyPath )
119
+ : fs . writeFileSync ( needsToApplyPath , "" ) ,
120
+ fs . existsSync ( appliedPath ) && fs . unlinkSync ( appliedPath ) ,
121
+ void 0
122
+ )
123
+ ) [ 0 ] ;
124
+
125
+ export const markThatApplied = ( pathToStackedRebaseDirInsideDotGit : string ) : void =>
126
+ [ getPaths ( pathToStackedRebaseDirInsideDotGit ) ] . map (
127
+ ( { rewrittenListPath, needsToApplyPath, appliedPath } ) => (
128
+ fs . existsSync ( needsToApplyPath ) && fs . unlinkSync ( needsToApplyPath ) , //
129
+ fs . copyFileSync ( rewrittenListPath , appliedPath ) ,
130
+ void 0
131
+ )
132
+ ) [ 0 ] ;
133
+
134
+ const doesNeedToApply = ( pathToStackedRebaseDirInsideDotGit : string ) : boolean => {
135
+ const { rewrittenListPath, needsToApplyPath, appliedPath } = getPaths ( pathToStackedRebaseDirInsideDotGit ) ;
136
+
137
+ const needsToApplyPart1 : boolean = fs . existsSync ( needsToApplyPath ) ;
138
+ if ( needsToApplyPart1 ) {
139
+ return true ;
140
+ }
104
141
105
- const getPathOfFilenameOfNeedsToApply = ( pathToStackedRebaseDirInsideDotGit : string ) : string =>
106
- path . join ( pathToStackedRebaseDirInsideDotGit , filenameOfNeedsToApply ) ;
142
+ const needsToApplyPart2 : boolean = fs . existsSync ( appliedPath )
143
+ ? /**
144
+ * check if has been applied, but that apply is outdated
145
+ */
146
+ fs . readFileSync ( appliedPath ) !== fs . readFileSync ( rewrittenListPath )
147
+ : false ;
107
148
108
- /**
109
- * TODO rename "markThatApplied" because we are _not_ reversing the action
110
- * (undo-ing the file rename),
111
- * we are invoking a new action of removing the files.
112
- */
113
- export const unmarkThatNeedsToApply = (
114
- pathToStackedRebaseDirInsideDotGit : string ,
115
- mark = getPathOfFilenameOfNeedsToApply ( pathToStackedRebaseDirInsideDotGit ) ,
116
- rewrittenListFile : string = path . join ( pathToStackedRebaseDirInsideDotGit , filenames . rewrittenList ) ,
117
- rewrittenListAppliedFile : string = path . join ( pathToStackedRebaseDirInsideDotGit , filenames . rewrittenListApplied )
118
- ) : void => (
119
- fs . existsSync ( mark ) && fs . unlinkSync ( mark ) ,
120
- fs . existsSync ( rewrittenListFile ) && fs . renameSync ( rewrittenListFile , rewrittenListAppliedFile ) ,
121
- void 0
122
- ) ;
149
+ return needsToApplyPart2 ;
150
+ } ;
123
151
124
152
export async function applyIfNeedsToApply ( {
125
153
repo,
@@ -132,34 +160,13 @@ export async function applyIfNeedsToApply({
132
160
autoApplyIfNeeded : boolean ; //
133
161
config : Git . Config ;
134
162
} ) : Promise < ReturnOfApplyIfNeedsToApply > {
135
- /**
136
- * currently we're not saving the branch names
137
- * & where they point to etc.,
138
- * so doing rebase after rebase without --apply
139
- * will break the workflow after the 1st one.
140
- *
141
- * thus, until we have a more sophisticated solution,
142
- * automatically --apply'ing (when needed) should do just fine.
143
- *
144
- */
145
- const pathToFileIndicatingThatNeedsToApply = getPathOfFilenameOfNeedsToApply ( pathToStackedRebaseDirInsideDotGit ) ;
146
- const needsToApply : boolean = fs . existsSync ( pathToFileIndicatingThatNeedsToApply ) ;
147
-
148
- const pathToAppliedRewrittenListFile = path . join (
149
- pathToStackedRebaseDirInsideDotGit ,
150
- filenames . rewrittenListApplied
151
- ) ;
152
-
153
- const markThatNeedsToApply = ( ) : void => (
154
- fs . writeFileSync ( pathToFileIndicatingThatNeedsToApply , "" ) ,
155
- fs . existsSync ( pathToAppliedRewrittenListFile ) && fs . unlinkSync ( pathToAppliedRewrittenListFile ) ,
156
- void 0
157
- ) ;
163
+ const needsToApply : boolean = doesNeedToApply ( pathToStackedRebaseDirInsideDotGit ) ;
164
+ const _markThatNeedsToApply = ( ) : void => markThatNeedsToApply ( pathToStackedRebaseDirInsideDotGit ) ;
158
165
159
166
if ( ! needsToApply ) {
160
167
return {
161
168
neededToApply : false ,
162
- markThatNeedsToApply,
169
+ markThatNeedsToApply : _markThatNeedsToApply ,
163
170
} ;
164
171
}
165
172
@@ -181,7 +188,7 @@ export async function applyIfNeedsToApply({
181
188
return {
182
189
neededToApply : true ,
183
190
userAllowedToApplyAndWeApplied : false ,
184
- markThatNeedsToApply,
191
+ markThatNeedsToApply : _markThatNeedsToApply ,
185
192
} ;
186
193
}
187
194
@@ -196,13 +203,11 @@ export async function applyIfNeedsToApply({
196
203
pathToStackedRebaseDirInsideDotGit, //
197
204
...rest ,
198
205
} ) ;
199
-
200
- unmarkThatNeedsToApply ( pathToStackedRebaseDirInsideDotGit ) ;
201
206
}
202
207
203
208
return {
204
209
neededToApply : true ,
205
210
userAllowedToApplyAndWeApplied : true , //
206
- markThatNeedsToApply,
211
+ markThatNeedsToApply : _markThatNeedsToApply ,
207
212
} ;
208
213
}
0 commit comments