-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_shared_git
88 lines (74 loc) · 2.34 KB
/
_shared_git
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env sh
# SPDX-FileCopyrightText: 2021 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# A kind of mini library of useful, git related BASH code.
# It can be included in scripts like this:
#
## sh
#script_dir=`dirname "${0}"`
#. "$script_dir/_shared_git"
#
## bash
#script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
#source "$script_dir/_shared_git"
# Tries to fetch a git-portal access-token from the local config files.
# @param portal name of the portal to fetch the access token of
# ("github"|"gitlab", default: "github")
# Examples:
# $ _git_get_access_token
# 1234567890abcdef1234567890abcdef
# $ _git_get_access_token "github"
# 1234567890abcdef1234567890abcdef
# $ _git_get_access_token "gitlab"
# abd-456-34Hr65
_git_get_access_token() {
portal="${1:-github}"
access_token_file=~/.config/${portal}-access-token.txt
if [ -e "$access_token_file" ]
then
access_token=$(cat "$access_token_file")
else
access_token="<ACCESS_TOKEN_NOT_FOUND>"
>&2 echo "Access-token file does not exist: '$access_token_file'"
fi
echo -n "$access_token"
}
# Given a (GitHub like) git repo URL, returns the owner of the repo/project.
_git_url_extract_owner() {
repo_url="$1"
repo_owner=$(echo "$repo_url" | sed -e 's|\.git$||' -e 's|/$||' -e 's|/[^/]*$||' -e 's|.*[:/]||')
echo "${repo_owner}"
}
# Given a (GitHub like) git repo URL, returns the name of the repo/project.
_git_url_extract_name() {
repo_url="$1"
repo_name=$(echo "$repo_url" | sed -e 's|\.git$||' -e 's|/$||' -e 's|.*[:/]||')
echo "${repo_name}"
}
# Returns the name of the currently checked out branch in the git repo at $(pwd).
_git_get_current_branch() {
#git name-rev HEAD 2> /dev/null | sed 's#HEAD\ \(.*\)#\1#'
_branch="$(git branch | grep "[*]" | cut -d ' ' -f2)"
if [ "$_branch" = "(HEAD" ]
then
_branch="NONE"
fi
echo "$_branch"
}
_git_local_branch() {
repo_path="${1:-.}"
git -C "$repo_path" rev-parse --abbrev-ref HEAD
}
# Returns the remote- and branch-name of the remote tracking branch.
# Example:
# $ _git_remote_tracking_branch . master
# origin/master
_git_remote_tracking_branch() {
repo_path="${1:-.}"
local_branch="${2:-"$(_git_local_branch "$repo_path")"}"
git -C "$repo_path" branch -vv \
| sed -e 's/^..//' \
| grep -E "^${local_branch} " \
| sed -e 's/^[^ ]\+ \+[^ ]\+ \+\[//' -e 's/. .*//'
}