Permalink
Browse files

refactored fatal

  • Loading branch information...
1 parent aeb7e8c commit b696b023d9a07a10ab785ccedf29c4dbdcb42a97 @orefalo committed Mar 5, 2013
View
@@ -6,27 +6,13 @@ g2_initializeANSI()
yellowf="${g2esc}[33m" bluef="${g2esc}[34m"; purplef="${g2esc}[35m";
cyanf="${g2esc}[36m"; whitef="${g2esc}[37m";
-# blackb="${g2esc}[40m"; redb="${g2esc}[41m"; greenb="${g2esc}[42m";
-# yellowb="${g2esc}[43m" blueb="${g2esc}[44m"; purpleb="${g2esc}[45m";
-# cyanb="${g2esc}[46m"; whiteb="${g2esc}[47m";
-
boldon="${g2esc}[1m"; boldoff="${g2esc}[22m";
-# italicson="${g2esc}[3m"; italicsoff="${g2esc}[23m";
-# ulon="${g2esc}[4m"; uloff="${g2esc}[24m";
-# invon="${g2esc}[7m"; invoff="${g2esc}[27m";
reset="${g2esc}[0m";
}
g2_initializeANSI
-echo_fatal()
-{
-echo -e "${redf}$1${reset}";
-}
-
-
-echo_info()
-{
-echo -e "${boldon}$1${reset}";
-}
+fatal() { echo -e "${redf}fatal: $1${reset}"; }
+error() { fatal $1; exit 1; }
+echo_info() { echo -e "${boldon}$1${reset}"; }
View
@@ -4,4 +4,4 @@
source "$G2_HOME/cmds/color.sh"
-[[ ! $("$GIT_EXE" rev-parse --git-dir 2> /dev/null) ]] && echo_fatal "fatal: Not a Git Repository" || ("$GIT_EXE" merge --abort 2> /dev/null || "$GIT_EXE" rebase --abort 2> /dev/null)
+[[ ! $("$GIT_EXE" rev-parse --git-dir 2> /dev/null) ]] && fatal "Not a Git Repository" || ("$GIT_EXE" merge --abort 2> /dev/null || "$GIT_EXE" rebase --abort 2> /dev/null)
View
@@ -4,18 +4,20 @@
#
# Can also delete, rename and create branches (wizards)
+source "$G2_HOME/cmds/color.sh"
+
hasDMFlag() {
local opt
while getopts ":dDmM:" opt; do
case $opt in
d|D|m|M)
- echo "true"; return ;;
+ flag="true"; return ;;
\?)
- echo "Usage: g br <?-D> <?-M> <?branch>" >&2
- echo "exit"; return ;;
+ echo_info "Usage: g br <?-D> <?-M> <?branch>" >&2
+ flag="exit"; return ;;
esac
done
- echo "false"
+ flag="false"
}
br_status() {
@@ -26,7 +28,7 @@ br_status() {
"$GIT_EXE" rev-list --left-right ${local}...${remote} -- 2> /dev/null > /tmp/git_upstream_status_delta || continue
LEFT_AHEAD=$(grep -c "^<" /tmp/git_upstream_status_delta)
RIGHT_AHEAD=$(grep -c "^>" /tmp/git_upstream_status_delta)
- echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
+ echo_info "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
echo
done
}
@@ -36,16 +38,16 @@ if [[ $# -eq 0 ]]; then
echo "-------"
br_status
else
- flag=$(hasDMFlag "$@")
+ hasDMFlag "$@"
[[ $flag = "exit" ]] && exit 1;
[[ $flag = "true" ]] && { "$GIT_EXE" branch "$@"; exit $?; }
$("$GIT_EXE" g2haschanges) || exit 1;
shift $(( OPTIND - 1 ))
branch="${1:-/}"
- [[ $branch = */* ]] && echo "fatal: $branch is not a valid branch name" && exit 1
- [[ -n $("$GIT_EXE" branch | grep "^$branch$") ]] && echo "fatal: branch $branch already exist" && exit 1
+ [[ $branch = */* ]] && error "${boldon}$branch${boldoff} is not a valid branch name"
+ [[ -n $("$GIT_EXE" branch | grep "^$branch$") ]] && error "branch ${boldon}$branch${boldoff} already exist"
remote=$("$GIT_EXE" g2getremote)
[[ -n $remote ]] && {
View
@@ -4,4 +4,4 @@
source "$G2_HOME/cmds/color.sh"
$("$GIT_EXE" g2iswip) || exit 1
-[[ $("$GIT_EXE" diff --cached --numstat | wc -l) -eq 0 ]] && echo_fatal "fatal: No files to commit, please <freeze> changes first." || "$GIT_EXE" commit -u "$@"
+[[ $("$GIT_EXE" diff --cached --numstat | wc -l) -eq 0 ]] && fatal "No files to commit, please use ${boldon}g freeze${boldoff} to stage changes." || "$GIT_EXE" commit -u "$@"
View
@@ -8,20 +8,17 @@ source "$G2_HOME/cmds/color.sh"
state=$("$GIT_EXE" g2brstatus)
-[[ $state = "rebase" ]] && {
-
+if [[ $state = "rebase" ]]; then
action="--continue"
if git diff-index --quiet HEAD --; then
echo_info "The last commit brings no significant changes -- automatically skipping"
action="--skip"
fi
-
"$GIT_EXE" rebase $action 2> /dev/null
-
-}
+fi
[[ $state = "merge" ]] && {
# Count the number of unmerged files
count=$("$GIT_EXE" ls-files --unmerged | wc -l)
- [[ $count -ne 0 ]] && echo_fatal "I am afraid you still have unmerged files, please run <g mt> to resolve conflicts" ||"$GIT_EXE" commit
+ [[ $count -ne 0 ]] && fatal "Hey! you still have unmerged files, please run ${boldon}g mt${boldoff} to resolve conflicts" ||"$GIT_EXE" commit
}
View
@@ -9,7 +9,7 @@ fi
( [[ -z "$@" ]] && "$GIT_EXE" add -A || "$GIT_EXE" add -A "$GIT_PREFIX$@" ) && {
[[ -n $msg ]] && {
- $("$GIT_EXE" g2iswip) || exit 1
+ "$GIT_EXE" g2iswip || exit 1
"$GIT_EXE" commit -m "$msg" && "$GIT_EXE" status -s
}
}
View
@@ -1,4 +1,4 @@
#!/bin/bash
#
source "$G2_HOME/cmds/color.sh"
-echo_fatal "Don't use <add> with G2, <freeze> and <unfreeze> are powerfull commands"
+fatal "Don't use ${boldon}add${boldoff} with G2, ${boldon}freeze${boldoff} and ${boldon}unfreeze${boldoff} are powerfull commands"
View
@@ -2,7 +2,7 @@
#
source "$G2_HOME/cmds/color.sh"
-[[ $("$GIT_EXE" diff --cached --numstat | wc -l) -eq 0 ]] && error "fatal: no files to amend, please <freeze> the changes to amend."
+[[ $("$GIT_EXE" diff --cached --numstat | wc -l) -eq 0 ]] && error "No files to amend, please use ${boldon}g freeze${boldoff} to stage the changes to amend."
$("$GIT_EXE" g2haslocalcommit)
if [[ $? -eq 0 ]]; then
@@ -11,8 +11,7 @@ if [[ $? -eq 0 ]]; then
echo
[[ $REPLY != [yY]* ]] && exit 1
else
- echo_fatal "fatal: no local commits to amend."
- exit 1
+ error "No local commits to amend."
fi
fi
View
@@ -1,12 +1,12 @@
#!/bin/bash
#
-# Internal command that return "rebase", "merge" or "false"
+# Internal command that return "rebase", "merge" or "false" depending on the repository status
source "$G2_HOME/cmds/color.sh"
GDIR=$("$GIT_EXE" rev-parse --git-dir 2> /dev/null)
-[[ ! $GDIR ]] && echo_fatal "fatal: Not a Git Repository" && exit 1
+[[ ! $GDIR ]] && error "Not a Git Repository"
[[ -d "$GDIR/rebase-merge" || -d "$GDIR/rebase-apply" ]] && echo "rebase" && exit 0
[[ -f "$GDIR/MERGE_HEAD" ]] && echo "merge" && exit 0
echo "false"
View
@@ -5,13 +5,11 @@
source "$G2_HOME/cmds/color.sh";
if [[ $("$GIT_EXE" diff --cached --numstat | wc -l) -ne 0 ]]; then
- echo_fatal "fatal: you have changes in the staging area, please commit them ${boldon}g ci -m mymessage${boldooff} or save them as a work in progress ${boldon}g wip${boldooff}.";
- exit 1;
+ error "There are changes in the staging area, please commit them ${boldon}g ci -m mymessage${boldooff} or save them as a work in progress ${boldon}g wip${boldooff}.";
fi
if [[ $("$GIT_EXE" diff --numstat | wc -l) -ne 0 ]]; then
- echo_fatal "fatal: some files were modified, either commit then ${boldon}g ci -m mymessage${boldooff}, save them as a work in progress ${boldon}g wip${boldooff} or discard them ${boldon}g panic${boldoff}.";
- exit 1;
+ error "Some files were modified, either commit then ${boldon}g ci -m mymessage${boldooff}, save them as a work in progress ${boldon}g wip${boldooff} or discard them ${boldon}g panic${boldoff}.";
fi
exit 0;
View
@@ -4,11 +4,6 @@
source "$G2_HOME/cmds/color.sh"
-error() {
- echo_fatal "fatal: Work In Progress (wip) detected, please run <g unwip> to resume progress first.";
- exit 1;
-}
-
remote=$1;
[[ -z $1 ]] && remote=$("$GIT_EXE" g2getremote);
@@ -19,4 +14,4 @@ else
[[ $("$GIT_EXE" log $remote..HEAD --oneline 2>/dev/null | cut -f 2 -d " " | uniq | grep -c wip) -gt 0 ]] && wip=1;
fi
-[[ $wip -eq 1 ]] && error || exit 0;
+[[ $wip -eq 1 ]] && error "Work In Progress (wip) detected, please run ${boldon}g unwip${boldoff} to resume progress first." || exit 0;
View
@@ -3,19 +3,18 @@
source "$G2_HOME/cmds/color.sh"
-error() {
- echo_fatal "fatal: use <sync> to synchronize the current branch"
- echo_info "remember, <sync>hing applies to the working branch, <pull>ing applies when merging feature branches."
- exit 1
+err() {
+ fatal "Use ${boldon}sync${boldoff} to synchronize the current branch"
+ error "remember, ${boldon}sync${boldoff}hing applies to the working branch, ${boldon}pull${boldoff}ing applies when merging feature branches."
}
usage() {
- echo_fatal "Usage: pull <?opts> <remote> <branch>"
+ echo_info "Usage: pull <?opts> <remote> <branch>"
exit 1
}
n=$#;
-[[ $n -eq 0 || ${!n} = -* ]] && error
+[[ $n -eq 0 || ${!n} = -* ]] && err
$("$GIT_EXE" g2iswip) || exit 1
[[ ${!n} = */* ]] && usage
branch=${!n}
@@ -27,7 +26,7 @@ if [[ -z $remote ]]; then
read -p "Would you like to track $to (y/n)? " -n 1 -r
echo
else
- [[ $to = $remote ]] && error
+ [[ $to = $remote ]] && err
fi
[[ $REPLY = [yY]* ]] && "$GIT_EXE" track "$to"
"$GIT_EXE" fetch "$rmt"
View
@@ -6,13 +6,12 @@
source "$G2_HOME/cmds/color.sh"
-error() {
- echo_fatal "fatal: sorry you can't use <push> in this context, please use the <sync> command to synchronize the current branch"
- exit 1
+err() {
+ error "sorry you can't use ${boldon}push${boldoff} in this context, please use the ${boldon}sync${boldoff} command to synchronize the current branch"
}
usage() {
- echo_fatal "Usage: push <?opts> <remote> <branch>"
+ echo_info "Usage: push <?opts> <remote> <branch>"
exit 1
}
@@ -30,7 +29,7 @@ hasFFlag() {
fflg=$( hasFFlag "$@" )
n=$#;
-[[ $fflg = "false" && $n -eq 0 ]] && error
+[[ $fflg = "false" && $n -eq 0 ]] && err
$("$GIT_EXE" g2iswip) || exit 1
[[ $fflg = "true" ]] && {
read -p "warning: you are about to force push history, please confirm (y/n)? " -n 1 -r;
@@ -50,6 +49,6 @@ if [[ -z $remote ]]; then
read -p "Would you like to track $to (y/n)? " -n 1 -r
echo
else
- [[ $to = $remote ]] && error
+ [[ $to = $remote ]] && err
fi
"$GIT_EXE" push "$@" && [[ $REPLY == [yY]* ]] && "$GIT_EXE" track $to
View
@@ -2,4 +2,6 @@
#
# Ignores a file and automatically adds it to gitignore & removes from source control
-[[ -z "$@" ]] && echo "usage: ignore [file]" || { ([ ! -e .gitignore ] && touch .gitignore); echo "$GIT_PREFIX$1" >> .gitignore && echo "Ignoring file $1" && "$GIT_EXE" rm --cached "$GIT_PREFIX$@" > /dev/null 2>&1 && "$GIT_EXE" st; }
+source "$G2_HOME/cmds/color.sh"
+
+[[ -z "$@" ]] && echo_info "Usage: ignore [file]" || { ([ ! -e .gitignore ] && touch .gitignore); echo "$GIT_PREFIX$1" >> .gitignore && echo "Ignoring file $1" && "$GIT_EXE" rm --cached "$GIT_PREFIX$@" > /dev/null 2>&1 && "$GIT_EXE" st; }
View
@@ -18,7 +18,7 @@ gen() {
if [[ $1 != -gen ]]; then
- [[ ! -f $HOME/.ssh/id_rsa.pub ]] && echo_fatal "fatal: SSH key not found: $HOME/.ssh/id_rsa.pub" || displayKey
+ [[ ! -f $HOME/.ssh/id_rsa.pub ]] && fatal "SSH key not found at ${boldon}$HOME/.ssh/id_rsa.pub${boldoff}" || displayKey
else
if [[ -f $HOME/.ssh/id_rsa.pub ]]; then
read -p "Regenerate SSH Key (y/n)? " -n 1 -r
View
@@ -12,7 +12,7 @@ for a in "${v[@]}"
do
[[ "$a" = "upstream" ]] && {
remote=$("$GIT_EXE" g2getremote)
- [[ -z $remote ]] && echo_fatal "fatal: upstream not found, please setup tracking for this branch, ie. <g track remote/branch>" && exit 1
+ [[ -z $remote ]] && error "Upstream not found, please setup tracking for this branch, ie. ${boldon}g track remote/branch${boldoff}"
set -- "${@:1:$i}" "origin/master" "${@:($i+2)}";
} && break
let i++
View
@@ -10,7 +10,7 @@ for a in "${v[@]}"
do
[[ "$a" = "upstream" ]] && {
remote=$("$GIT_EXE" g2getremote)
- [[ -z $remote ]] && echo_fatal "fatal: upstream not found, please setup tracking for this branch, ie. <g track remote/branch>" && exit 1
+ [[ -z $remote ]] && error "Upstream not found, please setup tracking for this branch, ie. ${boldon}g track remote/branch${boldoff}"
set -- "${@:1:$i}" "origin/master" "${@:($i+2)}";
} && break
let i++
View
@@ -1,12 +1,14 @@
#!/bin/bash
#
+source "$G2_HOME/cmds/color.sh"
+
[[ $1 == "upstream" ]] && {
read -p "warning: resetting to the upstream may erase any local changes, are you sure (y/n)? " -n 1 -r
echo
[[ $REPLY == [yY]* ]] && { echo
"$GIT_EXE" abort
remote=$("$GIT_EXE" g2getremote)
- [[ -n $remote ]] && { echo "Resetting branch to $remote" && "$GIT_EXE" reset --hard "$remote"; } || echo "fatal: please setup tracking for this branch, see <track>";
+ [[ -n $remote ]] && { echo "Resetting branch to $remote" && "$GIT_EXE" reset --hard "$remote"; } || fatal "Please setup tracking for this branch, see ${boldon}g track remote/branch${boldoff}";
}
} || "$GIT_EXE" reset "$@";
View
@@ -7,15 +7,15 @@ source "$G2_HOME/cmds/color.sh"
[[ $1 == "upstream" ]] && pullOnly=true && shift
-[[ $# -gt 0 ]] && echo_fatal "Usage: <sync>" && echo_fatal "Remember, <sync>hing applies to the working branch, when <pull>ing applies when merging feature branches" && exit 1
+[[ $# -gt 0 ]] && echo_info "Usage: ${boldon}sync${boldoff}" && echo_info "Remember, ${boldon}sync${boldoff}hing applies to the working branch, when ${boldon}pull${boldoff}ing applies when merging feature branches" && exit 1
remote=$("$GIT_EXE" g2getremote)
$("$GIT_EXE" g2iswip $remote) || exit 1;
-[[ -z $remote ]] && echo_fatal "fatal: please use <track> to setup the remote/branch to track with" && exit 2
-[[ $# -ne 0 ]] && echo_fatal "fatal: sorry, you may only <sync> against the tracking remote/branch, use <pull> or <push> to deal with other branches." && exit 3
+[[ -z $remote ]] && fatal "Please use ${boldon}g track remote/branch${boldoff} to setup tracking" && exit 2
+[[ $# -ne 0 ]] && fatal "Sorry, you may only ${boldon}sync${boldoff} against the tracking remote/branch, use ${boldon}pull${boldoff} or ${boldon}push${boldoff} to deal with other branches." && exit 3
"$GIT_EXE" fetch || exit $?;
-$("$GIT_EXE" g2isforced $remote) && echo_fatal "abort: it appears the history of the branch was changed on the server." && echo_fatal " please issue a <g rs upstream> or a <g rb $remote> to resume" && exit 1;
+$("$GIT_EXE" g2isforced $remote) && fatal "It appears the history of the branch was changed on the server." && error " please issue a ${boldon}g rs upstream${boldoff} or a ${boldon}g rb $remote${boldoff} to resume";
branch=$("$GIT_EXE" branch | grep "*" | sed "s/* //")
"$GIT_EXE" rev-list --left-right $branch...$remote -- 2> /dev/null > /tmp/git_upstream_status_delta
lchg=$(grep -c "^<" /tmp/git_upstream_status_delta);
@@ -24,9 +24,9 @@ rchg=$(grep -c "^>" /tmp/git_upstream_status_delta);
[[ $rchg -gt 0 ]] && { "$GIT_EXE" rebase $remote || {
unmerged=$("$GIT_EXE" ls-files --unmerged)
if [[ -n $unmerged ]]; then
- echo_info "A few files need to be merged manually, please use <g mt> to fix conflicts."
- echo_info " Once all conflicts are resolved, do NOT commit, but use <g continue> to resume."
- echo_info " Note: you may abort the merge at any time with <g abort> ."
+ echo_info "A few files need to be merged manually, please use ${boldon}g mt${boldoff} to fix conflicts."
+ echo_info " Once all conflicts are resolved, do NOT commit, but use ${boldon}g continue${boldoff} to resume."
+ echo_info " Note: you may abort the merge at any time with ${boldon}g abort${boldoff}."
fi
exit 1;
}
View
@@ -3,7 +3,7 @@
source "$G2_HOME/cmds/color.sh"
if [[ $# -eq 1 ]]; then
- [[ $1 != */* ]] && echo_fatal "fatal: $1 is not an upstream branch (ie. origin/xxx)." && exit 1
+ [[ $1 != */* ]] && error "${boldon}$1${boldoff} is not an upstream branch (ie. origin/xxx)."
"$GIT_EXE" branch --set-upstream $1
else
echo -e -n "${greenf}"
View
@@ -3,6 +3,6 @@
source "$G2_HOME/cmds/color.sh"
-([[ -z "$@" ]] && ("$GIT_EXE" reset -q HEAD > /dev/null || echo_fatal "fatal: first commit must be unfrozen file by file.") || ("$GIT_EXE" reset -q HEAD -- $@ > /dev/null || "$GIT_EXE" rm -q --cached $@))
+([[ -z "$@" ]] && ("$GIT_EXE" reset -q HEAD > /dev/null || fatal "The first commit must be unfrozen ${boldon}file by file${boldoff}. Sorry about that...") || ("$GIT_EXE" reset -q HEAD -- $@ > /dev/null || "$GIT_EXE" rm -q --cached $@))
"$GIT_EXE" status -s;
Oops, something went wrong.

0 comments on commit b696b02

Please sign in to comment.