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

View file

@ -20,9 +20,6 @@ Tags:
- -
--- ---
" > "$filename" " > "$filename"
echo "INSERT INTO items (filename, title, type)
VALUES ( '$(safeSQL "$filename")', '$(safeSQL "$title")', 'normal' );" |
sqlite3 "${sqliteFile}"
editFile "$filename" editFile "$filename"
} }
@ -31,32 +28,62 @@ editFile(){
vecho "editFile $*" vecho "editFile $*"
cd "$dataDir" || return cd "$dataDir" || return
local filename local filename
local oldTitle
local newTitle
filename="$(findFile "$*")" filename="$(findFile "$*")"
[ ! -e "$filename" ] && exit 1 [ ! -e "$filename" ] && exit 1
oldTitle="$(getYamlTitle "$filename")"
"$editor" "$filename" "$editor" "$filename"
newTitle="$(getYamlTitle "$filename")" updateFileChange "$filename"
getYamlTags "$filename" | assignTags "$filename" }
if [ "$newTitle" != "$oldTitle" ]; then
vecho "Changed title" # This is used to update the DB when a file is changed
local newfilename # param1 is the file
newfilename="$(escapeFilename "$newTitle.md")" updateFileChange(){
if [ -e "$newfilename" ]; then local filename
echo -e "${YELLOW}File name $newfilename already exists${NC}" local title
echo -e "Please fix manually" local newFilename
exit 1 filename="$(findFile "$1")"
else [ ! -e "$filename" ] && die "No such file $1"
mv "$filename" "$newfilename" title="$(getYamlTitle "$filename")"
echo "UPDATE items newFilename="$(escapeFilename "$title.md")"
SET (filename,title) = ('$(safeSQL "$newfilename")','$(safeSQL "$newTitle")') echo "Filename: '$filename'
WHERE filename = '$(safeSQL "$filename")';" | new filename: '$newFilename'"
sqlite3 "${sqliteFile}" if [ "$filename" = "$newFilename" ]; then
gitChange "$newfilename" # The title hasn't changed
# check if the file is in the DB
if ! fileInDB "$filename"; then
# If not, add it
echo "INSERT INTO items (filename, title, type)
VALUES ( '$(safeSQL "$filename")',
'$(safeSQL "$title")', 'normal' );" |
sqlite3 "${sqliteFile}"
fi fi
else # Make sure all the tags are up to date
getYamlTags "$filename" | assignTags "$filename"
gitChange "$filename" gitChange "$filename"
else
# The title has changed so we need to move the file
if [ -e "$newFilename" ]; then
# If the place we need to move it to already exists, die
die -e "$newFilename already exists
$filename title changed
Please resolve manually"
else
mv "$filename" "$newFilename"
if fileInDB "$filename"; then
echo "UPDATE items
SET (filename,title) =
('$(safeSQL "$newFilename")','$(safeSQL "$title")')
WHERE filename = '$(safeSQL "$filename")';" |
sqlite3 "${sqliteFile}"
else
# We get here if the title was changed in the create process
echo "INSERT INTO items (filename, title, type)
VALUES ( '$(safeSQL "$newFilename")',
'$(safeSQL "$title")', 'normal' );" |
sqlite3 "${sqliteFile}"
fi
gitChange "$filename" "$newFilename"
fi
fi fi
} }
@ -78,6 +105,7 @@ deleteFile(){
DELETE FROM links DELETE FROM links
WHERE itemID = '$(safeSQL "$fileID")';" | WHERE itemID = '$(safeSQL "$fileID")';" |
sqlite3 --column --header "${sqliteFile}" sqlite3 --column --header "${sqliteFile}"
gitChange "$filename"
fi fi
} }

26
kb
View file

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