Skip to content

Commit c61846b

Browse files
committed
Implement defaultBranch()
1 parent 293e1ff commit c61846b

34 files changed

+153
-8
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ Common parameters:
4444
- `ref`: string – a reference to an object in repository
4545
- `withOid`: boolean – a flag to include resolved oid for a reference
4646

47+
#### repo.defaultBranch()
48+
49+
Returns default branch name used in a repo:
50+
51+
```js
52+
const defaultBranch = await repo.defaultBranch();
53+
// 'main'
54+
```
55+
56+
The algorithm to identify a default branch name:
57+
58+
- if there is only one branch, that must be the default
59+
- otherwise looking for specific branch names, in this order:
60+
- `upstream/HEAD`
61+
- `origin/HEAD`
62+
- `main`
63+
- `master`
64+
4765
#### repo.expandRef(ref)
4866

4967
Expands a `ref` into a full form, e.g. `'main'` -> `'refs/heads/main'`.

fixtures/clean/_git/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/development

fixtures/clean/_git/config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true

fixtures/clean/_git/description

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.

fixtures/clean/_git/info/exclude

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~

fixtures/clean/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

fixtures/no-remotes/_git/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/main

fixtures/no-remotes/_git/config

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true
8+
[remote "origin"]
9+
url = /scan-git/
10+
fetch = +refs/heads/*:refs/remotes/origin/*
11+
[branch "main"]
12+
remote = origin
13+
merge = refs/heads/main

fixtures/no-remotes/_git/index

4.37 KB
Binary file not shown.

fixtures/no-remotes/_git/info/refs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7b84f676f2fbea2a3c6d83924fa63059c7bdfbe2 refs/heads/main

0 commit comments

Comments
 (0)