Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions aliases/available/git.aliases.bash
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ function gdv() {
}

function get_default_branch() {
branch=$(git symbolic-ref refs/remotes/origin/HEAD)
echo "${branch#refs/remotes/origin/}"
local default_remote branch

# Try origin first (most common)
if git remote | grep -q "^origin$"; then
default_remote="origin"
else
# Fall back to the first available remote
default_remote=$(git remote | head -n 1)
fi

if [ -n "$default_remote" ] && branch=$(git symbolic-ref --quiet --short refs/remotes/"${default_remote}"/HEAD); then
echo "${branch#"${default_remote}"/}"
fi
}
82 changes: 82 additions & 0 deletions test/aliases/git.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# SPDX-FileCopyrightText: 2026 Shine Nelson <bash-it@shinenelson.com>
# SPDX-License-Identifier: MIT

# shellcheck shell=bats

load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"

function local_setup_file() {
setup_libs ""
}

function local_setup() {
TEST_REPO="${BATS_FILE_TMPDIR}/test-repo-${BATS_TEST_NUMBER}"
mkdir -p "${TEST_REPO}"
cd "${TEST_REPO}" || false
git init . 2>&1 | grep -v "Reinitialized" || true

source "${BASH_IT?}/aliases/available/git.aliases.bash"
}

@test "get_default_branch: returns main when origin points to main" {
# Set up a local remote-like structure
git remote add origin "https://github.com/test/repo.git"
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

run get_default_branch
assert_success
assert_output "main"
}

@test "get_default_branch: returns trunk when origin points to trunk" {
git remote add origin "https://github.com/test/repo.git"
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/trunk

run get_default_branch
assert_success
assert_output "trunk"
}

@test "get_default_branch: falls back to first remote when origin doesn't exist" {
git remote add upstream "https://github.com/upstream/repo.git"
git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main

run get_default_branch
assert_success
assert_output "main"
}

@test "get_default_branch: returns empty when no remotes exist" {
run get_default_branch
assert_success
assert_output ""
}

@test "get_default_branch: returns empty when remote has no HEAD ref" {
git remote add origin "https://github.com/test/repo.git"
# Don't set a HEAD ref, so the git symbolic-ref command will fail

run get_default_branch
assert_success
assert_output ""
}

@test "get_default_branch: properly strips remote prefix from branch name" {
git remote add origin "https://github.com/test/repo.git"
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/trunk

run get_default_branch
assert_success
assert_output "trunk"
}

@test "get_default_branch: uses origin when both origin and another remote exist" {
git remote add upstream "https://github.com/upstream/repo.git"
git remote add origin "https://github.com/test/repo.git"
git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/trunk

run get_default_branch
assert_success
assert_output "trunk"
}
2 changes: 1 addition & 1 deletion test/run
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fi

# Which tests do we run?
if [[ $# -eq '0' ]]; then
test_dirs=("${test_directory}"/{bash_it,completion,install,lib,plugins,themes})
test_dirs=("${test_directory}"/{aliases,bash_it,completion,install,lib,plugins,themes})
Comment thread
seefood marked this conversation as resolved.
else
test_dirs=("$@")
fi
Expand Down
Loading