App-git-hub
view release on metacpan or search on metacpan
share/lib/git-hub view on Meta::CPAN
set-var() {
local name="$1"
local value="$2"
if [ "$value" == '@' ]; then
login="$(git config -f "$GIT_HUB_CONFIG" github.login || echo '')"
value="$login"
fi
printf -v "$name" "$value"
}
get-var() {
local var="${1//-/_}"
[ -n "${!var}" ] && return
local env="GIT_HUB_$(echo "$var" | tr 'a-z' 'A-Z')"
printf -v "$var" "${!env}"
[ -n "${!var}" ] && return
read-config-value "$1" || true
printf -v "$var" "$value"
true
}
get-var-bool() {
get-var "$1"
local var="${1//-/_}"
[ -z "${!var}" ] && printf -v "$var" false
[ "${!var}" != false ] && printf -v "$var" true
true
}
#------------------------------------------------------------------------------
# Detailed error messages:
#------------------------------------------------------------------------------
need-api-token() {
cat <<...
Can't determine your Personal API Access Token, which is required by this
command. Usually you put your token into the ~/.git-hub/config file, like this:
git hub config api-token <your-personal-api-access-token>
You can also specify the token using the --token= commandline option, or via
the GIT_HUB_API_TOKEN environment variable.
To list your API tokens:
git hub tokens
To create a new api token:
git hub token-new "my git-hub token"
# You should probably add at least the 'repo' and 'user' scopes:
git hub scope-add <token-id> repo user
You can also just run setup command, and it will guide you:
git hub setup
Would you like to run it now?
...
prompt-to-run-setup
}
config-not-setup() {
cat <<...
Greetings!!!
You seem to be a brand new 'git hub' user, since your config is not set up.
The 'git-hub' should soon be your new best friend. Configuration is really
easy. Just run the following command and answer a few simple questions:
git hub setup
This will guide you through the setup process. Press <ENTER> to run it now.
NOTE: You can also do the setup process by hand using the 'config', 'token'
and 'scope' commands. See the 'git help hub' documentation for more
information on these commands. Or just manually create a config file
in ~/.git-hub/config that looks like this:
[github]
login = your-github-login-id
api-token = c956f87abab2da54882cc1f1ade42efcc2b15dc7
json-lib = json-perl.bash
use-auth = 1
Would you like to start the automated 'setup' process right now? (Do it!)
...
prompt-to-run-setup
}
prompt-to-run-setup() {
prompt
source-ext git-hub-setup
command:setup || exit 1
exit 0
}
#------------------------------------------------------------------------------
# Reusable helper functions:
#------------------------------------------------------------------------------
editor-title-body() {
rm -f "$GIT_HUB_MSG_FILE"
echo "$1" > "$GIT_HUB_MSG_FILE"
$GIT_HUB_EDITOR "$GIT_HUB_MSG_FILE"
local line
body=''
title="$(head -n1 "$GIT_HUB_MSG_FILE")"
if [[ ! "$title" =~ [^[:space:]] ]]; then
abort "no title provided"
fi
line="$(head -n2 "$GIT_HUB_MSG_FILE" | tail -n1)"
if [[ ! "$line" =~ ^\s*$ ]] && [[ ! "$line" =~ ^\# ]]; then
error "malformed message in '$GIT_HUB_MSG_FILE'"
fi
local count=0
local ifs="$IFS"
IFS=''
while read -r line; do
[[ $((count++)) -lt 2 ]] && continue
[[ "$line" =~ ^\# ]] && break
body+="$line"$'\n'
done < "$GIT_HUB_MSG_FILE"
IFS="$ifs"
}
editor-comment() {
rm -f "$GIT_HUB_MSG_FILE"
echo "$1" > "$GIT_HUB_MSG_FILE"
$GIT_HUB_EDITOR "$GIT_HUB_MSG_FILE"
local line
body=''
local ifs="$IFS"
IFS=''
while read -r line; do
[[ "$line" =~ ^\# ]] && break
body+="$line"$'\n'
done < "$GIT_HUB_MSG_FILE"
IFS="$ifs"
}
editor-comment-state() {
rm -f "$GIT_HUB_MSG_FILE"
comment= title= state= assignee= milestone=
echo "$1" > "$GIT_HUB_MSG_FILE"
$GIT_HUB_EDITOR "$GIT_HUB_MSG_FILE"
local line started=false
local ifs="$IFS"
IFS=''
while read -r line; do
[[ "$line" =~ ^\# ]] && break
if $started; then
if [[ "$line" =~ ^title:\ +(.*) ]]; then
title="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^state:\ +(.*) ]]; then
state="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^assignee:\ +(.*) ]]; then
assignee="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^milestone:\ +(.*) ]]; then
milestone="${BASH_REMATCH[1]}"
fi
else
if [[ "$line" == '---' ]]; then
started=true
elif [ -n "$comment" ] || [[ "$line" =~ [^[:space:]] ]]; then
comment+="$line"$'\n'
fi
fi
done < "$GIT_HUB_MSG_FILE"
IFS="$ifs"
if [[ ! "$title" =~ [^[:space:]] ]]; then
abort "no title provided."
fi
}
#------------------------------------------------------------------------------
# General purpose functions:
#------------------------------------------------------------------------------
quiet_output=false
say() { ! "$quiet_output" && out "$@"; true; }
say-() { ! "$quiet_output" && out- "$@"; true; }
nay() { ! "$quiet_output" && err "$@"; true; }
nay-() { ! "$quiet_output" && err- "$@"; true; }
out() { echo -e "$@" >&1; }
out-() { echo -en "$@" >&1; }
err() { echo -e "$@" >&2; }
err-() { echo -en "$@" >&2; }
error() {
local msg="Error: $1" usage=
source-ext help-functions.bash
if can "help:$command"; then
msg=$'\n'"$msg"$'\n'"$("help:$command")"$'\n'
fi
echo "$msg" >&2
exit 1
}
abort() { echo "Abort: $1" >&2; exit 1; }
prompt() {
local msg answer default yn=false password=false
case $# in
0) msg="${prompt_msg:-Press <ENTER> to continue, or <CTL>-C to exit.}" ;;
1)
msg="$1"
if [[ "$msg" =~ \[yN\] ]]; then
default=n
yn=true
elif [[ "$msg" =~ \[Yn\] ]]; then
default=y
yn=true
fi
;;
2)
msg="$1"
default="$2"
;;
*) die "Invalid usage of prompt" ;;
esac
if [[ "$msg" =~ [Pp]assword ]]; then
password=true
msg=$'\n'"$msg"
fi
while true; do
if "$password"; then
read -s -p "$msg" answer
else
read -p "$msg" answer
fi
[ $# -eq 0 ] && return 0
[ -n "$answer" -o -n "$default" ] && break
done
if "$yn"; then
[[ "$answer" =~ ^[yY] ]] && echo y && return 0
[[ "$answer" =~ ^[nN] ]] && echo n && return 0
echo "$default"
return 0
fi
if [ -n "$answer" ]; then
echo "$answer"
else
echo "$default"
fi
}
callable-or-source() {
if can "command:$1"; then
return 0
else
if source-ext-maybe git-hub-"${1/:*/}" && can "command:$1"; then
return 0
elif source-ext-maybe git-hub-"${1%s}" && can "command:$1"; then
return 0
elif source-ext-maybe git-hub-other && can "command:$1"; then
return 0
elif source-ext-maybe git-hub-"${1/-*/}" && can "command:$1"; then
return 0
fi
fi
return 1
}
assert-inside-git-repo() {
inside-git-repo ||
abort "not inside a git repo"
}
assert-repo-top-level() {
[ "$(git rev-parse --git-dir)" == '.git' ] ||
abort "not at top level directory of repo"
}
assert-git-repo-is-clean() {
# Repo is in a clean state:
git update-index -q --ignore-submodules --refresh
git diff-files --quiet --ignore-submodules ||
abort "unstaged changes."
git diff-index --quiet --ignore-submodules HEAD ||
( run in 0.952 second using v1.01-cache-2.11-cpan-0b5f733616e )