From 53bb80b7f98e55733361489d3294afefafba1169 Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Sat, 4 Jul 2020 10:46:34 +1000 Subject: [PATCH 01/24] init preview script --- helper/preview.sh | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 helper/preview.sh diff --git a/helper/preview.sh b/helper/preview.sh new file mode 100755 index 0000000..80402e4 --- /dev/null +++ b/helper/preview.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +# +# dynamic preview command for dotbare +# borrowed and modified from fzf.vim +# +# @params +# Globals +# reverse_seq: reverse highlight sequence for easier print +# reset_seq: reset highlight sequence for eaiser print +# input_option: the array containing informaiton from cmd argument +# preview_file: the file path to be previewed +# preview_center: the center line of the preview, if not set, display entire file +# Arguments +# $1: The filename and line info to be previewed +# Format: filepath[:lineno][:ignored] +# Example +# preview "$HOME/.bashrc:15" + +function display_preview() { + local fallback_cmd preview_cmd preview_file preview_first preview_last preview_center + preview_file="$1" + preview_first="$2" + preview_center="$3" + preview_last="$4" + fallback_cmd="highlight -O ansi -l {} || coderay {} || rougify {} || cat {}" + preview_cmd=${DOTBARE_PREVIEW:-${fallback_cmd}} + preview_cmd=${preview_cmd//{\}/$(printf %q "${preview_file}")} + + if [[ "$#" -lt 2 ]]; then + if [[ -z "${DOTBARE_PREVIEW}" ]] && command -v bat > /dev/null; then + bat --style="${BAT_STYLE:-numbers}" --color=always --pager=never "${preview_file}" + exit $? + fi + eval "${preview_cmd}" 2> /dev/null + exit 0 + else + if [ -z "${DOTBARE_PREVIEW}" ] && command -v bat > /dev/null; then + bat --style="${BAT_STYLE:-numbers}" --color=always --pager=never \ + --line-range="${preview_first}":"${preview_last}" --highlight-line="${preview_center}" "${preview_file}" + exit $? + fi + eval "${preview_cmd}" 2> /dev/null \ + | awk "NR >= ${preview_first} && NR <= ${preview_last} { \ + if (NR == ${preview_center}) \ + { gsub(/\x1b[[0-9;]*m/, \"&${reverse_seq}\"); printf(\"${reverse_seq}%s\n${reset_seq}\", \$0); } \ + else \ + printf(\"${reset_seq}%s\n\", \$0); \ + }" + exit 0 + fi +} + +reverse_seq="\x1b[7m" +reset_seq="\x1b[m" + +IFS=':' read -r -a input_option <<< "$1" +preview_file=${input_option[0]} +preview_center=${input_option[1]} + +# potential fix for windows? Although I don't think dotbare will be usable in windows yet +if [[ $1 =~ ^[A-Z]:\\ ]]; then + preview_file=$preview_file:${input_option[1]} + preview_center=${input_option[2]} +fi + +preview_file="${preview_file/#\~\//$HOME/}" +if [ ! -r "${preview_file}" ]; then + echo "File not found ${preview_file}" + exit 1 +fi + +file_length="${#preview_file}" + +file_mime=$(file --dereference --mime "${preview_file}") +if [[ "${file_mime:file_length}" =~ binary ]]; then + echo "${file_mime}" + exit 0 +fi + +[[ -n "${preview_center}" && ! "${preview_center}" =~ ^[0-9] ]] && display_preview "${preview_file}" + +[[ -z "${preview_center}" ]] && display_preview "${preview_file}" + +preview_center=${preview_center/[^0-9]*/} + +if [ -r /dev/tty ]; then + preview_lines=$(stty size < /dev/tty | awk '{print $1}') +else + preview_lines=40 +fi + +preview_first=$(($preview_center-$preview_lines/3)) +preview_first=$(($preview_first < 1 ? 1 : $preview_first)) +preview_last=$((${preview_first}+${preview_lines}-1)) + +display_preview "${preview_file}" "${preview_first}" "${preview_center}" "${preview_last}" From b70e87c93f18478452c2e91a4328ffd54dade7c0 Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Sat, 4 Jul 2020 19:37:24 +1000 Subject: [PATCH 02/24] fix: fix the preview script --- helper/preview.sh | 58 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/helper/preview.sh b/helper/preview.sh index 80402e4..2afa7f0 100755 --- a/helper/preview.sh +++ b/helper/preview.sh @@ -9,13 +9,30 @@ # reset_seq: reset highlight sequence for eaiser print # input_option: the array containing informaiton from cmd argument # preview_file: the file path to be previewed -# preview_center: the center line of the preview, if not set, display entire file +# preview_center: the center/highlight line of the preview, if not set, display entire file +# preview_lines: total lines to be previed, used to determine the first line and last line to be printed +# preview_first: the first line number of the file to be printed +# preview_last: the last line number of the file to be printed +# file_mime: mime of the file, used to display information for binary file # Arguments # $1: The filename and line info to be previewed # Format: filepath[:lineno][:ignored] # Example # preview "$HOME/.bashrc:15" +####################################### +# display the preview of the file +# construct argument for the action, execute and exit +# Arguments: +# $1: the file path to be previewed +# $2: the first line number to be previewd, optional +# $3: the center/highlight line in the preview, optional +# $4: the last line number to be previewed, optional +# Note: +# if the line number information such as $2 $3 $4 is not given, the entire +# file will be printed. +# Otherwise only the $2-$4 parts of the file will be printed and $3 will be highlighted +####################################### function display_preview() { local fallback_cmd preview_cmd preview_file preview_first preview_last preview_center preview_file="$1" @@ -26,16 +43,16 @@ function display_preview() { preview_cmd=${DOTBARE_PREVIEW:-${fallback_cmd}} preview_cmd=${preview_cmd//{\}/$(printf %q "${preview_file}")} - if [[ "$#" -lt 2 ]]; then + if [[ -z "${preview_first}" ]] || [[ -z "${preview_center}" ]] || [[ -z "${preview_last}" ]]; then if [[ -z "${DOTBARE_PREVIEW}" ]] && command -v bat > /dev/null; then - bat --style="${BAT_STYLE:-numbers}" --color=always --pager=never "${preview_file}" + bat --style="${BAT_STYLE:-plain}" --color=always --pager=never "${preview_file}" exit $? fi eval "${preview_cmd}" 2> /dev/null exit 0 else if [ -z "${DOTBARE_PREVIEW}" ] && command -v bat > /dev/null; then - bat --style="${BAT_STYLE:-numbers}" --color=always --pager=never \ + bat --style="${BAT_STYLE:-plain}" --color=always --pager=never \ --line-range="${preview_first}":"${preview_last}" --highlight-line="${preview_center}" "${preview_file}" exit $? fi @@ -69,28 +86,33 @@ if [ ! -r "${preview_file}" ]; then exit 1 fi -file_length="${#preview_file}" - +# if binary, display binary info and exit file_mime=$(file --dereference --mime "${preview_file}") -if [[ "${file_mime:file_length}" =~ binary ]]; then - echo "${file_mime}" - exit 0 -fi - -[[ -n "${preview_center}" && ! "${preview_center}" =~ ^[0-9] ]] && display_preview "${preview_file}" +[[ "${file_mime}" =~ binary ]] \ + && echo "${file_mime}" \ + && exit 0 +# if no line number was given, just preview the entire file [[ -z "${preview_center}" ]] && display_preview "${preview_file}" +# if invalid line number was given, just preview the entire file +[[ -n "${preview_center}" && ! "${preview_center}" =~ ^[0-9] ]] && display_preview "${preview_file}" + +# get the size of the termianl window and determine the first line and last line preview_center=${preview_center/[^0-9]*/} -if [ -r /dev/tty ]; then - preview_lines=$(stty size < /dev/tty | awk '{print $1}') +if [ -n "${FZF_PREVIEW_LINES}" ]; then + preview_lines="${FZF_PREVIEW_LINES}" else - preview_lines=40 + if [ -r /dev/tty ]; then + preview_lines=$(stty size < /dev/tty | awk '{print $1}') + else + preview_lines=40 + fi fi -preview_first=$(($preview_center-$preview_lines/3)) -preview_first=$(($preview_first < 1 ? 1 : $preview_first)) -preview_last=$((${preview_first}+${preview_lines}-1)) +preview_first=$((preview_center-preview_lines/3)) +preview_first=$((preview_first < 1 ? 1 : preview_first)) +preview_last=$((preview_first+preview_lines-1)) display_preview "${preview_file}" "${preview_first}" "${preview_center}" "${preview_last}" From 983e89ee14a5015c335f847284521103d27d5afc Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Sat, 4 Jul 2020 19:37:42 +1000 Subject: [PATCH 03/24] docs: update changelog and readme --- CHANGELOG.md | 15 ++++++++++++--- README.md | 10 +++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29e87e8..eb8905e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,16 @@ Noteble changes are documentated in this file. -## 1.2.0 +## dev + +### Added + +- dynamic preview (detect bats, hightlight etc) +- Custom preview ENV (DOTBARE_PREVIEW) + Note: has to be this format `export DOTBARE_PREVIEW='cat {}'`, the `{}` is + used for fzf to subsitute for the filepath. + +## 1.2.0 (01/07/2020) ### Added @@ -27,7 +36,7 @@ Noteble changes are documentated in this file. behavior is actually achieved by `dotbare fcheckout -a`, use `dotbare fcheckout -a` instead. (Edit: `dotbare fcheckout -a` is now `dotbare fcheckout -s` or `dotbare fcheckout --select`) -## 1.1.0 +## 1.1.0 (28/06/2020) ### Added @@ -47,4 +56,4 @@ Noteble changes are documentated in this file. - Removed global .gitignore manipulation during migration, not needed. Added .gitignore tips to README and let user handle it -## 1.0.0 +## 1.0.0 (20/05/2020) diff --git a/README.md b/README.md index c27c7f5..55c7fd3 100644 --- a/README.md +++ b/README.md @@ -338,7 +338,7 @@ within your dotfile directory. ```sh # Default -DOTBARE_DIR="$HOME/.cfg" +export DOTBARE_DIR="$HOME/.cfg" ``` ### DOTBARE_TREE @@ -350,7 +350,7 @@ set this variable to point to the folder containing your dotfiles. ```sh # Default -DOTBARE_TREE="$HOME" +export DOTBARE_TREE="$HOME" ``` ### DOTBARE_BACKUP @@ -364,7 +364,7 @@ automatically backup conflicted files to this location. # Default # 2. If XDG_DATA_HOME exist, use XDG_DATA_HOME/dotbare # 3. otherwise, use $HOME/.local/share/dotbare -DOTBARE_BACKUP="${XDG_DATA_HOME:-$HOME/.local/share}/dotbare" +export DOTBARE_BACKUP="${XDG_DATA_HOME:-$HOME/.local/share}/dotbare" ``` ### EDITOR @@ -374,7 +374,7 @@ which editor to use when running `dotbare fedit`. ```sh # Default -EDITOR="vim" +export EDITOR="vim" ``` ### DOTBARE_KEY @@ -384,7 +384,7 @@ to set [here](https://github.com/junegunn/fzf/blob/97a725fbd0e54cbc07e4d72661ea2 ```sh # Default -DOTBARE_KEY=" +export DOTBARE_KEY=" --bind=alt-a:toggle-all # toggle all selection --bind=alt-j:jump # label jump mode, sort of like vim-easymotion --bind=alt-0:top # set cursor back to top From 20cccb1f8d3a3d7d9d4603f062c37cc2686843af Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Sat, 4 Jul 2020 21:43:59 +1000 Subject: [PATCH 04/24] feat: show relative date in git log helper and use the preview script in fedit and search_file --- helper/git_query.sh | 6 ++++-- helper/search_file.sh | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/helper/git_query.sh b/helper/git_query.sh index e46efa0..96602f3 100644 --- a/helper/git_query.sh +++ b/helper/git_query.sh @@ -4,6 +4,7 @@ ####################################### # let user select a commit interactively +# credit to forgit for the git log format # Arguments: # $1: the helper message to display in the fzf header # $2: files to show diff against HEAD @@ -19,7 +20,7 @@ function get_commit() { local files=("${@:2}") if [[ "${#files[@]}" -eq 0 ]]; then /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ - log --oneline --color=always --decorate=short \ + log --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' \ | fzf --no-multi --header="${header}" \ --preview "echo {} \ | awk '{print \$1}' \ @@ -86,13 +87,14 @@ function get_branch() { # e.g.$HOME/.config/nvim/init.vim ####################################### function get_git_file() { + local mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" local header="${1:-select tracked file}" local print_opt="${2:-full}" set_fzf_multi "$3" /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ ls-files --full-name --directory "${DOTBARE_TREE}" \ | fzf --header="${header}" \ - --preview "cat ${DOTBARE_TREE}/{}" \ + --preview "${mydir}/../helper/preview.sh ${DOTBARE_TREE}/{}" \ | awk -v home="${DOTBARE_TREE}" -v print_opt="${print_opt}" '{ if (print_opt == "full") { print home "/" $0 diff --git a/helper/search_file.sh b/helper/search_file.sh index d0514af..87c6ad3 100644 --- a/helper/search_file.sh +++ b/helper/search_file.sh @@ -11,8 +11,9 @@ ####################################### function search_file() { local search_type="$1" + local mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" if [[ "${search_type}" == "f" ]]; then - find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --preview "cat {}" + find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --preview "${mydir}/preview.sh {}" elif [[ "${search_type}" == "d" ]]; then if tree --version &>/dev/null; then find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){gsub(/^\.\//, "", $0);print $0}}' | fzf --multi --preview "tree -L 1 -C --dirsfirst {}" From 9353df5552e7873ea3a61ff4576a05bcbd10522d Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Sat, 4 Jul 2020 21:47:06 +1000 Subject: [PATCH 05/24] fix(test): avoid shellcheck fail --- helper/git_query.sh | 3 ++- helper/search_file.sh | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/helper/git_query.sh b/helper/git_query.sh index 96602f3..84718a5 100644 --- a/helper/git_query.sh +++ b/helper/git_query.sh @@ -87,9 +87,10 @@ function get_branch() { # e.g.$HOME/.config/nvim/init.vim ####################################### function get_git_file() { - local mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + local mydir local header="${1:-select tracked file}" local print_opt="${2:-full}" + mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" set_fzf_multi "$3" /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ ls-files --full-name --directory "${DOTBARE_TREE}" \ diff --git a/helper/search_file.sh b/helper/search_file.sh index 87c6ad3..7850e4c 100644 --- a/helper/search_file.sh +++ b/helper/search_file.sh @@ -10,8 +10,8 @@ # A user selected file path ####################################### function search_file() { - local search_type="$1" - local mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + local search_type="$1" mydir + mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" if [[ "${search_type}" == "f" ]]; then find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --preview "${mydir}/preview.sh {}" elif [[ "${search_type}" == "d" ]]; then From 12765d9b0281c16d0f6df0c89efb2bacaf40d21b Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Sat, 4 Jul 2020 22:09:11 +1000 Subject: [PATCH 06/24] feat: support tools like diff-so-fancy and delta for pretty diff preview --- CHANGELOG.md | 2 ++ helper/git_query.sh | 12 ++++++++---- helper/set_variable.sh | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb8905e..72aa483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ Noteble changes are documentated in this file. - Custom preview ENV (DOTBARE_PREVIEW) Note: has to be this format `export DOTBARE_PREVIEW='cat {}'`, the `{}` is used for fzf to subsitute for the filepath. +- Added support for external diff tools like "diff-so-fancy" or "delta" + This is optional, only takes effect if installed and set as `git config core.pager` ## 1.2.0 (01/07/2020) diff --git a/helper/git_query.sh b/helper/git_query.sh index 84718a5..1a5249a 100644 --- a/helper/git_query.sh +++ b/helper/git_query.sh @@ -25,7 +25,8 @@ function get_commit() { --preview "echo {} \ | awk '{print \$1}' \ | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ - show --color=always __" \ + show --color=always __ \ + | ${DOTBARE_DIFF_PAGER}" \ | awk '{print $1}' else /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ @@ -33,7 +34,8 @@ function get_commit() { | fzf --no-multi --header="${header}" --preview "echo {} \ | awk '{print \$1}' \ | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ - diff --color=always __ ${files[*]}" \ + diff --color=always __ ${files[*]} \ + | ${DOTBARE_DIFF_PAGER}" \ | awk '{print $1}' fi } @@ -146,7 +148,8 @@ function get_modified_file() { | fzf --header="${header}" --preview "echo {} \ | awk '{sub(\$1 FS,\"\");print \$0}' \ | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ - diff HEAD --color=always -- ${DOTBARE_TREE}/__" \ + diff HEAD --color=always -- ${DOTBARE_TREE}/__ \ + | ${DOTBARE_DIFF_PAGER}" \ | awk -v home="${DOTBARE_TREE}" -v format="${output_format}" '{ if (format == "name") { $1="" @@ -179,7 +182,8 @@ function get_stash() { print \$1 }' \ | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ - show -p __ --color=always" \ + stash show -p __ --color=always \ + | ${DOTBARE_DIFF_PAGER}" \ | awk '{ gsub(/:/, "", $1) print $1 diff --git a/helper/set_variable.sh b/helper/set_variable.sh index f5c1377..667cbbc 100644 --- a/helper/set_variable.sh +++ b/helper/set_variable.sh @@ -16,6 +16,7 @@ export DOTBARE_DIR="${DOTBARE_DIR:-$HOME/.cfg/}" export DOTBARE_TREE="${DOTBARE_TREE:-$HOME}" export DOTBARE_BACKUP="${DOTBARE_BACKUP:-${XDG_DATA_HOME:-$HOME/.local/share}/dotbare}" export DOTBARE_VERSION="v1.2.0" +export DOTBARE_DIFF_PAGER=$(git config core.pager || echo 'cat') export EDITOR="${EDITOR:-vim}" if [[ -z "${DOTBARE_KEY}" ]]; then From 248802baa8ea588e97126211b278ce04063f7f57 Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Sat, 4 Jul 2020 22:25:55 +1000 Subject: [PATCH 07/24] fix(test): shellcheck .. --- helper/set_variable.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/helper/set_variable.sh b/helper/set_variable.sh index 667cbbc..c29ec5e 100644 --- a/helper/set_variable.sh +++ b/helper/set_variable.sh @@ -16,7 +16,8 @@ export DOTBARE_DIR="${DOTBARE_DIR:-$HOME/.cfg/}" export DOTBARE_TREE="${DOTBARE_TREE:-$HOME}" export DOTBARE_BACKUP="${DOTBARE_BACKUP:-${XDG_DATA_HOME:-$HOME/.local/share}/dotbare}" export DOTBARE_VERSION="v1.2.0" -export DOTBARE_DIFF_PAGER=$(git config core.pager || echo 'cat') + +export DOTBARE_DIFF_PAGER="${DOTBARE_DIFF_PAGER:-$(git config core.pager || echo 'cat')}" export EDITOR="${EDITOR:-vim}" if [[ -z "${DOTBARE_KEY}" ]]; then From b78fc7e027fea8ecadb5a44871af0fe62120fe70 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 10:54:00 +1000 Subject: [PATCH 08/24] feat: update branch log preivew to align with git log new style --- CHANGELOG.md | 1 + helper/git_query.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72aa483..fc4fd6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Noteble changes are documentated in this file. used for fzf to subsitute for the filepath. - Added support for external diff tools like "diff-so-fancy" or "delta" This is optional, only takes effect if installed and set as `git config core.pager` + Also configurable through DOTBARE_DIFF_PAGER ## 1.2.0 (01/07/2020) diff --git a/helper/git_query.sh b/helper/git_query.sh index 1a5249a..d197f60 100644 --- a/helper/git_query.sh +++ b/helper/git_query.sh @@ -75,7 +75,7 @@ function get_branch() { } }' \ | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ - log --oneline --graph --color=always --decorate=short __" + log --color=always --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' __" } ####################################### From 1bcdfe5ccefeb3f282f2747867859308b4d06aee Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 11:05:08 +1000 Subject: [PATCH 09/24] chore(preview): remove unnecessary flag with bat --- helper/preview.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helper/preview.sh b/helper/preview.sh index 2afa7f0..c2ec354 100755 --- a/helper/preview.sh +++ b/helper/preview.sh @@ -45,14 +45,14 @@ function display_preview() { if [[ -z "${preview_first}" ]] || [[ -z "${preview_center}" ]] || [[ -z "${preview_last}" ]]; then if [[ -z "${DOTBARE_PREVIEW}" ]] && command -v bat > /dev/null; then - bat --style="${BAT_STYLE:-plain}" --color=always --pager=never "${preview_file}" + bat --color=always --pager=never "${preview_file}" exit $? fi eval "${preview_cmd}" 2> /dev/null exit 0 else if [ -z "${DOTBARE_PREVIEW}" ] && command -v bat > /dev/null; then - bat --style="${BAT_STYLE:-plain}" --color=always --pager=never \ + bat --color=always --pager=never \ --line-range="${preview_first}":"${preview_last}" --highlight-line="${preview_center}" "${preview_file}" exit $? fi From abafd6b7afe1ca709bf9add2bf5a9af201f90406 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 11:06:27 +1000 Subject: [PATCH 10/24] refactor(main): remove the /usr/bin/git invoke, directly use git commands Main reason is that this is not really needed and it makes all the git testing impossible to mock and lead to inconsistent test result in different environments --- dotbare | 2 +- helper/git_query.sh | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/dotbare b/dotbare index b34fc7b..b8793be 100755 --- a/dotbare +++ b/dotbare @@ -67,4 +67,4 @@ case "$1" in ;; esac -/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" "$@" +git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" "$@" diff --git a/helper/git_query.sh b/helper/git_query.sh index d197f60..b2a58c3 100644 --- a/helper/git_query.sh +++ b/helper/git_query.sh @@ -19,21 +19,21 @@ function get_commit() { local header="${1:-select a commit}" local files=("${@:2}") if [[ "${#files[@]}" -eq 0 ]]; then - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ log --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' \ | fzf --no-multi --header="${header}" \ --preview "echo {} \ | awk '{print \$1}' \ - | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ + | xargs -I __ git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ show --color=always __ \ | ${DOTBARE_DIFF_PAGER}" \ | awk '{print $1}' else - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ log --oneline --color=always --decorate=short \ | fzf --no-multi --header="${header}" --preview "echo {} \ | awk '{print \$1}' \ - | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ + | xargs -I __ git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ diff --color=always __ ${files[*]} \ | ${DOTBARE_DIFF_PAGER}" \ | awk '{print $1}' @@ -50,7 +50,7 @@ function get_commit() { ####################################### function get_branch() { local header="${1:-select a branch}" - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" branch -a \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" branch -a \ | awk '{ if ($0 ~ /\*.*\(HEAD.*/) { gsub(/\* /, "", $0) @@ -74,7 +74,7 @@ function get_branch() { print \$0 } }' \ - | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ + | xargs -I __ git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ log --color=always --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' __" } @@ -94,7 +94,7 @@ function get_git_file() { local print_opt="${2:-full}" mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" set_fzf_multi "$3" - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ ls-files --full-name --directory "${DOTBARE_TREE}" \ | fzf --header="${header}" \ --preview "${mydir}/../helper/preview.sh ${DOTBARE_TREE}/{}" \ @@ -130,7 +130,7 @@ function get_modified_file() { local display_mode="${2:-all}" local output_format="${3:-name}" set_fzf_multi "$4" - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ status --porcelain \ | awk -v display_mode="${display_mode}" '{ if ($0 ~ /^[A-Za-z][A-Za-z].*$/) { @@ -147,7 +147,7 @@ function get_modified_file() { }' \ | fzf --header="${header}" --preview "echo {} \ | awk '{sub(\$1 FS,\"\");print \$0}' \ - | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ + | xargs -I __ git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ diff HEAD --color=always -- ${DOTBARE_TREE}/__ \ | ${DOTBARE_DIFF_PAGER}" \ | awk -v home="${DOTBARE_TREE}" -v format="${output_format}" '{ @@ -174,14 +174,14 @@ function get_modified_file() { function get_stash() { local header="${1:-select a stash}" set_fzf_multi "$2" - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ stash list \ | fzf --header="${header}" --preview "echo {} \ | awk '{ gsub(/:/, \"\", \$1) print \$1 }' \ - | xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ + | xargs -I __ git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ stash show -p __ --color=always \ | ${DOTBARE_DIFF_PAGER}" \ | awk '{ From cba4e36faaf6ed750325c6107897adec0b2bbc5e Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 11:20:22 +1000 Subject: [PATCH 11/24] test: begin update all mocks --- tests/dotbare.bats | 15 ++++++++-- tests/fzf | 70 +--------------------------------------------- tests/git | 9 ++++++ tests/tree | 5 ++-- 4 files changed, 26 insertions(+), 73 deletions(-) create mode 100755 tests/git diff --git a/tests/dotbare.bats b/tests/dotbare.bats index f2e5d6a..66a2ce3 100755 --- a/tests/dotbare.bats +++ b/tests/dotbare.bats @@ -17,6 +17,7 @@ routing2() { } normal_git() { + export PATH="${BATS_TEST_DIRNAME}:$PATH" "${BATS_TEST_DIRNAME}"/../dotbare add -h } @@ -25,10 +26,14 @@ invalid_command() { } version() { - source "${BATS_TEST_DIRNAME}"/../helper/set_variable.sh "${BATS_TEST_DIRNAME}"/../dotbare --version } +no_argument() { + export PATH="${BATS_TEST_DIRNAME}:$PATH" + "${BATS_TEST_DIRNAME}"/../dotbare +} + @test "main help" { run help [ "${status}" -eq 0 ] @@ -61,10 +66,16 @@ version() { @test "main git command" { run normal_git - [ "${status}" -eq 129 ] + [ "${status}" -eq 0 ] + [[ "${output}" =~ "--git-dir=$DOTBARE_DIR --work-tree=$DOTBARE_TREE add -h" ]] } @test "main invliad command" { run invalid_command [ "${status}" -eq 1 ] } + +@test "main no argument" { + run no_argument + [[ "${output}" =~ "Available commands" ]] +} diff --git a/tests/fzf b/tests/fzf index 8b00580..0fde7a1 100755 --- a/tests/fzf +++ b/tests/fzf @@ -5,73 +5,5 @@ # # usage: # export PATH="${BATS_TEST_DIRNAME}:$PATH" -# example: -# echo "commitdiff" when attempting to mock a local file search -# error code 128 -# fatal: pathspec '$HOME/modifiedfile' did not match any files -# echo "-- modifiedfile" when attempting to mock a local file search -# error code 1 or code 128 -# error: pathspec '$HOME/modifiedfile' did not match any file(s) known to git -# echo "--branch" when attempting to mock branch commit -# error code 129 -# error: unknown option `commitshow' -# use something like tr "`" "'" to avoid any potential error bats would raise -if [[ "$*" =~ "--header=select a commit to checkout" ]] && [[ "$*" =~ "show --color" ]]; then - # dotbare fcheckout --c -- "./fcheckout.bats" @test "fcheckout commit" - echo "--fcheckout_commit" -elif [[ "$*" =~ '--no-multi --header=select the target commit for HEAD' ]] && [[ "$*" =~ "show --color" ]]; then - # dotbare freset --commit -y -- "./freset.bats" @test "freset select commit" - echo "--freset_commit" -elif [[ "$*" =~ "--no-multi --header=select a commit to rename" ]] && [[ "$*" =~ "show --color=always" ]]; then - # dotbare fedit --commit -- "./fedit.bats" @test "fedit edit commits" - echo "fedit_commits" -elif [[ "$*" =~ '--header=select a commit' ]] && [[ "$*" =~ "show --color" ]]; then - # dotbare flog --reset -y -- "./flog.bats" @test "flog reset" - echo "--flog_reset" -elif [[ "$*" =~ "--no-multi --header=select a branch to checkout" ]]; then - # dotbare fcheckout --branch -- "./fcheckout.bats" @test "fcheckout branch" - echo "--fcheckout_branch" -elif [[ "$*" =~ '--header=select a file to checkout' ]] && [[ "$*" =~ "cat ${DOTBARE_TREE}/{}" ]]; then - # dotbare fcheckout --yes -s -- "./fcheckout.bats" @test "fcheckout select" - echo "fcheckout_select_gitfile" -elif [[ "$*" =~ '--header=select files to backup' ]] && [[ "$*" =~ "cat ${DOTBARE_TREE}/{}" ]]; then - # dotbare fbackup --select -- "./fbackup.bats" @test "fbackup select file" - echo "fbackup_select_file" -elif [[ "$*" =~ "--header=select files to untrack" ]] && [[ "$*" =~ "cat ${DOTBARE_TREE}/{}" ]]; then - # dotbare funtrack -- "./funtrack.bats" @test "funtrack untrack file" - echo "funtrack_file" -elif [[ "$*" =~ '--multi --preview ' ]] && [[ "$*" =~ "tree -L 1 -C --dirsfirst {}" ]]; then - # dotbare fadd --dir -- "./fadd.bats" @test "fadd stage selected dir" - echo "fadd_stage_dir" -elif [[ "$*" =~ '--header=select the target commit' ]] && [[ "$*" =~ "diff --color" ]]; then - # dotbare fcheckout --yes -s -- "./fcheckout.bats" @test "fcheckout select" - echo "fcheckout_select_commitdiff" -elif [[ "$*" =~ '--multi --preview ' ]] && [[ "$*" =~ "cat {}" ]]; then - # dotbare fadd -f -- "./fadd.bats" @test "fadd stage selected file" - echo "fadd_stage_file" -elif [[ "$*" =~ '--header=select files to add to a stash' ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then - # dotbare fstash -s -- "./fstash.bats" @test "fstash stash select file" - echo "-- fstash_select" -elif [[ "$*" =~ '--header=select stash to delete' ]] && [[ "$*" =~ "show -p __ --color=always" ]]; then - # dotbare fstash --delete -- "./fstash.bats" @test "fstash stash delete" - echo "fstash_delete" -elif [[ "$*" =~ '--header=select stash to apply' ]] && [[ "$*" =~ "show -p __ --color=always" ]]; then - # dotbare fstash -- "./fstash.bats" @test "fstash apply stash" - echo "fstash_apply" -elif [[ "$*" =~ "--header=select a file to checkout version in HEAD" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then - # dotbare fcheckout -y -- "./fcheckout.bats" @test "fcheckout modified" - echo "-- fcheckout_modified" -elif [[ "$*" =~ '--header=select files to stage' ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then - # dotbare fadd -- "./fadd.bats" @test "fadd stage modified files" - echo "-- fadd_add_modified" -elif [[ "$*" =~ "--header=select files to unstage" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then - # dotbare freset -- "./freset.bats" @test "freset select files" - echo "-- freset_file" -elif [[ "$*" =~ "--header=select tracked files to edit" ]]; then - # dotbare fedit -- "./fedit.bats" @test "fedit edit files" - exit -elif [[ "$*" =~ "--no-multi --header=commit --flog_reset" ]]; then - # dotbare flog -- "./flog.bats" @test "flog checkout routing" - echo "exit" -fi +echo "$@" diff --git a/tests/git b/tests/git new file mode 100755 index 0000000..fbc1217 --- /dev/null +++ b/tests/git @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +# +# This script is for mocking git during unit test, +# used to mock all the git calls. +# +# usage: +# export PATH="${BATS_TEST_DIRNAME}:$PATH" + +echo "$@" diff --git a/tests/tree b/tests/tree index 0fde7a1..3338101 100755 --- a/tests/tree +++ b/tests/tree @@ -1,7 +1,8 @@ #!/usr/bin/env bash # -# This script is for mocking fzf for testing, -# it will stop fzf for goin into a interactive mode +# This script is for mocking tree during unit test, +# just that we don't need install tree during different +# test environment. # # usage: # export PATH="${BATS_TEST_DIRNAME}:$PATH" From 27c923a55aa050b0feb475619320ad708120cce8 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 11:49:17 +1000 Subject: [PATCH 12/24] test(fadd): update fadd test --- scripts/fadd | 2 +- tests/fadd.bats | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/fadd b/scripts/fadd index 49e5f1f..dff6e2d 100755 --- a/scripts/fadd +++ b/scripts/fadd @@ -39,7 +39,7 @@ function usage() { function stage_file() { local files=("$@") [[ "${#files[@]}" -eq 0 ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${files[@]}" } stage_type="modified" diff --git a/tests/fadd.bats b/tests/fadd.bats index e86ef89..c6e04c6 100755 --- a/tests/fadd.bats +++ b/tests/fadd.bats @@ -47,18 +47,18 @@ stage_modified_file() { @test "fadd stage selected file" { run stage_selected_file - [ "${status}" -eq 128 ] - [[ "${output}" =~ "fadd_stage_file" ]] + [[ "${output}" =~ "add --multi --preview" ]] + [[ "${output}" =~ "preview.sh {}" ]] } @test "fadd stage selected dir" { run stage_selected_dir - [ "${status}" -eq 128 ] - [[ "${output}" =~ "fadd_stage_dir" ]] + [[ "${output}" =~ "add --multi --preview tree -L 1 -C --dirsfirst {}" ]] } @test "fadd stage modified file" { run stage_modified_file - [ "${status}" -eq 128 ] - [[ "${output}" =~ "fadd_add_modified" ]] + [[ "${output}" =~ "add" ]] + [[ "${output}" =~ "files to stage --preview echo {}" ]] + [[ "${output}" =~ "diff HEAD --color=always" ]] } From 245b9a66a2eea98e70419db42517a4b046c8ff2a Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 11:49:26 +1000 Subject: [PATCH 13/24] test(fbackup): update fbackup test --- scripts/fbackup | 2 +- tests/fbackup.bats | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/scripts/fbackup b/scripts/fbackup index 6158fd7..c38971b 100755 --- a/scripts/fbackup +++ b/scripts/fbackup @@ -89,7 +89,7 @@ cd "${DOTBARE_TREE}" if [[ "${backup_type}" == 'select' ]]; then selected_files=$(get_git_file "select files to backup" "raw") else - selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + selected_files=$(git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ ls-files --full-name --directory "${DOTBARE_TREE}") fi diff --git a/tests/fbackup.bats b/tests/fbackup.bats index ba7cf24..df73bfc 100755 --- a/tests/fbackup.bats +++ b/tests/fbackup.bats @@ -32,14 +32,10 @@ move_file() { bash "${BATS_TEST_DIRNAME}"/../dotbare fbackup --path bats_test.txt -m } -select_file() { - export PATH="${BATS_TEST_DIRNAME}:$PATH" - bash "${BATS_TEST_DIRNAME}"/../dotbare fbackup --select -} - @test "fbackup help" { run help [ "${status}" -eq 0 ] + [ "${lines[0]}" = "Usage: dotbare fbackup [-h] [-m] [-s] [-p PATH] ..." ] } @test "fbackup invalid option" { @@ -71,10 +67,3 @@ select_file() { [ -f "${DOTBARE_BACKUP}"/bats_test.txt ] [ ! -f "${BATS_TEST_DIRNAME}"/bats_test.txt ] } - -@test "fbackup select file" { - run select_file - [ "${status}" -eq 1 ] - [[ "${output}" =~ 'No such file or directory' ]] - [[ "${output}" =~ 'fbackup_select_file' ]] -} From 4c2a5a9b28a5789b1c053995e0447a6ee3fa107a Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 12:01:59 +1000 Subject: [PATCH 14/24] test(fcheckout): update test for fcheckout --- helper/git_query.sh | 4 ++-- scripts/fcheckout | 8 ++++---- tests/fcheckout.bats | 30 +++++++++--------------------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/helper/git_query.sh b/helper/git_query.sh index b2a58c3..3fb92f9 100644 --- a/helper/git_query.sh +++ b/helper/git_query.sh @@ -21,7 +21,7 @@ function get_commit() { if [[ "${#files[@]}" -eq 0 ]]; then git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ log --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' \ - | fzf --no-multi --header="${header}" \ + | fzf --header="${header}" --no-multi \ --preview "echo {} \ | awk '{print \$1}' \ | xargs -I __ git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ @@ -31,7 +31,7 @@ function get_commit() { else git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ log --oneline --color=always --decorate=short \ - | fzf --no-multi --header="${header}" --preview "echo {} \ + | fzf --header="${header}" --no-multi --preview "echo {} \ | awk '{print \$1}' \ | xargs -I __ git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \ diff --color=always __ ${files[*]} \ diff --git a/scripts/fcheckout b/scripts/fcheckout index 018ae9f..62a4458 100755 --- a/scripts/fcheckout +++ b/scripts/fcheckout @@ -79,12 +79,12 @@ if [[ "${action_type}" == "branch" ]]; then # checkout branch selected_branch=$(get_branch 'select a branch to checkout') [[ -z "${selected_branch}" ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_branch}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_branch}" elif [[ "${action_type}" == "commit" ]]; then # checkout commit selected_commit=$(get_commit 'select a commit to checkout') [[ -z "${selected_commit}" ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}" elif [[ "${action_type}" == "modified" ]]; then # checkout modified file back to version in HEAD while IFS= read -r line; do @@ -94,7 +94,7 @@ elif [[ "${action_type}" == "modified" ]]; then [[ -z "${confirm}" ]] && echo "(dryrun) dotbare checkout --" "${selected_files[@]}" [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?") [[ "${confirm}" != 'y' ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout -- "${selected_files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout -- "${selected_files[@]}" elif [[ "${action_type}" == "select" ]]; then # checkout selected files to a selected commit while IFS= read -r line; do @@ -107,5 +107,5 @@ elif [[ "${action_type}" == "select" ]]; then [[ -z "${confirm}" ]] && echo "(dryrun) dotbare checkout ${selected_commit} --" "${selected_files[@]}" [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?") [[ "${confirm}" != 'y' ]] && exit 0 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}" "${selected_files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}" "${selected_files[@]}" fi diff --git a/tests/fcheckout.bats b/tests/fcheckout.bats index 329bf6b..4955de3 100755 --- a/tests/fcheckout.bats +++ b/tests/fcheckout.bats @@ -41,38 +41,26 @@ checkout_selected_file() { } @test "fcheckout branch" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run checkout_branch - [ "${status}" -eq 129 ] - [[ "${output}" =~ "fcheckout_branch" ]] + [[ "${output}" =~ "checkout" ]] + [[ "${output}" =~ "--no-multi --header=select a branch to checkout" ]] } @test "fcheckout commit" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run checkout_commit - [ "${status}" -eq 129 ] - [[ "${output}" =~ "fcheckout_commit" ]] + [[ "${output}" =~ "checkout --header=select" ]] } @test "fcheckout modified" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run checkout_modified_file - [ "${status}" -eq 1 ] - [[ "${output}" =~ "fcheckout_modified" ]] + [[ "${output}" =~ "checkout" ]] + [[ "${output}" =~ "file to checkout version in HEAD" ]] + [[ "${output}" =~ "diff HEAD --color=always" ]] } @test "fcheckout select" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run checkout_selected_file - [[ "${lines[0]}" =~ "fcheckout_select_commitdiff" ]] - [[ "${lines[1]}" =~ "fcheckout_select_gitfile" ]] - [ "${status}" -eq 1 ] + [[ "${output}" =~ "checkout" ]] + [[ "${output}" =~ "--header=select a file to checkout" ]] + [[ "${output}" =~ "preview.sh" ]] } From d897c5f2b4f416644c2fd01c0075bfc76d777507 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 13:43:51 +1000 Subject: [PATCH 15/24] revert: revert the fzf mock file --- tests/fzf | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/fzf b/tests/fzf index 0fde7a1..c8e6f40 100755 --- a/tests/fzf +++ b/tests/fzf @@ -6,4 +6,60 @@ # usage: # export PATH="${BATS_TEST_DIRNAME}:$PATH" -echo "$@" +if [[ "$*" =~ '--multi --preview ' ]] && [[ "$*" =~ "tree -L 1 -C --dirsfirst {}" ]]; then + # dotbare fadd --dir -- "./fadd.bats" @test "fadd stage selected dir" + echo "fadd_stage_dir" +elif [[ "$*" =~ '--multi --preview' ]] && [[ "$*" =~ "preview.sh {}" ]]; then + # dotbare fadd -f -- "./fadd.bats" @test "fadd stage selected file" + echo "fadd_stage_file" +elif [[ "$*" =~ '--header=select files to stage' ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then + # dotbare fadd -- "./fadd.bats" @test "fadd stage modified files" + echo "fadd_stage_modified" +elif [[ "$*" =~ "--no-multi --header=select a branch to checkout" ]]; then + # dotbare fcheckout --branch -- "./fcheckout.bats" @test "fcheckout branch" + echo "fcheckout_branch" +elif [[ "$*" =~ "--header=select a commit to checkout" ]] && [[ "$*" =~ "show --color" ]]; then + # dotbare fcheckout --c -- "./fcheckout.bats" @test "fcheckout commit" + echo "fcheckout_commit" +elif [[ "$*" =~ "--header=select a file to checkout version in HEAD" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then + # dotbare fcheckout -y -- "./fcheckout.bats" @test "fcheckout modified" + echo "-- fcheckout_modified" +elif [[ "$*" =~ '--header=select the target commit' ]] && [[ "$*" =~ "diff --color" ]]; then + # dotbare fcheckout --yes -s -- "./fcheckout.bats" @test "fcheckout select" + echo "fcheckout_select_commitdiff" +elif [[ "$*" =~ '--header=select a file to checkout' ]] && [[ "$*" =~ "preview.sh ${DOTBARE_TREE}/{}" ]]; then + # dotbare fcheckout --yes -s -- "./fcheckout.bats" @test "fcheckout select" + echo "fcheckout_select_gitfile" +elif [[ "$*" =~ '--no-multi --header=select the target commit for HEAD' ]] && [[ "$*" =~ "show --color" ]]; then + # dotbare freset --commit -y -- "./freset.bats" @test "freset select commit" + echo "--freset_commit" +elif [[ "$*" =~ "--no-multi --header=select a commit to rename" ]] && [[ "$*" =~ "show --color=always" ]]; then + # dotbare fedit --commit -- "./fedit.bats" @test "fedit edit commits" + echo "fedit_commits" +elif [[ "$*" =~ '--header=select a commit' ]] && [[ "$*" =~ "show --color" ]]; then + # dotbare flog --reset -y -- "./flog.bats" @test "flog reset" + echo "--flog_reset" +elif [[ "$*" =~ "--header=select files to untrack" ]] && [[ "$*" =~ "cat ${DOTBARE_TREE}/{}" ]]; then + # dotbare funtrack -- "./funtrack.bats" @test "funtrack untrack file" + echo "funtrack_file" +elif [[ "$*" =~ '--header=select files to add to a stash' ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then + # dotbare fstash -s -- "./fstash.bats" @test "fstash stash select file" + echo "-- fstash_select" +elif [[ "$*" =~ '--header=select stash to delete' ]] && [[ "$*" =~ "show -p __ --color=always" ]]; then + # dotbare fstash --delete -- "./fstash.bats" @test "fstash stash delete" + echo "fstash_delete" +elif [[ "$*" =~ '--header=select stash to apply' ]] && [[ "$*" =~ "show -p __ --color=always" ]]; then + # dotbare fstash -- "./fstash.bats" @test "fstash apply stash" + echo "fstash_apply" +elif [[ "$*" =~ "--header=select files to unstage" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then + # dotbare freset -- "./freset.bats" @test "freset select files" + echo "-- freset_file" +elif [[ "$*" =~ "--header=select tracked files to edit" ]]; then + # dotbare fedit -- "./fedit.bats" @test "fedit edit files" + exit +elif [[ "$*" =~ "--no-multi --header=commit --flog_reset" ]]; then + # dotbare flog -- "./flog.bats" @test "flog checkout routing" + echo "exit" +else + echo "$@" +fi From 42f530d611486d01aa3e0906891ff532e99eb243 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 13:44:06 +1000 Subject: [PATCH 16/24] test: update fcheckout and fadd test with the reverted fzf executable --- tests/fadd.bats | 8 +++----- tests/fcheckout.bats | 12 +++++------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/fadd.bats b/tests/fadd.bats index c6e04c6..1114f8d 100755 --- a/tests/fadd.bats +++ b/tests/fadd.bats @@ -47,18 +47,16 @@ stage_modified_file() { @test "fadd stage selected file" { run stage_selected_file - [[ "${output}" =~ "add --multi --preview" ]] - [[ "${output}" =~ "preview.sh {}" ]] + [[ "${output}" =~ "add fadd_stage_file" ]] } @test "fadd stage selected dir" { run stage_selected_dir - [[ "${output}" =~ "add --multi --preview tree -L 1 -C --dirsfirst {}" ]] + [[ "${output}" =~ "add fadd_stage_dir" ]] } @test "fadd stage modified file" { run stage_modified_file [[ "${output}" =~ "add" ]] - [[ "${output}" =~ "files to stage --preview echo {}" ]] - [[ "${output}" =~ "diff HEAD --color=always" ]] + [[ "${output}" =~ "fadd_stage_modified" ]] } diff --git a/tests/fcheckout.bats b/tests/fcheckout.bats index 4955de3..d9a9130 100755 --- a/tests/fcheckout.bats +++ b/tests/fcheckout.bats @@ -42,25 +42,23 @@ checkout_selected_file() { @test "fcheckout branch" { run checkout_branch - [[ "${output}" =~ "checkout" ]] - [[ "${output}" =~ "--no-multi --header=select a branch to checkout" ]] + [[ "${output}" =~ "checkout fcheckout_branch" ]] } @test "fcheckout commit" { run checkout_commit - [[ "${output}" =~ "checkout --header=select" ]] + [[ "${output}" =~ "checkout fcheckout_commit" ]] } @test "fcheckout modified" { run checkout_modified_file [[ "${output}" =~ "checkout" ]] - [[ "${output}" =~ "file to checkout version in HEAD" ]] - [[ "${output}" =~ "diff HEAD --color=always" ]] + [[ "${output}" =~ "fcheckout_modified" ]] } @test "fcheckout select" { run checkout_selected_file [[ "${output}" =~ "checkout" ]] - [[ "${output}" =~ "--header=select a file to checkout" ]] - [[ "${output}" =~ "preview.sh" ]] + [[ "${output}" =~ "fcheckout_select_gitfile" ]] + [[ "${output}" =~ "fcheckout_select_commitdiff" ]] } From 7f24d4319a829968e736ce4e54fd080080647833 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 14:34:22 +1000 Subject: [PATCH 17/24] test(fedit): update fedit test --- scripts/fedit | 2 +- tests/fedit.bats | 7 +++---- tests/fzf | 12 ++++++------ 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/scripts/fedit b/scripts/fedit index 518a2c1..e6c2cbd 100755 --- a/scripts/fedit +++ b/scripts/fedit @@ -59,7 +59,7 @@ done if [[ "${edit_type}" == "commit" ]]; then selected_commit=$(get_commit "select a commit to rename") [[ -z "${selected_commit}" ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rebase -i "${selected_commit}"~ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rebase -i "${selected_commit}"~ else while IFS= read -r line; do selected_files+=("${line}") diff --git a/tests/fedit.bats b/tests/fedit.bats index 4995450..fee0883 100755 --- a/tests/fedit.bats +++ b/tests/fedit.bats @@ -41,15 +41,14 @@ edit_files() { } @test "fedit edit commits" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run edit_commits + [[ "${output}" =~ "rebase -i" ]] [[ "${output}" =~ "fedit_commits" ]] } @test "fedit edit files" { run edit_files + # to avoid actually invoking vim, the fzf mock file exit without pringting anything + # when detecting the correct flag called with fzf [ "${status}" -eq 1 ] - [ -z "${output}" ] } diff --git a/tests/fzf b/tests/fzf index c8e6f40..46aeb57 100755 --- a/tests/fzf +++ b/tests/fzf @@ -30,12 +30,15 @@ elif [[ "$*" =~ '--header=select the target commit' ]] && [[ "$*" =~ "diff --col elif [[ "$*" =~ '--header=select a file to checkout' ]] && [[ "$*" =~ "preview.sh ${DOTBARE_TREE}/{}" ]]; then # dotbare fcheckout --yes -s -- "./fcheckout.bats" @test "fcheckout select" echo "fcheckout_select_gitfile" +elif [[ "$*" =~ "--header=select a commit to rename --no-multi" ]] && [[ "$*" =~ "show --color=always" ]]; then + # dotbare fedit --commit -- "./fedit.bats" @test "fedit edit commits" + echo "fedit_commits" +elif [[ "$*" =~ "--header=select tracked files to edit" ]]; then + # dotbare fedit -- "./fedit.bats" @test "fedit edit files" + exit elif [[ "$*" =~ '--no-multi --header=select the target commit for HEAD' ]] && [[ "$*" =~ "show --color" ]]; then # dotbare freset --commit -y -- "./freset.bats" @test "freset select commit" echo "--freset_commit" -elif [[ "$*" =~ "--no-multi --header=select a commit to rename" ]] && [[ "$*" =~ "show --color=always" ]]; then - # dotbare fedit --commit -- "./fedit.bats" @test "fedit edit commits" - echo "fedit_commits" elif [[ "$*" =~ '--header=select a commit' ]] && [[ "$*" =~ "show --color" ]]; then # dotbare flog --reset -y -- "./flog.bats" @test "flog reset" echo "--flog_reset" @@ -54,9 +57,6 @@ elif [[ "$*" =~ '--header=select stash to apply' ]] && [[ "$*" =~ "show -p __ -- elif [[ "$*" =~ "--header=select files to unstage" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then # dotbare freset -- "./freset.bats" @test "freset select files" echo "-- freset_file" -elif [[ "$*" =~ "--header=select tracked files to edit" ]]; then - # dotbare fedit -- "./fedit.bats" @test "fedit edit files" - exit elif [[ "$*" =~ "--no-multi --header=commit --flog_reset" ]]; then # dotbare flog -- "./flog.bats" @test "flog checkout routing" echo "exit" From b1ee2f11fa05df4c479405cd8f844eb0341a6bd8 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 14:35:56 +1000 Subject: [PATCH 18/24] refactor(finit): remove the /usr/bin/git invokation --- scripts/finit | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/finit b/scripts/finit index 9a60fc5..5a17332 100755 --- a/scripts/finit +++ b/scripts/finit @@ -82,17 +82,17 @@ if [[ -z "${remote_url}" ]]; then exit 1 else git init --bare "${DOTBARE_DIR}" - /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \ + git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \ config --local status.showUntrackedFiles no fi else [[ ! -d "${DOTBARE_TREE}" ]] && mkdir -p "${DOTBARE_TREE}" cd "${DOTBARE_TREE}" git clone --bare "${remote_url}" "${DOTBARE_DIR}" - if ! /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2> /dev/null; then + if ! git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2> /dev/null; then echo "File checkout failed" echo "Backing up pre-existing dotfiles ..." - /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2>&1 \ + git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2>&1 \ | awk '{ if ($0 ~ /[\t].*/) { gsub(/^[\t]/, "", $0) @@ -101,9 +101,9 @@ else }' \ | xargs -I __ "${mydir}"/fbackup -p __ -m echo "dotfiles backup succeeded, checkout continue" - /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout + git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout fi - /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \ + git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \ config --local status.showUntrackedFiles no echo "File checkout succeeded" @@ -111,7 +111,7 @@ else case "${hook}" in submodule) echo "Cloning submodules ..." - /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \ + git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \ submodule update --init --recursive ;; esac From ec37d7914bd81e7626b454eef88dace1b3e7fc98 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 14:58:43 +1000 Subject: [PATCH 19/24] test(flog): update flog test --- scripts/flog | 10 +++++----- tests/flog.bats | 12 ++---------- tests/fzf | 6 +++--- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/scripts/flog b/scripts/flog index 89711ee..21bf562 100755 --- a/scripts/flog +++ b/scripts/flog @@ -59,7 +59,7 @@ function draw_menu() { menu="${menu}checkout: checkout the selected commit\n" menu="${menu}exit: quit dotbare flog" message=$( - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ log --format=%B -n 1 "${selected_commit}" ) header="commit ${selected_commit}: ${message}" @@ -130,19 +130,19 @@ fi case "${selected_action}" in revert) - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ revert "${selected_commit}" ;; reset) - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ reset "${selected_commit}" ;; edit) - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ rebase -i "${selected_commit}"~ ;; checkout) - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ checkout "${selected_commit}" ;; exit) diff --git a/tests/flog.bats b/tests/flog.bats index 1f7b58c..e237907 100755 --- a/tests/flog.bats +++ b/tests/flog.bats @@ -30,20 +30,12 @@ reset() { [ "${lines[0]}" = "Invalid option: -p" ] } -@test "flog check routing" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi +@test "flog menu" { run menu [ "${status}" -eq 0 ] } @test "flog reset" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run reset - [ "${status}" -eq 129 ] - [[ "${output}" =~ "usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] []" ]] - [[ "${output}" =~ "flog_reset" ]] + [[ "${output}" =~ "reset --flog_reset" ]] } diff --git a/tests/fzf b/tests/fzf index 46aeb57..0681015 100755 --- a/tests/fzf +++ b/tests/fzf @@ -39,6 +39,9 @@ elif [[ "$*" =~ "--header=select tracked files to edit" ]]; then elif [[ "$*" =~ '--no-multi --header=select the target commit for HEAD' ]] && [[ "$*" =~ "show --color" ]]; then # dotbare freset --commit -y -- "./freset.bats" @test "freset select commit" echo "--freset_commit" +elif [[ "$*" =~ "--no-multi --header=commit --flog_reset" ]]; then + # dotbare flog -- "./flog.bats" @test "flog checkout routing" + echo "exit" elif [[ "$*" =~ '--header=select a commit' ]] && [[ "$*" =~ "show --color" ]]; then # dotbare flog --reset -y -- "./flog.bats" @test "flog reset" echo "--flog_reset" @@ -57,9 +60,6 @@ elif [[ "$*" =~ '--header=select stash to apply' ]] && [[ "$*" =~ "show -p __ -- elif [[ "$*" =~ "--header=select files to unstage" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then # dotbare freset -- "./freset.bats" @test "freset select files" echo "-- freset_file" -elif [[ "$*" =~ "--no-multi --header=commit --flog_reset" ]]; then - # dotbare flog -- "./flog.bats" @test "flog checkout routing" - echo "exit" else echo "$@" fi From c8e5a68d8f248252251daafc24b5915c03364d9e Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 15:06:12 +1000 Subject: [PATCH 20/24] test(freset): update freset test --- scripts/freset | 4 ++-- tests/freset.bats | 11 ++--------- tests/fzf | 10 +++++----- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/scripts/freset b/scripts/freset index 176b833..281f5ed 100755 --- a/scripts/freset +++ b/scripts/freset @@ -80,11 +80,11 @@ if [[ "${reset_type}" == "commit" ]]; then [[ -z "${selected_commit}" ]] && exit 1 [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Reset HEAD to ${selected_commit} ${reset_option}?") [[ "${confirm}" != 'y' ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commit}" "${reset_option}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commit}" "${reset_option}" else while IFS= read -r line; do selected_files+=("${line}") done < <(get_modified_file 'select files to unstage' 'staged') [[ "${#selected_files[@]}" -eq 0 ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_files[@]}" fi diff --git a/tests/freset.bats b/tests/freset.bats index 3049d05..fea4c0e 100755 --- a/tests/freset.bats +++ b/tests/freset.bats @@ -40,19 +40,12 @@ select_files() { } @test "freset select commit" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run select_commit - [ "${status}" -eq 129 ] - [[ "${output}" =~ "freset_commit" ]] + [[ "${output}" =~ "reset freset_commit --mixed" ]] } @test "freset select files" { - if ! "${BATS_TEST_DIRNAME}"/../dotbare log &>/dev/null; then - skip - fi run select_files - [ "${status}" -eq 128 ] + [[ "${output}" =~ "reset" ]] [[ "${output}" =~ "freset_file" ]] } diff --git a/tests/fzf b/tests/fzf index 0681015..a2278f1 100755 --- a/tests/fzf +++ b/tests/fzf @@ -36,9 +36,12 @@ elif [[ "$*" =~ "--header=select a commit to rename --no-multi" ]] && [[ "$*" =~ elif [[ "$*" =~ "--header=select tracked files to edit" ]]; then # dotbare fedit -- "./fedit.bats" @test "fedit edit files" exit -elif [[ "$*" =~ '--no-multi --header=select the target commit for HEAD' ]] && [[ "$*" =~ "show --color" ]]; then +elif [[ "$*" =~ '--header=select the target commit for HEAD --no-multi' ]] && [[ "$*" =~ "show --color" ]]; then # dotbare freset --commit -y -- "./freset.bats" @test "freset select commit" - echo "--freset_commit" + echo "freset_commit" +elif [[ "$*" =~ "--header=select files to unstage" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then + # dotbare freset -- "./freset.bats" @test "freset select files" + echo "-- freset_file" elif [[ "$*" =~ "--no-multi --header=commit --flog_reset" ]]; then # dotbare flog -- "./flog.bats" @test "flog checkout routing" echo "exit" @@ -57,9 +60,6 @@ elif [[ "$*" =~ '--header=select stash to delete' ]] && [[ "$*" =~ "show -p __ - elif [[ "$*" =~ '--header=select stash to apply' ]] && [[ "$*" =~ "show -p __ --color=always" ]]; then # dotbare fstash -- "./fstash.bats" @test "fstash apply stash" echo "fstash_apply" -elif [[ "$*" =~ "--header=select files to unstage" ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then - # dotbare freset -- "./freset.bats" @test "freset select files" - echo "-- freset_file" else echo "$@" fi From 85066017be4f7c56356508de3c2bf919333de674 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 15:15:18 +1000 Subject: [PATCH 21/24] test(freset): update freset test --- scripts/fstash | 6 +++--- tests/fstash.bats | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/fstash b/scripts/fstash index 62e170b..e0d3a01 100755 --- a/scripts/fstash +++ b/scripts/fstash @@ -74,7 +74,7 @@ if [[ "${stash_command}" == "select" ]]; then selected_files+=("${line}") done < <(get_modified_file "select files to add to a stash") [[ "${#selected_files[@]}" -eq 0 ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash -- "${selected_files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash -- "${selected_files[@]}" elif [[ "${stash_command}" == "delete" ]]; then selected_stash=$(get_stash "select stash to delete") [[ -z "${selected_stash}" ]] && exit 1 @@ -85,7 +85,7 @@ elif [[ "${stash_command}" == "delete" ]]; then [[ -z "${confirm}" ]] && confirm=$(get_confirmation) [[ "${confirm}" != 'y' ]] && exit 1 while IFS= read -r line; do - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash drop "${line}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash drop "${line}" done <<< "${selected_stash}" else selected_stash=$(get_stash "select stash to apply" "true") @@ -93,5 +93,5 @@ else [[ -z "${confirm}" ]] && echo "(dryrun) ${stash_command} ${selected_stash}" [[ -z "${confirm}" ]] && confirm=$(get_confirmation) [[ "${confirm}" != 'y' ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash "${stash_command}" "${selected_stash}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash "${stash_command}" "${selected_stash}" fi diff --git a/tests/fstash.bats b/tests/fstash.bats index bad27e7..31277fd 100755 --- a/tests/fstash.bats +++ b/tests/fstash.bats @@ -38,18 +38,16 @@ stash_apply() { @test "fstash stash select file" { run stash_file - [ "${status}" -eq 1 ] + [[ "${output}" =~ "stash" ]] [[ "${output}" =~ "fstash_select" ]] } @test "fstash stash delete" { run stash_delete - [ "${status}" -eq 1 ] - [[ "${output}" =~ "fstash_delete" ]] + [[ "${output}" =~ "stash drop fstash_delete" ]] } @test "fstash apply stash" { run stash_apply - [ "${status}" -eq 1 ] - [[ "${output}" =~ "fstash_apply" ]] + [[ "${output}" =~ "stash apply fstash_apply" ]] } From 450625de1dccd8cdcc418d672ac740a455bd4339 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 15:18:43 +1000 Subject: [PATCH 22/24] test(funtrack): update funtrack test --- scripts/fstat | 4 ++-- scripts/funtrack | 6 +++--- tests/funtrack.bats | 10 +++++----- tests/fzf | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/fstat b/scripts/fstat index 772f081..e9e9a83 100755 --- a/scripts/fstat +++ b/scripts/fstat @@ -77,8 +77,8 @@ while :; do ) if [[ -z "${stage_file}" ]]; then - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset --quiet HEAD "${selected_filenames[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset --quiet HEAD "${selected_filenames[@]}" else - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${selected_filenames[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${selected_filenames[@]}" fi done diff --git a/scripts/funtrack b/scripts/funtrack index bf2fbd4..2820fd0 100755 --- a/scripts/funtrack +++ b/scripts/funtrack @@ -78,7 +78,7 @@ if [[ "${track_type}" == "temp" ]]; then [[ -z "${confirm}" ]] && echo "(dryrun) dotbare update-index --assume-unchanged" "${selected_files[@]}" [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Files will be temporarily stop being tracked for changes, continue?") [[ "${confirm}" != 'y' ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --assume-unchanged "${selected_files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --assume-unchanged "${selected_files[@]}" echo -e " " echo "Selected files are temporarily untracked by git, use dotbare funtrack -r to continue tracking changes." echo "Although dotbare funtrack -t won't delete the files on other machines, it is not the recommended way to untrack files." @@ -88,7 +88,7 @@ elif [[ "${track_type}" == "retrack" ]]; then [[ -z "${confirm}" ]] && echo "(dryrun) dotbare update-index --no-assume-unchanged" "${selected_files[@]}" [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Files will resume being tracked by git, continue?") [[ "${confirm}" != 'y' ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --no-assume-unchanged "${selected_files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --no-assume-unchanged "${selected_files[@]}" echo " " echo "Selected files are being resumed for tracking by git." echo "Although dotbare funtrack -t won't delete the files on other machines, it is not the recommended way to untrack files." @@ -98,7 +98,7 @@ else [[ -z "${confirm}" ]] && echo "(dryrun) dotbare rm --cached" "${selected_files[@]}" [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Untrack the selected files?") [[ "${confirm}" != 'y' ]] && exit 1 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rm --cached "${selected_files[@]}" + git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rm --cached "${selected_files[@]}" echo -e " " echo "Selected files are being untracked by git, make sure to run dotbare fbackup on your other systems." echo "When other systems pull down this change, selected files will be deleted on other systems." diff --git a/tests/funtrack.bats b/tests/funtrack.bats index 4abe10c..2ccc617 100755 --- a/tests/funtrack.bats +++ b/tests/funtrack.bats @@ -38,18 +38,18 @@ resume_track() { @test "funtrack untrack file" { run untrack_file - [ "${status}" -eq 128 ] + [[ "${output}" =~ "rm --cached" ]] [[ "${output}" =~ "funtrack_file" ]] } @test "funtrack temp untrack" { run temp_untrack - [ "${status}" -eq 128 ] - [[ "${output}" =~ "fatal: Unable to mark file funtrack_file" ]] + [[ "${output}" =~ "update-index --assume-unchanged" ]] + [[ "${output}" =~ "funtrack_file" ]] } @test "funtrack resume track" { run resume_track - [ "${status}" -eq 128 ] - [[ "${output}" =~ "fatal: Unable to mark file funtrack_file" ]] + [[ "${output}" =~ "update-index --no-assume-unchanged" ]] + [[ "${output}" =~ "funtrack_file" ]] } diff --git a/tests/fzf b/tests/fzf index a2278f1..b915274 100755 --- a/tests/fzf +++ b/tests/fzf @@ -48,7 +48,7 @@ elif [[ "$*" =~ "--no-multi --header=commit --flog_reset" ]]; then elif [[ "$*" =~ '--header=select a commit' ]] && [[ "$*" =~ "show --color" ]]; then # dotbare flog --reset -y -- "./flog.bats" @test "flog reset" echo "--flog_reset" -elif [[ "$*" =~ "--header=select files to untrack" ]] && [[ "$*" =~ "cat ${DOTBARE_TREE}/{}" ]]; then +elif [[ "$*" =~ "--header=select files to untrack" ]] && [[ "$*" =~ "preview.sh ${DOTBARE_TREE}/{}" ]]; then # dotbare funtrack -- "./funtrack.bats" @test "funtrack untrack file" echo "funtrack_file" elif [[ "$*" =~ '--header=select files to add to a stash' ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then From 613305a1155aaae570f68a4ec2fa2b3ac5759e71 Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Tue, 7 Jul 2020 15:20:31 +1000 Subject: [PATCH 23/24] fix(test): fix test faile --- tests/dotbare.bats | 2 +- tests/fzf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/dotbare.bats b/tests/dotbare.bats index 66a2ce3..d8b0f3c 100755 --- a/tests/dotbare.bats +++ b/tests/dotbare.bats @@ -67,7 +67,7 @@ no_argument() { @test "main git command" { run normal_git [ "${status}" -eq 0 ] - [[ "${output}" =~ "--git-dir=$DOTBARE_DIR --work-tree=$DOTBARE_TREE add -h" ]] + [[ "${output}" =~ "add -h" ]] } @test "main invliad command" { diff --git a/tests/fzf b/tests/fzf index b915274..ba2a5b1 100755 --- a/tests/fzf +++ b/tests/fzf @@ -14,7 +14,7 @@ elif [[ "$*" =~ '--multi --preview' ]] && [[ "$*" =~ "preview.sh {}" ]]; then echo "fadd_stage_file" elif [[ "$*" =~ '--header=select files to stage' ]] && [[ "$*" =~ "diff HEAD --color=always" ]]; then # dotbare fadd -- "./fadd.bats" @test "fadd stage modified files" - echo "fadd_stage_modified" + echo "-- fadd_stage_modified" elif [[ "$*" =~ "--no-multi --header=select a branch to checkout" ]]; then # dotbare fcheckout --branch -- "./fcheckout.bats" @test "fcheckout branch" echo "fcheckout_branch" From 3a557d643edb853c71a4302872e120f3c23fad8f Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Thu, 9 Jul 2020 11:04:31 +1000 Subject: [PATCH 24/24] docs: update changelog and readme --- CHANGELOG.md | 12 +- README.md | 406 ++++++++++++--------------------------------------- 2 files changed, 97 insertions(+), 321 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc4fd6b..7b0db5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,13 +6,13 @@ Noteble changes are documentated in this file. ### Added -- dynamic preview (detect bats, hightlight etc) -- Custom preview ENV (DOTBARE_PREVIEW) - Note: has to be this format `export DOTBARE_PREVIEW='cat {}'`, the `{}` is - used for fzf to subsitute for the filepath. -- Added support for external diff tools like "diff-so-fancy" or "delta" +- dynamic preview function, detect bats, hightlight etc to provide syntax hightlighting when previewing files. +- Custom preview ENV variable (DOTBARE_PREVIEW) + Note: has to be this format `export DOTBARE_PREVIEW='cat -n {}'`, the `{}` is + used in preview functions to subsitute for the filepath. +- Added support for fancy diff tools like "diff-so-fancy" or "delta" This is optional, only takes effect if installed and set as `git config core.pager` - Also configurable through DOTBARE_DIFF_PAGER + Also configurable through DOTBARE_DIFF_PAGER, these are documentated in the README. ## 1.2.0 (01/07/2020) diff --git a/README.md b/README.md index 55c7fd3..2ebea43 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ ## Introduction -dotbare is a command line utility to help you manage your dotfiles. It wraps around git bare +`dotbare` is a command line utility to help manage dotfiles. It wraps around git bare repository and heavily utilises [fzf](https://github.com/junegunn/fzf) for an interactive experience. It is inspired by [forgit](https://github.com/wfxr/forgit), a git wrapper using fzf. -dotbare uses a different implementation approach and focuses on managing and interacting with your dotfiles. -Don't worry about migration if you have a symlink/GNU stow setup, you can easily integrate dotbare with them. +`dotbare` uses a different implementation approach and focuses on managing and interacting with system dotfiles. +Don't worry about migration if you have a symlink/GNU stow setup, you can easily integrate `dotbare` with them. Pros: @@ -25,75 +25,32 @@ Pros: You could find out how git bare repository could be used for managing dotfiles [here](https://www.atlassian.com/git/tutorials/dotfiles). Or a [video](https://www.youtube.com/watch?v=tBoLDpTWVOM&t=288s) explanation that helped me to get started. If you are currently -using a symlink/GNU stow setup, checkout how to integrate dotbare with them [here](#migrating-from-a-generic-symlink-setup-or-gnu-stow). +using a symlink/GNU stow setup, checkout how to integrate `dotbare` with them [here](#migrating-from-a-generic-symlink-setup-or-gnu-stow). -![Demo](https://user-images.githubusercontent.com/43941510/82142379-4a1e7500-987f-11ea-8d35-8588a413efd3.png) +![Demo](https://user-images.githubusercontent.com/43941510/86587894-e33f5180-bfcd-11ea-87a9-da28bb103710.png) ## Why It has always been a struggle for me to get started with managing dotfiles using version control, -as some tools like "GNU stow" really scares me off with all the symlinks, until I found +as tools like "GNU stow" really scares me off with all the symlinks, until I found out about using git bare repository for managing dotfiles, zero symlinks, minimal setup -required and you keep your dotfiles at the location they should be. +required while keeping dotfiles at the location they should be. However, it has always lack some interactive experience as it does not provide any auto completion on git commands nor file paths by default. It is also a pain when migrating the setup over to another system as you will have to manually resolve all the git checkout issues. -dotbare solves the above problems by providing a series of scripts starts with a prefix f +`dotbare` solves the above problems by providing a series of scripts starts with a prefix f (e.g. `dotbare fadd`, `dotbare flog` etc) that will enable a interactive experience by processing -all the git information and display it through fzf. dotbare also comes with the ability to integrate with +all the git information and display it through fzf. `dotbare` also comes with the ability to integrate with GNU stow or any symlink set up as long as you are using git. It is easy to migrate to any system with minimal set up required. -## Table of Contents - -- [Install](#install) - - [zsh](#zsh) - - [bash](#bash) - - [others](#others) -- [Getting started](#getting-started) - - [Dependencies](#dependencies) - - [Setup](#setup) - - [Migration](#migration) - - [Migrating from normal git bare repository](#migrating-from-normal-git-bare-repository) - - [Migrating from a generic symlink setup or GNU stow](#migrating-from-a-generic-symlink-setup-or-gnu-stow) - - [Migrating dotbare to a new system](#migrating-dotbare-to-a-new-system) - - [Test it in docker](#test-it-in-docker) -- [Customization](#customization) - - [DOTBARE_DIR: location of the git directory](#dotbare_dir) - - [DOTBARE_TREE: location of the working index](#dotbare_key) - - [DOTBARE_BACKUP: location to backup files](#dotbare_backup) - - [EDITOR: editor used to open files](#editor) - - [DOTBARE_KEY: keybinds](#dotbare_key) - - [DOTBARE_FZF_DEFAULT_OPTS: fzf customization](#dotbare_fzf_default_opts) -- [Commands](#commands) - - [Checkout all available scripts and their help manual](#checkout-all-available-scripts-and-their-help-manual) - - [fedit: edit dotfiles](#fedit) - - [fadd: stage changes](#fadd) - - [freset: unstage changes](#freset) - - [fcheckout: discard changes/checkout commits and branch](#fcheckout) - - [flog: interactive log viewer](#flog) - - [fstash: stash management](#fstash) - - [fbackup: backup tracked files](#fbackup) - - [fstat: toggle stage/unstage files](#fstat) - - [finit: initialise/migrating dotbare](#finit) - - [funtrack: untrack files](#funtrack) - - [fupgrade: update dotbare](#fupgrade) -- [Custom scripts](#custom-scripts) -- [Tips and Tricks](#tips-and-tricks) -- [Testing](#testing) -- [Contributing](#contributing) -- [Coming up](#coming-up) -- [Background](#background) -- [Credit](#credit) -- [Demo](#demo) - ## Install ### zsh -dotbare should work with any zsh plugin manager, below is only demonstration. +`dotbare` should work with any zsh plugin manager, below is only demonstration. #### zinit @@ -103,13 +60,13 @@ zinit light kazhala/dotbare #### oh-my-zsh -- Clone the repository in to [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh) plugins directory +- Clone the repository in to [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh) plugins directory. ```sh git clone https://github.com/kazhala/dotbare.git $HOME/.oh-my-zsh/custom/plugins/dotbare ``` -- Activate the plugin in `~/.zshrc` +- Activate the plugin in `~/.zshrc`. ```zsh plugins=( [plugins...] dotbare [plugins...] ) @@ -123,13 +80,13 @@ antigen bundle kazhala/dotbare #### Manual -- Clone the repository (change ~/.dotbare to the location of your preference) +- Clone the repository (change ~/.dotbare to the location of your preference). ```sh git clone https://github.com/kazhala/dotbare.git ~/.dotbare ``` -- Put below into `.zshrc` +- Put below into `.zshrc`. ```sh source ~/.dotbare/dotbare.plugin.zsh @@ -137,17 +94,16 @@ antigen bundle kazhala/dotbare ### bash -dotbare comes with a `dotbare.plugin.bash` which will enable both bash command line -completion for dotbare commands and adding dotbare to your PATH. If you don't want the completion, -follow the instructions in [others](#others) which simply add dotbare to your PATH. +`dotbare` comes with a `dotbare.plugin.bash` which will enable both bash command line +completion for dotbare commands and adding dotbare to your PATH. -- Clone the repository (change ~/.dotbare to the location of your preference) +- Clone the repository (change ~/.dotbare to the location of your preference). ```sh git clone https://github.com/kazhala/dotbare.git ~/.dotbare ``` -- Put below into `.bashrc` or `.bash_profile` +- Put below into `.bashrc` or `.bash_profile`. ```sh source ~/.dotbare/dotbare.plugin.bash @@ -155,13 +111,13 @@ follow the instructions in [others](#others) which simply add dotbare to your PA ### others -1. Clone the repository (change ~/.dotbare to the location of your preference) +1. Clone the repository (change ~/.dotbare to the location of your preference). ```sh git clone https://github.com/kazhala/dotbare.git ~/.dotbare ``` -2. Add dotbare to your PATH +2. Add `dotbare` to your PATH. ```sh # This is only an example command for posix shell @@ -169,7 +125,7 @@ follow the instructions in [others](#others) which simply add dotbare to your PA export PATH=$PATH:$HOME/.dotbare ``` -3. Or you could create a alias which point to dotbare executable + Or you could create a alias which point to dotbare executable. ```sh alias dotbare="$HOME/.dotbare/dotbare" @@ -187,16 +143,21 @@ follow the instructions in [others](#others) which simply add dotbare to your PA - Optional dependency - - [tree](https://linux.die.net/man/1/tree) (Will provide a directory tree view when finding directory) + - [tree](https://linux.die.net/man/1/tree) (Provide a directory tree view when finding directory) ```sh # if you are on macos brew install tree ``` + - [bat](https://github.com/sharkdp/bat), [highlight](http://www.andre-simon.de/doku/highlight/en/highlight.php), [coderay](https://github.com/rubychan/coderay) + or [rougify](https://github.com/rouge-ruby/rouge) (Syntax highlighting when previewing files) + - [delta](https://github.com/dandavison/delta), [diff-so-fancy](https://github.com/so-fancy/diff-so-fancy) + or any diff tools of your choice (Fancy git diff preview like in the screen shot) + ### Setup -1. init git bare repository +1. init git bare repository. Note: by default, `dotbare finit` will set up a bare repository in \$HOME/.cfg, to customize location and various other settings, checkout [customization](#customization) @@ -205,22 +166,22 @@ follow the instructions in [others](#others) which simply add dotbare to your PA dotbare finit ``` -2. add dotfiles you want to track +2. add dotfiles you want to track. - Treat dotbare as normal `git` commands. + Treat `dotbare` as normal `git` commands. ```sh dotbare fadd -f # or dotbare add [FIELNAME] - # add entire repository like .config + # add entire repository like .config directory dotbare fadd -d # or dotbare add [DIRECTORY] ``` -3. commit changes and push to remote +3. commit changes and push to remote. ```sh dotbare commit -m "First commit" @@ -232,24 +193,24 @@ follow the instructions in [others](#others) which simply add dotbare to your PA #### Migrating from normal git bare repository -1. follow the steps in [install](#install) to install dotbare -2. check your current alias of git bare reference +1. follow the steps in [install](#install) to install `dotbare`. +2. check your current alias of git bare reference. ```sh # Below is an example alias, check yours for reference alias config=/usr/bin/git --git-dir=$HOME/.cfg --work-tree=$HOME ``` -3. set env variable for dotbare +3. set env variable for `dotbare`. ```sh export DOTBARE_DIR="$HOME/.cfg" export DOTBARE_TREE="$HOME" ``` -4. remove the original alias and use `dotbare` +4. remove the original alias and use `dotbare`. -5. optionally you could alias config to dotbare so you keep your muscle memory +5. optionally you could alias `config` to `dotbare` so you keep your muscle memory. ```sh alias config=dotbare @@ -259,31 +220,32 @@ follow the instructions in [others](#others) which simply add dotbare to your PA ##### Keep your current setup but integrate dotbare -1. follow the steps in [install](#install) to install dotbare -2. set environment variable so that dotbare knows where to look for git information +1. follow the steps in [install](#install) to install `dotbare`. +2. set environment variable so that `dotbare` knows where to look for git information. ```sh # e.g. I have all my dotfiles stored in folder $HOME/.myworld and symlinks all of them to appropriate location. # export DOTBARE_DIR="$HOME/.myworld/.git" # export DOTBARE_TREE="$HOME/.myworld" - export DOTBARE_DIR= + export DOTBARE_DIR= export DOTBARE_TREE= ``` -3. Run dotbare anywhere in your system -4. Note: with this method, you do not run `dotbare finit -u [URL]` when migrating to new system, - you will do your normal migration steps and then do the above steps. +3. Run `dotbare` anywhere in your system. + +Note: with this method, you do not run `dotbare finit -u [URL]` when migrating to new system, +you will do your normal migration steps and then do the above steps. ##### Complete migration **NOTE**: There's an open [issue](https://github.com/kazhala/dotbare/issues/12) where if you have more than 100 dotfiles tracked, this method would cause a crash during migration. Please don't use this method before the issue get resolved. -While bare method is great and easy, I recommend keeping your current GNU stow/symlink setup and integrate it with dotbare instead of a migration. +While bare method is great and easy, I recommend keeping your current symlink/GNU stow setup and integrate it with `dotbare` instead of a migration. If you are really happy with `dotbare`, as long as your remote repository resembles the structure of your home holder (reference what I mean in my [repo](https://github.com/kazhala/dotfiles.git)), simply run the command below. -**Disclaimer**: I have not done nearly enough test on this as I don't personally use GNU stow or symlink setup, migrate this way with caution. +**Disclaimer**: I have not done nearly enough test on this as I don't personally use symlink/GNU stow setup, migrate this way with caution. I recommend you test this migration in docker, see [Test-it-in-docker](#test-it-in-docker). ```sh @@ -293,17 +255,15 @@ dotbare finit -u [URL] #### Migrating dotbare to a new system -1. follow the steps in [install](#install) to install dotbare -2. Optionally set env variable to customize dotbare location - - > Checkout [customization](#customization) +1. follow the steps in [install](#install) to install `dotbare`. +2. Optionally set env variable to customize `dotbare` location (checkout [customization](#customization)). ```sh export DOTBARE_DIR="$HOME/.cfg" export DOTBARE_TREE="$HOME" ``` -3. give dotbare your remote URL and let it handle the rest +3. give `dotbare` your remote URL and let it handle the rest. ```sh dotbare finit -u https://github.com/kazhala/dotfiles.git @@ -312,7 +272,7 @@ dotbare finit -u [URL] #### Test it in docker When you are about to do migrations, I strongly suggest you give the migration a try in docker first. -The dotbare image is based on alpine linux. +The `dotbare` image is based on alpine linux. ```sh docker pull kazhala/dotbare:latest @@ -323,21 +283,21 @@ docker container run -it --rm --name dotbare kazhala/dotbare:latest ## Customization -dotbare could be customized through modification of env variables. +`dotbare` could be customized through modification of env variables. -Note: customization of fzf is not covered here, you should checkout their [wiki](https://github.com/junegunn/fzf/wiki). +Note: customization of fzf is not covered here, checkout their [wiki](https://github.com/junegunn/fzf/wiki). ### DOTBARE_DIR -This is the location of the bare repository, dotbare will look for this directory -and query git information or it will create this directory when initializing dotbare. +This is the location of the bare repository, `dotbare` will look for this directory +and query git information or it will create this directory when initializing `dotbare`. Change this to location or rename the directory to your liking. If you are using symlink/GNU stow setup, set this variable point to the .git folder within your dotfile directory. ```sh -# Default +# Default value export DOTBARE_DIR="$HOME/.cfg" ``` @@ -345,11 +305,11 @@ export DOTBARE_DIR="$HOME/.cfg" This is the working tree for the git bare repository, meaning this is where the version control will take place. I don't recommend changing this one unless **ALL** of your config -file is in something like \$XDG_CONFIG_HOME or if you are using symlink/GNU stow setup, -set this variable to point to the folder containing your dotfiles. +file is in something like \$XDG_CONFIG_HOME. If you are using symlink/GNU stow setup, +set this variable to point to the folder containing all of your dotfiles. ```sh -# Default +# Default value export DOTBARE_TREE="$HOME" ``` @@ -357,33 +317,31 @@ export DOTBARE_TREE="$HOME" This variable is used to determine where to store the backup of your files. It is used mainly by `dotbare fbackup` which will back up all of your tracked dotfiles into this location. -It is also used by `dotbare finit -u [URL]`, when there is checkout conflict, dotbare will +It is also used by `dotbare finit -u [URL]`, when there is checkout conflict, `dotbare` will automatically backup conflicted files to this location. ```sh -# Default -# 2. If XDG_DATA_HOME exist, use XDG_DATA_HOME/dotbare -# 3. otherwise, use $HOME/.local/share/dotbare +# Default value export DOTBARE_BACKUP="${XDG_DATA_HOME:-$HOME/.local/share}/dotbare" ``` ### EDITOR -This is probably already set in your ENV. dotbare uses this variable to determine +This is probably already set in your ENV. `dotbare` uses this variable to determine which editor to use when running `dotbare fedit`. ```sh -# Default +# Default value export EDITOR="vim" ``` ### DOTBARE_KEY -This variable set default keybinds for fzf in dotbare. You could checkout a list of keybinds +This variable set default keybinds for fzf in `dotbare`. You could checkout a list of keybinds to set [here](https://github.com/junegunn/fzf/blob/97a725fbd0e54cbc07e4d72661ea2bd2bb7c01c1/man/man1/fzf.1#L648). ```sh -# Default +# Default value export DOTBARE_KEY=" --bind=alt-a:toggle-all # toggle all selection --bind=alt-j:jump # label jump mode, sort of like vim-easymotion @@ -399,232 +357,50 @@ Customize fzf settings for dotbare. This is useful when you want a different fzf behavior from your normal system fzf settings. ```sh -# Default is unset +# By default this variable is not set # More settings checkout fzf man page and their wiki ``` -## Commands +### DOTBARE_PREVIEW -> dotbare doesn't have a man page yet (it is planned tho), for help, type dotbare [COMMAND] -h - -### Checkout all available scripts and their help manual +This variable determines the preview command for file previews. By default, the preview is automatically determined +using fall back (bat -> highlight -> coderay -> rougify -> cat). Set this variable to control the preview command if +you have a specific preference or if you want extra flags/settings. Reference [here](https://github.com/kazhala/dotbare/blob/master/helper/preview.sh). ```sh -# run dotbare without any arguments will display all available `f` scripts -dotbare -# or checkout help for dotbare -dotbare -h -dotbare --help -# for normal git help -dotbare help +# By default this value is not set, dotbare uses a fall back method to determine which command to use. +# Make sure to have "{}" included when customizing it, the preview script substitute "{}" for actual filename. +export DOTBARE_PREVIEW="cat -n {}" ``` -### fedit - -Select files/commits through fzf and edit selected files/commits in \$EDITOR. -Editing commits will perform a interactive rebase. - -- Default: list all tracked files and open \$EDITOR to edit the selected files. Support multi selection. -- `-h, --help`: show the help message of `dotbare fedit` and exit. -- `-m, --modified`: list all modified files and open \$EDITOR to edit the selected files. Support multi selection. -- `-c, --commit`: list all commits and edit the selected commit through interactive rebase. - -![fedit](https://user-images.githubusercontent.com/43941510/82388905-0d6c9c80-9a7e-11ea-845f-21338c2d3a1f.png) - -### fadd - -Select files/directories or modified files through fzf and stage the selected files/directories. - -- Default: list all modified files and stage selected files. Support multi selection. -- `-h, --help`: show the help message of `dotbare fadd` and exit. -- `-f, --file`: list all files in current directory and stage selected files. Support multi selection. (Used for staging new files to index). -- `-d, --dir`: list all directory under current directory and stage selected directory. Support multi selection. (Used for staging new files to index). - -![fadd demo](https://user-images.githubusercontent.com/43941510/82388994-4e64b100-9a7e-11ea-953a-621d85347c57.png) - -### freset - -Select staged files or commits through fzf and then reset(unstage) staged files or reset HEAD back to certain commits. -Also supports reset HEAD back to certain commits using either `--soft`, `--hard`, `--mixed` flags. -More information on differences between flags [here](https://git-scm.com/docs/git-reset#Documentation/git-reset.txt-emgitresetemltmodegtltcommitgt). - -- Default: list all staged files and unstage the selected files. Support multi selection. -- `-h, --help`: show the help message of `dotbare freset` and exit. -- `-c, --commit`: list all commits and then reset HEAD back to the selected commits. (Default: `--mixed`, put all changes into modified state). -- `-S, --soft`: use `--soft` flag instead of `--mixed` flag, reset HEAD to certain commit without modifying working tree. -- `-H, --hard`: use `--hard` flag instead of `--mixed` flag, reset HEAD to certain commit discard all changes from the working tree. -- `-y, --yes`: acknowledge all actions that will be taken and skip confirmation. - -### fcheckout - -Checkout files/commit/branch interactively through fzf. - -- Default: list all modified files and reset selected files back to HEAD. Support multi selection. (Discard all changes) - **Note**: if your file is staged, you will need to unstage first before running fcheckout to make it work. -- `-h, --help`: show the help message of `dotbare fcheckout` and exit. -- `-s, --select`: list all tracked files and select a commit to checkout the selected files. Support multi selection. -- `-b, --branch`: list all branch and switch/checkout the selected branch. -- `-c, --commit`: list all commits and checkout selected commit. -- `-y, --yes`: acknowledge all actions that will be taken and skip confirmation. - -![fcheckout demo](https://user-images.githubusercontent.com/43941510/82389569-e2834800-9a7f-11ea-92b5-ed20c8f2ecda.png) - -### flog - -Interactive log viewer that will prompt you with a menu after selecting a commit. The action menu contains options including -edit, reset, revert and checkout the selected commits. - -- Default: list all commits and then prompt menu to select action to perform. -- `-h, --help`: show the help message of `dotbare flog` and exit. -- `-r, --revert`: revert the selected commit and skip action menu. -- `-R, --reset`: reset HEAD back to the selected commit and skip action menu. -- `-e, --edit`: edit selected commit through interactive rebase and skip action menu. -- `-c, --checkout`: checkout selected commit and skip action menu. -- `-y, --yes`: acknowledge all actions that will be taken and skip confirmation. - -![flog demo](https://user-images.githubusercontent.com/43941510/82389843-810fa900-9a80-11ea-9653-544816eb9eb8.png) - -### fstash - -View and manage stash interactively. - -- Default: list all stashes and apply the selected stash. (Default: `apply`). -- `-h, --help`: show the help message of `dotbare fstash` and exit. -- `-s, --select`: list modified files and stash the selected files. Support multi selection. -- `-d, --delete`: list all stashes and delete selected stash. Support multi selection. -- `-p, --pop`: use `pop` instead of `apply`. (`pop` would remove the stash while `apply` preserve the stash). - -![fstash demo](https://user-images.githubusercontent.com/43941510/82390106-275bae80-9a81-11ea-8c7c-6573bb1ecada.png) - -### fbackup - -Backup files to \$DOTBARE_BACKUP directory. This is particular useful when untracking files -or migrating to new machines. This is used by `dotbare finit -u [URL]` for backing up conflicted checkout files. - -- Default: backup all tracked files to \$DOTBARE_BACKUP directory. (Default: use `cp` command). -- `-h, --help`: show the help message of `dotbare fbackup` and exit. -- `-s, --select`: list all tracked files and only backup the selected files. Support multi selection. -- `-p PATH, --path PATH`: specify path to files to backup. (This is mainly used by `dotbare finit -u [URL]`). -- `-m, --move`: use `mv` instead of the default `cp` command to backup. (This is mainly used by `dotbare finit -u [URL]`). - -### fstat - -Display interactive git status menu. Toggle file stage/unstage status interactively. +### DOTBARE_DIFF_PAGER -- `-h, --help`: show the help message of `dotbare fstat` and exit. +This variable controls the diff output pager in previews like `dotbare flog`, `dotbare fadd` etc. It will read the value +of `git config core.pager` to determine the pager to use. If you have a specific preference for `dotbare` or have not set +the global pager, you could use this variable to customize the diff preview. -### finit - -Initialise dotbare with a bare repository or add -u [URL] flag for migrating existing dotfiles from -remote git repo to current system. - -Note: do not use this command if you are using symlink/GNU stow and want to keep your current setup. - -- Default: init the bare repository at \$DOTBARE_DIR. -- `-h, --help`: show the help message of `dotbare finit` and exit. -- `-u URL, --url URL`: migrate existing dotfiles from remote git repo to current system. -- `-s, --submodule`: if you have defined submodules in your bare repo (i.e. containes .gitmodule), add -s - flag to clone submodules as well during migration. -- `-y, --yes`: acknowledge all actions that will be taken and skip confirmation. - -### funtrack - -Stop tracking the selected git files. It could also be used to temporarily stop tracking changes -for files and then later on resume tracking changes. - -**Note: This command has severe limitations.** - -By default, selected files are permanently untracked from git. Selected files will be -remove from index while keeping the file in your current system. However, when your other -machines pull down the changes, the untracked files will be deleted by git. This is a limitation -with git, to overcome this, after untracking the files, run `dotbare fbackup` to backup all files on -other machines before pulling down the changesto avoid any file loss. -After pulling new changes, move the deleted files from backup back to their original position. -More discussions [here](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore). - -`dotbare funtrack` does come with capabilities to temporarily untrack files, which will not -remove the untracked files from other system. However, this is **NOT** recommended -way to untrack files, explained [here](https://www.git-scm.com/docs/git-update-index#_notes). - -- Default: list all tracked files and permanently untrack the selected files. Support multi selection. -- `-h, --help`: show the help message of `dotbare funtrack` and exit. -- `-t, --temp`: list all tracked files and temporarily untrack changes of the selected files. Support multi selection. -- `-r, --resume`: list all tracked files and resume tracking changes of the selected files. -- `-y, --yes`: acknowledge all actions that will be taken and skip confirmation. - -### fupgrade - -Update dotbare to the latest master. It will perform a `git pull --rebase --stat` and also -performing a auto-stash if needed (copied the method from OMZ). This is useful if you are not -using a plugin manager. - -- `-h, --help`: show the help message of `dotbare fstat` and exit. - -## Custom scripts - -`dotbare` may not contains all the functionalities of your need, feel free to open up feature request. You could also -define your custom scripts under the scripts folder in `dotbare`. The `.gitignore` are configured to ignore everything except the scripts used by `dotbare` default, -so you can name your scripts any name you want. You can then track your custom scripts using `dotbare` to ensure the scripts are available in all of your machines. -Feel free to open pull request for your script, I'm happy to review and discuss for merging. - -A dedicated wiki page will be setup to document API for helper functions. - -## Tips and Tricks - -- Most commands related to files support multi selection (default fzf setting is TAB for multi select). -- Most commands related to commits and branches doesn't support multi selection. -- Checkout fzf [doc](https://github.com/junegunn/fzf/blob/97a725fbd0e54cbc07e4d72661ea2bd2bb7c01c1/man/man1/fzf.1#L648) - for more default fzf keybinds information. -- Alias dotbare to shorter words to type less. - - ```sh - alias dots=dotbare - ``` - -- Create keybinds for dotbare (e.g. bind ctrl-g to launch fedit and edit files). - - ```sh - # zsh example - bindkey -s '^g' "dotbare fedit"^j - # bash example - bind -x '"\C-g":"dotbare fedit"' - ``` +```sh +# By default this value uses fall back (git config core.pager -> cat) +export DOTBARE_DIFF_PAGER="delta --diff-so-fancy --line-numbers" +``` -- `dotbare` has disabled the command `dotbare add --All` as it is a really dangerous command in the conext of `dotbare` as it will stage everything in your \$DOTBARE_TREE to the index. +## Usage - ```sh - # Recommended ways - dotbare fadd # and then press alt-a to select all - dotbare add -u # stage all modified file to index - dotbare commit -am "message" # this also works, it will stage all modified files and then commit - ``` +A full list of `dotbare` commands and their usage are documented in **[wiki](https://github.com/kazhala/dotbare/wiki/Commands)**. -- Add the value of `$DOTBARE_DIR` to global .gitignore to avoid any weird recursion problem if accidentally adding - `$DOTBARE_DIR` to index, the value needs to be relative path to `$DOTBARE_TREE` +## Custom Scripts - ```sh - # e.g. DOTBARE_DIR="$HOME/.cfg", DOTBARE_TREE="$HOME" - echo ".cfg" >> $HOME/.gitignore - # e.g. DOTBARE_DIR="$HOME/.config/.cfg" DOTBARE_TREE="$HOME" - echo ".config/.cfg" >> $HOME/.gitignore - # e.g. DOTBARE_DIR="$HOME/.config/.cfg" DOTBARE_TREE="$HOME/.config" - echo ".cfg" >> $HOME/.gitignore - ``` +Detailed explanation of how to create custom scripts and the API of `dotbare` helper functions +is documented over in **[wiki](https://github.com/kazhala/dotbare/wiki/Custom-Scripts)**. -- Define a custom vim command to select dotfiles using [fzf.vim](https://github.com/junegunn/fzf.vim) +## Changes - ```vim - command! Dots call fzf#run(fzf#wrap({ - \ 'source': 'dotbare ls-files --full-name --directory "${DOTBARE_TREE}" | awk -v home="$HOME/" "{print home \$0}"', - \ 'sink': 'e', - \ 'options': [ '--multi', '--preview', 'cat {}' ] - \ })) - ``` +Latest changes are documented in CHANGELOG.md. View the upcoming changes in the [CHANGELOG](https://github.com/kazhala/dotbare/blob/dev/CHANGELOG.md) of dev branch. ## Testing -`dotbare` is unit tested using [bats](https://github.com/bats-core/bats-core). A very weird mock tests are implemented using PATH override method. -This will be improved later and documented in a dedicated README in tests folder for more readability and extensibility. +`dotbare` is unit tested using [bats](https://github.com/bats-core/bats-core). Mock tests are implemented using PATH override method. +This documented in a dedicated README in tests folder for more readability and extensibility. Not all functions have 100% coverage and lots of user interaction cannot be effectively tested, please fire up issues if something went wrong. @@ -653,7 +429,7 @@ Leave a star :) ## Background -dotbare was initially part of my personal scripts, I had a hard time sharing those scripts +`dotbare` was initially part of my personal scripts, I had a hard time sharing those scripts and as the number of scripts grows, I feel like is more appropriate to make a dedicated project for it. I hope you find it useful and enjoy it, thanks! @@ -667,5 +443,5 @@ for it. I hope you find it useful and enjoy it, thanks! ## Demo -You could find some more gif demo [here](https://github.com/kazhala/dotbare/issues/1) +You could find some more demo [here](https://github.com/kazhala/dotbare/issues/1) [![asciicast](https://asciinema.org/a/332231.svg)](https://asciinema.org/a/332231)