Commit 610b6ac1 authored by cmosh's avatar cmosh

Merge branch 'release/0.4.3'

parents 1ffb6b10 9180f569
debian/files
debian/*.substvars
debian/*.debhelper.log
debian/*/*
.timelog
.seconds
.seconds-feature-workingtime
.timelog-feature-workingtime
.seconds-feature-sdsa
.timelog-feature-sdsa
.seconds-feature-finish_fix
.timelog-feature-finish_fix
.seconds-feature-finish_fix
.timelog-feature-finish_fix
.seconds-feature-finish_fix
.timelog-feature-finish_fix
.seconds-feature-interrupt
.timelog-feature-interrupt
.seconds-feature-interrupt_final_test
.timelog-feature-interrupt_final_test
.breaktime-feature-interrupt_final_test
.seconds-feature-interrupt_final_test
.timelog-feature-interrupt_final_test
.breaktime-feature-interrupt_final_test
.seconds-feature-interrupt_final_test
.timelog-feature-interrupt_final_test
.breaktime-feature-interrupt_final_test
.seconds-feature-shortcuts
.timelog-feature-shortcuts
.breaktime-feature-shortcuts
.seconds-feature-test
.timelog-feature-test
.breaktime-feature-test
...@@ -11,5 +11,14 @@ Authors are (ordered by first commit date): ...@@ -11,5 +11,14 @@ Authors are (ordered by first commit date):
- Felipe Talavera - Felipe Talavera
- Guillaume-Jean Herbiet - Guillaume-Jean Herbiet
- Joseph A. Levin - Joseph A. Levin
- Jannis Leidel
- Konstantin Tjuterev
- Kiall Mac Innes
- Jon Bernard
- Olivier Mengué
- Emre Berge Ergenekon
- Eric Holmes
- Vedang Manerikar
- Myke Hines
Portions derived from other open source works are clearly marked. Portions derived from other open source works are clearly marked.
0.4.2:
-----
Release date: **not yet**
* `git flow init` now detects situations where origin already has gitflow
branches set up, and behaves accordingly (thanks Emre Berge Ergenekon).
* `git flow feature finish` can now be called without a feature branch
name(prefix) argument and will finish the current branch, if on any.
* `git flow feature pull` now has a `-r` flag, to support `pull --rebase`
semantics (thanks Vedang Manerikar).
* Various minor bug fixes related to internal argument passing.
* Improved some documentation.
* Better support for Windows and BSD users.
* Add package installer for the Windows platform.
0.4.1: 0.4.1:
----- -----
Release date: **2011/02/04** Release date: **2011/02/04**
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# those of the authors and should not be interpreted as representing official # those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of Vincent Driessen. # policies, either expressed or implied, of Vincent Driessen.
# #
prefix=/usr/local prefix=/usr/local
# files that need mode 755 # files that need mode 755
......
git-flow ![Project status](http://stillmaintained.com/nvie/gitflow.png) git-flow
======== ========
A collection of Git extensions to provide high-level repository operations A collection of Git extensions to provide high-level repository operations
for Vincent Driessen's [branching model](http://nvie.com/git-model "original for Vincent Driessen's [branching model](http://nvie.com/git-model "original
blog post"). blog post").
...@@ -14,86 +15,14 @@ Kreeftmeijer's blog post: ...@@ -14,86 +15,14 @@ Kreeftmeijer's blog post:
Or have a look at one of these screen casts: Or have a look at one of these screen casts:
* [How to use a scalable Git branching model called git-flow](http://buildamodule.com/video/change-management-and-version-control-deploying-releases-features-and-fixes-with-git-how-to-use-a-scalable-git-branching-model-called-gitflow) (by Build a Module)
* [A short introduction to git-flow](http://vimeo.com/16018419) (by Mark Derricutt) * [A short introduction to git-flow](http://vimeo.com/16018419) (by Mark Derricutt)
* [On the path with git-flow](http://codesherpas.com/screencasts/on_the_path_gitflow.mov) (by Dave Bock) * [On the path with git-flow](http://codesherpas.com/screencasts/on_the_path_gitflow.mov) (by Dave Bock)
Installing git-flow Installing git-flow
------------------- -------------------
See the Wiki for up-to-date [Installation Instructions](https://github.com/nvie/gitflow/wiki/Installation).
### Mac OS
If you're on a Mac and use [homebrew](http://github.com/mxcl/homebrew), it's simple:
$ brew install git-flow
If you're on a Mac and use [MacPorts](http://macports.org/), it's simple:
$ port install git-flow
### Linux, etc.
Another easy way to install git-flow is using Rick Osborne's excellent git-flow
installer, which can be run using the following command:
$ wget --no-check-certificate -q -O - https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | sudo sh
### Windows
#### Using Cygwin
For Windows users who wish to use the automated install, it is suggested that you install [Cygwin](http://www.cygwin.com/)
first to install tools like `git`, `util-linux` and `wget` (with those three being packages that can be selected
during installation). Then simply run this command from a Cygwin shell:
$ wget -q -O - https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | sh
#### Using msysgit
This is much like the manual installation below, but there are additional steps required to install some extra tools that
are not distributed with [msysgit](http://code.google.com/p/msysgit/).
Clone the git-flow sources from Github:
$ git clone --recursive git://github.com/nvie/gitflow.git
Copy git-flow's relevant files to your msysgit installation directory:
$ mkdir /usr/local/bin
$ cp git-flow* gitflow* /usr/local/bin/
$ cp shFlags/src/shflags /usr/local/bin/gitflow-shFlags
Next up we need to borrow a couple of binaries from [Cygwin](http://www.cygwin.com/). If you don't have Cygwin installed, please
install it including the `util-linux` package. Apart from `util-linux`'s dependencies, no other packages are required. When you
finished installation, copy the following files using msysgit's _Git Bash_. We assume the Cygwin's default installation path in C:\cygwin.
$ cd /c/cygwin/
$ cp bin/getopt.exe /usr/local/bin/
$ cp bin/cyggcc_s-1.dll /usr/local/bin/
$ cp bin/cygiconv-2.dll /usr/local/bin/
$ cp bin/cygintl-8.dll /usr/local/bin/
$ cp bin/cygwin1.dll /usr/local/bin/
After copying the files above, you can safely uninstall your Cygwin installation by deleting the C:\cygwin directory.
### Manual installation
If you prefer a manual installation, please use the following instructions:
$ git clone --recursive git://github.com/nvie/gitflow.git
Then, you can install `git-flow`, using:
$ sudo make install
By default, git-flow will be installed in /usr/local. To change the prefix
where git-flow will be installed, simply specify it explicitly, using:
$ sudo make prefix=/opt/local install
Or simply point your `PATH` environment variable to your git-flow checkout
directory.
*Installation note:*
git-flow depends on the availability of the command line utility `getopt`,
which may not be available in your Unix/Linux environment. Please use your
favorite package manager to install `getopt`. For Cygwin, install the
`util-linux` package to get `getopt`. If you use `apt-get` as your install
manager, the package name is `opt`.
Integration with your shell Integration with your shell
...@@ -104,9 +33,6 @@ For those who use the [Bash](http://www.gnu.org/software/bash/) or ...@@ -104,9 +33,6 @@ For those who use the [Bash](http://www.gnu.org/software/bash/) or
by [bobthecow](http://github.com/bobthecow). It offers tab-completion for all by [bobthecow](http://github.com/bobthecow). It offers tab-completion for all
git-flow subcommands and branch names. git-flow subcommands and branch names.
For Windows users, [msysgit](http://code.google.com/p/msysgit/) is a good
starting place for installing git.
FAQ FAQ
--- ---
...@@ -127,6 +53,23 @@ contributors, please see the [AUTHORS](AUTHORS) file. ...@@ -127,6 +53,23 @@ contributors, please see the [AUTHORS](AUTHORS) file.
Any questions, tips, or general discussion can be posted to our Google group: Any questions, tips, or general discussion can be posted to our Google group:
[http://groups.google.com/group/gitflow-users](http://groups.google.com/group/gitflow-users) [http://groups.google.com/group/gitflow-users](http://groups.google.com/group/gitflow-users)
Contributing
------------
Fork the repository. Then, run:
git clone --recursive git@github.com:<username>/gitflow.git
cd gitflow
git branch master origin/master
git flow init -d
git flow feature start <your feature>
Then, do work and commit your changes. **Hint**: ``export PATH=`pwd`:$PATH``
from within the gitflow directory makes sure you're using the version of
gitflow you're currently developing.
git flow feature publish <your feature>
When done, open a pull request to your feature branch.
License terms License terms
------------- -------------
...@@ -141,13 +84,15 @@ in a Github fork, of course. ...@@ -141,13 +84,15 @@ in a Github fork, of course.
To initialize a new repo with the basic branch structure, use: To initialize a new repo with the basic branch structure, use:
git flow init git flow init [-d]
This will then interactively prompt you with some questions on which branches This will then interactively prompt you with some questions on which branches
you would like to use as development and production branches, and how you you would like to use as development and production branches, and how you
would like your prefixes be named. You may simply press Return on any of would like your prefixes be named. You may simply press Return on any of
those questions to accept the (sane) default suggestions. those questions to accept the (sane) default suggestions.
The ``-d`` flag will accept all defaults.
### Creating feature/release/hotfix/support branches ### Creating feature/release/hotfix/support branches
...@@ -159,6 +104,11 @@ those questions to accept the (sane) default suggestions. ...@@ -159,6 +104,11 @@ those questions to accept the (sane) default suggestions.
For feature branches, the `<base>` arg must be a commit on `develop`. For feature branches, the `<base>` arg must be a commit on `develop`.
* To push/pull a feature branch to the remote repository, use:
git flow feature publish <name>
git flow feature pull <remote> <name>
* To list/start/finish release branches, use: * To list/start/finish release branches, use:
git flow release git flow release
...@@ -188,7 +138,7 @@ Showing your appreciation ...@@ -188,7 +138,7 @@ Showing your appreciation
A few people already requested it, so now it's here: a Flattr button. A few people already requested it, so now it's here: a Flattr button.
Of course, the best way to show your appreciation for the original Of course, the best way to show your appreciation for the original
[blog post](http://nvie.com/git-model) or the git-flow tool itself remains [blog post](http://nvie.com/posts/a-successful-git-branching-model/) or the git-flow tool itself remains
contributing to the community. If you'd like to show your appreciation in contributing to the community. If you'd like to show your appreciation in
another way, however, consider Flattr'ing me: another way, however, consider Flattr'ing me:
......
#!/bin/sh #!/bin/bash
# git-flow make-less installer for *nix systems, by Rick Osborne # git-flow make-less installer for *nix systems, by Rick Osborne
# Based on the git-flow core Makefile: # Based on the git-flow core Makefile:
...@@ -50,7 +50,7 @@ case "$1" in ...@@ -50,7 +50,7 @@ case "$1" in
;; ;;
*) *)
echo "Installing git-flow to $INSTALL_PREFIX" echo "Installing git-flow to $INSTALL_PREFIX"
if [[ -d "$REPO_NAME" && -d "$REPO_NAME/.git" ]] ; then if [ -d "$REPO_NAME" -a -d "$REPO_NAME/.git" ] ; then
echo "Using existing repo: $REPO_NAME" echo "Using existing repo: $REPO_NAME"
else else
echo "Cloning repo from GitHub to $REPO_NAME" echo "Cloning repo from GitHub to $REPO_NAME"
......
@echo off
setlocal
if not "%~1"=="" set GIT_HOME=%~f1
if "%GIT_HOME%"=="" call :FindGitHome "git.cmd"
if exist "%GIT_HOME%" goto :GitHomeOK
echo MsysGit installation directory not found.>&2
echo Try to give the directory name on the command line:>&2
echo %0 "%ProgramFiles%\Git"
endlocal
exit /B 1
:GitHomeOK
set ERR=0
echo Installing gitflow into "%GIT_HOME%"...
call :ChkGetopt getopt.exe || set ERR=1
if %ERR%==1 goto :End
echo getopt.exe... Found
if not exist "%GIT_HOME%\bin\git-flow" goto :Install
echo GitFlow is already installed.>&2
set /p mychoice="Do you want to replace it [y/n]"
if "%mychoice%"=="y" goto :DeleteOldFiles
goto :Abort
:DeleteOldFiles
echo Deleting old files...
for /F %%i in ("%GIT_HOME%\git-flow*" "%GIT_HOME%\gitflow-*") do if exist "%%~fi" del /F /Q "%%~fi"
:Install
echo Copying files...
::goto :EOF
xcopy "%~dp0\..\git-flow" "%GIT_HOME%\bin" /Y /R /F
if errorlevel 4 if not errorlevel 5 goto :AccessDenied
if errorlevel 1 set ERR=1
xcopy "%~dp0\..\git-flow*" "%GIT_HOME%\bin" /Y /R /F || set ERR=1
xcopy "%~dp0\..\gitflow-*" "%GIT_HOME%\bin" /Y /R /F || set ERR=1
xcopy "%~dp0\..\shFlags\src\shflags" "%GIT_HOME%\bin\gitflow-shFlags" /Y /R /F || set ERR=1
if %ERR%==1 choice /T 30 /C Y /D Y /M "Some unexpected errors happened. Sorry, you'll have to fix them by yourself."
:End
endlocal & exit /B %ERR%
goto :EOF
:AccessDenied
set ERR=1
echo.
echo You should run this script with "Full Administrator" rights:>&2
echo - Right-click with Shift on the script from the Explorer>&2
echo - Select "Run as administrator">&2
choice /T 30 /C YN /D Y /N >nul
goto :End
:Abort
echo Installation canceled.>&2
set ERR=1
goto :End
:ChkGetopt
:: %1 is getopt.exe
if exist "%GIT_HOME%\bin\%1" goto :EOF
if exist "%USERPROFILE%\bin\%1" goto :EOF
if exist "%~f$PATH:1" goto :EOF
echo %GIT_HOME%\bin\%1 not found.>&2
echo You have to install this file manually. See the GitFlow README.
exit /B 1
:FindGitHome
setlocal
set GIT_CMD_DIR=%~dp$PATH:1
if "%GIT_CMD_DIR%"=="" endlocal & goto :EOF
endlocal & set GIT_HOME=%GIT_CMD_DIR:~0,-5%
goto :EOF
...@@ -37,12 +37,17 @@ ...@@ -37,12 +37,17 @@
# policies, either expressed or implied, of Vincent Driessen. # policies, either expressed or implied, of Vincent Driessen.
# #
# set this to workaround expr problems in shFlags on freebsd
if uname -s | egrep -iq 'bsd'; then export EXPR_COMPAT=1; fi
# enable debug mode # enable debug mode
if [ "$DEBUG" = "yes" ]; then if [ "$DEBUG" = "yes" ]; then
set -x set -x
fi fi
export GITFLOW_DIR=$(dirname "$0") # The sed expression here replaces all backslashes by forward slashes.
# This helps our Windows users, while not bothering our Unix users.
export GITFLOW_DIR=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
usage() { usage() {
echo "usage: git flow <subcommand>" echo "usage: git flow <subcommand>"
...@@ -75,6 +80,11 @@ main() { ...@@ -75,6 +80,11 @@ main() {
# use the shFlags project to parse the command line arguments # use the shFlags project to parse the command line arguments
. "$GITFLOW_DIR/gitflow-shFlags" . "$GITFLOW_DIR/gitflow-shFlags"
FLAGS_PARENT="git flow" FLAGS_PARENT="git flow"
# allow user to request git action logging
DEFINE_boolean show_commands false 'show actions taken (git commands)' g
# do actual parsing
FLAGS "$@" || exit $? FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}" eval set -- "${FLAGS_ARGV}"
...@@ -94,7 +104,7 @@ main() { ...@@ -94,7 +104,7 @@ main() {
# in that case, we interpret this arg as a flag for the default # in that case, we interpret this arg as a flag for the default
# command # command
SUBACTION="default" SUBACTION="default"
if [ "$1" != "" ] && ! echo "$1" | grep -q "^-"; then if [ "$1" != "" ] && { ! echo "$1" | grep -q "^-"; } then
SUBACTION="$1"; shift SUBACTION="$1"; shift
fi fi
if ! type "cmd_$SUBACTION" >/dev/null 2>&1; then if ! type "cmd_$SUBACTION" >/dev/null 2>&1; then
...@@ -104,6 +114,9 @@ main() { ...@@ -104,6 +114,9 @@ main() {
fi fi
# run the specified action # run the specified action
if [ $SUBACTION != "help" ] && [ $SUBCOMMAND != "init" ] ; then
init
fi
cmd_$SUBACTION "$@" cmd_$SUBACTION "$@"
} }
......
...@@ -36,21 +36,26 @@ ...@@ -36,21 +36,26 @@
# policies, either expressed or implied, of Vincent Driessen. # policies, either expressed or implied, of Vincent Driessen.
# #
require_git_repo init() {
require_gitflow_initialized require_git_repo
gitflow_load_settings require_gitflow_initialized
PREFIX=$(git config --get gitflow.prefix.feature) gitflow_load_settings
parse_args "$@"
PREFIX=$(git config --get gitflow.prefix.feature)
}
usage() { usage() {
echo "usage: git flow feature [list] [-v]" echo "usage: git flow feature [list] [-v] s"
echo " git flow feature start [-F] <name> [<base>]" echo " git flow feature start [-F] <name> [<base>]"
echo " git flow feature finish [-rFk] <name|nameprefix>" echo " git flow feature finish [-rFkDS] [<name|nameprefix>]"
echo " git flow feature publish <name>" echo " git flow feature publish <name>"
echo " git flow feature track <name>" echo " git flow feature track <name>"
echo " git flow feature diff [<name|nameprefix>]" echo " git flow feature diff [<name|nameprefix>]"
echo " git flow feature rebase [-i] [<name|nameprefix>]" echo " git flow feature rebase [-i] [<name|nameprefix>]"
echo " git flow feature checkout [<name|nameprefix>]" echo " git flow feature checkout [<name|nameprefix>]"
echo " git flow feature pull <remote> [<name>]" echo " git flow feature pull [-r] <remote> [<name>]"
echo " git flow feature pause <name>"
} }
cmd_default() { cmd_default() {
...@@ -202,7 +207,7 @@ cmd_start() { ...@@ -202,7 +207,7 @@ cmd_start() {
# update the local repo with remote changes, if asked # update the local repo with remote changes, if asked
if flag fetch; then if flag fetch; then
git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" git_do fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
fi fi
# if the origin branch counterpart exists, assert that the local branch # if the origin branch counterpart exists, assert that the local branch
...@@ -212,10 +217,24 @@ cmd_start() { ...@@ -212,10 +217,24 @@ cmd_start() {
fi fi
# create branch # create branch
if ! git checkout -b "$BRANCH" "$BASE"; then if ! git_do checkout -b "$BRANCH" "$BASE"; then
die "Could not create feature branch '$BRANCH'" die "Could not create feature branch '$BRANCH'"
fi fi
echo "How long do you think this feature will take in hours (e.g 1.5)?"
read TIME_REQUIRED
echo $TIME_REQUIRED >> "./.git/.gitflow/.timelog-feature-$NAME"
git add .
git commit --allow-empty -m "--Flow message(start) -- Started working on $BRANCH on $(date) estimated Time $TIME_REQUIRED"
echo $(date +%s) >> "./.git/.gitflow/.timelog-feature-$NAME"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
echo "- A new branch '$BRANCH' was created, based on '$BASE'" echo "- A new branch '$BRANCH' was created, based on '$BASE'"
...@@ -231,8 +250,10 @@ cmd_finish() { ...@@ -231,8 +250,10 @@ cmd_finish() {
DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
DEFINE_boolean rebase false "rebase instead of merge" r DEFINE_boolean rebase false "rebase instead of merge" r
DEFINE_boolean keep false "keep branch after performing finish" k DEFINE_boolean keep false "keep branch after performing finish" k
DEFINE_boolean force_delete false "force delete feature branch after finish" D
DEFINE_boolean squash false "squash feature during merge" S
parse_args "$@" parse_args "$@"
expand_nameprefix_arg expand_nameprefix_arg_or_current
# sanity checks # sanity checks
require_branch "$BRANCH" require_branch "$BRANCH"
...@@ -247,7 +268,7 @@ cmd_finish() { ...@@ -247,7 +268,7 @@ cmd_finish() {
# TODO: git_is_clean_working_tree() should provide an alternative # TODO: git_is_clean_working_tree() should provide an alternative
# exit code for "unmerged changes in working tree", which we should # exit code for "unmerged changes in working tree", which we should
# actually be testing for here # actually be testing for
if git_is_clean_working_tree; then if git_is_clean_working_tree; then
FINISH_BASE=$(cat "$DOT_GIT_DIR/.gitflow/MERGE_BASE") FINISH_BASE=$(cat "$DOT_GIT_DIR/.gitflow/MERGE_BASE")
...@@ -281,16 +302,17 @@ cmd_finish() { ...@@ -281,16 +302,17 @@ cmd_finish() {
require_clean_working_tree require_clean_working_tree
# update local repo with remote changes first, if asked # update local repo with remote changes first, if asked
if has "$ORIGIN/$BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$BRANCH" $(git_remote_branches); then
if flag fetch; then if flag fetch; then
git fetch -q "$ORIGIN" "$BRANCH" git_do fetch -q "$ORIGIN" "$BRANCH"
git_do fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
fi fi
fi fi
if has "$ORIGIN/$BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$BRANCH" $(git_remote_branches); then
require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH" require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
fi fi
if has "$ORIGIN/$DEVELOP_BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH" require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
fi fi
...@@ -306,11 +328,17 @@ cmd_finish() { ...@@ -306,11 +328,17 @@ cmd_finish() {
fi fi
# merge into BASE # merge into BASE
git checkout "$DEVELOP_BRANCH" git_do checkout "$DEVELOP_BRANCH"
if [ "$(git rev-list -n2 "$DEVELOP_BRANCH..$BRANCH" | wc -l)" -eq 1 ]; then if [ "$(git rev-list -n2 "$DEVELOP_BRANCH..$BRANCH" | wc -l)" -eq 1 ]; then
git merge --ff "$BRANCH" git_do merge --ff "$BRANCH"
else else
git merge --no-ff "$BRANCH" if noflag squash; then
git_do merge --no-ff "$BRANCH"
else
git_do merge --squash "$BRANCH"
git_do commit
git_do merge "$BRANCH"
fi
fi fi
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
...@@ -329,6 +357,34 @@ cmd_finish() { ...@@ -329,6 +357,34 @@ cmd_finish() {
exit 1 exit 1
fi fi
echo $(date +%s) >> "./.git/.gitflow/.timelog-feature-$NAME"
LAST_LINE="$(wc -l < ./.git/.gitflow/.timelog-feature-$NAME)"
PREV_LINE=$((LAST_LINE-1))
LAST_TIME=$(sed "${LAST_LINE}q;d" ./.git/.gitflow/.timelog-feature-$NAME)
PREV_TIME=$(sed "${PREV_LINE}q;d" ./.git/.gitflow/.timelog-feature-$NAME)
TIME_USED=$((LAST_TIME-PREV_TIME))
echo "$TIME_USED" >> "./.git/.gitflow/.seconds-feature-$NAME"
if [ -f ./.git/.gitflow/.breaktime-feature-$NAME ];
then
TOTAL_BREAK_TIME=$(awk '{ sum += $1 } END { print sum }' ./.git/.gitflow/.breaktime-feature-$NAME)
else
TOTAL_BREAK_TIME=0
fi
TOTAL_TIME=$(awk '{ sum += $1 } END { print sum }' ./.git/.gitflow/.seconds-feature-$NAME)
TIME_EXPECTED=$(head -n 1 ./.git/.gitflow/.timelog-feature-$NAME)
FINALTIME=$((TOTAL_TIME/86400))" days "$(date -d "1970-01-01 + $TOTAL_TIME seconds" "+%H hours %M minutes %S seconds")
TOTAL_TIME_OFF=$((TOTAL_BREAK_TIME/86400))" days "$(date -d "1970-01-01 + $TOTAL_BREAK_TIME seconds" "+%H hours %M minutes %S seconds")
git commit --allow-empty -m "--Flow message(finish) --$BRANCH was completed on $(date), total time taken:$FINALTIME expectde time was $TIME_EXPECTED, Time spent on breaks was $TOTAL_TIME_OFF"
# when no merge conflict is detected, just clean up the feature branch # when no merge conflict is detected, just clean up the feature branch
helper_finish_cleanup helper_finish_cleanup
} }
...@@ -340,12 +396,16 @@ helper_finish_cleanup() { ...@@ -340,12 +396,16 @@ helper_finish_cleanup() {
# delete branch # delete branch
if flag fetch; then if flag fetch; then
git push "$ORIGIN" ":refs/heads/$BRANCH" git_do push "$ORIGIN" ":refs/heads/$BRANCH"
fi fi
if noflag keep; then if noflag keep; then
git branch -d "$BRANCH" if flag force_delete; then
git_do branch -D "$BRANCH"
else
git_do branch -d "$BRANCH"
fi
fi fi
echo echo
...@@ -368,17 +428,17 @@ cmd_publish() { ...@@ -368,17 +428,17 @@ cmd_publish() {
# sanity checks # sanity checks
require_clean_working_tree require_clean_working_tree
require_branch "$BRANCH" require_branch "$BRANCH"
git fetch -q "$ORIGIN" git_do fetch -q "$ORIGIN"
require_branch_absent "$ORIGIN/$BRANCH" require_branch_absent "$ORIGIN/$BRANCH"
# create remote branch # create remote branch
git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH" git_do push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
git fetch -q "$ORIGIN" git_do fetch -q "$ORIGIN"
# configure remote tracking # configure remote tracking
git config "branch.$BRANCH.remote" "$ORIGIN" git_do config "branch.$BRANCH.remote" "$ORIGIN"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH" git_do config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
git checkout "$BRANCH" git_do checkout "$BRANCH"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
...@@ -395,11 +455,11 @@ cmd_track() { ...@@ -395,11 +455,11 @@ cmd_track() {
# sanity checks # sanity checks
require_clean_working_tree require_clean_working_tree
require_branch_absent "$BRANCH" require_branch_absent "$BRANCH"
git fetch -q "$ORIGIN" git_do fetch -q "$ORIGIN"
require_branch "$ORIGIN/$BRANCH" require_branch "$ORIGIN/$BRANCH"
# create tracking branch # create tracking branch
git checkout -b "$BRANCH" "$ORIGIN/$BRANCH" git_do checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
...@@ -430,7 +490,7 @@ cmd_checkout() { ...@@ -430,7 +490,7 @@ cmd_checkout() {
if [ "$NAME" != "" ]; then if [ "$NAME" != "" ]; then
expand_nameprefix_arg expand_nameprefix_arg
git checkout "$BRANCH" git_do checkout "$BRANCH"
else else
die "Name a feature branch explicitly." die "Name a feature branch explicitly."
fi fi
...@@ -449,12 +509,12 @@ cmd_rebase() { ...@@ -449,12 +509,12 @@ cmd_rebase() {
require_clean_working_tree require_clean_working_tree
require_branch "$BRANCH" require_branch "$BRANCH"
git checkout -q "$BRANCH" git_do checkout -q "$BRANCH"
local OPTS= local OPTS=
if flag interactive; then if flag interactive; then
OPTS="$OPTS -i" OPTS="$OPTS -i"
fi fi
git rebase $OPTS "$DEVELOP_BRANCH" git_do rebase $OPTS "$DEVELOP_BRANCH"
} }
avoid_accidental_cross_branch_action() { avoid_accidental_cross_branch_action() {
...@@ -469,6 +529,7 @@ avoid_accidental_cross_branch_action() { ...@@ -469,6 +529,7 @@ avoid_accidental_cross_branch_action() {
cmd_pull() { cmd_pull() {
#DEFINE_string prefix false 'alternative remote feature branch name prefix' p #DEFINE_string prefix false 'alternative remote feature branch name prefix' p
DEFINE_boolean rebase false "pull with rebase" r
parse_remote_name "$@" parse_remote_name "$@"
if [ -z "$REMOTE" ]; then if [ -z "$REMOTE" ]; then
...@@ -494,13 +555,179 @@ cmd_pull() { ...@@ -494,13 +555,179 @@ cmd_pull() {
# we already have a local branch called like this, so simply pull the # we already have a local branch called like this, so simply pull the
# remote changes in # remote changes in
git pull -q "$REMOTE" "$BRANCH" || die "Failed to pull from remote '$REMOTE'." if flag rebase; then
if ! git_do pull --rebase -q "$REMOTE" "$BRANCH"; then
warn "Pull was aborted. There might be conflicts during rebase or '$REMOTE' might be inaccessible."
exit 1
fi
else
git_do pull -q "$REMOTE" "$BRANCH" || die "Failed to pull from remote '$REMOTE'."
fi
echo "Pulled $REMOTE's changes into $BRANCH." echo "Pulled $REMOTE's changes into $BRANCH."
else else
# setup the local branch clone for the first time # setup the local branch clone for the first time
git fetch -q "$REMOTE" "$BRANCH" || die "Fetch failed." # stores in FETCH_HEAD git_do fetch -q "$REMOTE" "$BRANCH" || die "Fetch failed." # stores in FETCH_HEAD
git branch --no-track "$BRANCH" FETCH_HEAD || die "Branch failed." git_do branch --no-track "$BRANCH" FETCH_HEAD || die "Branch failed."
git checkout -q "$BRANCH" || die "Checking out new local branch failed." git_do checkout -q "$BRANCH" || die "Checking out new local branch failed."
echo "Created local branch $BRANCH based on $REMOTE's $BRANCH." echo "Created local branch $BRANCH based on $REMOTE's $BRANCH."
fi fi
} }
cmd_pause() {
parse_args "$@"
local current_branch
local current_name
current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
current_name=$(echo "$current_branch" | sed "s ^$PREFIX g")
echo $(date +%s) >> "./.git/.gitflow/.timelog-feature-$current_name"
LAST_LINE="$(wc -l < ./.git/.gitflow/.timelog-feature-$current_name)"
PREV_LINE=$((LAST_LINE-1))
LAST_TIME=$(sed "${LAST_LINE}q;d" ./.git/.gitflow/.timelog-feature-$current_name)
PREV_TIME=$(sed "${PREV_LINE}q;d" ./.git/.gitflow/.timelog-feature-$current_name)
TIME_USED=$((LAST_TIME-PREV_TIME))
echo "dummy" >> "./.git/.gitflow/.bool-$current_name"
echo "$TIME_USED" >> "./.git/.gitflow/.seconds-feature-$current_name"
seconds=$TIME_USED; TIMESTAMP=$((seconds/86400))" days "$(date -d "1970-01-01 + $seconds seconds" "+%H hours %M minutes %S seconds")
git add --all .
git commit -m "$current_branch/WIP"
git commit --allow-empty -m "--Flow message(pause) -- $current_branch/WIP time-paused:$(date), time taken thus far:$TIMESTAMP"
git checkout -b "$current_branch-paused" "flopharn"
git rm * --quiet --ignore-unmatch
echo
echo "Summary of actions:"
echo "- Created orphan branch called '$current_branch-paused'"
echo "- You are now free to take a break, you have spent $TIMESTAMP in this session"
echo ""
echo "Now, have some coffee and when you are done, use:"
echo ""
echo " git flow feature resume $current_name"
echo
}
cmd_interrupt() {
parse_args "$@"
local current_branch
local current_name
current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
current_name=$(echo "$current_branch" | sed "s ^$PREFIX g")
echo $(date +%s) >> "./.git/.gitflow/.timelog-feature-$current_name"
LAST_LINE="$(wc -l < ./.git/.gitflow/.timelog-feature-$current_name)"
PREV_LINE=$((LAST_LINE-1))
LAST_TIME=$(sed "${LAST_LINE}q;d" ./.git/.gitflow/.timelog-feature-$current_name)
PREV_TIME=$(sed "${PREV_LINE}q;d" ./.git/.gitflow/.timelog-feature-$current_name)
TIME_USED=$((LAST_TIME-PREV_TIME))
echo "$TIME_USED" >> "./.git/.gitflow/.seconds-feature-$current_name"
echo "dummy" >> "./.git/.gitflow/.interrupt-feature-$current_name"
seconds=$TIME_USED; TIMESTAMP=$((seconds/86400))" days "$(date -d "1970-01-01 + $seconds seconds" "+%H hours %M minutes %S seconds")
git add --all .
git commit -m "$current_branch/WIP"
git commit --allow-empty -m "--Flow message(interrupt) --$current_branch/WIP You were disturbed on $(date), time taken so far:$TIMESTAMP"
git checkout -b "$current_branch-paused" "flopharn"
git rm * --quiet --ignore-unmatch
echo
echo "Summary of actions:"
echo "- Created orphan branch called '$current_branch-paused'"
echo "- You are now free to take a break, you have spent $TIMESTAMP in this session"
echo ""
echo "Now, have some coffee and when you are done, use:"
echo ""
echo " git flow feature resume $current_branch"
echo
}
cmd_resume() {
parse_args "$@"
require_name_arg
# sanity checks
require_branch "$BRANCH"
if [ -f ./.git/.gitflow/.interrupt-feature-$NAME ];
then
echo "Why were you interrupted?"
read INTERRUPT_REASON
rm ./.git/.gitflow/.interrupt-feature-$NAME
else
INTERRUPT_REASON="No reason :)"
fi
LAST_LINE="$(wc -l < ./.git/.gitflow/.timelog-feature-$NAME)"
LAST_TIME=$(date +%s)
PREV_TIME=$(sed "${LAST_LINE}q;d" ./.git/.gitflow/.timelog-feature-$NAME)
BREAK_TIME=$((LAST_TIME-PREV_TIME))
echo "$BREAK_TIME" >> "./.git/.gitflow/.breaktime-feature-$NAME"
seconds=$BREAK_TIME; TIME_OFF=$((seconds/86400))" days "$(date -d "1970-01-01 + $seconds seconds" "+%H hours %M minutes %S seconds")
git add .
git checkout "$BRANCH"
git branch -D "$BRANCH-paused"
git commit --allow-empty -m "--Flow message(resume) --$BRANCH/Back to Work at:$(date) Time off:$TIME_OFF, Reason: $INTERRUPT_REASON"
echo $(date +%s) >> "./.git/.gitflow/.timelog-feature-$NAME"
echo
echo "Summary of actions:"
echo "Switches you back to the '$BRANCH' branch"
echo "- You are now on branch '$BRANCH'"
echo ""
echo "Remmember when you need a break use:"
echo ""
echo " git flow feature pause"
echo
}
...@@ -36,16 +36,20 @@ ...@@ -36,16 +36,20 @@
# policies, either expressed or implied, of Vincent Driessen. # policies, either expressed or implied, of Vincent Driessen.
# #
require_git_repo init() {
require_gitflow_initialized require_git_repo
gitflow_load_settings require_gitflow_initialized
VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`") gitflow_load_settings
PREFIX=$(git config --get gitflow.prefix.hotfix) VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`")
PREFIX=$(git config --get gitflow.prefix.hotfix)
}
usage() { usage() {
echo "usage: git flow hotfix [list] [-v]" echo "usage: git flow hotfix [list] [-v]"
echo " git flow hotfix start [-F] <version> [<base>]" echo " git flow hotfix start [-F] <version> [<base>]"
echo " git flow hotfix finish [-Fsumpk] <version>" echo " git flow hotfix finish [-Fsumpk] <version>"
echo " git flow hotfix publish <version>"
echo " git flow hotfix track <version>"
} }
cmd_default() { cmd_default() {
...@@ -165,14 +169,14 @@ cmd_start() { ...@@ -165,14 +169,14 @@ cmd_start() {
require_branch_absent "$BRANCH" require_branch_absent "$BRANCH"
require_tag_absent "$VERSION_PREFIX$VERSION" require_tag_absent "$VERSION_PREFIX$VERSION"
if flag fetch; then if flag fetch; then
git fetch -q "$ORIGIN" "$MASTER_BRANCH" git_do fetch -q "$ORIGIN" "$MASTER_BRANCH"
fi fi
if has "$ORIGIN/$MASTER_BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH" require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi fi
# create branch # create branch
git checkout -b "$BRANCH" "$BASE" git_do checkout -b "$BRANCH" "$BASE"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
...@@ -188,11 +192,59 @@ cmd_start() { ...@@ -188,11 +192,59 @@ cmd_start() {
echo echo
} }
cmd_publish() {
parse_args "$@"
require_version_arg
# sanity checks
require_clean_working_tree
require_branch "$BRANCH"
git_do fetch -q "$ORIGIN"
require_branch_absent "$ORIGIN/$BRANCH"
# create remote branch
git_do push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
git_do fetch -q "$ORIGIN"
# configure remote tracking
git config "branch.$BRANCH.remote" "$ORIGIN"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
git_do checkout "$BRANCH"
echo
echo "Summary of actions:"
echo "- A new remote branch '$BRANCH' was created"
echo "- The local branch '$BRANCH' was configured to track the remote branch"
echo "- You are now on branch '$BRANCH'"
echo
}
cmd_track() {
parse_args "$@"
require_version_arg
# sanity checks
require_clean_working_tree
require_branch_absent "$BRANCH"
git_do fetch -q "$ORIGIN"
require_branch "$ORIGIN/$BRANCH"
# create tracking branch
git_do checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
echo
echo "Summary of actions:"
echo "- A new remote tracking branch '$BRANCH' was created"
echo "- You are now on branch '$BRANCH'"
echo
}
cmd_finish() { cmd_finish() {
DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
DEFINE_boolean sign false "sign the release tag cryptographically" s DEFINE_boolean sign false "sign the release tag cryptographically" s
DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
DEFINE_string message "" "use the given tag message" m DEFINE_string message "" "use the given tag message" m
DEFINE_string messagefile "" "use the contents of the given file as tag message" f
DEFINE_boolean push false "push to $ORIGIN after performing finish" p DEFINE_boolean push false "push to $ORIGIN after performing finish" p
DEFINE_boolean keep false "keep branch after performing finish" k DEFINE_boolean keep false "keep branch after performing finish" k
DEFINE_boolean notag false "don't tag this release" n DEFINE_boolean notag false "don't tag this release" n
...@@ -208,15 +260,15 @@ cmd_finish() { ...@@ -208,15 +260,15 @@ cmd_finish() {
require_branch "$BRANCH" require_branch "$BRANCH"
require_clean_working_tree require_clean_working_tree
if flag fetch; then if flag fetch; then
git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \ git_do fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not fetch $MASTER_BRANCH from $ORIGIN." die "Could not fetch $MASTER_BRANCH from $ORIGIN."
git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \ git_do fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
die "Could not fetch $DEVELOP_BRANCH from $ORIGIN." die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
fi fi
if has "$ORIGIN/$MASTER_BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH" require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi fi
if has "$ORIGIN/$DEVELOP_BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH" require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
fi fi
...@@ -224,9 +276,9 @@ cmd_finish() { ...@@ -224,9 +276,9 @@ cmd_finish() {
# in case a previous attempt to finish this release branch has failed, # in case a previous attempt to finish this release branch has failed,
# but the merge into master was successful, we skip it now # but the merge into master was successful, we skip it now
if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
git checkout "$MASTER_BRANCH" || \ git_do checkout "$MASTER_BRANCH" || \
die "Could not check out $MASTER_BRANCH." die "Could not check out $MASTER_BRANCH."
git merge --no-ff "$BRANCH" || \ git_do merge --no-ff "$BRANCH" || \
die "There were merge conflicts." die "There were merge conflicts."
# TODO: What do we do now? # TODO: What do we do now?
fi fi
...@@ -241,7 +293,8 @@ cmd_finish() { ...@@ -241,7 +293,8 @@ cmd_finish() {
flag sign && opts="$opts -s" flag sign && opts="$opts -s"
[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'" [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'" [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
git tag $opts "$VERSION_PREFIX$VERSION" || \ [ "$FLAGS_messagefile" != "" ] && opts="$opts -F '$FLAGS_messagefile'"
eval git_do tag $opts "$VERSION_PREFIX$VERSION" "$BRANCH" || \
die "Tagging failed. Please run finish again to retry." die "Tagging failed. Please run finish again to retry."
fi fi
fi fi
...@@ -250,28 +303,28 @@ cmd_finish() { ...@@ -250,28 +303,28 @@ cmd_finish() {
# in case a previous attempt to finish this release branch has failed, # in case a previous attempt to finish this release branch has failed,
# but the merge into develop was successful, we skip it now # but the merge into develop was successful, we skip it now
if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
git checkout "$DEVELOP_BRANCH" || \ git_do checkout "$DEVELOP_BRANCH" || \
die "Could not check out $DEVELOP_BRANCH." die "Could not check out $DEVELOP_BRANCH."
# TODO: Actually, accounting for 'git describe' pays, so we should # TODO: Actually, accounting for 'git describe' pays, so we should
# ideally git merge --no-ff $tagname here, instead! # ideally git merge --no-ff $tagname here, instead!
git merge --no-ff "$BRANCH" || \ git_do merge --no-ff "$BRANCH" || \
die "There were merge conflicts." die "There were merge conflicts."
# TODO: What do we do now? # TODO: What do we do now?
fi fi
# delete branch # delete branch
if noflag keep; then if noflag keep; then
git branch -d "$BRANCH" git_do branch -d "$BRANCH"
fi fi
if flag push; then if flag push; then
git push "$ORIGIN" "$DEVELOP_BRANCH" || \ git_do push "$ORIGIN" "$DEVELOP_BRANCH" || \
die "Could not push to $DEVELOP_BRANCH from $ORIGIN." die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
git push "$ORIGIN" "$MASTER_BRANCH" || \ git_do push "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not push to $MASTER_BRANCH from $ORIGIN." die "Could not push to $MASTER_BRANCH from $ORIGIN."
if noflag notag; then if noflag notag; then
git push --tags "$ORIGIN" || \ git_do push --tags "$ORIGIN" || \
die "Could not push tags to $ORIGIN." die "Could not push tags to $ORIGIN."
fi fi
fi fi
......
...@@ -52,8 +52,10 @@ cmd_default() { ...@@ -52,8 +52,10 @@ cmd_default() {
DEFINE_boolean defaults false 'use default branch naming conventions' d DEFINE_boolean defaults false 'use default branch naming conventions' d
parse_args "$@" parse_args "$@"
if ! git rev-parse --git-dir >/dev/null 2>&1; then if ! git rev-parse --git-dir >/dev/null 2>&1; then
git init git_do init
else else
# assure that we are not working in a repo with local changes # assure that we are not working in a repo with local changes
git_repo_is_headless || require_clean_working_tree git_repo_is_headless || require_clean_working_tree
...@@ -117,12 +119,18 @@ cmd_default() { ...@@ -117,12 +119,18 @@ cmd_default() {
# check existence in case of an already existing repo # check existence in case of an already existing repo
if [ "$should_check_existence" = "YES" ]; then if [ "$should_check_existence" = "YES" ]; then
git_local_branch_exists "$master_branch" || \ # if no local branch exists and a remote branch of the same
# name exists, checkout that branch and use it for master
if ! git_local_branch_exists "$master_branch" && \
git_remote_branch_exists "origin/$master_branch"; then
git_do branch "$master_branch" "origin/$master_branch" >/dev/null 2>&1
elif ! git_local_branch_exists "$master_branch"; then
die "Local branch '$master_branch' does not exist." die "Local branch '$master_branch' does not exist."
fi fi
fi
# store the name of the master branch # store the name of the master branch
git config gitflow.branch.master "$master_branch" git_do config gitflow.branch.master "$master_branch"
fi fi
# add a develop branch if no such branch exists yet # add a develop branch if no such branch exists yet
...@@ -147,11 +155,17 @@ cmd_default() { ...@@ -147,11 +155,17 @@ cmd_default() {
default_suggestion= default_suggestion=
for guess in $(git config --get gitflow.branch.develop) \ for guess in $(git config --get gitflow.branch.develop) \
'develop' 'int' 'integration' 'master'; do 'develop' 'int' 'integration' 'master'; do
if git_local_branch_exists "$guess"; then if git_local_branch_exists "$guess" && [ "$guess" != "$master_branch" ]; then
default_suggestion="$guess" default_suggestion="$guess"
break break
fi fi
done done
if [ -z $default_suggestion ]; then
should_check_existence=NO
default_suggestion=$(git config --get gitflow.branch.develop || echo develop)
fi
fi fi
printf "Branch name for \"next release\" development: [$default_suggestion] " printf "Branch name for \"next release\" development: [$default_suggestion] "
...@@ -173,7 +187,7 @@ cmd_default() { ...@@ -173,7 +187,7 @@ cmd_default() {
fi fi
# store the name of the develop branch # store the name of the develop branch
git config gitflow.branch.develop "$develop_branch" git_do config gitflow.branch.develop "$develop_branch"
fi fi
# Creation of HEAD # Creation of HEAD
...@@ -182,8 +196,8 @@ cmd_default() { ...@@ -182,8 +196,8 @@ cmd_default() {
# it to be able to create new branches. # it to be able to create new branches.
local created_gitflow_branch=0 local created_gitflow_branch=0
if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then if ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
git symbolic-ref HEAD "refs/heads/$master_branch" git_do symbolic-ref HEAD "refs/heads/$master_branch"
git commit --allow-empty --quiet -m "Initial commit" git_do commit --allow-empty --quiet -m "Initial commit"
created_gitflow_branch=1 created_gitflow_branch=1
fi fi
...@@ -200,7 +214,11 @@ cmd_default() { ...@@ -200,7 +214,11 @@ cmd_default() {
# default production branch and develop was "created". We should create # default production branch and develop was "created". We should create
# the develop branch now in that case (we base it on master, of course) # the develop branch now in that case (we base it on master, of course)
if ! git_local_branch_exists "$develop_branch"; then if ! git_local_branch_exists "$develop_branch"; then
git branch --no-track "$develop_branch" "$master_branch" if git_remote_branch_exists "origin/$develop_branch"; then
git_do branch "$develop_branch" "origin/$develop_branch" >/dev/null 2>&1
else
git_do branch --no-track "$develop_branch" "$master_branch"
fi
created_gitflow_branch=1 created_gitflow_branch=1
fi fi
...@@ -209,7 +227,7 @@ cmd_default() { ...@@ -209,7 +227,7 @@ cmd_default() {
# switch to develop branch if its newly created # switch to develop branch if its newly created
if [ $created_gitflow_branch -eq 1 ]; then if [ $created_gitflow_branch -eq 1 ]; then
git checkout -q "$develop_branch" git_do checkout -q "$develop_branch"
fi fi
# finally, ask the user for naming conventions (branch and tag prefixes) # finally, ask the user for naming conventions (branch and tag prefixes)
...@@ -223,6 +241,17 @@ cmd_default() { ...@@ -223,6 +241,17 @@ cmd_default() {
echo "How to name your supporting branch prefixes?" echo "How to name your supporting branch prefixes?"
fi fi
git config --global alias.start 'flow feature start'
git config --global alias.finish 'flow feature finish'
git config --global alias.fin 'flow feature finish'
git config --global alias.stop 'flow feature finish'
git config --global alias.resume 'flow feature resume'
git config --global alias.res 'flow feature resume'
git config --global alias.interrupt 'flow feature interrupt'
git config --global alias.int 'flow feature interrupt'
git config --global alias.pause 'flow feature pause'
git config --global alias.p 'flow feature pause'
local prefix local prefix
# Feature branches # Feature branches
...@@ -235,7 +264,7 @@ cmd_default() { ...@@ -235,7 +264,7 @@ cmd_default() {
printf "\n" printf "\n"
fi fi
[ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
git config gitflow.prefix.feature "$prefix" git_do config gitflow.prefix.feature "$prefix"
fi fi
# Release branches # Release branches
...@@ -248,7 +277,7 @@ cmd_default() { ...@@ -248,7 +277,7 @@ cmd_default() {
printf "\n" printf "\n"
fi fi
[ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
git config gitflow.prefix.release "$prefix" git_do config gitflow.prefix.release "$prefix"
fi fi
...@@ -262,7 +291,7 @@ cmd_default() { ...@@ -262,7 +291,7 @@ cmd_default() {
printf "\n" printf "\n"
fi fi
[ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
git config gitflow.prefix.hotfix "$prefix" git_do config gitflow.prefix.hotfix "$prefix"
fi fi
...@@ -276,7 +305,7 @@ cmd_default() { ...@@ -276,7 +305,7 @@ cmd_default() {
printf "\n" printf "\n"
fi fi
[ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
git config gitflow.prefix.support "$prefix" git_do config gitflow.prefix.support "$prefix"
fi fi
...@@ -290,8 +319,13 @@ cmd_default() { ...@@ -290,8 +319,13 @@ cmd_default() {
printf "\n" printf "\n"
fi fi
[ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion} [ "$answer" = "-" ] && prefix= || prefix=${answer:-$default_suggestion}
git config gitflow.prefix.versiontag "$prefix" git_do config gitflow.prefix.versiontag "$prefix"
fi fi
mkdir -p "./.git/.gitflow"
git checkout --orphan flopharn
git rm * --quiet --ignore-unmatch
git commit --allow-empty -m "flopharn was created successfully"
git checkout $develop_branch
# TODO: what to do with origin? # TODO: what to do with origin?
......
...@@ -36,16 +36,18 @@ ...@@ -36,16 +36,18 @@
# policies, either expressed or implied, of Vincent Driessen. # policies, either expressed or implied, of Vincent Driessen.
# #
require_git_repo init() {
require_gitflow_initialized require_git_repo
gitflow_load_settings require_gitflow_initialized
VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`") gitflow_load_settings
PREFIX=$(git config --get gitflow.prefix.release) VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`")
PREFIX=$(git config --get gitflow.prefix.release)
}
usage() { usage() {
echo "usage: git flow release [list] [-v]" echo "usage: git flow release [list] [-v]"
echo " git flow release start [-F] <version>" echo " git flow release start [-F] <version> [<base>]"
echo " git flow release finish [-Fsumpk] <version>" echo " git flow release finish [-FsumpkS] <version>"
echo " git flow release publish <name>" echo " git flow release publish <name>"
echo " git flow release track <name>" echo " git flow release track <name>"
} }
...@@ -134,7 +136,7 @@ require_version_arg() { ...@@ -134,7 +136,7 @@ require_version_arg() {
} }
require_base_is_on_develop() { require_base_is_on_develop() {
if ! git branch --no-color --contains "$BASE" 2>/dev/null \ if ! git_do branch --no-color --contains "$BASE" 2>/dev/null \
| sed 's/[* ] //g' \ | sed 's/[* ] //g' \
| grep -q "^$DEVELOP_BRANCH\$"; then | grep -q "^$DEVELOP_BRANCH\$"; then
die "fatal: Given base '$BASE' is not a valid commit on '$DEVELOP_BRANCH'." die "fatal: Given base '$BASE' is not a valid commit on '$DEVELOP_BRANCH'."
...@@ -162,14 +164,14 @@ cmd_start() { ...@@ -162,14 +164,14 @@ cmd_start() {
require_branch_absent "$BRANCH" require_branch_absent "$BRANCH"
require_tag_absent "$VERSION_PREFIX$VERSION" require_tag_absent "$VERSION_PREFIX$VERSION"
if flag fetch; then if flag fetch; then
git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" git_do fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
fi fi
if has "$ORIGIN/$DEVELOP_BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH" require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
fi fi
# create branch # create branch
git checkout -b "$BRANCH" "$BASE" git_do checkout -b "$BRANCH" "$BASE"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
...@@ -190,9 +192,11 @@ cmd_finish() { ...@@ -190,9 +192,11 @@ cmd_finish() {
DEFINE_boolean sign false "sign the release tag cryptographically" s DEFINE_boolean sign false "sign the release tag cryptographically" s
DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
DEFINE_string message "" "use the given tag message" m DEFINE_string message "" "use the given tag message" m
DEFINE_string messagefile "" "use the contents of the given file as a tag message" f
DEFINE_boolean push false "push to $ORIGIN after performing finish" p DEFINE_boolean push false "push to $ORIGIN after performing finish" p
DEFINE_boolean keep false "keep branch after performing finish" k DEFINE_boolean keep false "keep branch after performing finish" k
DEFINE_boolean notag false "don't tag this release" n DEFINE_boolean notag false "don't tag this release" n
DEFINE_boolean squash false "squash release during merge" S
parse_args "$@" parse_args "$@"
require_version_arg require_version_arg
...@@ -206,15 +210,15 @@ cmd_finish() { ...@@ -206,15 +210,15 @@ cmd_finish() {
require_branch "$BRANCH" require_branch "$BRANCH"
require_clean_working_tree require_clean_working_tree
if flag fetch; then if flag fetch; then
git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \ git_do fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not fetch $MASTER_BRANCH from $ORIGIN." die "Could not fetch $MASTER_BRANCH from $ORIGIN."
git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \ git_do fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \
die "Could not fetch $DEVELOP_BRANCH from $ORIGIN." die "Could not fetch $DEVELOP_BRANCH from $ORIGIN."
fi fi
if has "$ORIGIN/$MASTER_BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH" require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi fi
if has "$ORIGIN/$DEVELOP_BRANCH" "$(git_remote_branches)"; then if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH" require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
fi fi
...@@ -222,11 +226,17 @@ cmd_finish() { ...@@ -222,11 +226,17 @@ cmd_finish() {
# in case a previous attempt to finish this release branch has failed, # in case a previous attempt to finish this release branch has failed,
# but the merge into master was successful, we skip it now # but the merge into master was successful, we skip it now
if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
git checkout "$MASTER_BRANCH" || \ git_do checkout "$MASTER_BRANCH" || \
die "Could not check out $MASTER_BRANCH." die "Could not check out $MASTER_BRANCH."
git merge --no-ff "$BRANCH" || \ if noflag squash; then
git_do merge --no-ff "$BRANCH" || \
die "There were merge conflicts." die "There were merge conflicts."
# TODO: What do we do now? # TODO: What do we do now?
else
git_do merge --squash "$BRANCH" || \
die "There were merge conflicts."
git_do commit
fi
fi fi
if noflag notag; then if noflag notag; then
...@@ -239,7 +249,8 @@ cmd_finish() { ...@@ -239,7 +249,8 @@ cmd_finish() {
flag sign && opts="$opts -s" flag sign && opts="$opts -s"
[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'" [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'" [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
git tag $opts "$tagname" || \ [ "$FLAGS_messagefile" != "" ] && opts="$opts -F '$FLAGS_messagefile'"
eval git_do tag $opts "$tagname" "$BRANCH" || \
die "Tagging failed. Please run finish again to retry." die "Tagging failed. Please run finish again to retry."
fi fi
fi fi
...@@ -248,34 +259,41 @@ cmd_finish() { ...@@ -248,34 +259,41 @@ cmd_finish() {
# in case a previous attempt to finish this release branch has failed, # in case a previous attempt to finish this release branch has failed,
# but the merge into develop was successful, we skip it now # but the merge into develop was successful, we skip it now
if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then
git checkout "$DEVELOP_BRANCH" || \ git_do checkout "$DEVELOP_BRANCH" || \
die "Could not check out $DEVELOP_BRANCH." die "Could not check out $DEVELOP_BRANCH."
# TODO: Actually, accounting for 'git describe' pays, so we should # TODO: Actually, accounting for 'git describe' pays, so we should
# ideally git merge --no-ff $tagname here, instead! # ideally git merge --no-ff $tagname here, instead!
git merge --no-ff "$BRANCH" || \ if noflag squash; then
git_do merge --no-ff "$BRANCH" || \
die "There were merge conflicts." die "There were merge conflicts."
# TODO: What do we do now? # TODO: What do we do now?
else
git_do merge --squash "$BRANCH" || \
die "There were merge conflicts."
# TODO: What do we do now?
git_do commit
fi
fi fi
# delete branch # delete branch
if noflag keep; then if noflag keep; then
if [ "$BRANCH" = "$(git_current_branch)" ]; then if [ "$BRANCH" = "$(git_current_branch)" ]; then
git checkout "$MASTER_BRANCH" git_do checkout "$MASTER_BRANCH"
fi fi
git branch -d "$BRANCH" git_do branch -d "$BRANCH"
fi fi
if flag push; then if flag push; then
git push "$ORIGIN" "$DEVELOP_BRANCH" || \ git_do push "$ORIGIN" "$DEVELOP_BRANCH" || \
die "Could not push to $DEVELOP_BRANCH from $ORIGIN." die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
git push "$ORIGIN" "$MASTER_BRANCH" || \ git_do push "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not push to $MASTER_BRANCH from $ORIGIN." die "Could not push to $MASTER_BRANCH from $ORIGIN."
if noflag notag; then if noflag notag; then
git push --tags "$ORIGIN" || \ git_do push --tags "$ORIGIN" || \
die "Could not push tags to $ORIGIN." die "Could not push tags to $ORIGIN."
fi fi
git push "$ORIGIN" :"$BRANCH" || \ git_do push "$ORIGIN" :"$BRANCH" || \
die "Could not delete the remote $BRANCH in $ORIGIN." die "Could not delete the remote $BRANCH in $ORIGIN."
fi fi
...@@ -306,17 +324,17 @@ cmd_publish() { ...@@ -306,17 +324,17 @@ cmd_publish() {
# sanity checks # sanity checks
require_clean_working_tree require_clean_working_tree
require_branch "$BRANCH" require_branch "$BRANCH"
git fetch -q "$ORIGIN" git_do fetch -q "$ORIGIN"
require_branch_absent "$ORIGIN/$BRANCH" require_branch_absent "$ORIGIN/$BRANCH"
# create remote branch # create remote branch
git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH" git_do push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
git fetch -q "$ORIGIN" git_do fetch -q "$ORIGIN"
# configure remote tracking # configure remote tracking
git config "branch.$BRANCH.remote" "$ORIGIN" git_do config "branch.$BRANCH.remote" "$ORIGIN"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH" git_do config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
git checkout "$BRANCH" git_do checkout "$BRANCH"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
...@@ -333,11 +351,11 @@ cmd_track() { ...@@ -333,11 +351,11 @@ cmd_track() {
# sanity checks # sanity checks
require_clean_working_tree require_clean_working_tree
require_branch_absent "$BRANCH" require_branch_absent "$BRANCH"
git fetch -q "$ORIGIN" git_do fetch -q "$ORIGIN"
require_branch "$ORIGIN/$BRANCH" require_branch "$ORIGIN/$BRANCH"
# create tracking branch # create tracking branch
git checkout -b "$BRANCH" "$ORIGIN/$BRANCH" git_do checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
......
...@@ -36,11 +36,13 @@ ...@@ -36,11 +36,13 @@
# policies, either expressed or implied, of Vincent Driessen. # policies, either expressed or implied, of Vincent Driessen.
# #
require_git_repo init() {
require_gitflow_initialized require_git_repo
gitflow_load_settings require_gitflow_initialized
VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`") gitflow_load_settings
PREFIX=$(git config --get gitflow.prefix.support) VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`")
PREFIX=$(git config --get gitflow.prefix.support)
}
warn "note: The support subcommand is still very EXPERIMENTAL!" warn "note: The support subcommand is still very EXPERIMENTAL!"
warn "note: DO NOT use it in a production situation." warn "note: DO NOT use it in a production situation."
...@@ -167,12 +169,12 @@ cmd_start() { ...@@ -167,12 +169,12 @@ cmd_start() {
# fetch remote changes # fetch remote changes
if flag fetch; then if flag fetch; then
git fetch -q "$ORIGIN" "$BASE" git_do fetch -q "$ORIGIN" "$BASE"
fi fi
require_branch_absent "$BRANCH" require_branch_absent "$BRANCH"
# create branch # create branch
git checkout -b "$BRANCH" "$BASE" git_do checkout -b "$BRANCH" "$BASE"
echo echo
echo "Summary of actions:" echo "Summary of actions:"
......
...@@ -36,14 +36,14 @@ ...@@ -36,14 +36,14 @@
# policies, either expressed or implied, of Vincent Driessen. # policies, either expressed or implied, of Vincent Driessen.
# #
GITFLOW_VERSION=0.4.1 GITFLOW_VERSION=0.4.2-pre
usage() { usage() {
echo "usage: git flow version" echo "usage: git flow version"
} }
cmd_default() { cmd_default() {
echo "$GITFLOW_VERSION" echo "$GITFLOW_VERSION dreidev"
} }
cmd_help() { cmd_help() {
......
...@@ -45,7 +45,7 @@ warn() { echo "$@" >&2; } ...@@ -45,7 +45,7 @@ warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; } die() { warn "$@"; exit 1; }
escape() { escape() {
echo "$1" | sed 's/\([\.\+\$\*]\)/\\\1/g' echo "$1" | sed 's/\([\.\$\*]\)/\\\1/g'
} }
# set logic # set logic
...@@ -70,6 +70,14 @@ noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; } ...@@ -70,6 +70,14 @@ noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
# Git specific common functionality # Git specific common functionality
# #
git_do() {
# equivalent to git, used to indicate actions that make modifications
if flag show_commands; then
echo "git $@" >&2
fi
git "$@"
}
git_local_branches() { git branch --no-color | sed 's/^[* ] //'; } git_local_branches() { git branch --no-color | sed 's/^[* ] //'; }
git_remote_branches() { git branch -r --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_all_branches() { ( git branch --no-color; git branch -r --no-color) | sed 's/^[* ] //'; }
...@@ -97,6 +105,10 @@ git_local_branch_exists() { ...@@ -97,6 +105,10 @@ git_local_branch_exists() {
has $1 $(git_local_branches) has $1 $(git_local_branches)
} }
git_remote_branch_exists() {
has $1 $(git_remote_branches)
}
git_branch_exists() { git_branch_exists() {
has $1 $(git_all_branches) has $1 $(git_all_branches)
} }
...@@ -181,7 +193,7 @@ gitflow_is_initialized() { ...@@ -181,7 +193,7 @@ gitflow_is_initialized() {
# loading settings that can be overridden using git config # loading settings that can be overridden using git config
gitflow_load_settings() { gitflow_load_settings() {
export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1) export DOT_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
export MASTER_BRANCH=$(git config --get gitflow.branch.master) export MASTER_BRANCH=$(git config --get gitflow.branch.master)
export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop) export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop)
export ORIGIN=$(git config --get gitflow.origin || echo origin) export ORIGIN=$(git config --get gitflow.origin || echo origin)
...@@ -288,9 +300,11 @@ require_branch_absent() { ...@@ -288,9 +300,11 @@ require_branch_absent() {
} }
require_tag_absent() { require_tag_absent() {
if has $1 $(git_all_tags); then for tag in $(git_all_tags); do
if [ "$1" = "$tag" ]; then
die "Tag '$1' already exists. Pick another name." die "Tag '$1' already exists. Pick another name."
fi fi
done
} }
require_branches_equal() { require_branches_equal() {
......
shFlags/src/shflags
\ No newline at end of file
# $Id$
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
#
# shFlags -- Advanced command-line flag library for Unix shell scripts.
# http://code.google.com/p/shflags/
#
# Author: kate.ward@forestent.com (Kate Ward)
#
# This module implements something like the google-gflags library available
# from http://code.google.com/p/google-gflags/.
#
# FLAG TYPES: This is a list of the DEFINE_*'s that you can do. All flags take
# a name, default value, help-string, and optional 'short' name (one-letter
# name). Some flags have other arguments, which are described with the flag.
#
# DEFINE_string: takes any input, and intreprets it as a string.
#
# DEFINE_boolean: typically does not take any argument: say --myflag to set
# FLAGS_myflag to true, or --nomyflag to set FLAGS_myflag to false.
# Alternately, you can say
# --myflag=true or --myflag=t or --myflag=0 or
# --myflag=false or --myflag=f or --myflag=1
# Passing an option has the same affect as passing the option once.
#
# DEFINE_float: takes an input and intreprets it as a floating point number. As
# shell does not support floats per-se, the input is merely validated as
# being a valid floating point value.
#
# DEFINE_integer: takes an input and intreprets it as an integer.
#
# SPECIAL FLAGS: There are a few flags that have special meaning:
# --help (or -?) prints a list of all the flags in a human-readable fashion
# --flagfile=foo read flags from foo. (not implemented yet)
# -- as in getopt(), terminates flag-processing
#
# EXAMPLE USAGE:
#
# -- begin hello.sh --
# #! /bin/sh
# . ./shflags
# DEFINE_string name 'world' "somebody's name" n
# FLAGS "$@" || exit $?
# eval set -- "${FLAGS_ARGV}"
# echo "Hello, ${FLAGS_name}."
# -- end hello.sh --
#
# $ ./hello.sh -n Kate
# Hello, Kate.
#
# NOTE: Not all systems include a getopt version that supports long flags. On
# these systems, only short flags are recognized.
#==============================================================================
# shFlags
#
# Shared attributes:
# flags_error: last error message
# flags_return: last return value
#
# __flags_longNames: list of long names for all flags
# __flags_shortNames: list of short names for all flags
# __flags_boolNames: list of boolean flag names
#
# __flags_opts: options parsed by getopt
#
# Per-flag attributes:
# FLAGS_<flag_name>: contains value of flag named 'flag_name'
# __flags_<flag_name>_default: the default flag value
# __flags_<flag_name>_help: the flag help string
# __flags_<flag_name>_short: the flag short name
# __flags_<flag_name>_type: the flag type
#
# Notes:
# - lists of strings are space separated, and a null value is the '~' char.
# return if FLAGS already loaded
[ -n "${FLAGS_VERSION:-}" ] && return 0
FLAGS_VERSION='1.0.3'
# return values
FLAGS_TRUE=0
FLAGS_FALSE=1
FLAGS_ERROR=2
# reserved flag names
FLAGS_RESERVED='ARGC ARGV ERROR FALSE HELP PARENT RESERVED TRUE VERSION'
_flags_debug() { echo "flags:DEBUG $@" >&2; }
_flags_warn() { echo "flags:WARN $@" >&2; }
_flags_error() { echo "flags:ERROR $@" >&2; }
_flags_fatal() { echo "flags:FATAL $@" >&2; }
# specific shell checks
if [ -n "${ZSH_VERSION:-}" ]; then
setopt |grep "^shwordsplit$" >/dev/null
if [ $? -ne ${FLAGS_TRUE} ]; then
_flags_fatal 'zsh shwordsplit option is required for proper zsh operation'
exit ${FLAGS_ERROR}
fi
if [ -z "${FLAGS_PARENT:-}" ]; then
_flags_fatal "zsh does not pass \$0 through properly. please declare' \
\"FLAGS_PARENT=\$0\" before calling shFlags"
exit ${FLAGS_ERROR}
fi
fi
#
# constants
#
# getopt version
__FLAGS_GETOPT_VERS_STD=0
__FLAGS_GETOPT_VERS_ENH=1
__FLAGS_GETOPT_VERS_BSD=2
getopt >/dev/null 2>&1
case $? in
0) __FLAGS_GETOPT_VERS=${__FLAGS_GETOPT_VERS_STD} ;; # bsd getopt
2)
# TODO(kward): look into '-T' option to test the internal getopt() version
if [ "`getopt --version`" = '-- ' ]; then
__FLAGS_GETOPT_VERS=${__FLAGS_GETOPT_VERS_STD}
else
__FLAGS_GETOPT_VERS=${__FLAGS_GETOPT_VERS_ENH}
fi
;;
*)
_flags_fatal 'unable to determine getopt version'
exit ${FLAGS_ERROR}
;;
esac
# getopt optstring lengths
__FLAGS_OPTSTR_SHORT=0
__FLAGS_OPTSTR_LONG=1
__FLAGS_NULL='~'
# flag info strings
__FLAGS_INFO_DEFAULT='default'
__FLAGS_INFO_HELP='help'
__FLAGS_INFO_SHORT='short'
__FLAGS_INFO_TYPE='type'
# flag lengths
__FLAGS_LEN_SHORT=0
__FLAGS_LEN_LONG=1
# flag types
__FLAGS_TYPE_NONE=0
__FLAGS_TYPE_BOOLEAN=1
__FLAGS_TYPE_FLOAT=2
__FLAGS_TYPE_INTEGER=3
__FLAGS_TYPE_STRING=4
# set the constants readonly
__flags_constants=`set |awk -F= '/^FLAGS_/ || /^__FLAGS_/ {print $1}'`
for __flags_const in ${__flags_constants}; do
# skip certain flags
case ${__flags_const} in
FLAGS_HELP) continue ;;
FLAGS_PARENT) continue ;;
esac
# set flag readonly
if [ -z "${ZSH_VERSION:-}" ]; then
readonly ${__flags_const}
else # handle zsh
case ${ZSH_VERSION} in
[123].*) readonly ${__flags_const} ;;
*) readonly -g ${__flags_const} ;; # declare readonly constants globally
esac
fi
done
unset __flags_const __flags_constants
#
# internal variables
#
__flags_boolNames=' ' # space separated list of boolean flag names
__flags_longNames=' ' # space separated list of long flag names
__flags_shortNames=' ' # space separated list of short flag names
__flags_columns='' # screen width in columns
__flags_opts='' # temporary storage for parsed getopt flags
#------------------------------------------------------------------------------
# private functions
#
# Define a flag.
#
# Calling this function will define the following info variables for the
# specified flag:
# FLAGS_flagname - the name for this flag (based upon the long flag name)
# __flags_<flag_name>_default - the default value
# __flags_flagname_help - the help string
# __flags_flagname_short - the single letter alias
# __flags_flagname_type - the type of flag (one of __FLAGS_TYPE_*)
#
# Args:
# _flags__type: integer: internal type of flag (__FLAGS_TYPE_*)
# _flags__name: string: long flag name
# _flags__default: default flag value
# _flags__help: string: help string
# _flags__short: string: (optional) short flag name
# Returns:
# integer: success of operation, or error
_flags_define()
{
if [ $# -lt 4 ]; then
flags_error='DEFINE error: too few arguments'
flags_return=${FLAGS_ERROR}
_flags_error "${flags_error}"
return ${flags_return}
fi
_flags_type_=$1
_flags_name_=$2
_flags_default_=$3
_flags_help_=$4
_flags_short_=${5:-${__FLAGS_NULL}}
_flags_return_=${FLAGS_TRUE}
# TODO(kward): check for validity of the flag name (e.g. dashes)
# check whether the flag name is reserved
echo " ${FLAGS_RESERVED} " |grep " ${_flags_name_} " >/dev/null
if [ $? -eq 0 ]; then
flags_error="flag name (${_flags_name_}) is reserved"
_flags_return_=${FLAGS_ERROR}
fi
# require short option for getopt that don't support long options
if [ ${_flags_return_} -eq ${FLAGS_TRUE} \
-a ${__FLAGS_GETOPT_VERS} -ne ${__FLAGS_GETOPT_VERS_ENH} \
-a "${_flags_short_}" = "${__FLAGS_NULL}" ]
then
flags_error="short flag required for (${_flags_name_}) on this platform"
_flags_return_=${FLAGS_ERROR}
fi
# check for existing long name definition
if [ ${_flags_return_} -eq ${FLAGS_TRUE} ]; then
if _flags_itemInList "${_flags_name_}" \
${__flags_longNames} ${__flags_boolNames}
then
flags_error="flag name ([no]${_flags_name_}) already defined"
_flags_warn "${flags_error}"
_flags_return_=${FLAGS_FALSE}
fi
fi
# check for existing short name definition
if [ ${_flags_return_} -eq ${FLAGS_TRUE} \
-a "${_flags_short_}" != "${__FLAGS_NULL}" ]
then
if _flags_itemInList "${_flags_short_}" ${__flags_shortNames}; then
flags_error="flag short name (${_flags_short_}) already defined"
_flags_warn "${flags_error}"
_flags_return_=${FLAGS_FALSE}
fi
fi
# handle default value. note, on several occasions the 'if' portion of an
# if/then/else contains just a ':' which does nothing. a binary reversal via
# '!' is not done because it does not work on all shells.
if [ ${_flags_return_} -eq ${FLAGS_TRUE} ]; then
case ${_flags_type_} in
${__FLAGS_TYPE_BOOLEAN})
if _flags_validateBoolean "${_flags_default_}"; then
case ${_flags_default_} in
true|t|0) _flags_default_=${FLAGS_TRUE} ;;
false|f|1) _flags_default_=${FLAGS_FALSE} ;;
esac
else
flags_error="invalid default flag value '${_flags_default_}'"
_flags_return_=${FLAGS_ERROR}
fi
;;
${__FLAGS_TYPE_FLOAT})
if _flags_validateFloat "${_flags_default_}"; then
:
else
flags_error="invalid default flag value '${_flags_default_}'"
_flags_return_=${FLAGS_ERROR}
fi
;;
${__FLAGS_TYPE_INTEGER})
if _flags_validateInteger "${_flags_default_}"; then
:
else
flags_error="invalid default flag value '${_flags_default_}'"
_flags_return_=${FLAGS_ERROR}
fi
;;
${__FLAGS_TYPE_STRING}) ;; # everything in shell is a valid string
*)
flags_error="unrecognized flag type '${_flags_type_}'"
_flags_return_=${FLAGS_ERROR}
;;
esac
fi
if [ ${_flags_return_} -eq ${FLAGS_TRUE} ]; then
# store flag information
eval "FLAGS_${_flags_name_}='${_flags_default_}'"
eval "__flags_${_flags_name_}_${__FLAGS_INFO_TYPE}=${_flags_type_}"
eval "__flags_${_flags_name_}_${__FLAGS_INFO_DEFAULT}=\
\"${_flags_default_}\""
eval "__flags_${_flags_name_}_${__FLAGS_INFO_HELP}=\"${_flags_help_}\""
eval "__flags_${_flags_name_}_${__FLAGS_INFO_SHORT}='${_flags_short_}'"
# append flag name(s) to list of names
__flags_longNames="${__flags_longNames}${_flags_name_} "
__flags_shortNames="${__flags_shortNames}${_flags_short_} "
[ ${_flags_type_} -eq ${__FLAGS_TYPE_BOOLEAN} ] && \
__flags_boolNames="${__flags_boolNames}no${_flags_name_} "
fi
flags_return=${_flags_return_}
unset _flags_default_ _flags_help_ _flags_name_ _flags_return_ _flags_short_ \
_flags_type_
[ ${flags_return} -eq ${FLAGS_ERROR} ] && _flags_error "${flags_error}"
return ${flags_return}
}
# Return valid getopt options using currently defined list of long options.
#
# This function builds a proper getopt option string for short (and long)
# options, using the current list of long options for reference.
#
# Args:
# _flags_optStr: integer: option string type (__FLAGS_OPTSTR_*)
# Output:
# string: generated option string for getopt
# Returns:
# boolean: success of operation (always returns True)
_flags_genOptStr()
{
_flags_optStrType_=$1
_flags_opts_=''
for _flags_flag_ in ${__flags_longNames}; do
_flags_type_=`_flags_getFlagInfo ${_flags_flag_} ${__FLAGS_INFO_TYPE}`
case ${_flags_optStrType_} in
${__FLAGS_OPTSTR_SHORT})
_flags_shortName_=`_flags_getFlagInfo \
${_flags_flag_} ${__FLAGS_INFO_SHORT}`
if [ "${_flags_shortName_}" != "${__FLAGS_NULL}" ]; then
_flags_opts_="${_flags_opts_}${_flags_shortName_}"
# getopt needs a trailing ':' to indicate a required argument
[ ${_flags_type_} -ne ${__FLAGS_TYPE_BOOLEAN} ] && \
_flags_opts_="${_flags_opts_}:"
fi
;;
${__FLAGS_OPTSTR_LONG})
_flags_opts_="${_flags_opts_:+${_flags_opts_},}${_flags_flag_}"
# getopt needs a trailing ':' to indicate a required argument
[ ${_flags_type_} -ne ${__FLAGS_TYPE_BOOLEAN} ] && \
_flags_opts_="${_flags_opts_}:"
;;
esac
done
echo "${_flags_opts_}"
unset _flags_flag_ _flags_opts_ _flags_optStrType_ _flags_shortName_ \
_flags_type_
return ${FLAGS_TRUE}
}
# Returns flag details based on a flag name and flag info.
#
# Args:
# string: long flag name
# string: flag info (see the _flags_define function for valid info types)
# Output:
# string: value of dereferenced flag variable
# Returns:
# integer: one of FLAGS_{TRUE|FALSE|ERROR}
_flags_getFlagInfo()
{
_flags_name_=$1
_flags_info_=$2
_flags_nameVar_="__flags_${_flags_name_}_${_flags_info_}"
_flags_strToEval_="_flags_value_=\"\${${_flags_nameVar_}:-}\""
eval "${_flags_strToEval_}"
if [ -n "${_flags_value_}" ]; then
flags_return=${FLAGS_TRUE}
else
# see if the _flags_name_ variable is a string as strings can be empty...
# note: the DRY principle would say to have this function call itself for
# the next three lines, but doing so results in an infinite loop as an
# invalid _flags_name_ will also not have the associated _type variable.
# Because it doesn't (it will evaluate to an empty string) the logic will
# try to find the _type variable of the _type variable, and so on. Not so
# good ;-)
_flags_typeVar_="__flags_${_flags_name_}_${__FLAGS_INFO_TYPE}"
_flags_strToEval_="_flags_type_=\"\${${_flags_typeVar_}:-}\""
eval "${_flags_strToEval_}"
if [ "${_flags_type_}" = "${__FLAGS_TYPE_STRING}" ]; then
flags_return=${FLAGS_TRUE}
else
flags_return=${FLAGS_ERROR}
flags_error="invalid flag name (${_flags_nameVar_})"
fi
fi
echo "${_flags_value_}"
unset _flags_info_ _flags_name_ _flags_strToEval_ _flags_type_ _flags_value_ \
_flags_nameVar_ _flags_typeVar_
[ ${flags_return} -eq ${FLAGS_ERROR} ] && _flags_error "${flags_error}"
return ${flags_return}
}
# check for presense of item in a list. passed a string (e.g. 'abc'), this
# function will determine if the string is present in the list of strings (e.g.
# ' foo bar abc ').
#
# Args:
# _flags__str: string: string to search for in a list of strings
# unnamed: list: list of strings
# Returns:
# boolean: true if item is in the list
_flags_itemInList()
{
_flags_str_=$1
shift
echo " ${*:-} " |grep " ${_flags_str_} " >/dev/null
if [ $? -eq 0 ]; then
flags_return=${FLAGS_TRUE}
else
flags_return=${FLAGS_FALSE}
fi
unset _flags_str_
return ${flags_return}
}
# Returns the width of the current screen.
#
# Output:
# integer: width in columns of the current screen.
_flags_columns()
{
if [ -z "${__flags_columns}" ]; then
# determine the value and store it
if eval stty size >/dev/null 2>&1; then
# stty size worked :-)
set -- `stty size`
__flags_columns=$2
elif eval tput cols >/dev/null 2>&1; then
set -- `tput cols`
__flags_columns=$1
else
__flags_columns=80 # default terminal width
fi
fi
echo ${__flags_columns}
}
# Validate a boolean.
#
# Args:
# _flags__bool: boolean: value to validate
# Returns:
# bool: true if the value is a valid boolean
_flags_validateBoolean()
{
_flags_bool_=$1
flags_return=${FLAGS_TRUE}
case "${_flags_bool_}" in
true|t|0) ;;
false|f|1) ;;
*) flags_return=${FLAGS_FALSE} ;;
esac
unset _flags_bool_
return ${flags_return}
}
# Validate a float.
#
# Args:
# _flags__float: float: value to validate
# Returns:
# bool: true if the value is a valid float
_flags_validateFloat()
{
_flags_float_=$1
if _flags_validateInteger ${_flags_float_}; then
flags_return=${FLAGS_TRUE}
else
flags_return=${FLAGS_TRUE}
case ${_flags_float_} in
-*) # negative floats
_flags_test_=`expr "${_flags_float_}" : '\(-[0-9][0-9]*\.[0-9][0-9]*\)'`
;;
*) # positive floats
_flags_test_=`expr "${_flags_float_}" : '\([0-9][0-9]*\.[0-9][0-9]*\)'`
;;
esac
[ "${_flags_test_}" != "${_flags_float_}" ] && flags_return=${FLAGS_FALSE}
fi
unset _flags_float_ _flags_test_
return ${flags_return}
}
# Validate an integer.
#
# Args:
# _flags__integer: interger: value to validate
# Returns:
# bool: true if the value is a valid integer
_flags_validateInteger()
{
_flags_int_=$1
flags_return=${FLAGS_TRUE}
case ${_flags_int_} in
-*) # negative ints
_flags_test_=`expr "${_flags_int_}" : '\(-[0-9][0-9]*\)'`
;;
*) # positive ints
_flags_test_=`expr "${_flags_int_}" : '\([0-9][0-9]*\)'`
;;
esac
[ "${_flags_test_}" != "${_flags_int_}" ] && flags_return=${FLAGS_FALSE}
unset _flags_int_ _flags_test_
return ${flags_return}
}
# Parse command-line options using the standard getopt.
#
# Note: the flag options are passed around in the global __flags_opts so that
# the formatting is not lost due to shell parsing and such.
#
# Args:
# @: varies: command-line options to parse
# Returns:
# integer: a FLAGS success condition
_flags_getoptStandard()
{
flags_return=${FLAGS_TRUE}
_flags_shortOpts_=`_flags_genOptStr ${__FLAGS_OPTSTR_SHORT}`
# check for spaces in passed options
for _flags_opt_ in "$@"; do
# note: the silliness with the x's is purely for ksh93 on Ubuntu 6.06
_flags_match_=`echo "x${_flags_opt_}x" |sed 's/ //g'`
if [ "${_flags_match_}" != "x${_flags_opt_}x" ]; then
flags_error='the available getopt does not support spaces in options'
flags_return=${FLAGS_ERROR}
break
fi
done
if [ ${flags_return} -eq ${FLAGS_TRUE} ]; then
__flags_opts=`getopt ${_flags_shortOpts_} $@ 2>&1`
_flags_rtrn_=$?
if [ ${_flags_rtrn_} -ne ${FLAGS_TRUE} ]; then
_flags_warn "${__flags_opts}"
flags_error='unable to parse provided options with getopt.'
flags_return=${FLAGS_ERROR}
fi
fi
unset _flags_match_ _flags_opt_ _flags_rtrn_ _flags_shortOpts_
return ${flags_return}
}
# Parse command-line options using the enhanced getopt.
#
# Note: the flag options are passed around in the global __flags_opts so that
# the formatting is not lost due to shell parsing and such.
#
# Args:
# @: varies: command-line options to parse
# Returns:
# integer: a FLAGS success condition
_flags_getoptEnhanced()
{
flags_return=${FLAGS_TRUE}
_flags_shortOpts_=`_flags_genOptStr ${__FLAGS_OPTSTR_SHORT}`
_flags_boolOpts_=`echo "${__flags_boolNames}" \
|sed 's/^ *//;s/ *$//;s/ /,/g'`
_flags_longOpts_=`_flags_genOptStr ${__FLAGS_OPTSTR_LONG}`
__flags_opts=`getopt \
-o ${_flags_shortOpts_} \
-l "${_flags_longOpts_},${_flags_boolOpts_}" \
-- "$@" 2>&1`
_flags_rtrn_=$?
if [ ${_flags_rtrn_} -ne ${FLAGS_TRUE} ]; then
_flags_warn "${__flags_opts}"
flags_error='unable to parse provided options with getopt.'
flags_return=${FLAGS_ERROR}
fi
unset _flags_boolOpts_ _flags_longOpts_ _flags_rtrn_ _flags_shortOpts_
return ${flags_return}
}
# Dynamically parse a getopt result and set appropriate variables.
#
# This function does the actual conversion of getopt output and runs it through
# the standard case structure for parsing. The case structure is actually quite
# dynamic to support any number of flags.
#
# Args:
# argc: int: original command-line argument count
# @: varies: output from getopt parsing
# Returns:
# integer: a FLAGS success condition
_flags_parseGetopt()
{
_flags_argc_=$1
shift
flags_return=${FLAGS_TRUE}
if [ ${__FLAGS_GETOPT_VERS} -ne ${__FLAGS_GETOPT_VERS_ENH} ]; then
set -- $@
else
# note the quotes around the `$@' -- they are essential!
eval set -- "$@"
fi
# provide user with number of arguments to shift by later
# NOTE: the FLAGS_ARGC variable is obsolete as of 1.0.3 because it does not
# properly give user access to non-flag arguments mixed in between flag
# arguments. Its usage was replaced by FLAGS_ARGV, and it is being kept only
# for backwards compatibility reasons.
FLAGS_ARGC=`expr $# - 1 - ${_flags_argc_}`
# handle options. note options with values must do an additional shift
while true; do
_flags_opt_=$1
_flags_arg_=${2:-}
_flags_type_=${__FLAGS_TYPE_NONE}
_flags_name_=''
# determine long flag name
case "${_flags_opt_}" in
--) shift; break ;; # discontinue option parsing
--*) # long option
_flags_opt_=`expr "${_flags_opt_}" : '--\(.*\)'`
_flags_len_=${__FLAGS_LEN_LONG}
if _flags_itemInList "${_flags_opt_}" ${__flags_longNames}; then
_flags_name_=${_flags_opt_}
else
# check for negated long boolean version
if _flags_itemInList "${_flags_opt_}" ${__flags_boolNames}; then
_flags_name_=`expr "${_flags_opt_}" : 'no\(.*\)'`
_flags_type_=${__FLAGS_TYPE_BOOLEAN}
_flags_arg_=${__FLAGS_NULL}
fi
fi
;;
-*) # short option
_flags_opt_=`expr "${_flags_opt_}" : '-\(.*\)'`
_flags_len_=${__FLAGS_LEN_SHORT}
if _flags_itemInList "${_flags_opt_}" ${__flags_shortNames}; then
# yes. match short name to long name. note purposeful off-by-one
# (too high) with awk calculations.
_flags_pos_=`echo "${__flags_shortNames}" \
|awk 'BEGIN{RS=" ";rn=0}$0==e{rn=NR}END{print rn}' \
e=${_flags_opt_}`
_flags_name_=`echo "${__flags_longNames}" \
|awk 'BEGIN{RS=" "}rn==NR{print $0}' rn="${_flags_pos_}"`
fi
;;
esac
# die if the flag was unrecognized
if [ -z "${_flags_name_}" ]; then
flags_error="unrecognized option (${_flags_opt_})"
flags_return=${FLAGS_ERROR}
break
fi
# set new flag value
[ ${_flags_type_} -eq ${__FLAGS_TYPE_NONE} ] && \
_flags_type_=`_flags_getFlagInfo \
"${_flags_name_}" ${__FLAGS_INFO_TYPE}`
case ${_flags_type_} in
${__FLAGS_TYPE_BOOLEAN})
if [ ${_flags_len_} -eq ${__FLAGS_LEN_LONG} ]; then
if [ "${_flags_arg_}" != "${__FLAGS_NULL}" ]; then
eval "FLAGS_${_flags_name_}=${FLAGS_TRUE}"
else
eval "FLAGS_${_flags_name_}=${FLAGS_FALSE}"
fi
else
_flags_strToEval_="_flags_val_=\
\${__flags_${_flags_name_}_${__FLAGS_INFO_DEFAULT}}"
eval "${_flags_strToEval_}"
if [ ${_flags_val_} -eq ${FLAGS_FALSE} ]; then
eval "FLAGS_${_flags_name_}=${FLAGS_TRUE}"
else
eval "FLAGS_${_flags_name_}=${FLAGS_FALSE}"
fi
fi
;;
${__FLAGS_TYPE_FLOAT})
if _flags_validateFloat "${_flags_arg_}"; then
eval "FLAGS_${_flags_name_}='${_flags_arg_}'"
else
flags_error="invalid float value (${_flags_arg_})"
flags_return=${FLAGS_ERROR}
break
fi
;;
${__FLAGS_TYPE_INTEGER})
if _flags_validateInteger "${_flags_arg_}"; then
eval "FLAGS_${_flags_name_}='${_flags_arg_}'"
else
flags_error="invalid integer value (${_flags_arg_})"
flags_return=${FLAGS_ERROR}
break
fi
;;
${__FLAGS_TYPE_STRING})
eval "FLAGS_${_flags_name_}='${_flags_arg_}'"
;;
esac
# handle special case help flag
if [ "${_flags_name_}" = 'help' ]; then
if [ ${FLAGS_help} -eq ${FLAGS_TRUE} ]; then
flags_help
flags_error='help requested'
flags_return=${FLAGS_FALSE}
break
fi
fi
# shift the option and non-boolean arguements out.
shift
[ ${_flags_type_} != ${__FLAGS_TYPE_BOOLEAN} ] && shift
done
# give user back non-flag arguments
FLAGS_ARGV=''
while [ $# -gt 0 ]; do
FLAGS_ARGV="${FLAGS_ARGV:+${FLAGS_ARGV} }'$1'"
shift
done
unset _flags_arg_ _flags_len_ _flags_name_ _flags_opt_ _flags_pos_ \
_flags_strToEval_ _flags_type_ _flags_val_
return ${flags_return}
}
#------------------------------------------------------------------------------
# public functions
#
# A basic boolean flag. Boolean flags do not take any arguments, and their
# value is either 1 (false) or 0 (true). For long flags, the false value is
# specified on the command line by prepending the word 'no'. With short flags,
# the presense of the flag toggles the current value between true and false.
# Specifying a short boolean flag twice on the command results in returning the
# value back to the default value.
#
# A default value is required for boolean flags.
#
# For example, lets say a Boolean flag was created whose long name was 'update'
# and whose short name was 'x', and the default value was 'false'. This flag
# could be explicitly set to 'true' with '--update' or by '-x', and it could be
# explicitly set to 'false' with '--noupdate'.
DEFINE_boolean() { _flags_define ${__FLAGS_TYPE_BOOLEAN} "$@"; }
# Other basic flags.
DEFINE_float() { _flags_define ${__FLAGS_TYPE_FLOAT} "$@"; }
DEFINE_integer() { _flags_define ${__FLAGS_TYPE_INTEGER} "$@"; }
DEFINE_string() { _flags_define ${__FLAGS_TYPE_STRING} "$@"; }
# Parse the flags.
#
# Args:
# unnamed: list: command-line flags to parse
# Returns:
# integer: success of operation, or error
FLAGS()
{
# define a standard 'help' flag if one isn't already defined
[ -z "${__flags_help_type:-}" ] && \
DEFINE_boolean 'help' false 'show this help' 'h'
# parse options
if [ $# -gt 0 ]; then
if [ ${__FLAGS_GETOPT_VERS} -ne ${__FLAGS_GETOPT_VERS_ENH} ]; then
_flags_getoptStandard "$@"
else
_flags_getoptEnhanced "$@"
fi
flags_return=$?
else
# nothing passed; won't bother running getopt
__flags_opts='--'
flags_return=${FLAGS_TRUE}
fi
if [ ${flags_return} -eq ${FLAGS_TRUE} ]; then
_flags_parseGetopt $# "${__flags_opts}"
flags_return=$?
fi
[ ${flags_return} -eq ${FLAGS_ERROR} ] && _flags_fatal "${flags_error}"
return ${flags_return}
}
# This is a helper function for determining the `getopt` version for platforms
# where the detection isn't working. It simply outputs debug information that
# can be included in a bug report.
#
# Args:
# none
# Output:
# debug info that can be included in a bug report
# Returns:
# nothing
flags_getoptInfo()
{
# platform info
_flags_debug "uname -a: `uname -a`"
_flags_debug "PATH: ${PATH}"
# shell info
if [ -n "${BASH_VERSION:-}" ]; then
_flags_debug 'shell: bash'
_flags_debug "BASH_VERSION: ${BASH_VERSION}"
elif [ -n "${ZSH_VERSION:-}" ]; then
_flags_debug 'shell: zsh'
_flags_debug "ZSH_VERSION: ${ZSH_VERSION}"
fi
# getopt info
getopt >/dev/null
_flags_getoptReturn=$?
_flags_debug "getopt return: ${_flags_getoptReturn}"
_flags_debug "getopt --version: `getopt --version 2>&1`"
unset _flags_getoptReturn
}
# Returns whether the detected getopt version is the enhanced version.
#
# Args:
# none
# Output:
# none
# Returns:
# bool: true if getopt is the enhanced version
flags_getoptIsEnh()
{
test ${__FLAGS_GETOPT_VERS} -eq ${__FLAGS_GETOPT_VERS_ENH}
}
# Returns whether the detected getopt version is the standard version.
#
# Args:
# none
# Returns:
# bool: true if getopt is the standard version
flags_getoptIsStd()
{
test ${__FLAGS_GETOPT_VERS} -eq ${__FLAGS_GETOPT_VERS_STD}
}
# This is effectively a 'usage()' function. It prints usage information and
# exits the program with ${FLAGS_FALSE} if it is ever found in the command line
# arguments. Note this function can be overridden so other apps can define
# their own --help flag, replacing this one, if they want.
#
# Args:
# none
# Returns:
# integer: success of operation (always returns true)
flags_help()
{
if [ -n "${FLAGS_HELP:-}" ]; then
echo "${FLAGS_HELP}" >&2
else
echo "USAGE: ${FLAGS_PARENT:-$0} [flags] args" >&2
fi
if [ -n "${__flags_longNames}" ]; then
echo 'flags:' >&2
for flags_name_ in ${__flags_longNames}; do
flags_flagStr_=''
flags_boolStr_=''
flags_default_=`_flags_getFlagInfo \
"${flags_name_}" ${__FLAGS_INFO_DEFAULT}`
flags_help_=`_flags_getFlagInfo \
"${flags_name_}" ${__FLAGS_INFO_HELP}`
flags_short_=`_flags_getFlagInfo \
"${flags_name_}" ${__FLAGS_INFO_SHORT}`
flags_type_=`_flags_getFlagInfo \
"${flags_name_}" ${__FLAGS_INFO_TYPE}`
[ "${flags_short_}" != "${__FLAGS_NULL}" ] \
&& flags_flagStr_="-${flags_short_}"
if [ ${__FLAGS_GETOPT_VERS} -eq ${__FLAGS_GETOPT_VERS_ENH} ]; then
[ "${flags_short_}" != "${__FLAGS_NULL}" ] \
&& flags_flagStr_="${flags_flagStr_},"
[ ${flags_type_} -eq ${__FLAGS_TYPE_BOOLEAN} ] \
&& flags_boolStr_='[no]'
flags_flagStr_="${flags_flagStr_}--${flags_boolStr_}${flags_name_}:"
fi
case ${flags_type_} in
${__FLAGS_TYPE_BOOLEAN})
if [ ${flags_default_} -eq ${FLAGS_TRUE} ]; then
flags_defaultStr_='true'
else
flags_defaultStr_='false'
fi
;;
${__FLAGS_TYPE_FLOAT}|${__FLAGS_TYPE_INTEGER})
flags_defaultStr_=${flags_default_} ;;
${__FLAGS_TYPE_STRING}) flags_defaultStr_="'${flags_default_}'" ;;
esac
flags_defaultStr_="(default: ${flags_defaultStr_})"
flags_helpStr_=" ${flags_flagStr_} ${flags_help_} ${flags_defaultStr_}"
flags_helpStrLen_=`expr "${flags_helpStr_}" : '.*'`
flags_columns_=`_flags_columns`
if [ ${flags_helpStrLen_} -lt ${flags_columns_} ]; then
echo "${flags_helpStr_}" >&2
else
echo " ${flags_flagStr_} ${flags_help_}" >&2
# note: the silliness with the x's is purely for ksh93 on Ubuntu 6.06
# because it doesn't like empty strings when used in this manner.
flags_emptyStr_="`echo \"x${flags_flagStr_}x\" \
|awk '{printf "%"length($0)-2"s", ""}'`"
flags_helpStr_=" ${flags_emptyStr_} ${flags_defaultStr_}"
flags_helpStrLen_=`expr "${flags_helpStr_}" : '.*'`
if [ ${__FLAGS_GETOPT_VERS} -eq ${__FLAGS_GETOPT_VERS_STD} \
-o ${flags_helpStrLen_} -lt ${flags_columns_} ]; then
# indented to match help string
echo "${flags_helpStr_}" >&2
else
# indented four from left to allow for longer defaults as long flag
# names might be used too, making things too long
echo " ${flags_defaultStr_}" >&2
fi
fi
done
fi
unset flags_boolStr_ flags_default_ flags_defaultStr_ flags_emptyStr_ \
flags_flagStr_ flags_help_ flags_helpStr flags_helpStrLen flags_name_ \
flags_columns_ flags_short_ flags_type_
return ${FLAGS_TRUE}
}
# Reset shflags back to an uninitialized state.
#
# Args:
# none
# Returns:
# nothing
flags_reset()
{
for flags_name_ in ${__flags_longNames}; do
flags_strToEval_="unset FLAGS_${flags_name_}"
for flags_type_ in \
${__FLAGS_INFO_DEFAULT} \
${__FLAGS_INFO_HELP} \
${__FLAGS_INFO_SHORT} \
${__FLAGS_INFO_TYPE}
do
flags_strToEval_=\
"${flags_strToEval_} __flags_${flags_name_}_${flags_type_}"
done
eval ${flags_strToEval_}
done
# reset internal variables
__flags_boolNames=' '
__flags_longNames=' '
__flags_shortNames=' '
unset flags_name_ flags_type_ flags_strToEval_
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment