From 0a7451999ef2d206784fed314cc67b319fb653fb Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Wed, 10 Jan 2018 10:25:25 -0500 Subject: [PATCH] [git] Review all functions but ones using `git fsck` as there are multiple solutions to list the lost commits and stashes, but I could not find reliable ones. See https://stackoverflow.com/q/89332/2654518. Well, the current `git-commit-lost` and `git-stash-dropped` implementations don't accurately work at least on the repositories I'm working with. The `git-hub` functions where removed at dcc3265, but aliases and docs remained. Remove these. Remove line break from `_git_log_oneline_format`, so it really occupies one line. And change date format in `glG` output from committer date (`%cd`) to relative author date (`%ar`) to conform with other formats. Refactor remaining functions. Most of the changes refer to using the already existing git error messages and return codes, instead of having code that creates that. (Error messages for 'not in a git repository' where not even consistent among functions, and there's were I started this...) The most drastic changes can be seen in `git-root`, then in `git-dir` and `git-stash-clear-interactive`. --- modules/git/README.md | 2 -- modules/git/functions/git-dir | 13 ++++--------- modules/git/functions/git-ignore-add | 10 ++++------ modules/git/functions/git-root | 11 ++--------- .../git/functions/git-stash-clear-interactive | 19 +++++++------------ modules/git/functions/git-stash-recover | 11 ++++------- modules/git/functions/git-submodule-move | 15 ++++++--------- modules/git/functions/git-submodule-remove | 17 +++++++++-------- modules/git/functions/git_current_branch | 13 +++++++------ modules/git/init.zsh | 7 +++---- 10 files changed, 46 insertions(+), 72 deletions(-) diff --git a/modules/git/README.md b/modules/git/README.md index b1db63a..bc08b33 100644 --- a/modules/git/README.md +++ b/modules/git/README.md @@ -211,8 +211,6 @@ Functions - `git-branch-current` displays the current branch. - `git-commit-lost` lists lost commits. - `git-dir` displays the path to the Git directory. - - `git-hub-browse` opens the GitHub repository in the default browser. - - `git-hub-shorten-url` shortens GitHub URLs. - `git-ignore-add` adds any arguments to the .gitignore in the project root. - `git-root` displays the path to the working tree root. - `git-stash-clear-interactive` asks for confirmation before clearing the stash. diff --git a/modules/git/functions/git-dir b/modules/git/functions/git-dir index 215c849..b18901f 100644 --- a/modules/git/functions/git-dir +++ b/modules/git/functions/git-dir @@ -1,9 +1,4 @@ -local git_dir="${$(command git rev-parse --git-dir):A}" - -if [[ -n "${git_dir}" ]]; then - print "${git_dir}" - return 0 -else - print "${0}: not a repository: ${PWD}" >&2 - return 1 -fi +# vim:et sts=2 sw=2 ft=zsh +local git_dir +git_dir=$(command git rev-parse --git-dir) || return 1 +print ${git_dir:A} diff --git a/modules/git/functions/git-ignore-add b/modules/git/functions/git-ignore-add index bb05ab8..b532345 100644 --- a/modules/git/functions/git-ignore-add +++ b/modules/git/functions/git-ignore-add @@ -1,10 +1,8 @@ -# make sure we have a git-root -if ! git-root &> /dev/null; then - print 'not in a git repository' >&2 - return 1 -fi +# vim:et sts=2 sw=2 ft=zsh +local git_root +git_root=$(git-root) || return 1 # we are in a git repository. add parameters to .gitignore for file in "${@}"; do - print "${file}" >>! $(git-root)/.gitignore + print "${file}" >>! "${git_root}/.gitignore" done diff --git a/modules/git/functions/git-root b/modules/git/functions/git-root index 7ed44ff..af4d0a5 100644 --- a/modules/git/functions/git-root +++ b/modules/git/functions/git-root @@ -1,9 +1,2 @@ -local root="$(command git rev-parse --show-toplevel 2> /dev/null)" - -if [[ -n "${root}" ]]; then - print "${root}" - return 0 -else - print "${0}: not a repository work tree: ${PWD}" >&2 - return 1 -fi +# vim:et sts=2 sw=2 ft=zsh +command git rev-parse --show-toplevel diff --git a/modules/git/functions/git-stash-clear-interactive b/modules/git/functions/git-stash-clear-interactive index 1342364..eb93178 100644 --- a/modules/git/functions/git-stash-clear-interactive +++ b/modules/git/functions/git-stash-clear-interactive @@ -1,15 +1,10 @@ -if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then - print "${0}: not a repository work tree: ${PWD}" >&2 - return 1 -fi +# vim:et sts=2 sw=2 ft=zsh +setopt LOCAL_OPTIONS PIPE_FAIL -local stashed - -if [[ -f "$(git-dir)/refs/stash" ]]; then - 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 - command git stash clear - fi +local -i stashed +stashed=$(command git stash list | wc -l) || return 1 +if (( stashed )); then + if read -q "?Clear ${stashed} stashed state(s) [y/N]? "; then + command git stash clear fi fi diff --git a/modules/git/functions/git-stash-recover b/modules/git/functions/git-stash-recover index cdfbc2f..ad44ab1 100644 --- a/modules/git/functions/git-stash-recover +++ b/modules/git/functions/git-stash-recover @@ -1,11 +1,8 @@ -if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then - print "${0}: not a repository work tree: ${PWD}" >&2 - return 1 -fi +# vim:et sts=2 sw=2 ft=zsh +command git rev-parse --is-inside-work-tree >/dev/null || return 1 local commit - for commit in "${@}"; do - git update-ref \ - -m "$(command git log -1 --pretty="format:%s" "$commit")" refs/stash "${commit}" + git update-ref -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 0596785..a3878d5 100644 --- a/modules/git/functions/git-submodule-move +++ b/modules/git/functions/git-submodule-move @@ -1,24 +1,21 @@ -if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then - print "${0}: not a repository work tree: ${PWD}" >&2 - return 1 -elif [[ "${PWD}" != "$(git-root)" ]]; then +# vim:et sts=2 sw=2 ft=zsh +local git_root +git_root=$(git-root) || return 1 + +if [[ ${PWD} != ${git_root} ]]; then print "${0}: must be run from the root of the work tree" >&2 return 1 fi local src="${1}" local dst="${2}" -local url - -url="$(command git config --file "$(git-root)/.gitmodules" --get "submodule.${src}.url")" - +local url=$(command git config --file .gitmodules --get "submodule.${src}.url") if [[ -z "${url}" ]]; then print "${0}: submodule not found: ${src}" >&2 return 1 fi mkdir -p "${dst:h}" - git-submodule-remove "${src}" git submodule add "${url}" "${dst}" diff --git a/modules/git/functions/git-submodule-remove b/modules/git/functions/git-submodule-remove index aa4c852..4ad00c6 100644 --- a/modules/git/functions/git-submodule-remove +++ b/modules/git/functions/git-submodule-remove @@ -1,7 +1,8 @@ -if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then - print "${0}: not a repository work tree: ${PWD}" >&2 - return 1 -elif [[ "${PWD}" != "$(git-root)" ]]; then +# vim:et sts=2 sw=2 ft=zsh +local git_dir +git_dir=$(git-dir) || return 1 + +if [[ ${PWD} != $(git-root) ]]; then print "${0}: must be run from the root of the work tree" >&2 return 1 elif ! command git config --file .gitmodules --get "submodule.${1}.path" &>/dev/null; then @@ -9,12 +10,12 @@ elif ! command git config --file .gitmodules --get "submodule.${1}.path" &>/dev/ return 1 fi -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 config --file "${git_dir}/config" --remove-section "submodule.${1}" &>/dev/null +command git config --file .gitmodules --remove-section "submodule.${1}" &>/dev/null command git add .gitmodules -command git rm --cached -rf "${1}" +command git rm --cached "${1}" &>/dev/null rm -rf "${1}" -rm -rf "$(git-dir)/modules/${1}" +rm -rf "${git_dir}/modules/${1}" return 0 diff --git a/modules/git/functions/git_current_branch b/modules/git/functions/git_current_branch index 7325a61..40c7068 100644 --- a/modules/git/functions/git_current_branch +++ b/modules/git/functions/git_current_branch @@ -1,9 +1,10 @@ +# vim:et sts=2 sw=2 ft=zsh # slightly modified git_current_branch from oh-my-zsh for theme compatibility local ref -ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null) -local ret=${?} -if [[ ${ret} != 0 ]]; then - [[ ${ret} == 128 ]] && return # no git repo. - ref=$(command git rev-parse --short HEAD 2> /dev/null) || return +ref=$(command git symbolic-ref -q --short HEAD 2>/dev/null) +local -i ret=${?} +if (( ret )); then + (( ret == 128 )) && return # no git repo. + ref=$(command git rev-parse --short HEAD 2>/dev/null) || return fi -print ${ref#refs/heads/} +print ${ref} diff --git a/modules/git/init.zsh b/modules/git/init.zsh index ccaae72..113d0c8 100644 --- a/modules/git/init.zsh +++ b/modules/git/init.zsh @@ -9,8 +9,8 @@ # Log colour scheme has yellow commit hash, bold blue author, cyan date, auto ref names # See https://git-scm.com/docs/pretty-formats _git_log_medium_format='%C(bold)Commit:%C(reset) %C(yellow)%H%C(auto)%d%n%C(bold)Author:%C(reset) %C(bold blue)%an <%ae>%n%C(bold)Date:%C(reset) %C(cyan)%ai (%ar)%C(reset)%n%+B' -_git_log_oneline_format='%C(yellow)%h%C(reset) %s%C(auto)%d%C(reset)%n' -_git_log_fullgraph_format='%C(yellow)%h%C(reset) %<|(60,trunc)%s %C(bold blue)<%an> %C(reset)%C(cyan)(%cd)%C(auto)%d%C(reset)%n' +_git_log_oneline_format='%C(yellow)%h%C(reset) %s%C(auto)%d%C(reset)' +_git_log_fullgraph_format='%C(yellow)%h%C(reset) %<|(60,trunc)%s %C(bold blue)<%an> %C(reset)%C(cyan)(%ar)%C(auto)%d%C(reset)%n' _git_log_brief_format='%C(yellow)%h%C(reset) %s%n%C(bold blue)(%ar by %an)%C(auto)%d%C(reset)%n' # @@ -136,7 +136,6 @@ alias gRm='git remote rename' alias gRu='git remote update' alias gRp='git remote prune' alias gRs='git remote show' -alias gRb='git-hub-browse' # Stash (s) alias gs='git stash' @@ -182,4 +181,4 @@ alias gwx='git rm -r' alias gwX='git rm -r --force' # Misc -alias g..='cd $(git-root || print .)' +alias g..='cd "$(git-root || print .)"'