From 5fcd29025e754fbedc4c64fb9bb217469d2c0c82 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Tue, 22 Nov 2011 23:55:50 +0100 Subject: [PATCH 01/16] Warn when source-only functions are called in executed context --- doc/error_codes.md | 3 +++ vcsh | 9 +++++++++ 2 files changed, 12 insertions(+) create mode 100644 doc/error_codes.md diff --git a/doc/error_codes.md b/doc/error_codes.md new file mode 100644 index 0000000..06d760d --- /dev/null +++ b/doc/error_codes.md @@ -0,0 +1,3 @@ +* 0: OK +* 1: Generic error +* 10: source-only command was called without vcsh being sourced diff --git a/vcsh b/vcsh index 6fa6644..4ba30e5 100755 --- a/vcsh +++ b/vcsh @@ -5,6 +5,13 @@ [ -z "$VCSH_BASE" ] && VCSH_BASE="$XDG_CONFIG_HOME/vcsh/repo.d" SELF=$(basename $0) +if [ "$SELF" = 'bash' ] || + [ "$SELF" = 'dash' ] || + [ "$SELF" = 'sh' ] || + [ "$SELF" = 'zsh' ]; then + SELF='vcsh' + VCSH_SOURCED=1 +fi help() { @@ -176,6 +183,7 @@ elif [ "$VCSH_COMMAND" = 'exit' ]; then # unset VCSH_NO_IGNORE_EOF # setopt NO_IGNORE_EOF # fi + [ -z "$VCSH_SOURCED" ] && echo "$SELF $VCSH_COMMAND: You need to source vcsh if you want to run in this mode" && return 10 leave # [ -n "$ZSH_VERSION" ] && [ "$USER" = richih ] && buildPS1 verbose "exit end" @@ -251,6 +259,7 @@ elif [ "$VCSH_COMMAND" = 'use' ]; then # zle -N vcsh_exit # bindkey '^d' 'vcsh_exit' # fi + [ -z "$VCSH_SOURCED" ] && echo "$SELF $VCSH_COMMAND: You need to source vcsh if you want to run in this mode" && return 10 use || return $? # [ -n "$ZSH_VERSION" ] && [ "$USER" = richih ] && buildPS1 verbose "use end" -- 2.39.5 From e73e39bb031b0958dfe7f01686f7cc1467a09c6d Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Wed, 23 Nov 2011 00:01:37 +0100 Subject: [PATCH 02/16] Update docs --- doc/vcsh.1.ronn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/vcsh.1.ronn b/doc/vcsh.1.ronn index 744a0b3..015e76e 100644 --- a/doc/vcsh.1.ronn +++ b/doc/vcsh.1.ronn @@ -5,7 +5,7 @@ vcsh(1) - manage and sync config files via git `vcsh` clone [] -`vcsh` delete +`vcsh` delete `vcsh` exit @@ -17,7 +17,7 @@ vcsh(1) - manage and sync config files via git `vcsh` run -`vcsh` seed-gitignore +`vcsh` seed-gitignore `vcsh` use -- 2.39.5 From 4424028c0faa46b809fff726c8ec3d96a8e34d32 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Wed, 23 Nov 2011 00:04:17 +0100 Subject: [PATCH 03/16] Introduce `vcsh enter` --- _vcsh | 1 + doc/vcsh.1.ronn | 9 +++++++-- vcsh | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/_vcsh b/_vcsh index 9f5b006..4249144 100644 --- a/_vcsh +++ b/_vcsh @@ -5,6 +5,7 @@ _arguments \ clone\:"clone from repo" help\:"display help" delete\:"delete repo" + enter\:"Enter repo; spawn new \$SHELL" exit\:"Exit repo; unset" init\:"init & clone from repo" list\:"list all repos" diff --git a/doc/vcsh.1.ronn b/doc/vcsh.1.ronn index 015e76e..71fbc6a 100644 --- a/doc/vcsh.1.ronn +++ b/doc/vcsh.1.ronn @@ -7,6 +7,8 @@ vcsh(1) - manage and sync config files via git `vcsh` delete +`vcsh` enter + `vcsh` exit `vcsh` help @@ -50,8 +52,11 @@ A sample configuration for `vcsh` and `mr` can be found at * delete: Delete an existing repository. +* enter: + Enter repository; spawn new <$SHELL>. + * exit: - Exit repository; unset ENV + Exit repository; unset ENV. * help: Display help. @@ -70,7 +75,7 @@ A sample configuration for `vcsh` and `mr` can be found at Seed .gitignore.d/ from git ls-files. * use: - Use repository; set ENV + Use repository; set ENV. * : Shortcut to run `vcsh` on a repo. Will prepend `git` to by itself. diff --git a/vcsh b/vcsh index 4ba30e5..b5e703e 100755 --- a/vcsh +++ b/vcsh @@ -21,6 +21,7 @@ help() { [] Clone from an existing repository help Display this help text delete Delete an existing repository + enter Enter repository; spawn new $SHELL exit Exit repository; unset ENV init Initialize a new repository list List all repositories @@ -89,6 +90,7 @@ if [ "$1" = 'clone' ]; then export VCSH_REPO_NAME export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" elif [ "$1" = 'delete' ] || + [ "$1" = 'enter' ] || [ "$1" = 'init' ] || [ "$1" = 'run' ] || [ "$1" = 'seed-gitignore' ] || @@ -177,6 +179,13 @@ To continue, type \"Yes, do as I say\"" cd "$old_dir" verbose "delete end" +elif [ "$VCSH_COMMAND" = 'enter' ]; then + verbose "enter begin" + use || return $? + $SHELL + leave + verbose "enter end" + elif [ "$VCSH_COMMAND" = 'exit' ]; then verbose "exit begin" # if [ -n "$ZSH_VERSION" ] && [ "$VCSH_NO_IGNORE_EOF" = '1' ]; then -- 2.39.5 From a60f8741490b0cc7292553e030ead9f9d86271a9 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Wed, 23 Nov 2011 00:04:33 +0100 Subject: [PATCH 04/16] Update docs --- _vcsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_vcsh b/_vcsh index 4249144..07376d2 100644 --- a/_vcsh +++ b/_vcsh @@ -6,7 +6,7 @@ _arguments \ help\:"display help" delete\:"delete repo" enter\:"Enter repo; spawn new \$SHELL" - exit\:"Exit repo; unset" + exit\:"Exit repo; unset ENV" init\:"init & clone from repo" list\:"list all repos" run\:"run command on repo" -- 2.39.5 From ccb770603b504ce05c8abc0282b4b3caed70fe8f Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Wed, 23 Nov 2011 12:46:38 +0100 Subject: [PATCH 05/16] Fix `vcsh clone` Forgot to set VCSH_COMMAND after the refactoring --- vcsh | 1 + 1 file changed, 1 insertion(+) diff --git a/vcsh b/vcsh index b5e703e..c8618f1 100755 --- a/vcsh +++ b/vcsh @@ -83,6 +83,7 @@ leave() { if [ "$1" = 'clone' ]; then + export VCSH_COMMAND="$1" GIT_REMOTE="$2" export GIT_REMOTE VCSH_REPO_NAME="$3" -- 2.39.5 From 663af6efe8d81d4386127b8fbd05320f44988658 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Wed, 23 Nov 2011 18:02:44 +0100 Subject: [PATCH 06/16] Re-arrange functions --- vcsh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/vcsh b/vcsh index c8618f1..cb9f216 100755 --- a/vcsh +++ b/vcsh @@ -44,18 +44,6 @@ verbose() { if [ -n "$VCSH_DEBUG" ] || [ -n "$VCSH_VERBOSE" ]; then echo "$SELF: verbose: $@"; fi } -use() { - verbose "use() begin" - if [ ! -d "$GIT_DIR" ]; then - echo E: no repository found for "$VCSH_REPO_NAME" >&2 - return 1 - fi - export GIT_DIR - export GIT_WORK_TREE="$(git config --get core.worktree)" - export VCSH_DIRECTORY="$VCSH_REPO_NAME" - verbose "use() end" -} - init() { verbose "init() begin" [ -e "$GIT_DIR" ] && @@ -81,6 +69,18 @@ leave() { unset VCSH_DIRECTORY } +use() { + verbose "use() begin" + if [ ! -d "$GIT_DIR" ]; then + echo E: no repository found for "$VCSH_REPO_NAME" >&2 + return 1 + fi + export GIT_DIR + export GIT_WORK_TREE="$(git config --get core.worktree)" + export VCSH_DIRECTORY="$VCSH_REPO_NAME" + verbose "use() end" +} + if [ "$1" = 'clone' ]; then export VCSH_COMMAND="$1" -- 2.39.5 From 5c2fca9c11b587c2c34ca52f65872056aa9c1b74 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Wed, 23 Nov 2011 18:38:36 +0100 Subject: [PATCH 07/16] Introduce `vcsh setup` --- _vcsh | 1 + doc/vcsh.1.ronn | 5 +++++ vcsh | 23 ++++++++++++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/_vcsh b/_vcsh index 07376d2..d5ad914 100644 --- a/_vcsh +++ b/_vcsh @@ -11,6 +11,7 @@ _arguments \ list\:"list all repos" run\:"run command on repo" seed-gitignore\:"seed .gitignore.d/foo from git ls-files" + setup\:"set up repo with recommended settings" use\:"Use repo; set ENV" \\:"Run git command directly" ))' diff --git a/doc/vcsh.1.ronn b/doc/vcsh.1.ronn index 71fbc6a..3ee97a4 100644 --- a/doc/vcsh.1.ronn +++ b/doc/vcsh.1.ronn @@ -21,6 +21,8 @@ vcsh(1) - manage and sync config files via git `vcsh` seed-gitignore +`vcsh` setup + `vcsh` use `vcsh` @@ -74,6 +76,9 @@ A sample configuration for `vcsh` and `mr` can be found at * seed-gitignore: Seed .gitignore.d/ from git ls-files. +* setup: + Set up repository with recommended settings. + * use: Use repository; set ENV. diff --git a/vcsh b/vcsh index cb9f216..c05210d 100755 --- a/vcsh +++ b/vcsh @@ -30,6 +30,7 @@ help() { seed-gitignore \\ Seed .gitignore.d/ from git ls-files + setup Set up repository with recommended settings use Use repository; set ENV Special command that allows you to run git commands @@ -44,6 +45,14 @@ verbose() { if [ -n "$VCSH_DEBUG" ] || [ -n "$VCSH_VERBOSE" ]; then echo "$SELF: verbose: $@"; fi } +setup() { + git config core.worktree "$GIT_WORK_TREE" + git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME" + git config vcsh.vcsh 'true' + touch "$HOME/.gitignore.d/$VCSH_REPO_NAME" + git add "$HOME/.gitignore.d/$VCSH_REPO_NAME" +} + init() { verbose "init() begin" [ -e "$GIT_DIR" ] && @@ -56,10 +65,7 @@ init() { return 1) || return $? cd "$GIT_WORK_TREE" git init - git config core.worktree "$GIT_WORK_TREE" - git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME" - touch "$HOME/.gitignore.d/$VCSH_REPO_NAME" - git add "$HOME/.gitignore.d/$VCSH_REPO_NAME" + setup verbose "init() end" } @@ -95,6 +101,7 @@ elif [ "$1" = 'delete' ] || [ "$1" = 'init' ] || [ "$1" = 'run' ] || [ "$1" = 'seed-gitignore' ] || + [ "$1" = 'setup' ] || [ "$1" = 'use' ]; then [ -z $2 ] && echo "$SELF $1: error: please specify repository to work on" && return 1 export VCSH_COMMAND="$1" @@ -226,7 +233,6 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then # Going back into old directory at the end in case `vcsh use` is reactivated. old_dir="$PWD" cd "$HOME" - git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME" gitignores=$(for file in $(git ls-files); do while true; do echo $file; new="${file%/*}" @@ -255,6 +261,13 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then cd "$old_dir" verbose "seed-gitignore end" +elif [ "$VCSH_COMMAND" = 'setup' ]; then + verbose "seed-gitignore begin" + use || return $? + setup || return $? + leave + verbose "seed-gitignore end" + elif [ "$VCSH_COMMAND" = 'use' ]; then verbose "use begin" # if [ -n "$ZSH_VERSION" ]; then -- 2.39.5 From 7ee75c01a94e3bbd03e809b66c0e17fb2e6791c0 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Thu, 24 Nov 2011 19:42:10 +0100 Subject: [PATCH 08/16] Remove `use` and `exit` --- _vcsh | 2 -- doc/vcsh.1.ronn | 10 --------- vcsh | 58 ++----------------------------------------------- 3 files changed, 2 insertions(+), 68 deletions(-) diff --git a/_vcsh b/_vcsh index d5ad914..d6850ce 100644 --- a/_vcsh +++ b/_vcsh @@ -6,12 +6,10 @@ _arguments \ help\:"display help" delete\:"delete repo" enter\:"Enter repo; spawn new \$SHELL" - exit\:"Exit repo; unset ENV" init\:"init & clone from repo" list\:"list all repos" run\:"run command on repo" seed-gitignore\:"seed .gitignore.d/foo from git ls-files" setup\:"set up repo with recommended settings" - use\:"Use repo; set ENV" \\:"Run git command directly" ))' diff --git a/doc/vcsh.1.ronn b/doc/vcsh.1.ronn index 3ee97a4..b348f92 100644 --- a/doc/vcsh.1.ronn +++ b/doc/vcsh.1.ronn @@ -9,8 +9,6 @@ vcsh(1) - manage and sync config files via git `vcsh` enter -`vcsh` exit - `vcsh` help `vcsh` init @@ -23,8 +21,6 @@ vcsh(1) - manage and sync config files via git `vcsh` setup -`vcsh` use - `vcsh` @@ -57,9 +53,6 @@ A sample configuration for `vcsh` and `mr` can be found at * enter: Enter repository; spawn new <$SHELL>. -* exit: - Exit repository; unset ENV. - * help: Display help. @@ -79,9 +72,6 @@ A sample configuration for `vcsh` and `mr` can be found at * setup: Set up repository with recommended settings. -* use: - Use repository; set ENV. - * : Shortcut to run `vcsh` on a repo. Will prepend `git` to by itself. diff --git a/vcsh b/vcsh index c05210d..a38e55f 100755 --- a/vcsh +++ b/vcsh @@ -5,14 +5,6 @@ [ -z "$VCSH_BASE" ] && VCSH_BASE="$XDG_CONFIG_HOME/vcsh/repo.d" SELF=$(basename $0) -if [ "$SELF" = 'bash' ] || - [ "$SELF" = 'dash' ] || - [ "$SELF" = 'sh' ] || - [ "$SELF" = 'zsh' ]; then - SELF='vcsh' - VCSH_SOURCED=1 -fi - help() { echo "usage: $SELF @@ -22,7 +14,6 @@ help() { help Display this help text delete Delete an existing repository enter Enter repository; spawn new $SHELL - exit Exit repository; unset ENV init Initialize a new repository list List all repositories run \\ @@ -31,7 +22,6 @@ help() { seed-gitignore \\ Seed .gitignore.d/ from git ls-files setup Set up repository with recommended settings - use Use repository; set ENV Special command that allows you to run git commands directly without having to type so much ;)" >&2 @@ -69,12 +59,6 @@ init() { verbose "init() end" } -leave() { - unset GIT_DIR - unset GIT_WORK_TREE - unset VCSH_DIRECTORY -} - use() { verbose "use() begin" if [ ! -d "$GIT_DIR" ]; then @@ -101,8 +85,7 @@ elif [ "$1" = 'delete' ] || [ "$1" = 'init' ] || [ "$1" = 'run' ] || [ "$1" = 'seed-gitignore' ] || - [ "$1" = 'setup' ] || - [ "$1" = 'use' ]; then + [ "$1" = 'setup' ]; then [ -z $2 ] && echo "$SELF $1: error: please specify repository to work on" && return 1 export VCSH_COMMAND="$1" export VCSH_REPO_NAME="$2" @@ -112,8 +95,7 @@ elif [ "$1" = 'delete' ] || if [ "$VCSH_COMMAND" = 'run' ]; then [ -z "$VCSH_EXTERNAL_COMMAND" ] && echo "$SELF $1 $2: error: please specify a command" && return 1 fi -elif [ "$1" = 'exit' ] || - [ "$1" = 'help' ] || +elif [ "$1" = 'help' ] || [ "$1" = 'list' ]; then export VCSH_COMMAND="$1" else @@ -158,7 +140,6 @@ if [ "$VCSH_COMMAND" = 'clone' ]; then echo " Once this situation has been resolved, run 'vcsh run git pull' to finish cloning.\n" && return 3 git merge origin/master -# use || return $? verbose "clone end" #elif [ "$VCSH_COMMAND" = 'help' ] || [ "$#" -eq 0 ]; then @@ -191,25 +172,11 @@ elif [ "$VCSH_COMMAND" = 'enter' ]; then verbose "enter begin" use || return $? $SHELL - leave verbose "enter end" -elif [ "$VCSH_COMMAND" = 'exit' ]; then - verbose "exit begin" -# if [ -n "$ZSH_VERSION" ] && [ "$VCSH_NO_IGNORE_EOF" = '1' ]; then -# unset VCSH_NO_IGNORE_EOF -# setopt NO_IGNORE_EOF -# fi - [ -z "$VCSH_SOURCED" ] && echo "$SELF $VCSH_COMMAND: You need to source vcsh if you want to run in this mode" && return 10 - leave -# [ -n "$ZSH_VERSION" ] && [ "$USER" = richih ] && buildPS1 - verbose "exit end" - return 0 - elif [ "$VCSH_COMMAND" = 'init' ]; then verbose "init begin" init -# use || return $? verbose "init end" elif [ "$VCSH_COMMAND" = 'list' ]; then @@ -223,7 +190,6 @@ elif [ "$VCSH_COMMAND" = 'run' ]; then verbose "run begin" use || return $? $VCSH_EXTERNAL_COMMAND - leave verbose "run end" elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then @@ -265,28 +231,8 @@ elif [ "$VCSH_COMMAND" = 'setup' ]; then verbose "seed-gitignore begin" use || return $? setup || return $? - leave verbose "seed-gitignore end" -elif [ "$VCSH_COMMAND" = 'use' ]; then - verbose "use begin" -# if [ -n "$ZSH_VERSION" ]; then -# if [ -o NO_IGNORE_EOF ]; then -# export VCSH_NO_IGNORE_EOF=1 -# setopt IGNORE_EOF -# fi -# vcsh_exit() { -# vcsh exit; -# zle reset-prompt; -# } -# zle -N vcsh_exit -# bindkey '^d' 'vcsh_exit' -# fi - [ -z "$VCSH_SOURCED" ] && echo "$SELF $VCSH_COMMAND: You need to source vcsh if you want to run in this mode" && return 10 - use || return $? -# [ -n "$ZSH_VERSION" ] && [ "$USER" = richih ] && buildPS1 - verbose "use end" - else verbose "defaulting to calling help()" help -- 2.39.5 From af9c23df7371cd61f8efb60a0654dda3f30f0370 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Thu, 24 Nov 2011 20:05:30 +0100 Subject: [PATCH 09/16] Use exit and {} instead of return and () --- doc/error_codes.md | 16 +++++++++++++++- vcsh | 48 ++++++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/doc/error_codes.md b/doc/error_codes.md index 06d760d..e663644 100644 --- a/doc/error_codes.md +++ b/doc/error_codes.md @@ -1,3 +1,17 @@ * 0: OK * 1: Generic error -* 10: source-only command was called without vcsh being sourced +* 10: Init failed because $GIT_DIR exists +* 11: Could not enter $GIT_WORK_TREE +* 12: No repository found +* 13: Required directory exists but is not a directory +* 14: Could not create directory +* 15: Could not create file +* 16: Potentially harmful operation aborted +* 17: Files that would be overwritten exist; fix manually + +* 50: Could not create directory +* 51: Could not create file +* 52: Could not move directory +* 53: Could not move file + +* 99: Error code reserved for actions that should never happen diff --git a/vcsh b/vcsh index a38e55f..8ba0f27 100755 --- a/vcsh +++ b/vcsh @@ -47,12 +47,11 @@ init() { verbose "init() begin" [ -e "$GIT_DIR" ] && echo "$SELF: fatal: $GIT_DIR exists" && - return 21 + return 10 export GIT_WORK_TREE="$HOME" mkdir -p "$GIT_WORK_TREE" cd "$GIT_WORK_TREE" || - (echo "$SELF: fatal: could not enter $GIT_WORK_TREE" && - return 1) || return $? + { echo "$SELF: fatal: could not enter $GIT_WORK_TREE"; return 11; } cd "$GIT_WORK_TREE" git init setup @@ -63,7 +62,7 @@ use() { verbose "use() begin" if [ ! -d "$GIT_DIR" ]; then echo E: no repository found for "$VCSH_REPO_NAME" >&2 - return 1 + return 12 fi export GIT_DIR export GIT_WORK_TREE="$(git config --get core.worktree)" @@ -86,24 +85,24 @@ elif [ "$1" = 'delete' ] || [ "$1" = 'run' ] || [ "$1" = 'seed-gitignore' ] || [ "$1" = 'setup' ]; then - [ -z $2 ] && echo "$SELF $1: error: please specify repository to work on" && return 1 + [ -z $2 ] && echo "$SELF $1: error: please specify repository to work on" && exit 1 export VCSH_COMMAND="$1" export VCSH_REPO_NAME="$2" export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" shift 2 export VCSH_EXTERNAL_COMMAND="$*" if [ "$VCSH_COMMAND" = 'run' ]; then - [ -z "$VCSH_EXTERNAL_COMMAND" ] && echo "$SELF $1 $2: error: please specify a command" && return 1 + [ -z "$VCSH_EXTERNAL_COMMAND" ] && echo "$SELF $1 $2: error: please specify a command" && exit 1 fi elif [ "$1" = 'help' ] || [ "$1" = 'list' ]; then export VCSH_COMMAND="$1" else - [ -z $1 ] && help && return 0 + [ -z $1 ] && help && exit 0 export VCSH_COMMAND='run' export VCSH_REPO_NAME="$1" export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" - [ -d $GIT_DIR ] || (help && return 1) || return 0 + [ -d $GIT_DIR ] || { help; exit 1; } shift 1 export VCSH_EXTERNAL_COMMAND="git $*" fi @@ -113,11 +112,11 @@ for check_directory in "$VCSH_BASE" "$HOME/.gitignore.d" do if [ ! -d "$check_directory" ]; then if [ -e "$check_directory" ]; then - echo "$SELF: error: $check_directory exists but is not a directory" >&2 - return 2 + echo "$SELF: fatal: $check_directory exists but is not a directory" >&2 + exit 13 else echo "$SELF: info: attempting to create $check_directory" - mkdir -p "$check_directory" || (echo "$SELF: error: could not create $check_directory" >&2; return 2) || return $? + mkdir -p "$check_directory" || { echo "$SELF: fatal: could not create $check_directory" >&2; exit 50; } fi fi done @@ -138,7 +137,7 @@ if [ "$VCSH_COMMAND" = 'clone' ]; then [ "$VCSH_CONFLICT" = '1' ] && echo "$SELF: fatal: will stop after fetching and not try to merge!\n" && echo " Once this situation has been resolved, run 'vcsh run git pull' to finish cloning.\n" && - return 3 + exit 17 git merge origin/master verbose "clone end" @@ -150,7 +149,7 @@ elif [ "$VCSH_COMMAND" = 'delete' ]; then verbose "delete begin" old_dir="$PWD" cd "$HOME" - use || return $? + use || exit $? echo "$SELF: info: This operation WILL DETROY DATA!" files=$(git ls-files) echo "These files will be deleted: @@ -160,7 +159,7 @@ $files AGAIN, THIS WILL DELETE YOUR DATA! To continue, type \"Yes, do as I say\"" read answer - [ "x$answer" = "xYes, do as I say" ] || return 1 + [ "x$answer" = "xYes, do as I say" ] || exit 16 for file in $files; do rm -f $file || echo "$SELF: info: could not delete '$file', continuing with deletion" done @@ -170,7 +169,7 @@ To continue, type \"Yes, do as I say\"" elif [ "$VCSH_COMMAND" = 'enter' ]; then verbose "enter begin" - use || return $? + use || exit $? $SHELL verbose "enter end" @@ -188,13 +187,13 @@ elif [ "$VCSH_COMMAND" = 'list' ]; then elif [ "$VCSH_COMMAND" = 'run' ]; then verbose "run begin" - use || return $? + use || exit $? $VCSH_EXTERNAL_COMMAND verbose "run end" elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then verbose "seed-gitignore begin" - use || return $? + use || exit $? # Switching directory as this has to be executed from $HOME to be of any use. # Going back into old directory at the end in case `vcsh use` is reactivated. old_dir="$PWD" @@ -207,7 +206,7 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then done; done | sort -u) tempfile=$(mktemp) || - (echo "$SELF: fatal: could not create tempfile" && return 1) || return $? + { echo "$SELF: fatal: could not create tempfile"; exit 51; } echo '*' > "$tempfile" for gitignore in $gitignores; do echo "$gitignore" | sed 's/^/!/' >> "$tempfile" @@ -215,28 +214,27 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then done diff -N "$tempfile" "$HOME/.gitignore.d/$VCSH_REPO_NAME" > /dev/null && rm -f "$tempfile" && - return + exit if [ -e "$HOME/.gitignore.d/$VCSH_REPO_NAME" ]; then echo "$SELF: info: $HOME/.gitignore.d/$VCSH_REPO_NAME differs from new data, moving it to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" mv -f "$HOME/.gitignore.d/$VCSH_REPO_NAME" "$HOME/.gitignore.d/$VCSH_REPO_NAME.bak" || - (echo "$SELF: fatal: could not move $HOME/.gitignore.d/$VCSH_REPO_NAME to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" && - return 1) || return $? + { echo "$SELF: fatal: could not move $HOME/.gitignore.d/$VCSH_REPO_NAME to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak"; exit 53; } fi mv -f "$tempfile" "$HOME/.gitignore.d/$VCSH_REPO_NAME" || - (echo "$SELF: fatal: could not move $tempfile to $HOME/.gitignore.d/$VCSH_REPO_NAME" && return 1) || return $? + { echo "$SELF: fatal: could not move $tempfile to $HOME/.gitignore.d/$VCSH_REPO_NAME" && exit 53; } cd "$old_dir" verbose "seed-gitignore end" elif [ "$VCSH_COMMAND" = 'setup' ]; then verbose "seed-gitignore begin" - use || return $? - setup || return $? + use || exit $? + setup || exit $? verbose "seed-gitignore end" else verbose "defaulting to calling help()" help echo "$SELF: fatal: You should never reach this code. File a bug, please." - return 99 + exit 99 fi -- 2.39.5 From fe0993a7947fc20b567e3dc6bd57f0eda5c49a01 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Thu, 24 Nov 2011 20:29:00 +0100 Subject: [PATCH 10/16] Make error reporting sane * refactor error logging code * make all errors go to stderr * make fatal errors actually fatal (exit instead of return) * make output consistent --- vcsh | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/vcsh b/vcsh index 8ba0f27..59c4237 100755 --- a/vcsh +++ b/vcsh @@ -35,6 +35,15 @@ verbose() { if [ -n "$VCSH_DEBUG" ] || [ -n "$VCSH_VERBOSE" ]; then echo "$SELF: verbose: $@"; fi } +error () { + echo "$SELF: error: $1" >&2 +} + +fatal () { + echo "$SELF: fatal error: $1" >&2 + exit $2 +} + setup() { git config core.worktree "$GIT_WORK_TREE" git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME" @@ -45,13 +54,10 @@ setup() { init() { verbose "init() begin" - [ -e "$GIT_DIR" ] && - echo "$SELF: fatal: $GIT_DIR exists" && - return 10 + [ ! -e "$GIT_DIR" ] || fatal "$GIT_DIR exists" 10 export GIT_WORK_TREE="$HOME" mkdir -p "$GIT_WORK_TREE" - cd "$GIT_WORK_TREE" || - { echo "$SELF: fatal: could not enter $GIT_WORK_TREE"; return 11; } + cd "$GIT_WORK_TREE" || fatal "could not enter $GIT_WORK_TREE" 11 cd "$GIT_WORK_TREE" git init setup @@ -61,7 +67,7 @@ init() { use() { verbose "use() begin" if [ ! -d "$GIT_DIR" ]; then - echo E: no repository found for "$VCSH_REPO_NAME" >&2 + error "no repository found for '$VCSH_REPO_NAME'" return 12 fi export GIT_DIR @@ -85,14 +91,14 @@ elif [ "$1" = 'delete' ] || [ "$1" = 'run' ] || [ "$1" = 'seed-gitignore' ] || [ "$1" = 'setup' ]; then - [ -z $2 ] && echo "$SELF $1: error: please specify repository to work on" && exit 1 + [ -z $2 ] && fatal "$1: please specify repository to work on" 1 export VCSH_COMMAND="$1" export VCSH_REPO_NAME="$2" export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" shift 2 export VCSH_EXTERNAL_COMMAND="$*" if [ "$VCSH_COMMAND" = 'run' ]; then - [ -z "$VCSH_EXTERNAL_COMMAND" ] && echo "$SELF $1 $2: error: please specify a command" && exit 1 + [ -z "$VCSH_EXTERNAL_COMMAND" ] && fatal "$1 $2: please specify a command" 1 fi elif [ "$1" = 'help' ] || [ "$1" = 'list' ]; then @@ -112,11 +118,10 @@ for check_directory in "$VCSH_BASE" "$HOME/.gitignore.d" do if [ ! -d "$check_directory" ]; then if [ -e "$check_directory" ]; then - echo "$SELF: fatal: $check_directory exists but is not a directory" >&2 - exit 13 + fatal "$check_directory exists but is not a directory" 13 else echo "$SELF: info: attempting to create $check_directory" - mkdir -p "$check_directory" || { echo "$SELF: fatal: could not create $check_directory" >&2; exit 50; } + mkdir -p "$check_directory" || fatal "could not create $check_directory" 50 fi fi done @@ -131,13 +136,12 @@ if [ "$VCSH_COMMAND" = 'clone' ]; then git fetch for object in $(git ls-tree -r origin/master | awk '{print $4}'); do [ -e "$object" ] && - echo "$SELF: error: $object exists." && + error "$object exists." && VCSH_CONFLICT=1; done [ "$VCSH_CONFLICT" = '1' ] && - echo "$SELF: fatal: will stop after fetching and not try to merge!\n" && - echo " Once this situation has been resolved, run 'vcsh run git pull' to finish cloning.\n" && - exit 17 + fatal "will stop after fetching and not try to merge! + Once this situation has been resolved, run 'vcsh run git pull' to finish cloning.\n" 17 git merge origin/master verbose "clone end" @@ -205,8 +209,7 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then file="$new" done; done | sort -u) - tempfile=$(mktemp) || - { echo "$SELF: fatal: could not create tempfile"; exit 51; } + tempfile=$(mktemp) || fatal "could not create tempfile" 51 echo '*' > "$tempfile" for gitignore in $gitignores; do echo "$gitignore" | sed 's/^/!/' >> "$tempfile" @@ -218,10 +221,10 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then if [ -e "$HOME/.gitignore.d/$VCSH_REPO_NAME" ]; then echo "$SELF: info: $HOME/.gitignore.d/$VCSH_REPO_NAME differs from new data, moving it to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" mv -f "$HOME/.gitignore.d/$VCSH_REPO_NAME" "$HOME/.gitignore.d/$VCSH_REPO_NAME.bak" || - { echo "$SELF: fatal: could not move $HOME/.gitignore.d/$VCSH_REPO_NAME to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak"; exit 53; } + fatal "could not move $HOME/.gitignore.d/$VCSH_REPO_NAME to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" 53 fi mv -f "$tempfile" "$HOME/.gitignore.d/$VCSH_REPO_NAME" || - { echo "$SELF: fatal: could not move $tempfile to $HOME/.gitignore.d/$VCSH_REPO_NAME" && exit 53; } + fatal "could not move $tempfile to $HOME/.gitignore.d/$VCSH_REPO_NAME" 53 cd "$old_dir" verbose "seed-gitignore end" @@ -234,7 +237,6 @@ elif [ "$VCSH_COMMAND" = 'setup' ]; then else verbose "defaulting to calling help()" help - echo "$SELF: fatal: You should never reach this code. File a bug, please." - exit 99 + fatal "You should never reach this code. File a bug, please." 99 fi -- 2.39.5 From 492f2eef5c2421ae1c05cde8119c7a46511a93a8 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Thu, 24 Nov 2011 20:31:52 +0100 Subject: [PATCH 11/16] Make info calls consistent --- vcsh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/vcsh b/vcsh index 59c4237..ae0fa68 100755 --- a/vcsh +++ b/vcsh @@ -44,6 +44,10 @@ fatal () { exit $2 } +info () { + echo "$SELF: info: $1" +} + setup() { git config core.worktree "$GIT_WORK_TREE" git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME" @@ -120,7 +124,7 @@ do if [ -e "$check_directory" ]; then fatal "$check_directory exists but is not a directory" 13 else - echo "$SELF: info: attempting to create $check_directory" + info "attempting to create $check_directory" mkdir -p "$check_directory" || fatal "could not create $check_directory" 50 fi fi @@ -154,7 +158,7 @@ elif [ "$VCSH_COMMAND" = 'delete' ]; then old_dir="$PWD" cd "$HOME" use || exit $? - echo "$SELF: info: This operation WILL DETROY DATA!" + info "This operation WILL DETROY DATA!" files=$(git ls-files) echo "These files will be deleted: @@ -165,9 +169,9 @@ To continue, type \"Yes, do as I say\"" read answer [ "x$answer" = "xYes, do as I say" ] || exit 16 for file in $files; do - rm -f $file || echo "$SELF: info: could not delete '$file', continuing with deletion" + rm -f $file || info "could not delete '$file', continuing with deletion" done - rm -rf "$GIT_DIR" || echo "$SELF: info: could not delete '$GIT_DIR'" + rm -rf "$GIT_DIR" || info "could not delete '$GIT_DIR'" cd "$old_dir" verbose "delete end" @@ -219,7 +223,7 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then rm -f "$tempfile" && exit if [ -e "$HOME/.gitignore.d/$VCSH_REPO_NAME" ]; then - echo "$SELF: info: $HOME/.gitignore.d/$VCSH_REPO_NAME differs from new data, moving it to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" + info "$HOME/.gitignore.d/$VCSH_REPO_NAME differs from new data, moving it to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" mv -f "$HOME/.gitignore.d/$VCSH_REPO_NAME" "$HOME/.gitignore.d/$VCSH_REPO_NAME.bak" || fatal "could not move $HOME/.gitignore.d/$VCSH_REPO_NAME to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" 53 fi -- 2.39.5 From ad4b54c8f747b631a9fc7b14a88509debb2799bd Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Thu, 24 Nov 2011 21:07:39 +0100 Subject: [PATCH 12/16] Clean up Dieter's patches --- vcsh | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/vcsh b/vcsh index ae0fa68..16bfa53 100755 --- a/vcsh +++ b/vcsh @@ -35,16 +35,16 @@ verbose() { if [ -n "$VCSH_DEBUG" ] || [ -n "$VCSH_VERBOSE" ]; then echo "$SELF: verbose: $@"; fi } -error () { - echo "$SELF: error: $1" >&2 +fatal() { + echo "$SELF: fatal: $1" >&2 + exit $2 } -fatal () { - echo "$SELF: fatal error: $1" >&2 - exit $2 +error() { + echo "$SELF: error: $1" >&2 } -info () { +info() { echo "$SELF: info: $1" } @@ -58,10 +58,10 @@ setup() { init() { verbose "init() begin" - [ ! -e "$GIT_DIR" ] || fatal "$GIT_DIR exists" 10 + [ ! -e "$GIT_DIR" ] || fatal "'$GIT_DIR' exists" 10 export GIT_WORK_TREE="$HOME" mkdir -p "$GIT_WORK_TREE" - cd "$GIT_WORK_TREE" || fatal "could not enter $GIT_WORK_TREE" 11 + cd "$GIT_WORK_TREE" || fatal "could not enter '$GIT_WORK_TREE'" 11 cd "$GIT_WORK_TREE" git init setup @@ -122,10 +122,10 @@ for check_directory in "$VCSH_BASE" "$HOME/.gitignore.d" do if [ ! -d "$check_directory" ]; then if [ -e "$check_directory" ]; then - fatal "$check_directory exists but is not a directory" 13 + fatal "'$check_directory' exists but is not a directory" 13 else - info "attempting to create $check_directory" - mkdir -p "$check_directory" || fatal "could not create $check_directory" 50 + info "attempting to create '$check_directory'" + mkdir -p "$check_directory" || fatal "could not create '$check_directory'" 50 fi fi done @@ -140,12 +140,12 @@ if [ "$VCSH_COMMAND" = 'clone' ]; then git fetch for object in $(git ls-tree -r origin/master | awk '{print $4}'); do [ -e "$object" ] && - error "$object exists." && + error "'$object' exists." && VCSH_CONFLICT=1; done [ "$VCSH_CONFLICT" = '1' ] && fatal "will stop after fetching and not try to merge! - Once this situation has been resolved, run 'vcsh run git pull' to finish cloning.\n" 17 + Once this situation has been resolved, run 'vcsh run $VCSH_REPO_NAME git pull' to finish cloning.\n" 17 git merge origin/master verbose "clone end" @@ -223,12 +223,12 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then rm -f "$tempfile" && exit if [ -e "$HOME/.gitignore.d/$VCSH_REPO_NAME" ]; then - info "$HOME/.gitignore.d/$VCSH_REPO_NAME differs from new data, moving it to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" + info "'$HOME/.gitignore.d/$VCSH_REPO_NAME' differs from new data, moving it to '$HOME/.gitignore.d/$VCSH_REPO_NAME.bak'" mv -f "$HOME/.gitignore.d/$VCSH_REPO_NAME" "$HOME/.gitignore.d/$VCSH_REPO_NAME.bak" || - fatal "could not move $HOME/.gitignore.d/$VCSH_REPO_NAME to $HOME/.gitignore.d/$VCSH_REPO_NAME.bak" 53 + fatal "could not move '$HOME/.gitignore.d/$VCSH_REPO_NAME' to '$HOME/.gitignore.d/$VCSH_REPO_NAME.bak'" 53 fi mv -f "$tempfile" "$HOME/.gitignore.d/$VCSH_REPO_NAME" || - fatal "could not move $tempfile to $HOME/.gitignore.d/$VCSH_REPO_NAME" 53 + fatal "could not move '$tempfile' to '$HOME/.gitignore.d/$VCSH_REPO_NAME'" 53 cd "$old_dir" verbose "seed-gitignore end" -- 2.39.5 From d4326c2c1c2d5e9626f8d9e2ba0e5e3848772238 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Thu, 24 Nov 2011 21:38:27 +0100 Subject: [PATCH 13/16] Cleanup argument parsing and delegation * move all backend code to separate functions * call those functions directly * simplify argument parsing * remove redundant verbose begin/end messages --- vcsh | 203 +++++++++++++++++++++++++---------------------------------- 1 file changed, 85 insertions(+), 118 deletions(-) diff --git a/vcsh b/vcsh index 16bfa53..50c1c31 100755 --- a/vcsh +++ b/vcsh @@ -48,91 +48,7 @@ info() { echo "$SELF: info: $1" } -setup() { - git config core.worktree "$GIT_WORK_TREE" - git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME" - git config vcsh.vcsh 'true' - touch "$HOME/.gitignore.d/$VCSH_REPO_NAME" - git add "$HOME/.gitignore.d/$VCSH_REPO_NAME" -} - -init() { - verbose "init() begin" - [ ! -e "$GIT_DIR" ] || fatal "'$GIT_DIR' exists" 10 - export GIT_WORK_TREE="$HOME" - mkdir -p "$GIT_WORK_TREE" - cd "$GIT_WORK_TREE" || fatal "could not enter '$GIT_WORK_TREE'" 11 - cd "$GIT_WORK_TREE" - git init - setup - verbose "init() end" -} - -use() { - verbose "use() begin" - if [ ! -d "$GIT_DIR" ]; then - error "no repository found for '$VCSH_REPO_NAME'" - return 12 - fi - export GIT_DIR - export GIT_WORK_TREE="$(git config --get core.worktree)" - export VCSH_DIRECTORY="$VCSH_REPO_NAME" - verbose "use() end" -} - - -if [ "$1" = 'clone' ]; then - export VCSH_COMMAND="$1" - GIT_REMOTE="$2" - export GIT_REMOTE - VCSH_REPO_NAME="$3" - [ -z "$VCSH_REPO_NAME" ] && VCSH_REPO_NAME=$(basename "$GIT_REMOTE" .git) - export VCSH_REPO_NAME - export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" -elif [ "$1" = 'delete' ] || - [ "$1" = 'enter' ] || - [ "$1" = 'init' ] || - [ "$1" = 'run' ] || - [ "$1" = 'seed-gitignore' ] || - [ "$1" = 'setup' ]; then - [ -z $2 ] && fatal "$1: please specify repository to work on" 1 - export VCSH_COMMAND="$1" - export VCSH_REPO_NAME="$2" - export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" - shift 2 - export VCSH_EXTERNAL_COMMAND="$*" - if [ "$VCSH_COMMAND" = 'run' ]; then - [ -z "$VCSH_EXTERNAL_COMMAND" ] && fatal "$1 $2: please specify a command" 1 - fi -elif [ "$1" = 'help' ] || - [ "$1" = 'list' ]; then - export VCSH_COMMAND="$1" -else - [ -z $1 ] && help && exit 0 - export VCSH_COMMAND='run' - export VCSH_REPO_NAME="$1" - export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" - [ -d $GIT_DIR ] || { help; exit 1; } - shift 1 - export VCSH_EXTERNAL_COMMAND="git $*" -fi - - -for check_directory in "$VCSH_BASE" "$HOME/.gitignore.d" -do - if [ ! -d "$check_directory" ]; then - if [ -e "$check_directory" ]; then - fatal "'$check_directory' exists but is not a directory" 13 - else - info "attempting to create '$check_directory'" - mkdir -p "$check_directory" || fatal "could not create '$check_directory'" 50 - fi - fi -done - - -if [ "$VCSH_COMMAND" = 'clone' ]; then - verbose "clone begin" +clone () { init git remote add origin "$GIT_REMOTE" git config branch.master.remote origin @@ -147,14 +63,9 @@ if [ "$VCSH_COMMAND" = 'clone' ]; then fatal "will stop after fetching and not try to merge! Once this situation has been resolved, run 'vcsh run $VCSH_REPO_NAME git pull' to finish cloning.\n" 17 git merge origin/master - verbose "clone end" - -#elif [ "$VCSH_COMMAND" = 'help' ] || [ "$#" -eq 0 ]; then -elif [ "$VCSH_COMMAND" = 'help' ]; then - help +} -elif [ "$VCSH_COMMAND" = 'delete' ]; then - verbose "delete begin" +delete () { old_dir="$PWD" cd "$HOME" use || exit $? @@ -173,34 +84,35 @@ To continue, type \"Yes, do as I say\"" done rm -rf "$GIT_DIR" || info "could not delete '$GIT_DIR'" cd "$old_dir" - verbose "delete end" +} -elif [ "$VCSH_COMMAND" = 'enter' ]; then - verbose "enter begin" +enter () { use || exit $? $SHELL - verbose "enter end" +} -elif [ "$VCSH_COMMAND" = 'init' ]; then - verbose "init begin" - init - verbose "init end" +init() { + [ ! -e "$GIT_DIR" ] || fatal "'$GIT_DIR' exists" 10 + export GIT_WORK_TREE="$HOME" + mkdir -p "$GIT_WORK_TREE" + cd "$GIT_WORK_TREE" || fatal "could not enter '$GIT_WORK_TREE'" 11 + cd "$GIT_WORK_TREE" + git init + setup +} -elif [ "$VCSH_COMMAND" = 'list' ]; then - verbose "list begin" +list () { for i in "$VCSH_BASE"/*.git; do echo $(basename "$i" .git) done - verbose "list end" +} -elif [ "$VCSH_COMMAND" = 'run' ]; then - verbose "run begin" +run () { use || exit $? $VCSH_EXTERNAL_COMMAND - verbose "run end" +} -elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then - verbose "seed-gitignore begin" +seed_gitignore () { use || exit $? # Switching directory as this has to be executed from $HOME to be of any use. # Going back into old directory at the end in case `vcsh use` is reactivated. @@ -230,17 +142,72 @@ elif [ "$VCSH_COMMAND" = 'seed-gitignore' ]; then mv -f "$tempfile" "$HOME/.gitignore.d/$VCSH_REPO_NAME" || fatal "could not move '$tempfile' to '$HOME/.gitignore.d/$VCSH_REPO_NAME'" 53 cd "$old_dir" - verbose "seed-gitignore end" +} -elif [ "$VCSH_COMMAND" = 'setup' ]; then - verbose "seed-gitignore begin" - use || exit $? - setup || exit $? - verbose "seed-gitignore end" +setup() { + use + git config core.worktree "$GIT_WORK_TREE" + git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME" + git config vcsh.vcsh 'true' + touch "$HOME/.gitignore.d/$VCSH_REPO_NAME" + git add "$HOME/.gitignore.d/$VCSH_REPO_NAME" +} -else - verbose "defaulting to calling help()" - help - fatal "You should never reach this code. File a bug, please." 99 +use() { + if [ ! -d "$GIT_DIR" ]; then + error "no repository found for '$VCSH_REPO_NAME'" + return 12 + fi + export GIT_WORK_TREE="$(git config --get core.worktree)" + export VCSH_DIRECTORY="$VCSH_REPO_NAME" +} +if [ "$1" = 'clone' ]; then + [ -z $2 ] && fatal "$1: please specify a remote" 1 + export VCSH_COMMAND="$1" + GIT_REMOTE="$2" + [ -n "$3" ] && VCSH_REPO_NAME="$3" || VCSH_REPO_NAME=$(basename "$GIT_REMOTE" .git) + export VCSH_REPO_NAME + export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" +elif [ "$1" = 'delete' ] || + [ "$1" = 'enter' ] || + [ "$1" = 'init' ] || + [ "$1" = 'run' ] || + [ "$1" = 'seed-gitignore' ] || + [ "$1" = 'setup' ]; then + [ -z $2 ] && fatal "$1: please specify repository to work on" 1 + [ "$1" = 'run' -a -z "$3" ] && fatal "$1: please specify a command" 1 + export VCSH_COMMAND="$1" + export VCSH_REPO_NAME="$2" + export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" + [ "$1" = 'run' ] && shift 2 && export VCSH_EXTERNAL_COMMAND="$@" + [ "$1" = 'seed-gitignore' ] && export VCSH_COMMAND=seed_gitignore +elif [ "$1" = 'list' ]; then + export VCSH_COMMAND="$1" +elif [ -n "$1" ]; then + export VCSH_COMMAND=run + export VCSH_REPO_NAME="$1" + export GIT_DIR="$VCSH_BASE/$VCSH_REPO_NAME.git" + [ -d $GIT_DIR ] || { help; exit 1; } + shift 1 + export VCSH_EXTERNAL_COMMAND="git $*" +else + # $1 is empty, or 'help' + help && exit fi + +for check_directory in "$VCSH_BASE" "$HOME/.gitignore.d" +do + if [ ! -d "$check_directory" ]; then + if [ -e "$check_directory" ]; then + fatal "'$check_directory' exists but is not a directory" 13 + else + info "attempting to create '$check_directory'" + mkdir -p "$check_directory" || fatal "could not create '$check_directory'" 50 + fi + fi +done + +verbose "$VCSH_COMMAND begin" +$VCSH_COMMAND +verbose "$VCSH_COMMAND end, exiting" -- 2.39.5 From b67f39a96f50708015c0986e741cfe621f255593 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Thu, 24 Nov 2011 21:58:31 +0100 Subject: [PATCH 14/16] Cleanup use() exit scenarios Instead of having use() report a normal error (which is actually fatal) and returning >0, and calling use || exit $? everytime; just report the fatal error and quit already --- vcsh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/vcsh b/vcsh index 50c1c31..ee35141 100755 --- a/vcsh +++ b/vcsh @@ -68,7 +68,7 @@ clone () { delete () { old_dir="$PWD" cd "$HOME" - use || exit $? + use info "This operation WILL DETROY DATA!" files=$(git ls-files) echo "These files will be deleted: @@ -87,7 +87,7 @@ To continue, type \"Yes, do as I say\"" } enter () { - use || exit $? + use $SHELL } @@ -108,12 +108,12 @@ list () { } run () { - use || exit $? + use $VCSH_EXTERNAL_COMMAND } seed_gitignore () { - use || exit $? + use # Switching directory as this has to be executed from $HOME to be of any use. # Going back into old directory at the end in case `vcsh use` is reactivated. old_dir="$PWD" @@ -155,8 +155,7 @@ setup() { use() { if [ ! -d "$GIT_DIR" ]; then - error "no repository found for '$VCSH_REPO_NAME'" - return 12 + fatal "no repository found for '$VCSH_REPO_NAME'" 12 fi export GIT_WORK_TREE="$(git config --get core.worktree)" export VCSH_DIRECTORY="$VCSH_REPO_NAME" -- 2.39.5 From 2f7b006ff5df72cff63f8d7cb40d8c68cac07196 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Fri, 25 Nov 2011 20:24:01 +0100 Subject: [PATCH 15/16] Minor formatting updates --- vcsh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vcsh b/vcsh index ee35141..a6d0106 100755 --- a/vcsh +++ b/vcsh @@ -48,7 +48,7 @@ info() { echo "$SELF: info: $1" } -clone () { +clone() { init git remote add origin "$GIT_REMOTE" git config branch.master.remote origin @@ -65,7 +65,7 @@ clone () { git merge origin/master } -delete () { +delete() { old_dir="$PWD" cd "$HOME" use @@ -86,7 +86,7 @@ To continue, type \"Yes, do as I say\"" cd "$old_dir" } -enter () { +enter() { use $SHELL } @@ -101,18 +101,18 @@ init() { setup } -list () { +list() { for i in "$VCSH_BASE"/*.git; do echo $(basename "$i" .git) done } -run () { +run() { use $VCSH_EXTERNAL_COMMAND } -seed_gitignore () { +seed_gitignore() { use # Switching directory as this has to be executed from $HOME to be of any use. # Going back into old directory at the end in case `vcsh use` is reactivated. -- 2.39.5 From 8ec994a32fa7ddf95b6fab261265d731ae7dfc92 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Sat, 26 Nov 2011 11:26:42 +0100 Subject: [PATCH 16/16] remove left-over useless exit code --- doc/error_codes.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/error_codes.md b/doc/error_codes.md index e663644..a6cc903 100644 --- a/doc/error_codes.md +++ b/doc/error_codes.md @@ -13,5 +13,3 @@ * 51: Could not create file * 52: Could not move directory * 53: Could not move file - -* 99: Error code reserved for actions that should never happen -- 2.39.5