Adds update command for externally modified files

This can be used when a file is modified externally. It will update the
sqlite database and potentially run git commands for the change.

If there are 2 files provided, it assumes that the file was moved. The
first filename should be the old file.

The new, edit and delete functions were also re factored slightly to use
this new update function.

Relevant to #4
Close #2
Close #1
This commit is contained in:
Jonathan Hodgson 2020-12-20 20:00:39 +00:00
parent 660c739c18
commit caab854c09
2 changed files with 77 additions and 25 deletions

26
kb
View file

@ -178,15 +178,26 @@ findFile(){
fi
}
fileInDB(){
local ids
ids="$(echo "SELECT id FROM items WHERE filename = '$(safeSQL "$1")'" |
sqlite3 "${sqliteFile}")"
[ -n "$ids" ] && return 0
return 1
}
externalgit(){
cd "$dataDir" || return
git "$@"
}
# This function will add and commit a file after it has been edited
# If 2 arguments are given, it assumes a rename has taken place.
# The first should be the old file (like mv)
gitChange(){
cd "$dataDir" || return
local filename="$1"
local newFilename="$2"
if [ "$dogit" -gt 0 ]; then
if [ -f "$filename" ]; then
if ! git diff --exit-code "$filename" > /dev/null 2>&1; then
@ -198,6 +209,16 @@ gitChange(){
git add "$filename"
git commit -m "KB auto-commit: New: $filename"
fi
else
# If the file name doesn't exist, we have probably moved it
if [ -n "$newFilename" ] && [ -f "$newFilename" ]; then
git add "$filename" "$newFilename"
git commit -m "KB auto-commit: move: $filename -> $newFilename"
else
# if we get here, the file has been deleted
git add "$filename"
git commit -m "KB auto-commit: delete: $filename"
fi
fi
fi
}
@ -293,7 +314,7 @@ mainScript() {
if [ "${args[0]}" != "init" ]; then
cd "$dataDir" || return
# Check to see if datadir is a git repo
if git rev-parse 2> /dev/null; then
if ! git rev-parse 2> /dev/null; then
# If not, ensure we don't run git commands
dogit=0
fi
@ -307,6 +328,7 @@ mainScript() {
list-tags) listTags "${args[@]:1}"; safeExit ;;
view) viewFile "${args[@]:1}"; safeExit ;;
fuzzy) fuzzySelect "${args[@]:1}"; safeExit ;;
update) updateFileChange "${args[@]:1}"; safeExit ;;
deepsearch) deepSearch "${args[@]:1}"; safeExit ;;
del|delete) deleteFile "${args[@]:1}"; safeExit ;;
git) externalgit "${args[@]:1}"; safeExit ;;
@ -348,6 +370,8 @@ usage() {
--noheader Don't include the header
list-tags Lists tags with the number of times its used
--noheader Don't include the header
update <file> [<file>] Updates the database and git repo of a changed file
If 2 files are given, it assumes a move
view View a file
fuzzy [command] Fuzzy select a file
command is what to do with the selected file