-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgit-sqlite.in
More file actions
170 lines (135 loc) · 3.63 KB
/
Copy pathgit-sqlite.in
File metadata and controls
170 lines (135 loc) · 3.63 KB
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
#!/usr/bin/env bash
## @autogenerated_warning@
## @autogenerated_timestamp@
prefix="@prefix@"
pkgdatadir="@datarootdir@/@PACKAGE@" # e.g. /usr/local/share/PACKAGE/
. "$pkgdatadir/util.sh"
die()
{
local base="$(basename "$0")"
printErr "$base: error: $@"
exit 1
}
show_version_and_exit()
{
echo "@PACKAGE_NAME@ - @VERSION@"
echo "License: MIT"
exit 0
}
show_help_and_exit()
{
local base="$(basename "$1")"
echo "@PACKAGE_NAME@ - @summary@
Usage: $BASE [OPTIONS] <COMMAND> DATABASE <SCHEMA>
COMMAND - one of 'init' or 'attach'
init - initialize the database
attach - attach the config
DATABASE - sqlite3 database
SCHEMA - alternate schema. Only used during 'init'. Sourced from package_dir (typically /usr/local/share/git-sqlite)
Options:
-h = This help screen.
-V = print version.
Website: @PACKAGE_URL@
bug-reports/questions: @PACKAGE_BUGREPORT@
"
exit
}
prjCheck()
{
local repo="$1"
local db="$2"
# test if theres a .git directory or file in the case of a submodule
if [ ! -d "$repo/.git" ] && [ ! -f "$repo/.git" ]
then
printErr "'$repo' isn't the parent directory of a git repository or submodule"
exit 1
fi
# test that the db path is relative
if isAbsPath "$db"; then
printErr "please use a relative path to the database"
exit 1
fi
}
init()
{
local repo="$(pwd)"
local db="$1"
schema=
if [ -n "$2" ]; then
if [ ! -f "$pkgdatadir/$2" ]; then
die "schema $2 doesn't exist"
else
schema="$2"
fi
else
schema="schema.sql"
fi
prjCheck "$repo" "$db"
# test that the db file doesn't exist
if [ -f "$repo/$db" ]; then
printErr "'$db' exists, refusing to create"
exit 1
fi
sqlite3 "$db" < "$pkgdatadir/$schema"
}
attach()
{
local repo="$(pwd)"
local db="$1"
prjCheck "$repo" "$db"
# test that the db file exists
if [ ! -f "$repo/$db" ]; then
printErr "'$db' doesn't exist"
exit 1
fi
# test that we've got a sqlite3 database
if ! stringContains "SQLite format 3" "$(head -c 15 < "$repo/$db")"; then
echo "'$db' is not sqlite database" >&2
exit 1
fi
# add diff section
git config diff.sqlite.binary "true"
git config diff.sqlite.command "git-sqlite-diff"
# add merge section
git config merge.sqlite.name "sqlite merge"
git config merge.sqlite.driver "git-sqlite-merge %O %A %B %L %P"
# add git show-sql alias
git config alias.show-sql "show --ext-diff -m"
# add git apply-sql alias
git config alias.apply-sql "!sqlite3 ${db} < ${db}-.merge_file.sql && rm ${db}-.merge_file.sql && git add ${db}"
# modify local .gitattributes
attributes="$db diff=sqlite merge=sqlite"
touch "$repo/.gitattributes"
if ! stringContains "$attributes" "$(cat "$repo/.gitattributes")"; then
echo "$attributes" >> "$repo/.gitattributes"
fi
}
# Default values for parameters
show_usage=
show_version=
verbose=
# Parse parameters
while getopts Vvh param
do
case $param in
h) show_help=1;;
V) show_version=1;;
?) die "unknown command line option";;
esac
done
shift $(($OPTIND - 1))
# Validate parameters
test -n "$show_help" && show_help_and_exit
test -n "$show_version" && show_version_and_exit
test -z "$1" && die "missing command. See -h for help"
test -z "$2" && die "missing database. See -h for help"
# Set inputs
cmd="$1"
db="$2"
schema="$3"
# run the command
case $cmd in
"init") init "$db" "$schema";;
"attach") attach "$db";;
*) die "unknown command. See -h for help"
esac