Skip to content

Commit

Permalink
echo mode for debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Motonori Iwata committed Sep 3, 2011
1 parent aeb2172 commit c90d515
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 68 deletions.
99 changes: 84 additions & 15 deletions git-now
@@ -1,20 +1,89 @@
#!/bin/sh

# enable debug mode
if [ "$DEBUG" = "yes" ]; then
set -x
fi

export GITNOW_DIR=$(dirname "$0")
PREFIX="from now"
MESSAGE="[${PREFIX}] `date +\"%Y/%m/%d %T\"`"

if [ $# -eq 0 ]
then
git add -u
printf "${MESSAGE}\n\n%s" "`git diff --cached`" | git commit -F -
else
then
if [ $1 != "--all" ]
then
git add $@

usage() {
echo "usage: git now [-n] [-v] [--force | -f] [--interactive | -i] [--patch | -p]"
echo " [--edit | -e] [--all | [--update | -u]] [--intent-to-add | -N]"
echo " [--refresh] [--ignore-errors] [--ignore-missing] [--]"
echo " [<filepattern>...]"
echo
echo " git now <subcommand>"
echo
echo "Available subcommands are:"
echo " rebase rebase for temporary commits."
echo
echo "Try 'git now <subcommand> help' for details."
}

main() {
# load common functionality
. "$GITNOW_DIR/gitnow-common"

# This environmental variable fixes non-POSIX getopt style argument
# parsing, effectively breaking git-now subcommand parsing on several
# Linux platforms.
export POSIXLY_CORRECT=1

# use the shFlags project to parse the command line arguments
. "$GITNOW_DIR/gitnow-shFlags"
FLAGS_PARENT="git now"
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"

# sanity checks
SUBCOMMAND="$1"; shift

if [ ! -e "$GITNOW_DIR/git-now-$SUBCOMMAND" ]; then
if [ "$SUBCOMMAND" = "help" ]; then
usage
else
main_add "$@"
fi
exit 0
fi

# run command
. "$GITNOW_DIR/git-now-$SUBCOMMAND"
FLAGS_PARENT="git now $SUBCOMMAND"

# test if the first argument is a flag (i.e. starts with '-')
# in that case, we interpret this arg as a flag for the default
# command
SUBACTION="default"
if [ "$1" = "help" ] && { ! echo "$1" | grep -q "^-"; } then
SUBACTION="$1"; shift
fi
#if ! type "cmd_$SUBACTION" >/dev/null 2>&1; then
#warn "Unknown subcommand: '$SUBACTION'"
#usage
#exit 1
#fi

# run the specified action
cmd_$SUBACTION "$@"
}

main_add() {
MESSAGE="[${PREFIX}] `date +\"%Y/%m/%d %T\"`"
if [ $# -eq 0 ]; then
echo "git add -u"
#printf "${MESSAGE}\n\n%s" "`git diff --cached`" | git commit -F -
else
git add -u
git add .
if [ $1 != "--all" ]; then
echo "git add $@"
else
echo "git add -u"
echo "git add ."
fi
#printf "${MESSAGE}\n\n%s" "`git diff --cached`" | git commit -F -
fi
printf "${MESSAGE}\n\n%s" "`git diff --cached`" | git commit -F -
fi
}

main "$@"
107 changes: 54 additions & 53 deletions git-now-rebase
@@ -1,66 +1,67 @@
#!/bin/sh

usage_exit() {
echo "Usage: git now rebase [-h] [-p] [-r remote] [base_branch] " 1>&2
exit 1
usage() {
echo "usage: git now rebase [<base_branch>]"
echo " git now rebase -p [-r <remote>] [<base_branch>]"
}
#
# Options
#
while getopts "pr:h" GETOPTS
do
case $GETOPTS in
p) PUSH_FLAG=yes
;;
r) REMOTE_NAME=$OPTARG
;;
h) usage_exit
;;
*) usage_exit
;;
esac
done

shift $((OPTIND - 1))

PREFIX="from now"
WORKING_BRANCH=`git branch -l | grep "*" | cut -d " " -f 2`
cmd_default() {
parse_args "$@"

WORKING_BRANCH=$(git_current_branch)
INITIAL_COMMIT=$(git_now_initial_commit)

if [ $# -eq 0 ]
then
FIRST_NOW_COMMIT=`git log --pretty=oneline --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1`
INITIAL_COMMIT=`git log --pretty=oneline | tail -n 1 | cut -d " " -f 1`
if [ -n "$BASE_BRANCH" ]; then
COMMON_ANCESTOR_COMMIT=`git merge-base ${BASE_BRANCH} ${WORKING_BRANCH}`
FIRST_NOW_COMMIT=$(git_now_first_commit $COMMON_ANCESTOR_COMMIT)

if [ ${FIRST_NOW_COMMIT} != ${INITIAL_COMMIT} ]
then
echo "git rebase -i ${FIRST_NOW_COMMIT}^"
if [ "$FIRST_NOW_COMMIT" != "$INITIAL_COMMIT" ]; then
echo "git rebase -i ${FIRST_NOW_COMMIT}^"
else
echo "git checkout ${FIRST_NOW_COMMIT}"
echo "git commit --amend"
echo "git rebase --onto HEAD ${FIRST_NOW_COMMIT} ${WORKING_BRANCH}"
fi
else
echo "git checkout ${FIRST_NOW_COMMIT}"
echo "git commit --amend"
echo "git rebase --onto HEAD ${FIRST_NOW_COMMIT} ${WORKING_BRANCH}"
FIRST_NOW_COMMIT=$(git_now_first_commit)

if [ "$FIRST_NOW_COMMIT" != "$INITIAL_COMMIT" ]; then
echo "git rebase -i ${FIRST_NOW_COMMIT}^"
else
echo "git checkout ${FIRST_NOW_COMMIT}"
echo "git commit --amend"
echo "git rebase --onto HEAD ${FIRST_NOW_COMMIT} ${WORKING_BRANCH}"
fi
fi
else
BASE_BRANCH=$1
COMMON_ANCESTOR_COMMIT=`git merge-base ${BASE_BRANCH} ${WORKING_BRANCH}`
FIRST_NOW_COMMIT=`git log ${COMMON_ANCESTOR_COMMIT}.. --pretty=oneline --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1`
INITIAL_COMMIT=`git log --pretty=oneline | tail -n 1 | cut -d " " -f 1`

if [ ${FIRST_NOW_COMMIT} != ${INITIAL_COMMIT} ]
then
echo "git rebase -i ${FIRST_NOW_COMMIT}^"
else
echo "git checkout ${FIRST_NOW_COMMIT}"
echo "git commit --amend"
echo "git rebase --onto HEAD ${FIRST_NOW_COMMIT} ${WORKING_BRANCH}"
if flag push; then
_cmd_push $WORKING_BRANCH $REMOTE
fi
fi
}

cmd_help() {
usage
exit 0
}

parse_args() {
# parse options
DEFINE_boolean push false 'push to remote repository finally' 'p'
DEFINE_string remote '' 'remote branch name to --set-upstream' 'r'
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"

if [ $PUSH_FLAG ]
then
if [ $REMOTE_NAME ]
then
echo "git push --set-upstream ${WORKING_BRANCH} ${REMOTE_NAME}/${WORKING_BRANCH}"
# read arguments into global variables
BASE_BRANCH=$1
REMOTE=$FLAGS_remote
}

_cmd_push() {
local branch=$1
local remote=$2
if [ -n "$remote" ]; then
echo "git push --set-upstream ${branch} ${remote}/${branch}"
else
echo "git push"
fi
fi
}

52 changes: 52 additions & 0 deletions gitnow-common
@@ -0,0 +1,52 @@
#
# Common functionality
#

# shell output
warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; }

escape() {
echo "$1" | sed 's/\([\.\+\$\*]\)/\\\1/g'
}

# set logic
has() {
local item=$1; shift
echo " $@ " | grep -q " $(escape $item) "
}

# basic math
min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }

# basic string matching
startswith() { [ "$1" != "${1#$2}" ]; }
endswith() { [ "$1" != "${1%$2}" ]; }

# convenience functions for checking shFlags flags
flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }

#
# Git specific common functionality
#

git_local_branches() { git branch --no-color | sed 's/^[* ] //'; }
git_remote_branches() { git branch -r --no-color | sed 's/^[* ] //'; }
git_all_branches() { ( git branch --no-color; git branch -r --no-color) | sed 's/^[* ] //'; }

git_current_branch() {
git branch -l | grep "*" | cut -d " " -f 2
}

git_now_first_commit() {
local common=$1
if [ $# -eq 1 ]; then
git log ${common}.. --pretty=oneline --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1
else
git log --pretty=oneline --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1
fi
}

git_now_initial_commit() { git log --pretty=oneline | tail -n 1 | cut -d " " -f 1; }
1 change: 1 addition & 0 deletions gitnow-shFlags

0 comments on commit c90d515

Please sign in to comment.