2
2
3
3
// import fs from "fs";
4
4
5
+ import Git from "nodegit" ;
6
+
5
7
import { getWantedCommitsWithBranchBoundariesOurCustomImpl } from "./git-stacked-rebase" ;
6
8
import {
7
9
branchSequencer , //
@@ -10,6 +12,8 @@ import {
10
12
// getBackupPathOfPreviousStackedRebase,
11
13
} from "./branchSequencer" ;
12
14
15
+ import { createQuestion } from "./util/createQuestion" ;
16
+
13
17
export const forcePush : BranchSequencerBase = ( argsBase ) =>
14
18
// /**
15
19
// * TODO TESTS we __really__ need to make sure we test this one lmao
@@ -36,8 +40,57 @@ export const forcePush: BranchSequencerBase = (argsBase) =>
36
40
branchSequencer ( {
37
41
...argsBase ,
38
42
// pathToStackedRebaseDirInsideDotGit,
39
- actionInsideEachCheckedOutBranch : ( { execSyncInRepo } ) => {
40
- execSyncInRepo ( `${ argsBase . gitCmd } push --force` ) ;
43
+ actionInsideEachCheckedOutBranch : async ( { execSyncInRepo, repo } ) => {
44
+ const branch : Git . Reference = await repo . getCurrentBranch ( ) ;
45
+ const upstreamBranch : Git . Reference | null = await Git . Branch . upstream ( branch ) . catch ( ( ) => null ) ;
46
+
47
+ if ( ! upstreamBranch ) {
48
+ const remotes : string [ ] = await repo . getRemoteNames ( ) ;
49
+
50
+ if ( remotes . length === 0 ) {
51
+ throw new Error ( "0 remotes found, cannot push a new branch into a remote." ) ;
52
+ }
53
+
54
+ let remote : string ;
55
+
56
+ if ( remotes . length === 1 ) {
57
+ remote = remotes [ 0 ] ;
58
+ } else {
59
+ const indices : string [ ] = remotes . map ( ( _ , i ) => i + 1 ) . map ( ( x ) => x . toString ( ) ) ;
60
+
61
+ const question = createQuestion ( ) ;
62
+
63
+ let answer : string = "" ;
64
+
65
+ let first = true ;
66
+ while ( ! remotes . includes ( answer ) ) {
67
+ answer = (
68
+ await question (
69
+ ( first ? "\n" : "" ) +
70
+ "multiple remotes detected, please choose one for new branch:" +
71
+ remotes . map ( ( r , i ) => `\n ${ i + 1 } ${ r } ` ) . join ( "" ) +
72
+ "\n> "
73
+ )
74
+ )
75
+ . trim ( )
76
+ . toLowerCase ( ) ;
77
+
78
+ if ( indices . includes ( answer ) ) {
79
+ answer = remotes [ Number ( answer ) - 1 ] ;
80
+ }
81
+
82
+ first = false ;
83
+ }
84
+
85
+ remote = answer ;
86
+ }
87
+
88
+ const cmd = `push -u ${ remote } ${ branch . name ( ) } --force` ;
89
+ console . log ( `running ${ cmd } ` ) ;
90
+ execSyncInRepo ( `${ argsBase . gitCmd } ${ cmd } ` ) ;
91
+ } else {
92
+ execSyncInRepo ( `${ argsBase . gitCmd } push --force` ) ;
93
+ }
41
94
} ,
42
95
delayMsBetweenCheckouts : 0 ,
43
96
getBoundariesInclInitial : ( ) =>
0 commit comments