moved helpers separately
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
# * with or without modification, are permitted provided
|
||||
# * that the following conditions are met:
|
||||
# *
|
||||
# * Redistributions of source code must retain the above
|
||||
# * copyright notice, this list of conditions and the
|
||||
# * following disclaimer.
|
||||
# *
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# * copyright notice, this list of conditions and the following
|
||||
# * disclaimer in the documentation and/or other materials
|
||||
# * provided with the distribution.
|
||||
# *
|
||||
# * Neither the name of the copyright holder nor the names
|
||||
# * of any other contributors may be used to endorse or
|
||||
# * promote products derived from this software without
|
||||
# * specific prior written permission.
|
||||
# *
|
||||
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
# * OF SUCH DAMAGE.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# Private helpers of profile.d/git.sh, loaded by the module itself.
|
||||
|
||||
# Internal helper: ensure git is available and cwd is a git worktree
|
||||
_git_require_repo()
|
||||
{
|
||||
if ! command -v git >/dev/null 2>&1; then
|
||||
disp E "git command not found."
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
disp E "Current directory is not inside a git repository."
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
export -f _git_require_repo
|
||||
|
||||
# Internal helper: return default branch from remote HEAD, fallback to config
|
||||
_git_default_branch()
|
||||
{
|
||||
local remote="${1:-$GIT_DEFAULT_REMOTE}"
|
||||
local head
|
||||
|
||||
head=$(git symbolic-ref --quiet --short "refs/remotes/${remote}/HEAD" 2>/dev/null) || true
|
||||
if [[ -n $head ]]; then
|
||||
printf "%s\n" "${head#"${remote}"/}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
printf "%s\n" "$GIT_MAIN_BRANCH"
|
||||
return 0
|
||||
}
|
||||
export -f _git_default_branch
|
||||
|
||||
# Internal helper: print the name of the checked-out branch; fail with an error
|
||||
# message on a detached HEAD (commands that push or rebase need a branch).
|
||||
_git_current_branch()
|
||||
{
|
||||
local b
|
||||
b=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || {
|
||||
disp E "HEAD is detached: check out a branch first."
|
||||
return 1
|
||||
}
|
||||
printf "%s\n" "$b"
|
||||
}
|
||||
export -f _git_current_branch
|
||||
|
||||
# Internal helper: print "<remote><TAB><branch>" for the remote branch tracked
|
||||
# by local branch $1, read from its configuration (branch.<name>.remote/.merge).
|
||||
# Unlike parsing the "origin/feature/x" form given by @{u}, this is exact for
|
||||
# remote and branch names containing slashes. Fails when nothing is tracked or
|
||||
# when the branch tracks another local branch (remote ".").
|
||||
_git_tracking()
|
||||
{
|
||||
local branch=$1 remote merge
|
||||
remote=$(git config --get "branch.${branch}.remote") || return 1
|
||||
merge=$(git config --get "branch.${branch}.merge") || return 1
|
||||
[[ $remote == "." ]] && return 1
|
||||
printf "%s\t%s\n" "$remote" "${merge#refs/heads/}"
|
||||
}
|
||||
export -f _git_tracking
|
||||
Reference in New Issue
Block a user