diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/sqlite3-git-clean | 85 | ||||
-rwxr-xr-x | bin/sqlite3-git-smudge | 36 |
2 files changed, 121 insertions, 0 deletions
diff --git a/bin/sqlite3-git-clean b/bin/sqlite3-git-clean new file mode 100755 index 0000000..cf176aa --- /dev/null +++ b/bin/sqlite3-git-clean @@ -0,0 +1,85 @@ +#!/bin/sh + +set -eu + +input_to_db() +{ + local input= + local db= + + input="$(mktemp)" || return 1 + cat - 1>"${input}" || { rm -f "${input}"; return 1; } + if file "${input}" | grep -Fq SQLite; then + # Input still a database: use it directly. + db="${input}" + else + # Input already dumped to SQL: enter it into temporary database. + db="$(mktemp)" || { rm -f "${input}"; return 1; } + sqlite3 "${db}" 0<"${input}" || { rm -f "${input}"; return 1; } + rm -f "${input}" || { rm -f "${db}"; return 1; } + fi + + printf '%s' "${db}" + return 0 +} + +del_from_db() +{ + local db="${1}" + local spec="${2}" + shift 2 + local table= + local column= + local value= + + value="${spec#*=}" + spec="${spec%%=*}" + table="${spec%%.*}" + column="${spec#*.}" + + sqlite3 "${db}" "DELETE FROM ${table} WHERE ${column} = ${value};" || \ + return 1 + + return 0 +} + +db_to_sql() +{ + local db="${1}" + shift 1 + + sqlite3 "${db}" '.dump' || return 1 + + return 0 +} + +usage() +{ + printf 'Usage: %s [<table>.<column>=<value> ...]\n' "${0}" + + return 0 +} + +main() +{ + local arg= + local db= + + for arg in "${@}"; do + case "${arg}" in + ?*.?*=?*);; + *) usage 1>&2; return 1;; + esac + done + + db="$(input_to_db)" || return 1 + for arg in "${@}"; do + del_from_db "${db}" "${arg}" || { rm -f "${db}"; return 1; } + done + db_to_sql "${db}" || { rm -f "${db}"; return 1; } + rm -f "${db}" || return 1 + + return 0 +} + +main "${@}" diff --git a/bin/sqlite3-git-smudge b/bin/sqlite3-git-smudge new file mode 100755 index 0000000..db1221b --- /dev/null +++ b/bin/sqlite3-git-smudge @@ -0,0 +1,36 @@ +#!/bin/sh + +set -eu + +sql_to_db() +{ + local db= + + db="$(mktemp)" || return 1 # Create new empty DB. + sqlite3 "${db}" || { rm -f "${db}"; return 1; } # Fill DB from stdin. + cat "${db}" || { rm -f "${db}"; return 1; } # Output DB. + rm -f "${db}" || return 1 # Clean up. + + return 0 +} + +usage() +{ + printf 'Usage: %s\n' "${0}" + + return 0 +} + +main() +{ + if [ ${#} -ne 0 ]; then + usage 1>&2 + return 1 + fi + + sql_to_db || return 1 + + return 0 +} + +main "${@}" |