-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-aux
executable file
·189 lines (147 loc) · 4.03 KB
/
git-aux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/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