private static bool CommitRepository(string provname, string provid, string repoPath, string firstname, string lastname, string mail, bool pushremote) { try { lock (_gitAccessLock) { var o1 = ProcessHelper.ProcExecute("git", "add .", repoPath); LoggerSingleton.Inst.Debug("LocalGitMirror", "git mirror [git add]", o1.ToString()); var o2 = ProcessHelper.ProcExecute("git", "status", repoPath); LoggerSingleton.Inst.Debug("LocalGitMirror", "git mirror [git status]", o2.ToString()); if (o2.StdOut.Contains("nothing to commit") || o2.StdErr.Contains("nothing to commit")) { LoggerSingleton.Inst.Debug("LocalGitMirror", "Local git mirror not updated ('nothing to commit')"); return(false); } var msg = "Automatic Mirroring of AlephNote notes" + "\n" + "" + "\n" + "# AlephNote Version: " + $"{AlephAppContext.AppVersion.Item1}.{AlephAppContext.AppVersion.Item2}.{AlephAppContext.AppVersion.Item3}.{AlephAppContext.AppVersion.Item4}" + "\n" + "# Timestamp(UTC): " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\n" + "# Provider: " + provname + "\n" + "# Provider (ID): " + provid + "\n"; var o3 = ProcessHelper.ProcExecute("git", $"commit -a --allow-empty --message=\"{msg}\" --author=\"{firstname} {lastname} <{mail}>\"", repoPath); LoggerSingleton.Inst.Debug("LocalGitMirror", "git mirror [git commit]", o3.ToString()); if (pushremote) { var o4 = ProcessHelper.ProcExecute("git", "push", repoPath); LoggerSingleton.Inst.Debug("LocalGitMirror", "git mirror [git push]", o4.ToString()); } LoggerSingleton.Inst.Info("LocalGitMirror", "Local git mirror updated" + (pushremote ? " (+ pushed)":"")); return(true); } } catch (Exception e) { LoggerSingleton.Inst.Error("PluginManager", "Local git mirroring (commit) failed with exception:\n" + e.Message, e.ToString()); LoggerSingleton.Inst.ShowExceptionDialog("Local git mirror (commit) failed", e); return(false); } }
private static void CollectGarbage(string repoPath, int gcCount) { try { var cache = LoadGitGCCache(); if (!cache.ContainsKey(repoPath)) { cache[repoPath] = 0; } cache[repoPath] = cache[repoPath] + 1; if (cache[repoPath] >= gcCount) { cache[repoPath] = 0; LoggerSingleton.Inst.Debug("LocalGitMirror", $"Resetting GC counter to {cache[repoPath]}/{gcCount}", ""); SaveGitGCCache(cache); // https://stackoverflow.com/a/18006967/1761622 // stupid git - supressing output if stderr is not a tty var o1 = ProcessHelper.ProcExecute("git", "gc", repoPath); LoggerSingleton.Inst.Debug("LocalGitMirror", "git mirror [git gc]", o1.ToString()); } else { LoggerSingleton.Inst.Debug("LocalGitMirror", $"Increment GC counter to {cache[repoPath]}/{gcCount}", ""); SaveGitGCCache(cache); } } catch (Exception e) { LoggerSingleton.Inst.Error("LocalGitMirror", "Could not load GC_CACHE file", e); LoggerSingleton.Inst.ShowExceptionDialog("Local git mirror (git gc) failed", e); } }
public static void UpdateRepository(NoteRepository nrepo, AppSettings config) { if (!config.DoGitMirror) { return; } if (string.IsNullOrWhiteSpace(config.GitMirrorPath)) { LoggerSingleton.Inst.Warn("LocalGitMirror", "Cannot do a local git mirror: Path is empty."); return; } if (string.IsNullOrWhiteSpace(config.GitMirrorFirstName)) { LoggerSingleton.Inst.Warn("LocalGitMirror", "Cannot do a local git mirror: Authorname is empty."); return; } if (string.IsNullOrWhiteSpace(config.GitMirrorLastName)) { LoggerSingleton.Inst.Warn("LocalGitMirror", "Cannot do a local git mirror: Authorname is empty."); return; } if (string.IsNullOrWhiteSpace(config.GitMirrorMailAddress)) { LoggerSingleton.Inst.Warn("LocalGitMirror", "Cannot do a local git mirror: Authormail is empty."); return; } try { lock (_gitAccessLock) { var notes = GetNotes(nrepo); if (!NeedsUpdate(notes, config)) { LoggerSingleton.Inst.Debug("LocalGitMirror", "git repository is up to date - no need to commit"); return; } Directory.CreateDirectory(config.GitMirrorPath); var githead = Path.Combine(config.GitMirrorPath, ".git", "HEAD"); if (!File.Exists(githead)) { if (Directory.EnumerateFileSystemEntries(config.GitMirrorPath).Any()) { LoggerSingleton.Inst.Warn("LocalGitMirror", "Cannot do a local git mirror: Targetfolder is neither a repository nor empty."); return; } var o = ProcessHelper.ProcExecute("git", "init", config.GitMirrorPath); LoggerSingleton.Inst.Debug("LocalGitMirror", "git mirror [git init]", o.ToString()); } string targetFolder = config.GitMirrorPath; if (config.GitMirrorSubfolders) { var subfolder = Path.Combine(config.GitMirrorPath, config.ActiveAccount.ID.ToString("B")); Directory.CreateDirectory(subfolder); targetFolder = subfolder; } var changed = SyncNotesToFolder(notes, targetFolder); if (!changed) { LoggerSingleton.Inst.Info("LocalGitMirror", "Local git synchronisation was triggered but no changes were found"); return; } } new Thread(() => { var succ = CommitRepository( nrepo.ConnectionName + " (" + nrepo.ConnectionUUID + ")", nrepo.ProviderID, config.GitMirrorPath, config.GitMirrorFirstName, config.GitMirrorLastName, config.GitMirrorMailAddress, config.GitMirrorDoPush); if (succ && config.GitMirrorAutoGC > 0) { CollectGarbage(config.GitMirrorPath, config.GitMirrorAutoGC); } }).Start(); } catch (Exception e) { LoggerSingleton.Inst.Error("LocalGitMirror", "Local git mirroring failed with exception:\n" + e.Message, e.ToString()); LoggerSingleton.Inst.ShowExceptionDialog("Local git mirror failed", e); } }