obsidian-git icon indicating copy to clipboard operation
obsidian-git copied to clipboard

Option take last commit time into account for backup

Open NomarCub opened this issue 4 years ago • 15 comments

I'd like the plugin to optionally check the last commit time and update the last auto-backup time to that. For example let's say the last auto-backup was 30 minutes ago, the interval is an hour and I make a manual commit now. With this option auto-backup won't trigger 30 minutes from now, but an hour from now, an hour from the last commit.

NomarCub avatar May 09 '21 10:05 NomarCub

I see your point, but honestly that's not really what I understand under an auto backup. What's your opinion @denolehov ?

Vinzent03 avatar May 10 '21 14:05 Vinzent03

The plugin's auto-backup internal timer is reset once you manually commit from within Obsidian. Tracking commits made from outside of Obsidian would be overkill.

denolehov avatar May 11 '21 13:05 denolehov

@denolehov Are you sure? Did I remove that with my changes? I can only find the update of lastUpdate, but that's only for the status bar.

Vinzent03 avatar May 11 '21 13:05 Vinzent03

Personally, I am fine with either having a backup timer reset on manual commit from within Obsidian UI or not having it reset at all and have it free-running.

But in any case, having a reset timer rely on git history (i.e. periodically fetching git log and seeing what was the latest timestamp) is definitely an overkill.

But maybe it's just me, and if more people request this change, we could perhaps implement a "Backup Mode" where people would be able to choose whether they want to have the timer free-running or be based on the latest commit, or be reset on manual commits from Obsidian.

denolehov avatar May 15 '21 11:05 denolehov

I imagined the plugin wouldn't need to check the git log periodically, only on startups and before scheduled backups, like with loadLastAuto() in init now. Are you open to me implementing this in the future?

NomarCub avatar May 15 '21 13:05 NomarCub

For anyone still interested, I found a workaround with git hooks. It works well for me on Windows. I made a post commit hook (in .git/hooks/post-commit), that overwrites the plugin's data file with the current commit time, like so:

#!/bin/bash
stamp=$(date +"%Y-%m-%dT%T%z")
echo $stamp
echo -e "$stamp\n$stamp" > .obsidian-git-data

This makes the shell print the current date in JavaScript's Date.parse() format used by the plugin.

NomarCub avatar May 28 '21 07:05 NomarCub

The plugin's auto-backup internal timer is reset once you manually commit from within Obsidian. Tracking commits made from outside of Obsidian would be overkill.

This seems to be broken again @Vinzent03, would you reopen the issue? Auto backup only considers when the last auto backup was, not even manual backups from the app. The .obsidian-git-data file is not used anymore in the code base, as far as I can see. The other current behaviour that considers last editing time is not what I'm looking for. I can't find any way now to make auto backup dependent on the last commit.

NomarCub avatar Mar 05 '22 22:03 NomarCub

I switched from an extra file to localstorage, which is still used on startup. I don't know if we used that file for this issue too. Did we even "fix" this issue? I don't know why denolehov closed it instantly.

Vinzent03 avatar Mar 05 '22 22:03 Vinzent03

I seem to remember this was fixed, but maybe not. Either way, it doesn't work now. I hope you can reproduce it. Is the .obsidian-git-data file no longer used at all? If it's functionality was moved to localStorage is there any way for outside git operations to influence the plugin?

NomarCub avatar Mar 05 '22 23:03 NomarCub

is there any way for outside git operations to influence the plugin?

I think that's right

To conclude, this feature isn't currently planned, because that's not what I understand under planned automatic backups. They should run independent of manual backups.

Vinzent03 avatar Mar 23 '22 12:03 Vinzent03

"I think that's right", as in there's no way for outside git operations to influence the plugin?

I would have liked to use this functionality as something that makes a backup when neither automatic nor manual backups were made for a while. Should I open a separate feature request for that?

NomarCub avatar Mar 23 '22 12:03 NomarCub

"I think that's right", as in there's no way for outside git operations to influence the plugin?

I would have liked to use this functionality as something that makes a backup when neither automatic nor manual backups were made for a while. Should I open a separate feature request for that?

@Vinzent03 could you confirm this?

NomarCub avatar Apr 25 '22 17:04 NomarCub

@NomarCub I don't really know what exactly you want to influence, but you would need another plugin to interact with this plugin to achieve this feature request. I will reopen this issue, but the backup system is already a bit complicated and I don't know if I would merge a pull request, because I don't want to overcomplicate things.

Vinzent03 avatar Apr 27 '22 14:04 Vinzent03

My foremost goal is the functionality, so maybe I should rephrase it. I'd like some mechanism, whereby a backup is made automatically when the last backup is over a specified age. Maybe the last backup was an automatic one, maybe a manual one, (or even a commit made outside Obsidian, but if that's hard to deal with it's fine if that is not addressed in this PR). The current auto backup feature doesn't do this, it triggers in relation to the last auto backup, not any backup (or commit), even if say, a manual backup was made recently.


Ideally for me, you or someone familiar with the plugin would implement this.

To conclude, this feature isn't currently planned, because that's not what I understand under planned automatic backups. They should run independent of manual backups.

If auto backup should only do what you described, then let's call this new mechanism scheduled fallback or something, to make it clear to people that it's something different.

"The backup system is already a bit complicated" sounds to me like the code is hard to extend and should be refactored to make further changes easier. But if that time investment is not justified, and you're against this being included in the plugin I understand.
In that case please help me work around it. Can I access the localStorage data from an outside script, say, a post commit hook? Do I need a plugin to do it? Does the plugin need to use this plugin's API, or can it read the data directly?

NomarCub avatar Apr 27 '22 17:04 NomarCub

With too complicated, I meant there are already many settings for auto backup. By adding another option, it may be too complicated for the user to understand everything. Giving it a different name, may be helpful.

You can only access the localStorage from an Obsidian plugin or a script running directly in Obsidian (I think there are some plugins to run js). When writing your own plugin, I suggest you to use this plugin's api to run a backup.

Vinzent03 avatar May 02 '22 15:05 Vinzent03

I am trying my hand at this, and I think I am done on desktop, but I don't know how to do it on mobile. Right now I use SimpleGit.log() to get the date of the latest commit on startup and then pass it to saveLastAuto, but mobile uses isogit instead, so there's no log method.

@Vinzent03 Could you either please:

  • help me access the log on mobile through isogit
  • make log() available in GitManager too and implement it on mobile
  • help me find a simpler way to find the latest commit date?

NomarCub avatar Oct 09 '22 22:10 NomarCub

Since you only want the timestamp, this should do the trick. Use the example at the bottom and replace ref with HEAD. I don't know the difference between committer and author, but I guess you can research it.

Vinzent03 avatar Oct 09 '22 22:10 Vinzent03

What object do I call and readCommit on? gitManager doesn't have it on mobile. What arguments do I give them? I got an error when I tried gitManager.resolveRef({fs: gitManager.fs, dir:"/", ref:"master"}).

NomarCub avatar Oct 10 '22 07:10 NomarCub

May you just create the pull requests and I do the isogit part?

Vinzent03 avatar Oct 10 '22 09:10 Vinzent03

I thought this functionality is not intended, so no pull request would be accepted. I load my script using User Plugins.

This is what I have so far:

module.exports = {};
module.exports.onload = function (plugin) {
  if (app.isMobile) return;
  (async () => {
    const gitPlugin = app.plugins.plugins['obsidian-git'];
    const log = await gitPlugin.gitManager.log(); // SimpleGit.log()
    const date = log.first().date;
    // const lastAuto = await gitPlugin.loadLastAuto();
    gitPlugin.saveLastAuto(new Date(date), 'backup');
  })().catch((err) => console.error(err));
};

NomarCub avatar Oct 10 '22 10:10 NomarCub

I've done it now! The new setting Auto backup after latest commit should do what you want. Let me know if you have any issues!

Vinzent03 avatar Nov 27 '22 15:11 Vinzent03