Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 190 lines (147 sloc) 4.03 KB
#!/bin/bash
set -e
VERSION=0.3
ga_help() {
echo "usage: $0 [command] <[arguments]>"
echo
echo "Commands:"
echo " init [dir] Initialise git aux to use [dir] as the aux directory"
echo " add [file(s)] Add files from the aux directory to the repo"
echo " sync Synchronise changes from the aux directory to the repo"
echo " apply Apply changes from the repo to the aux directory"
exit
}
die() {
echo $@ 1>&2
kill 0
}
last_commit() {
git show | grep ^commit | cut -d ' ' -f 2
}
current_branch() {
git branch | grep "^*" | sed -e 's/^\*\s*//'
}
git_root() {
git rev-parse --show-toplevel
}
aux_dir() {
git config aux.home || die "Not a git aux repository"
}
update_aux() {
(for file in $(git ls-files); do
if [ "$file" != ".gitaux" -a -e "$file" ]; then
echo $file:$(stat -c "%a" $file)
fi
done) > $(git_root)/.gitaux
git add $(git_root)/.gitaux
}
repo_files() {
find $(git_root) -type f -not -path "$(git_root)/.git/*" -not -path "$(git_root)/.gitaux"
}
relative() {
local file=$1
local dir=$2
echo $file | sed -e "s/^$(echo $dir | sed -e 's/\//\\\//g')//"
}
ga_init() {
git config aux.home >/dev/null && die "This repository is already initialised for use with git-aux: $(aux_dir)"
if [ -z "$1" -o ! -d "$1" ]; then
die "You must supply a directory path"
fi
git config aux.home $(readlink -f $1)
update_aux
echo "Done"
}
add_all() {
# Add files to repo
if [ "$1" == "-f" ]; then
git add -u
else
git add -p
fi
# Add new files too
if [ -n "$(git ls-files -o --exclude-standard)" ]; then
git add $(git ls-files -o --exclude-standard)
fi
}
ga_add() {
for file in $@; do
local file=$(readlink -f $file)
local rel_path=$(relative $file $(aux_dir))
if [ -z "$(find "$file" -type f -path "$(aux_dir)*")" ]; then
die "$file is not in the aux root"
else
echo "Adding $file..."
cp -af "$file" "$(git_root)/$rel_path"
git add "$(git_root)/$rel_path"
fi
done
update_aux
echo "Done"
}
ga_sync() {
for file in $(repo_files); do
file=$(relative $file $(git_root))
rm -r $(git_root)/$file
# Copy or remove file/dir
if [ -e "$(aux_dir)/$file" ]; then
cp -af "$(aux_dir)/$file" "$(git_root)/$file"
fi
done
add_all $@
update_aux
# Show status
git status
}
ga_apply() {
local current_branch=$(current_branch)
local temp_branch=git-aux-temp-$RANDOM
local stashed=true
if [ "$(git stash save -u)" == "No local changes to save" ]; then
stashed=false
fi
# Copy the files home
for file in $(repo_files); do
file=$(relative $file $(git_root))
mkdir -p "$(dirname $(aux_dir)/$file)" || {
# This catches the edge case that we've moved from a file to a directory
rm -r "$(dirname $(aux_dir)/$file)"
mkdir -p "$(dirname $(aux_dir)/$file)"
}
echo $file...
cp -af "$(git_root)/$file" "$(aux_dir)/$file"
done
# Apply permissions
for line in $(cat $(git_root)/.gitaux); do
local path=$(echo $line | sed -e 's/:[0-9]*$//')
local permissions=$(echo $line | sed -e 's/^.*:\([0-9]*\)$/\1/')
chmod $permissions $(aux_dir)/$path
done
if $stashed; then
git stash pop
fi
echo "Done"
}
ga_diff() {
echo "Not yet implemented"
}
command=$1
shift || ga_help
args=$@
git branch &>/dev/null || die "Not a git repository"
case $command in
init) ga_init ${args[0]} ;;
add) ga_add $args ;;
sync) ga_sync ;;
apply)
read -p "Are you sure you want to apply changes to the aux directory? (y/n) " choice
if [[ "$choice" =~ ^[Yy] ]]; then
ga_apply
else
echo "Aborted"
fi
;;
diff) ga_diff ;;
--version) echo git aux version $VERSION ;;
*) ga_help ;;
esac
Jump to Line
Something went wrong with that request. Please try again.