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
master
Jonathan Hodgson 4 years ago
parent 660c739c18
commit caab854c09
  1. 68
      inc/file-management
  2. 26
      kb

@ -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"
}
# 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
# Make sure all the tags are up to date
getYamlTags "$filename" | assignTags "$filename" getYamlTags "$filename" | assignTags "$filename"
if [ "$newTitle" != "$oldTitle" ]; then gitChange "$filename"
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 else
mv "$filename" "$newfilename" # 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 echo "UPDATE items
SET (filename,title) = ('$(safeSQL "$newfilename")','$(safeSQL "$newTitle")') SET (filename,title) =
('$(safeSQL "$newFilename")','$(safeSQL "$title")')
WHERE filename = '$(safeSQL "$filename")';" | WHERE filename = '$(safeSQL "$filename")';" |
sqlite3 "${sqliteFile}" sqlite3 "${sqliteFile}"
gitChange "$newfilename"
fi
else else
gitChange "$filename" # 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

@ -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

Loading…
Cancel
Save