@@ -7,13 +7,60 @@ import parseGitConfig from 'parse-git-config'
7
7
import parseGitHubURL from 'parse-github-url'
8
8
import doCreateGit from 'simple-git/promise'
9
9
10
+ /**
11
+ * Extract the github repo name and owner from the git config. If anything goes
12
+ * wrong during extraction a specific error about it will be thrown.
13
+ */
14
+ export const parseGitHubRepoInfoFromGitConfig = async ( params ?: {
15
+ cwd ?: string
16
+ } ) : Promise < BasicGitHubRepoInfo > => {
17
+ const { cwd = process . cwd ( ) } = params ?? { }
18
+ // Inspiration from how `$ hub pr show` works
19
+ // https://github.com/github/hub/blob/a5fbf29be61a36b86c7f0ff9e9fd21090304c01f/commands/pr.go#L327
20
+
21
+ const gitConfig = await parseGitConfig ( { path : `${ cwd } /.git/config` } )
22
+ if ( gitConfig === null ) {
23
+ throw new Error ( `Could not parse your git config` )
24
+ }
25
+
26
+ const gitOrigin = gitConfig [ `remote "origin"` ]
27
+ if ( gitOrigin === undefined ) {
28
+ throw new Error ( `Could not find a configured origin in your git config` )
29
+ }
30
+
31
+ const gitOriginURL : string = gitOrigin [ `url` ]
32
+ if ( gitOriginURL === undefined ) {
33
+ throw new Error ( `Could not find a URL in your remote origin config in your git config` )
34
+ }
35
+
36
+ const githubRepoURL = parseGitHubURL ( gitOriginURL )
37
+ if ( githubRepoURL === null ) {
38
+ throw new Error ( `Could not parse the URL in your remote origin config in your git config` )
39
+ }
40
+ if ( githubRepoURL . owner === null ) {
41
+ throw new Error (
42
+ `Could not parse out the GitHub owner from the URL in your remote origin config in your git config`
43
+ )
44
+ }
45
+ if ( githubRepoURL . name === null ) {
46
+ throw new Error (
47
+ `Could not parse out the GitHub repo name from the URL in your remote origin config in your git config`
48
+ )
49
+ }
50
+
51
+ return {
52
+ name : githubRepoURL . name ,
53
+ owner : githubRepoURL . owner ,
54
+ }
55
+ }
56
+
10
57
export type Simple = ReturnType < typeof doCreateGit >
11
58
12
- export function createGit ( ) : Simple {
59
+ export const createGit = ( ) : Simple => {
13
60
return doCreateGit ( )
14
61
}
15
62
16
- function parseGitTags ( tagsString : null | string ) : string [ ] {
63
+ const parseGitTags = ( tagsString : null | string ) : string [ ] => {
17
64
if ( tagsString === null ) return [ ]
18
65
const tags = tagsString
19
66
. trim ( )
@@ -62,7 +109,7 @@ export async function gitReset(git: Simple): Promise<void> {
62
109
*/
63
110
export async function gitResetToInitialCommit ( git : Simple ) : Promise < void > {
64
111
await git . raw ( [ `clean` , `-d` , `-x` , `-f` ] )
65
- const trunkBranch = `master `
112
+ const trunkBranch = `main `
66
113
await git . raw ( `checkout ${ trunkBranch } ` . split ( ` ` ) )
67
114
await git . raw ( `rev-list --max-parents=0 HEAD` . split ( ` ` ) ) . then ( ( initialCommitSHA ) => {
68
115
git . raw ( [ `reset` , `--hard` , initialCommitSHA . trim ( ) ] )
@@ -160,7 +207,7 @@ export async function gitDeleteAllTagsInRepo(git: Simple): Promise<void> {
160
207
// }
161
208
// }
162
209
//
163
- // const githubRepo = await parseGitHubRepoInfoFromGitConfig ()
210
+ // const githubRepo = await pars eGitHubRepoInfoFromGitConfig ()
164
211
165
212
// // TODO Refactor this to have instance passed as arg.
166
213
// const octoOps = {} as Octokit.Options
@@ -230,59 +277,15 @@ export interface BasicGitHubRepoInfo {
230
277
owner : string
231
278
}
232
279
233
- /**
234
- * Extract the github repo name and owner from the git config. If anything goes
235
- * wrong during extraction a specific error about it will be thrown.
236
- */
237
- export async function parseGitHubRepoInfoFromGitConfig ( ) : Promise < BasicGitHubRepoInfo > {
238
- // Inspiration from how `$ hub pr show` works
239
- // https://github.com/github/hub/blob/a5fbf29be61a36b86c7f0ff9e9fd21090304c01f/commands/pr.go#L327
240
-
241
- const gitConfig = await parseGitConfig ( )
242
- if ( gitConfig === null ) {
243
- throw new Error ( `Could not parse your git config` )
244
- }
245
-
246
- const gitOrigin = gitConfig [ `remote "origin"` ]
247
- if ( gitOrigin === undefined ) {
248
- throw new Error ( `Could not find a configured origin in your git config` )
249
- }
250
-
251
- const gitOriginURL : string = gitOrigin [ `url` ]
252
- if ( gitOriginURL === undefined ) {
253
- throw new Error ( `Could not find a URL in your remote origin config in your git config` )
254
- }
255
-
256
- const githubRepoURL = parseGitHubURL ( gitOriginURL )
257
- if ( githubRepoURL === null ) {
258
- throw new Error ( `Could not parse the URL in your remote origin config in your git config` )
259
- }
260
- if ( githubRepoURL . owner === null ) {
261
- throw new Error (
262
- `Could not parse out the GitHub owner from the URL in your remote origin config in your git config`
263
- )
264
- }
265
- if ( githubRepoURL . name === null ) {
266
- throw new Error (
267
- `Could not parse out the GitHub repo name from the URL in your remote origin config in your git config`
268
- )
269
- }
270
-
271
- return {
272
- name : githubRepoURL . name ,
273
- owner : githubRepoURL . owner ,
274
- }
275
- }
276
-
277
280
/**
278
281
* Determine if the current branch is trunk or not. Currently a simple check
279
- * against if current branch is master or not but TODO in the future will
282
+ * against if current branch is main or not but TODO in the future will
280
283
* account for checking against the remote Git repo for if the so-called `base`
281
- * branch of the repo is set to something else than `master `.
284
+ * branch of the repo is set to something else than `main `.
282
285
*/
283
286
export async function isTrunk ( git : Simple ) : Promise < boolean > {
284
287
const branchSummary = await git . branch ( { } )
285
- return branchSummary . current === `master `
288
+ return branchSummary . current === `main `
286
289
}
287
290
288
291
/**
0 commit comments