11#!/usr/bin/env ts-node-dev
22
33import fs from "fs" ;
4- import path from "path" ;
5- import assert from "assert" ;
64
7- import Git from "nodegit " ;
5+ import { setupRepoWithStackedBranches } from "./setupRepo " ;
86
97import { gitStackedRebase , defaultGitCmd } from "../git-stacked-rebase" ;
10-
11- import { RegularRebaseCommand } from "../parse-todo-of-stacked-rebase/validator" ;
12- import { createExecSyncInRepo } from "../util/execSyncInRepo" ;
13- import { configKeys } from "../configKeys" ;
8+ import { humanOpChangeCommandOfNthCommitInto } from "../humanOp" ;
149
1510export async function testCase ( ) {
1611 const {
17- repo, //
18- config,
19- sig,
12+ initialBranch, //
2013 dir,
21- } = await setupRepo ( ) ;
22-
23- const commitOidsInInitial : Git . Oid [ ] = [ ] ;
24- const initialBranch : Git . Reference = await appendCommitsTo ( commitOidsInInitial , 3 , repo , sig ) ;
25-
26- const latestStackedBranch : Git . Reference = await Git . Branch . create (
27- repo ,
28- "stack-latest" ,
29- await repo . getHeadCommit ( ) ,
30- 0
31- ) ;
32- await repo . checkoutBranch ( latestStackedBranch ) ;
33-
34- const execSyncInRepo = createExecSyncInRepo ( repo ) ;
35-
36- // const read = () => execSyncInRepo("read");
37- const read = ( ) => void 0 ;
38-
39- read ( ) ;
40-
41- const commitOidsInLatestStacked : Git . Oid [ ] = [ ] ;
42- await appendCommitsTo ( commitOidsInLatestStacked , 12 , repo , sig ) ;
43-
44- const newPartialBranches = [
45- [ "partial-1" , 4 ] ,
46- [ "partial-2" , 6 ] ,
47- [ "partial-3" , 8 ] ,
48- ] as const ;
49-
50- console . log ( "launching 1st rebase to create partial branches" ) ;
51- await gitStackedRebase ( initialBranch . shorthand ( ) , {
52- gitDir : dir ,
53- getGitConfig : ( ) => config ,
54- editor : async ( { filePath } ) => {
55- console . log ( "filePath %s" , filePath ) ;
56-
57- for ( const [ newPartial , nthCommit ] of newPartialBranches ) {
58- await humanOpAppendLineAfterNthCommit (
59- filePath ,
60- commitOidsInLatestStacked [ nthCommit ] . tostrS ( ) ,
61- `branch-end-new ${ newPartial } `
62- ) ;
63- }
64-
65- console . log ( "finished editor" ) ;
66-
67- read ( ) ;
68- } ,
69- } ) ;
70-
71- console . log ( "looking up branches to make sure they were created successfully" ) ;
72- read ( ) ;
73- for ( const [ newPartial ] of newPartialBranches ) {
74- /**
75- * will throw if branch does not exist
76- * TODO "properly" expect to not throw
77- */
78- await Git . Branch . lookup ( repo , newPartial , Git . Branch . BRANCH . LOCAL ) ;
79- }
14+ config,
15+ commitOidsInLatestStacked,
16+ read,
17+ execSyncInRepo,
18+ } = await setupRepoWithStackedBranches ( ) ;
8019
8120 /**
8221 *
@@ -92,7 +31,7 @@ export async function testCase() {
9231 editor : async ( { filePath } ) => {
9332 const SHA = commitOidsInLatestStacked [ nthCommit2ndRebase ] . tostrS ( ) ;
9433
95- humanOpChangeCommandOfNthCommitInto ( "edit" , SHA , filePath ) ;
34+ humanOpChangeCommandOfNthCommitInto ( "edit" , { filePath, commitSHA : SHA } ) ;
9635 } ,
9736 } ) ;
9837 /**
@@ -133,122 +72,3 @@ export async function testCase() {
13372 apply : true ,
13473 } ) ;
13574}
136-
137- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
138- export async function setupRepo ( ) {
139- const dir : string = path . join ( __dirname , ".tmp" ) ;
140- if ( fs . existsSync ( dir ) ) {
141- fs . rmdirSync ( dir , { recursive : true } ) ;
142- }
143- fs . mkdirSync ( dir ) ;
144- console . log ( "tmpdir path %s" , dir ) ;
145-
146- const foldersToDeletePath : string = path . join ( __dirname , "folders-to-delete" ) ;
147- fs . appendFileSync ( foldersToDeletePath , dir + "\n" , { encoding : "utf-8" } ) ;
148-
149- process . chdir ( dir ) ;
150- console . log ( "chdir to tmpdir" ) ;
151-
152- const isBare = 0 ;
153- const repo : Git . Repository = await Git . Repository . init ( dir , isBare ) ;
154-
155- const config : Git . Config = await repo . config ( ) ;
156-
157- await config . setBool ( configKeys . autoApplyIfNeeded , Git . Config . MAP . FALSE ) ;
158- await config . setString ( "user.email" , "tester@test.com" ) ;
159- await config . setString ( "user.name" , "tester" ) ;
160-
161- /**
162- * gpg signing in tests not possible i believe,
163- * at least wasn't working.
164- */
165- await config . setBool ( configKeys . gpgSign , Git . Config . MAP . FALSE ) ;
166-
167- /**
168- * fixups / not implemented in libgit2.
169- * though, would be better if received empty/minimal config by default..
170- */
171- await config . setString ( "merge.conflictStyle" , "diff3" ) ; // zdiff3
172-
173- const sig : Git . Signature = await Git . Signature . default ( repo ) ;
174- console . log ( "sig %s" , sig ) ;
175-
176- const inicialCommitId = "Initial commit (from setupRepo)" ;
177- const initialCommit : Git . Oid = await fs . promises
178- . writeFile ( inicialCommitId , inicialCommitId ) //
179- . then ( ( ) => repo . createCommitOnHead ( [ inicialCommitId ] , sig , sig , inicialCommitId ) ) ;
180-
181- return {
182- dir,
183- repo,
184- config,
185- sig,
186- initialCommit,
187- } as const ;
188- }
189-
190- async function appendCommitsTo (
191- alreadyExistingCommits : Git . Oid [ ] ,
192- n : number ,
193- repo : Git . Repository , //
194- sig : Git . Signature
195- ) : Promise < Git . Reference > {
196- assert ( n > 0 , "cannot append <= 0 commits" ) ;
197-
198- const commits : string [ ] = new Array ( n )
199- . fill ( 0 ) //
200- . map ( ( _ , i ) => "a" . charCodeAt ( 0 ) + i + alreadyExistingCommits . length )
201- . map ( ( ascii ) => String . fromCharCode ( ascii ) ) ;
202-
203- for ( const c of commits ) {
204- const branchName : string = repo . isEmpty ( ) ? "<initial>" : ( await repo . getCurrentBranch ( ) ) . shorthand ( ) ;
205- const cInBranch : string = c + " in " + branchName ;
206-
207- const oid : Git . Oid = await fs . promises
208- . writeFile ( c , cInBranch ) //
209- . then ( ( ) => repo . createCommitOnHead ( [ c ] , sig , sig , cInBranch ) ) ;
210-
211- alreadyExistingCommits . push ( oid ) ;
212-
213- console . log ( `oid of commit "%s" in branch "%s": %s` , c , branchName , oid ) ;
214- }
215-
216- return repo . getCurrentBranch ( ) ;
217- }
218-
219- /**
220- * TODO general "HumanOp" for `appendLineAfterNthCommit` & similar utils
221- */
222- async function humanOpAppendLineAfterNthCommit (
223- filePath : string , //
224- commitSHA : string ,
225- newLine : string
226- ) : Promise < void > {
227- const file = await fs . promises . readFile ( filePath , { encoding : "utf-8" } ) ;
228- const lines = file . split ( "\n" ) ;
229- const lineIdx : number = lines . findIndex ( ( line ) => line . startsWith ( `pick ${ commitSHA } ` ) ) ;
230-
231- console . log ( "commitSHA: %s, lineIdx: %s, newLine: %s" , commitSHA , lineIdx , newLine ) ;
232-
233- lines . splice ( lineIdx , 0 , newLine ) ;
234-
235- await fs . promises . writeFile ( filePath , lines . join ( "\n" ) ) ;
236- }
237-
238- function humanOpChangeCommandOfNthCommitInto (
239- newCommand : RegularRebaseCommand , //
240- commitSHA : string ,
241- filePath : string
242- ) : void {
243- const file = fs . readFileSync ( filePath , { encoding : "utf-8" } ) ;
244- const lines = file . split ( "\n" ) ;
245- const lineIdx : number = lines . findIndex ( ( line ) => line . startsWith ( `pick ${ commitSHA } ` ) ) ;
246-
247- console . log ( "commitSHA: %s, lineIdx: %s, newCommand: %s" , commitSHA , lineIdx , newCommand ) ;
248-
249- const parts = lines [ lineIdx ] . split ( " " ) ;
250- parts [ 0 ] = newCommand ;
251- lines [ lineIdx ] = parts . join ( " " ) ;
252-
253- fs . writeFileSync ( filePath , lines . join ( "\n" ) ) ;
254- }
0 commit comments