I'm currently using leo as my main note-taking application and I find it
very useful to have auto-saving, so I enabled the auto-save module.

I felt a little nervous about using auto-save and decided I wanted a git
commit on every save so I would have a history in case anything goes wrong.

I read Vitalije's post from not too long ago about overriding the ctrl-s
key to perform extra actions, but with auto-save I'm not using any
keystrokes. I decided to use the 'entr' utility (http://entrproject.org -
which I learned about on this list a long time ago) to watch for changes to
my .leo file and automatically execute a git commit.

I use an @script script in my leo outline to launch the entr process in the
background and it is working great. I did encounter one problem with the
entr process out-living the leo process. IOW, it doesn't exit when leo
exits. So then when I launch leo again I have two entr processes running,
both of which are trying to run auto git commits.

To work around that problem, I found a way to ensure a 2nd instance of that
process doesn't get launched if it is already running and I'm satisfied
with it. It doesn't solve the issue of the entr process out-living leo, but
it solves the more serious concern of having two instances competing with
each other. It would have been better if instead (or in addtion), I would
have found a way to get the pid of the backround entr process and kill it
when leo exits. But as I said, I'm satisfied enough for now with what I
have.

Here is the (linux only) script code (assumes the 'git init' has already
been run in the directory with the leo file and the leo file has been
commited once):

# The ampersand is special syntax for leo to execute the command
# in the background. The "flock ... 9>" part is because when leo
# exits, the background process doesn't get cleaned up and this
# flock "incantation" will prevent a second instance from launching
# see http://man7.org/linux/man-pages/man1/flock.1.html
# The flock utility is Linux only.
# The 'entr' utility (http://entrproject.org) will watch the given list
# of filenames and run the commands whenever changed
g.execute_shell_commands(
    f"""&(
        flock -n 9 || exit 1
        ls {c.fileName()} | entr -s 'git diff . | cat
        git commit -m "$(date +%Y-%m-%d) autocommit" .'
        ) 9> {c.fileName()}.entr.lock
    """
)


Brian

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAO5X8CyruizJRFHk-woZWHKn_Fz5a0T1gy5vBX9TivJ42rWv8w%40mail.gmail.com.

Reply via email to