-
Notifications
You must be signed in to change notification settings - Fork 37
/
getDefaultRemoteBranch.ts
56 lines (49 loc) · 2.11 KB
/
getDefaultRemoteBranch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { getDefaultRemote, GetDefaultRemoteOptions } from "./getDefaultRemote";
import { git } from "./git";
import { getDefaultBranch } from "./gitUtilities";
export type GetDefaultRemoteBranchOptions = GetDefaultRemoteOptions & {
/** Name of branch to use. If undefined, uses the default branch name (falling back to `master`). */
branch?: string;
};
/**
* Gets a reference to `options.branch` or the default branch relative to the default remote.
* (See {@link getDefaultRemote} for how the default remote is determined.)
* Throws if `options.cwd` is not in a git repo or there's no package.json at the repo root.
* @returns A branch reference like `upstream/master` or `origin/master`.
*/
export function getDefaultRemoteBranch(options: GetDefaultRemoteBranchOptions): string;
/**
* First param: `branch`. Second param: `cwd`. See {@link GetDefaultRemoteBranchOptions} for more info.
* (This had to be changed to `...args` to avoid a conflict with the object param version.)
* @deprecated Use the object param version
*/
export function getDefaultRemoteBranch(...args: string[]): string;
export function getDefaultRemoteBranch(...args: (string | GetDefaultRemoteBranchOptions)[]) {
const [branchOrOptions, argsCwd] = args;
const options =
typeof branchOrOptions === "string"
? ({ branch: branchOrOptions, cwd: argsCwd } as GetDefaultRemoteBranchOptions)
: branchOrOptions;
const { cwd, branch } = options;
const defaultRemote = getDefaultRemote(options);
if (branch) {
return `${defaultRemote}/${branch}`;
}
const showRemote = git(["remote", "show", defaultRemote], { cwd });
let remoteDefaultBranch: string | undefined;
if (showRemote.success) {
/**
* `showRemote.stdout` is something like this:
*
* * remote origin
* Fetch URL: .../monorepo-upstream
* Push URL: .../monorepo-upstream
* HEAD branch: main
*/
remoteDefaultBranch = showRemote.stdout
.split(/\n/)
.find((line) => line.includes("HEAD branch"))
?.replace(/^\s*HEAD branch:\s+/, "");
}
return `${defaultRemote}/${remoteDefaultBranch || getDefaultBranch(cwd)}`;
}