# helper variables
LONG_OUTPUT=false
POSITIONAL_ARGUMENTS=
-PREFIX=
PREV_COMMIT=
-UTILITY=ls
+UTILITY="ls"
WORKING_BRANCH=
detect_git() {
- if [[ -z "`git --version`" ]]; then
+ if [[ -z "$(git --version)" ]]; then
log_err "git not found in path"
exit 1
fi
detect_commits() {
set +e
- git rev-list --count HEAD > /dev/null 2>&1
- if [[ $? -ne 0 ]]; then
+ if ! git rev-list --count HEAD > /dev/null 2>&1; then
log_err "no commits found, create initial commit first"
exit 1
fi
esac
done
- shift $(expr $OPTIND - 1) # remove options
+ shift $(( OPTIND - 1 )) # remove options
if [[ "$#" -gt 0 ]]; then
UTILITY="$1"
shift
- POSITIONAL_ARGUMENTS="$@"
+ POSITIONAL_ARGUMENTS="$*"
fi
}
branch_create() {
+ local HASH
+ local TREE_HASH
+
log_info "creating branch $BRANCH"
git check-ref-format --branch "$BRANCH" > /dev/null
- local HASH=`echo "" | git mktree --batch`
- local TREE_HASH=`git commit-tree -m "init" "$HASH"`
+
+ HASH=$(echo "" | git mktree --batch)
+ TREE_HASH=$(git commit-tree -m "init" "$HASH")
+
git update-ref "refs/heads/$BRANCH" "$TREE_HASH"
}
branch_exists() {
- if test $(git branch --list "$BRANCH" | wc -c) == 0; then
+ if test "$(git branch --list "$BRANCH" | wc -c)" == 0; then
branch_create
fi
}
# changes to extra branch and stores prevous commit hash in PREV_COMMIT
branch_exists
- WORKING_BRANCH=`git rev-parse --verify HEAD --abbrev-ref`
+ WORKING_BRANCH=$(git rev-parse --verify HEAD --abbrev-ref)
git symbolic-ref HEAD "refs/heads/$BRANCH"
git reset > /dev/null
- PREV_COMMIT=`git rev-parse --verify HEAD`
+ PREV_COMMIT=$(git rev-parse --verify HEAD)
}
branch_reset() {
}
add_to_index() {
+ local commit_hash
+ local hash
+ local tree_hash
+
log_info "adding $2 to index"
# fills PREV_COMMIT with $BRANCH's previous commit hash
branch_hash
- local hash=`git hash-object "$1" -w`
+ hash=$(git hash-object "$1" -w)
rm "$1"
git update-index --add --cacheinfo 100644 "$hash" "$2"
- local tree_hash=`git write-tree`
- local commit_hash=`git commit-tree -m "${MESSAGE:-updated using git-extra}" -p "$PREV_COMMIT" "$tree_hash"`
+ tree_hash=$(git write-tree)
+ commit_hash=$(git commit-tree -m "${MESSAGE:-updated using git-extra}" -p "$PREV_COMMIT" "$tree_hash")
git update-ref "refs/heads/$BRANCH" "$commit_hash"
}
add() {
+ local tmpfile
branch_exists
if [[ "$#" -ne 1 ]]; then
usage
fi
- if test $(git ls-tree "$BRANCH" "$1" | wc -c) != 0; then
+ if test "$(git ls-tree "$BRANCH" "$1" | wc -c)" != 0; then
log_err "file '$1' already exists; did you mean to edit '$1' instead?"
usage
fi
- local tmpfile=`mktemp`
+ tmpfile=$(mktemp)
if [[ -n "$TEMPLATE" ]]; then
if [[ -r "$TEMPLATE" ]]; then
}
cat_file() {
+ local HASH
branch_exists
- local HASH=`git ls-tree "$BRANCH" "$1" | awk -F ' ' '{print $3}'`
+ HASH=$(git ls-tree "$BRANCH" "$1" | awk -F ' ' '{print $3}')
if [[ -z "$HASH" ]]; then
log_err "$1 doesn't exist"
exit 1
}
edit() {
- local tmpfile=`mktemp`
+ local tmpfile
+ tmpfile=$(mktemp)
log_info "editing $1 using $tmpfile"
cat_file "$1" "$tmpfile"
- editor $tmpfile
+ editor "$tmpfile"
add_to_index "$tmpfile" "$1"
}
editor() {
+ local CMD
+ local pre_edit_hash
+ local post_edit_hash
+
log_info "opening temporary file '$1'"
- local pre_edit_hash=`b2sum $1`
+ pre_edit_hash=$(b2sum "$1")
if [[ -z "$EDITOR" ]]; then
log_info "no editor found, checking git config"
set +e
- EDITOR="`git config core.editor`"
+ EDITOR="$(git config core.editor)"
set -e
if [[ -z "$EDITOR" ]]; then
log_err "no editor defined; use --editor option, set EDITOR environmental variable, or set git config core.editor."
fi
fi
- local CMD=`printf "$EDITOR %s\n" "$1"`
+ CMD=$(printf "$EDITOR %s\n" "$1")
eval "$CMD"
- local post_edit_hash=`b2sum $1`
+ post_edit_hash=$(b2sum "$1")
if [[ "$pre_edit_hash" == "$post_edit_hash" ]]; then
log_err "no changes detected"
ls() {
branch_exists
+ local SEARCH
+ SEARCH="${*:-.}"
+
if [[ "$LONG_OUTPUT" == "true" ]]; then
- git ls-tree "$BRANCH" -r $@
+ git ls-tree "$BRANCH" -r "$SEARCH"
else
- git ls-tree "$BRANCH" -r --name-only $@
+ git ls-tree "$BRANCH" -r --name-only "$SEARCH"
fi
}