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"
echo "INSERT INTO items (filename, title, type)
VALUES ( '$(safeSQL "$filename")', '$(safeSQL "$title")', 'normal' );" |
sqlite3 "${sqliteFile}"
editFile "$filename"
}
@ -31,32 +28,62 @@ editFile(){
vecho "editFile $*"
cd "$dataDir" || return
local filename
local oldTitle
local newTitle
filename="$(findFile "$*")"
[ ! -e "$filename" ] && exit 1
oldTitle="$(getYamlTitle "$filename")"
"$editor" "$filename"
newTitle="$(getYamlTitle "$filename")"
getYamlTags "$filename" | assignTags "$filename"
if [ "$newTitle" != "$oldTitle" ]; then
vecho "Changed title"
local newfilename
newfilename="$(escapeFilename "$newTitle.md")"
if [ -e "$newfilename" ]; then
echo -e "${YELLOW}File name $newfilename already exists${NC}"
echo -e "Please fix manually"
exit 1
else
mv "$filename" "$newfilename"
echo "UPDATE items
SET (filename,title) = ('$(safeSQL "$newfilename")','$(safeSQL "$newTitle")')
WHERE filename = '$(safeSQL "$filename")';" |
sqlite3 "${sqliteFile}"
gitChange "$newfilename"
updateFileChange "$filename"
}
# This is used to update the DB when a file is changed
# param1 is the file
updateFileChange(){
local filename
local title
local newFilename
filename="$(findFile "$1")"
[ ! -e "$filename" ] && die "No such file $1"
title="$(getYamlTitle "$filename")"
newFilename="$(escapeFilename "$title.md")"
echo "Filename: '$filename'
new filename: '$newFilename'"
if [ "$filename" = "$newFilename" ]; then
# 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
else
# Make sure all the tags are up to date
getYamlTags "$filename" | assignTags "$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
}
@ -78,6 +105,7 @@ deleteFile(){
DELETE FROM links
WHERE itemID = '$(safeSQL "$fileID")';" |
sqlite3 --column --header "${sqliteFile}"
gitChange "$filename"
fi
}