From 0c1aa6e133124738c8b820625c22c1755b89670e Mon Sep 17 00:00:00 2001 From: Matt Hamilton Date: Thu, 4 Feb 2016 15:33:12 -0500 Subject: [PATCH] [git] fix some functions * remove unneeded explination and authorship comments (as it's noted in the README.md) * s/git/command git/ (see issue #30) * add is-true function, as it is required by a few other functions. --- modules/git/functions/git-commit-lost | 13 +++---------- modules/git/functions/git-dir | 9 +-------- modules/git/functions/git-ignore-add | 7 ------- modules/git/functions/git-root | 9 +-------- .../git/functions/git-stash-clear-interactive | 13 +++---------- modules/git/functions/git-stash-dropped | 13 +++---------- modules/git/functions/git-stash-recover | 11 ++--------- modules/git/functions/git-submodule-move | 11 ++--------- modules/git/functions/git-submodule-remove | 19 ++++++------------- modules/git/functions/is-true | 5 +++++ 10 files changed, 26 insertions(+), 84 deletions(-) create mode 100644 modules/git/functions/is-true diff --git a/modules/git/functions/git-commit-lost b/modules/git/functions/git-commit-lost index ef7c5ac..e801e6d 100644 --- a/modules/git/functions/git-commit-lost +++ b/modules/git/functions/git-commit-lost @@ -1,19 +1,12 @@ -# -# Lists lost Git commits. -# -# Authors: -# Sorin Ionescu -# - -if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then +if ! is-true "$(commmand git rev-parse --is-inside-work-tree 2> /dev/null)"; then print "${0}: not a repository work tree: ${PWD}" >&2 return 1 fi -git fsck 2> /dev/null \ +command git fsck 2> /dev/null \ | grep "^dangling commit" \ | awk '{print $3}' \ - | git log \ + | command git log \ --date-order \ --no-walk \ --stdin \ diff --git a/modules/git/functions/git-dir b/modules/git/functions/git-dir index 429ae25..215c849 100644 --- a/modules/git/functions/git-dir +++ b/modules/git/functions/git-dir @@ -1,11 +1,4 @@ -# -# Displays the path to the Git directory. -# -# Authors: -# Sorin Ionescu -# - -local git_dir="${$(git rev-parse --git-dir):A}" +local git_dir="${$(command git rev-parse --git-dir):A}" if [[ -n "${git_dir}" ]]; then print "${git_dir}" diff --git a/modules/git/functions/git-ignore-add b/modules/git/functions/git-ignore-add index bd16c01..bb05ab8 100644 --- a/modules/git/functions/git-ignore-add +++ b/modules/git/functions/git-ignore-add @@ -1,10 +1,3 @@ -# -# Adds files passed as parameters to .gitignore in project root -# -# Authors: -# Matt Hamilton -# - # make sure we have a git-root if ! git-root &> /dev/null; then print 'not in a git repository' >&2 diff --git a/modules/git/functions/git-root b/modules/git/functions/git-root index a68f9f6..7ed44ff 100644 --- a/modules/git/functions/git-root +++ b/modules/git/functions/git-root @@ -1,11 +1,4 @@ -# -# Displays the path to the working tree root. -# -# Authors: -# Sorin Ionescu -# - -local root="$(git rev-parse --show-toplevel 2> /dev/null)" +local root="$(command git rev-parse --show-toplevel 2> /dev/null)" if [[ -n "${root}" ]]; then print "${root}" diff --git a/modules/git/functions/git-stash-clear-interactive b/modules/git/functions/git-stash-clear-interactive index a2ad7dc..0197be9 100644 --- a/modules/git/functions/git-stash-clear-interactive +++ b/modules/git/functions/git-stash-clear-interactive @@ -1,11 +1,4 @@ -# -# Asks for confirmation before clearing the Git stash. -# -# Authors: -# Sorin Ionescu -# - -if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then +if ! is-true "$(command git rev-parse --is-inside-work-tree 2> /dev/null)"; then print "${0}: not a repository work tree: ${PWD}" >&2 return 1 fi @@ -13,10 +6,10 @@ fi local stashed if [[ -f "$(git-dir)/refs/stash" ]]; then - stashed="$(git stash list 2> /dev/null | wc -l | awk '{print $1}')" + stashed="$(command git stash list 2> /dev/null | wc -l | awk '{print $1}')" if (( ${stashed} > 0 )); then if read -q "?Clear ${stashed} stashed state(s) [y/N]? "; then - git stash clear + command git stash clear fi fi fi diff --git a/modules/git/functions/git-stash-dropped b/modules/git/functions/git-stash-dropped index 2fabed9..1cc979b 100644 --- a/modules/git/functions/git-stash-dropped +++ b/modules/git/functions/git-stash-dropped @@ -1,19 +1,12 @@ -# -# Lists dropped Git stashed states. -# -# Authors: -# Sorin Ionescu -# - -if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then +if ! is-true "$(command git rev-parse --is-inside-work-tree 2> /dev/null)"; then print "${0}: not a repository work tree: ${PWD}" >&2 return 1 fi -git fsck --unreachable 2> /dev/null \ +command git fsck --unreachable 2> /dev/null \ | grep 'commit' \ | awk '{print $3}' \ - | git log \ + | command git log \ --pretty=format:${_git_log_oneline_format} \ --extended-regexp \ --grep="${1:-(WIP )?[Oo]n [^:]+:}" \ diff --git a/modules/git/functions/git-stash-recover b/modules/git/functions/git-stash-recover index 1f1916d..8b5adec 100644 --- a/modules/git/functions/git-stash-recover +++ b/modules/git/functions/git-stash-recover @@ -1,11 +1,4 @@ -# -# Recovers dropped Git stashed states. -# -# Authors: -# Sorin Ionescu -# - -if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then +if ! is-true "$(command git rev-parse --is-inside-work-tree 2> /dev/null)"; then print "${0}: not a repository work tree: ${PWD}" >&2 return 1 fi @@ -14,5 +7,5 @@ local commit for commit in "${@}"; do git update-ref \ - -m "$(git log -1 --pretty="format:%s" "$commit")" refs/stash "${commit}" + -m "$(command git log -1 --pretty="format:%s" "$commit")" refs/stash "${commit}" done diff --git a/modules/git/functions/git-submodule-move b/modules/git/functions/git-submodule-move index d2d5ae4..2f7132c 100644 --- a/modules/git/functions/git-submodule-move +++ b/modules/git/functions/git-submodule-move @@ -1,11 +1,4 @@ -# -# Moves a Git submodule. -# -# Authors: -# Sorin Ionescu -# - -if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then +if ! is-true "$(command git rev-parse --is-inside-work-tree 2> /dev/null)"; then print "${0}: not a repository work tree: ${PWD}" >&2 return 1 elif [[ "${PWD}" != "$(git-root)" ]]; then @@ -17,7 +10,7 @@ local src="${1}" local dst="${2}" local url -url="$(git config --file "$(git-root)/.gitmodules" --get "submodule.${src}.url")" +url="$(command git config --file "$(git-root)/.gitmodules" --get "submodule.${src}.url")" if [[ -z "${url}" ]]; then print "${0}: submodule not found: ${src}" >&2 diff --git a/modules/git/functions/git-submodule-remove b/modules/git/functions/git-submodule-remove index 51c214b..033d39a 100644 --- a/modules/git/functions/git-submodule-remove +++ b/modules/git/functions/git-submodule-remove @@ -1,26 +1,19 @@ -# -# Removes a Git submodule. -# -# Authors: -# Sorin Ionescu -# - -if ! is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then +if ! is-true "$(command git rev-parse --is-inside-work-tree 2> /dev/null)"; then print "${0}: not a repository work tree: ${PWD}" >&2 return 1 elif [[ "${PWD}" != "$(git-root)" ]]; then print "${0}: must be run from the root of the work tree" >&2 return 1 -elif ! git config --file .gitmodules --get "submodule.${1}.path" &>/dev/null; then +elif ! command git config --file .gitmodules --get "submodule.${1}.path" &>/dev/null; then print "${0}: submodule not found: ${1}" >&2 return 1 fi -git config --file "$(git-dir)/config" --remove-section "submodule.${1}" &>/dev/null -git config --file "$(git-root)/.gitmodules" --remove-section "submodule.${1}" &>/dev/null -git add .gitmodules +command git config --file "$(git-dir)/config" --remove-section "submodule.${1}" &>/dev/null +command git config --file "$(git-root)/.gitmodules" --remove-section "submodule.${1}" &>/dev/null +command git add .gitmodules -git rm --cached -rf "${1}" +command git rm --cached -rf "${1}" rm -rf "${1}" rm -rf "$(git-dir)/modules/${1}" diff --git a/modules/git/functions/is-true b/modules/git/functions/is-true new file mode 100644 index 0000000..e56f2bb --- /dev/null +++ b/modules/git/functions/is-true @@ -0,0 +1,5 @@ +# Checks a boolean variable for "true". +# Case insensitive: "1", "y", "yes", "t", "true", "o", and "on". +is-true() { + [[ -n "$1" && "$1" == (1|[Yy]([Ee][Ss]|)|[Tt]([Rr][Uu][Ee]|)|[Oo]([Nn]|)) ]] +}