[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`.
This commit is contained in:
Eric Nielsen 2018-01-10 10:25:25 -05:00
parent 4156606e67
commit 0a7451999e
10 changed files with 46 additions and 72 deletions

View File

@ -211,8 +211,6 @@ Functions
- `git-branch-current` displays the current branch. - `git-branch-current` displays the current branch.
- `git-commit-lost` lists lost commits. - `git-commit-lost` lists lost commits.
- `git-dir` displays the path to the Git directory. - `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-ignore-add` adds any arguments to the .gitignore in the project root.
- `git-root` displays the path to the working tree root. - `git-root` displays the path to the working tree root.
- `git-stash-clear-interactive` asks for confirmation before clearing the stash. - `git-stash-clear-interactive` asks for confirmation before clearing the stash.

View File

@ -1,9 +1,4 @@
local git_dir="${$(command git rev-parse --git-dir):A}" # vim:et sts=2 sw=2 ft=zsh
local git_dir
if [[ -n "${git_dir}" ]]; then git_dir=$(command git rev-parse --git-dir) || return 1
print "${git_dir}" print ${git_dir:A}
return 0
else
print "${0}: not a repository: ${PWD}" >&2
return 1
fi

View File

@ -1,10 +1,8 @@
# make sure we have a git-root # vim:et sts=2 sw=2 ft=zsh
if ! git-root &> /dev/null; then local git_root
print 'not in a git repository' >&2 git_root=$(git-root) || return 1
return 1
fi
# we are in a git repository. add parameters to .gitignore # we are in a git repository. add parameters to .gitignore
for file in "${@}"; do for file in "${@}"; do
print "${file}" >>! $(git-root)/.gitignore print "${file}" >>! "${git_root}/.gitignore"
done done

View File

@ -1,9 +1,2 @@
local root="$(command git rev-parse --show-toplevel 2> /dev/null)" # vim:et sts=2 sw=2 ft=zsh
command git rev-parse --show-toplevel
if [[ -n "${root}" ]]; then
print "${root}"
return 0
else
print "${0}: not a repository work tree: ${PWD}" >&2
return 1
fi

View File

@ -1,15 +1,10 @@
if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then # vim:et sts=2 sw=2 ft=zsh
print "${0}: not a repository work tree: ${PWD}" >&2 setopt LOCAL_OPTIONS PIPE_FAIL
return 1
fi
local stashed local -i stashed
stashed=$(command git stash list | wc -l) || return 1
if [[ -f "$(git-dir)/refs/stash" ]]; then if (( stashed )); then
stashed="$(command git stash list 2> /dev/null | wc -l | awk '{print $1}')" if read -q "?Clear ${stashed} stashed state(s) [y/N]? "; then
if (( ${stashed} > 0 )); then command git stash clear
if read -q "?Clear ${stashed} stashed state(s) [y/N]? "; then
command git stash clear
fi
fi fi
fi fi

View File

@ -1,11 +1,8 @@
if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then # vim:et sts=2 sw=2 ft=zsh
print "${0}: not a repository work tree: ${PWD}" >&2 command git rev-parse --is-inside-work-tree >/dev/null || return 1
return 1
fi
local commit local commit
for commit in "${@}"; do for commit in "${@}"; do
git update-ref \ git update-ref -m "$(command git log -1 --pretty='format:%s' ${commit})" \
-m "$(command git log -1 --pretty="format:%s" "$commit")" refs/stash "${commit}" refs/stash ${commit}
done done

View File

@ -1,24 +1,21 @@
if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then # vim:et sts=2 sw=2 ft=zsh
print "${0}: not a repository work tree: ${PWD}" >&2 local git_root
return 1 git_root=$(git-root) || return 1
elif [[ "${PWD}" != "$(git-root)" ]]; then
if [[ ${PWD} != ${git_root} ]]; then
print "${0}: must be run from the root of the work tree" >&2 print "${0}: must be run from the root of the work tree" >&2
return 1 return 1
fi fi
local src="${1}" local src="${1}"
local dst="${2}" local dst="${2}"
local url local url=$(command git config --file .gitmodules --get "submodule.${src}.url")
url="$(command git config --file "$(git-root)/.gitmodules" --get "submodule.${src}.url")"
if [[ -z "${url}" ]]; then if [[ -z "${url}" ]]; then
print "${0}: submodule not found: ${src}" >&2 print "${0}: submodule not found: ${src}" >&2
return 1 return 1
fi fi
mkdir -p "${dst:h}" mkdir -p "${dst:h}"
git-submodule-remove "${src}" git-submodule-remove "${src}"
git submodule add "${url}" "${dst}" git submodule add "${url}" "${dst}"

View File

@ -1,7 +1,8 @@
if ! command git rev-parse --is-inside-work-tree >/dev/null 2>&1; then # vim:et sts=2 sw=2 ft=zsh
print "${0}: not a repository work tree: ${PWD}" >&2 local git_dir
return 1 git_dir=$(git-dir) || return 1
elif [[ "${PWD}" != "$(git-root)" ]]; then
if [[ ${PWD} != $(git-root) ]]; then
print "${0}: must be run from the root of the work tree" >&2 print "${0}: must be run from the root of the work tree" >&2
return 1 return 1
elif ! command git config --file .gitmodules --get "submodule.${1}.path" &>/dev/null; then 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 return 1
fi fi
command git config --file "$(git-dir)/config" --remove-section "submodule.${1}" &>/dev/null 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 .gitmodules --remove-section "submodule.${1}" &>/dev/null
command git add .gitmodules command git add .gitmodules
command git rm --cached -rf "${1}" command git rm --cached "${1}" &>/dev/null
rm -rf "${1}" rm -rf "${1}"
rm -rf "$(git-dir)/modules/${1}" rm -rf "${git_dir}/modules/${1}"
return 0 return 0

View File

@ -1,9 +1,10 @@
# vim:et sts=2 sw=2 ft=zsh
# slightly modified git_current_branch from oh-my-zsh for theme compatibility # slightly modified git_current_branch from oh-my-zsh for theme compatibility
local ref local ref
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null) ref=$(command git symbolic-ref -q --short HEAD 2>/dev/null)
local ret=${?} local -i ret=${?}
if [[ ${ret} != 0 ]]; then if (( ret )); then
[[ ${ret} == 128 ]] && return # no git repo. (( ret == 128 )) && return # no git repo.
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return ref=$(command git rev-parse --short HEAD 2>/dev/null) || return
fi fi
print ${ref#refs/heads/} print ${ref}

View File

@ -9,8 +9,8 @@
# Log colour scheme has yellow commit hash, bold blue author, cyan date, auto ref names # Log colour scheme has yellow commit hash, bold blue author, cyan date, auto ref names
# See https://git-scm.com/docs/pretty-formats # 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_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_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)(%cd)%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)(%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' _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 gRu='git remote update'
alias gRp='git remote prune' alias gRp='git remote prune'
alias gRs='git remote show' alias gRs='git remote show'
alias gRb='git-hub-browse'
# Stash (s) # Stash (s)
alias gs='git stash' alias gs='git stash'
@ -182,4 +181,4 @@ alias gwx='git rm -r'
alias gwX='git rm -r --force' alias gwX='git rm -r --force'
# Misc # Misc
alias g..='cd $(git-root || print .)' alias g..='cd "$(git-root || print .)"'