-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Show a different color and/or add a symbol to the branch column (like *) unless it shows the default branch name (mostly master, main, release etc.).
Unfortunately, this issue is a bit complex as there's no standard way of getting the default branch name right away.
Programmatically, it's possible to get the branch name in multiple ways:
1. If default branch is set in git config
git config --get init.defaultBranch
2. If the repo was downloaded with git clone
then origin/HEAD is set automatically.
git symbolic-ref refs/remotes/origin/HEAD | cut -d '/' -f4
master
git rev-parse --abbrev-ref origin/HEAD
origin/master
3. Get from the remote
git ls-remote --symref origin HEAD
ref: refs/heads/master HEAD
28c8d9f868eb9af98618407170da6a63c43e7f62 HEAD
git ls-remote --symref origin HEAD
ref: refs/heads/main HEAD
597b74a4a04c8741db1000901627745b65e5d69a HEAD
The problem with this way is: It takes a while to get this from remote, like (~ 2000ms) so it's better
to create a cache file in the home directory (e.g. $HOME/.cache/git-substatus/cache.json)
to make the process faster.
Example cache file:
{
"cache": [
{
"repository": "git-substatus",
"defaultBranch": "master",
"cacheTime": "1631264137"
},
{
"repository": "test",
"default-branch": "main",
"cacheTime": "1631264137"
}
]
}The risk of caching is that the repository name isn't failproof as it may be replaced with another repository having the same name. That's why need to find a unique identifier for it?
The caching invalidation can happen every week (168h) and on every time when git-substatus is called with the --fetch option.
TODO:
- Detect default branch in a repository
- Display a visual cue if it's checked out to a non default branch
- Write unit tests (e.g. repos which default branch name isn't
master) - (Optional) Implement a caching mechanism to retrieve default branch information faster