From febd993f294028a63510e5db70e2da6f65daa419 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Sun, 10 Jun 2012 23:56:39 +0100 Subject: [PATCH] Refactor adding of (empty) repos. Fixes #773 --- SparkleLib/Git/SparkleFetcherGit.cs | 17 +++++++------ SparkleLib/Git/SparkleRepoGit.cs | 35 +++++++++------------------ SparkleLib/SparkleFetcherBase.cs | 3 ++- SparkleLib/SparkleHelpers.cs | 15 ++++++++++-- SparkleLib/SparkleRepoBase.cs | 25 ++++++++++--------- SparkleShare/Linux/SparkleAbout.cs | 2 +- SparkleShare/Mac/SparkleAbout.cs | 2 +- SparkleShare/SparkleControllerBase.cs | 13 +++++----- SparkleShare/Windows/SparkleAbout.cs | 2 +- 9 files changed, 57 insertions(+), 57 deletions(-) diff --git a/SparkleLib/Git/SparkleFetcherGit.cs b/SparkleLib/Git/SparkleFetcherGit.cs index 5c00dbb7..7c5cb5ee 100755 --- a/SparkleLib/Git/SparkleFetcherGit.cs +++ b/SparkleLib/Git/SparkleFetcherGit.cs @@ -46,7 +46,6 @@ namespace SparkleLib.Git { uri = new Uri ("ssh://" + uri); } - if (uri.Host.Equals ("gitorious.org")) { if (!uri.AbsolutePath.Equals ("/") && !uri.AbsolutePath.EndsWith (".git")) { @@ -77,7 +76,6 @@ namespace SparkleLib.Git { } } - TargetFolder = target_folder; RemoteUrl = uri; } @@ -90,7 +88,6 @@ namespace SparkleLib.Git { "clone " + "--progress " + "--no-checkout " + - "--depth=1 " + "\"" + RemoteUrl + "\" \"" + TargetFolder + "\""); } else { @@ -98,6 +95,7 @@ namespace SparkleLib.Git { "clone " + "--progress " + "--no-checkout " + + "--depth=1 " + "\"" + RemoteUrl + "\" \"" + TargetFolder + "\""); } @@ -173,15 +171,15 @@ namespace SparkleLib.Git { public override bool IsFetchedRepoEmpty { get { - SparkleGit git = new SparkleGit (TargetFolder, "rev-list --reverse HEAD"); + SparkleGit git = new SparkleGit (TargetFolder, "rev-parse HEAD"); git.Start (); // Reading the standard output HAS to go before // WaitForExit, or it will hang forever on output > 4096 bytes - string output = git.StandardOutput.ReadToEnd (); + git.StandardOutput.ReadToEnd (); git.WaitForExit (); - return (output.Length < 40); + return (git.ExitCode != 0); } } @@ -280,6 +278,9 @@ namespace SparkleLib.Git { public override void Complete () { + if (IsFetchedRepoEmpty) + return; + SparkleGit git = new SparkleGit (TargetFolder, "checkout HEAD"); git.Start (); @@ -440,7 +441,7 @@ namespace SparkleLib.Git { private void AddWarnings () { - SparkleGit git = new SparkleGit (TargetFolder, + /*SparkleGit git = new SparkleGit (TargetFolder, "config --global core.excludesfile"); git.Start (); @@ -454,6 +455,6 @@ namespace SparkleLib.Git { return; else this.warnings.Add ("You seem to have a system wide ‘gitignore’ file, this may affect SparkleShare files."); - } + */ } } } diff --git a/SparkleLib/Git/SparkleRepoGit.cs b/SparkleLib/Git/SparkleRepoGit.cs index 9f20ee15..06490915 100644 --- a/SparkleLib/Git/SparkleRepoGit.cs +++ b/SparkleLib/Git/SparkleRepoGit.cs @@ -30,26 +30,6 @@ namespace SparkleLib.Git { } - public override string ComputeIdentifier () { - // Because git computes a hash based on content, - // author, and timestamp; it is unique enough to - // use the hash of the first commit as an identifier - // for our folder - SparkleGit git = new SparkleGit (LocalPath, "rev-list --reverse HEAD"); - git.Start (); - - // Reading the standard output HAS to go before - // WaitForExit, or it will hang forever on output > 4096 bytes - string output = git.StandardOutput.ReadToEnd (); - git.WaitForExit (); - - if (output.Length < 40) - return null; - - return output.Substring (0, 40); - } - - public override List ExcludePaths { get { List rules = new List (); @@ -104,6 +84,13 @@ namespace SparkleLib.Git { } + public override void CreateInitialChangeSet () + { + base.CreateInitialChangeSet (); + SyncUp (); // FIXME: Weird freeze happens when base class handles this + } + + public override string [] UnsyncedFilePaths { get { List file_paths = new List (); @@ -138,15 +125,17 @@ namespace SparkleLib.Git { // Remove stale rebase-apply files because it // makes the method return the wrong hashes. string rebase_apply_file = SparkleHelpers.CombineMore (LocalPath, ".git", "rebase-apply"); + if (File.Exists (rebase_apply_file)) File.Delete (rebase_apply_file); SparkleGit git = new SparkleGit (LocalPath, "rev-parse HEAD"); git.Start (); + + string output = git.StandardOutput.ReadToEnd (); git.WaitForExit (); if (git.ExitCode == 0) { - string output = git.StandardOutput.ReadToEnd (); return output.TrimEnd (); } else { @@ -161,7 +150,7 @@ namespace SparkleLib.Git { SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Checking for remote changes..."); string current_revision = CurrentRevision; - SparkleGit git = new SparkleGit (LocalPath, "ls-remote \"" + RemoteUrl + "\" master"); + SparkleGit git = new SparkleGit (LocalPath, "ls-remote --exit-code \"" + RemoteUrl + "\" master"); git.Start (); git.WaitForExit (); @@ -198,7 +187,6 @@ namespace SparkleLib.Git { Commit (message); } - SparkleGit git = new SparkleGit (LocalPath, "push --progress " + // Redirects progress stats to standarderror "\"" + RemoteUrl + "\" master"); @@ -248,7 +236,6 @@ namespace SparkleLib.Git { SparkleHelpers.DebugInfo ("Git", "[" + Name + "] " + line); } - if (number >= percentage) { percentage = number; base.OnProgressChanged (percentage, speed); diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs index be212512..c436a522 100755 --- a/SparkleLib/SparkleFetcherBase.cs +++ b/SparkleLib/SparkleFetcherBase.cs @@ -140,7 +140,8 @@ namespace SparkleLib { IsActive = false; - bool repo_is_encrypted = RemoteUrl.ToString ().Contains ("crypto"); // TODO + // TODO: Find better way to determine if folder should have crypto setup + bool repo_is_encrypted = RemoteUrl.ToString ().Contains ("crypto"); if (Finished != null) Finished (repo_is_encrypted, IsFetchedRepoEmpty, Warnings); diff --git a/SparkleLib/SparkleHelpers.cs b/SparkleLib/SparkleHelpers.cs index 5fb9a5eb..b674ac81 100755 --- a/SparkleLib/SparkleHelpers.cs +++ b/SparkleLib/SparkleHelpers.cs @@ -17,12 +17,14 @@ using System; using System.IO; +using System.Security.Cryptography; +using System.Text; namespace SparkleLib { public static class SparkleHelpers { - private static object debug_lock = new object (); + private static Object debug_lock = new Object (); // Show debug info if needed public static void DebugInfo (string type, string message) @@ -101,6 +103,15 @@ namespace SparkleLib { platform == PlatformID.Win32Windows); } } + + + public static string SHA1 (string s) + { + SHA1 sha1 = new SHA1CryptoServiceProvider (); + Byte [] bytes = ASCIIEncoding.Default.GetBytes (s); + Byte [] enc_bytes = sha1.ComputeHash (bytes); + + return BitConverter.ToString (enc_bytes).ToLower ().Replace ("-", ""); + } } } - diff --git a/SparkleLib/SparkleRepoBase.cs b/SparkleLib/SparkleRepoBase.cs index c77324da..af5b283f 100755 --- a/SparkleLib/SparkleRepoBase.cs +++ b/SparkleLib/SparkleRepoBase.cs @@ -18,7 +18,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -35,15 +34,10 @@ namespace SparkleLib { } - public static class PollInterval { - public static TimeSpan Short { get { return new TimeSpan (0, 0, 5, 0); }} - public static TimeSpan Long { get { return new TimeSpan (0, 0, 15, 0); }} - } public abstract class SparkleRepoBase { - public abstract string ComputeIdentifier (); public abstract string CurrentRevision { get; } public abstract double Size { get; } public abstract double HistorySize { get; } @@ -95,10 +89,15 @@ namespace SparkleLib { return this.identifier; } else { - this.identifier = ComputeIdentifier (); + Random random = new Random (); + string number = "" + random.Next () + "" + random.Next () + "" + random.Next (); + this.identifier = SparkleHelpers.SHA1 (number); + File.WriteAllText (id_path, this.identifier); File.SetAttributes (id_path, FileAttributes.Hidden); + SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Assigned identifier: " + this.identifier); + return this.identifier; } } @@ -131,6 +130,11 @@ namespace SparkleLib { } } + private static class PollInterval { + public static TimeSpan Short { get { return new TimeSpan (0, 0, 5, 0); }} + public static TimeSpan Long { get { return new TimeSpan (0, 0, 15, 0); }} + } + public SparkleRepoBase (string path) { @@ -162,7 +166,7 @@ namespace SparkleLib { bool time_to_poll = (DateTime.Compare (this.last_poll, DateTime.Now.Subtract (this.poll_interval)) < 0); - if (time_to_poll) { + if (time_to_poll && !is_syncing) { this.last_poll = DateTime.Now; if (HasRemoteChanges) @@ -182,13 +186,10 @@ namespace SparkleLib { // Sync up everything that changed // since we've been offline if (HasLocalChanges) { - this.watcher.Disable (); SyncUpBase (); while (HasUnsyncedChanges) SyncUpBase (); - - this.watcher.Enable (); } this.remote_timer.Start (); @@ -208,7 +209,7 @@ namespace SparkleLib { "Any files you add or change in this folder will be automatically synced to " + n + RemoteUrl + " and everyone connected to it." + n + "" + n + - "SparkleShare is a Free and Open Source software program that helps people " + n + + "SparkleShare is an Open Source software program that helps people " + n + "collaborate and share files. If you like what we do, please consider a small " + n + "donation to support the project: http://sparkleshare.org/support-us/" + n + "" + n + diff --git a/SparkleShare/Linux/SparkleAbout.cs b/SparkleShare/Linux/SparkleAbout.cs index 7bdad222..c6c1168d 100755 --- a/SparkleShare/Linux/SparkleAbout.cs +++ b/SparkleShare/Linux/SparkleAbout.cs @@ -137,7 +137,7 @@ namespace SparkleShare { LineWrap = true, LineWrapMode = Pango.WrapMode.Word, Markup = "" + - "SparkleShare is Free and Open Source Software. You are free to use, modify, " + + "SparkleShare Open Source software. You are free to use, modify, " + "and redistribute it under the GNU General Public License version 3 or later." + "", WidthRequest = 330, diff --git a/SparkleShare/Mac/SparkleAbout.cs b/SparkleShare/Mac/SparkleAbout.cs index 8bbb4953..5b289077 100755 --- a/SparkleShare/Mac/SparkleAbout.cs +++ b/SparkleShare/Mac/SparkleAbout.cs @@ -168,7 +168,7 @@ namespace SparkleShare { StringValue = @"Copyright © 2010–" + DateTime.Now.Year + " Hylke Bons and others." + "\n" + "\n" + - "SparkleShare is Free and Open Source Software. You are free to use, modify, and redistribute it " + + "SparkleShare is Open Source software. You are free to use, modify, and redistribute it " + "under the GNU General Public License version 3 or later.", Frame = new RectangleF (295, Frame.Height - 260, 318, 98), TextColor = NSColor.White, diff --git a/SparkleShare/SparkleControllerBase.cs b/SparkleShare/SparkleControllerBase.cs index 7cf10bf9..ccf87081 100644 --- a/SparkleShare/SparkleControllerBase.cs +++ b/SparkleShare/SparkleControllerBase.cs @@ -1006,7 +1006,6 @@ namespace SparkleShare { public void FinishFetcher () { this.fetcher.Complete (); - string canonical_name = Path.GetFileNameWithoutExtension (this.fetcher.RemoteUrl.AbsolutePath); bool target_folder_exists = Directory.Exists ( @@ -1039,19 +1038,19 @@ namespace SparkleShare { string backend = SparkleFetcherBase.GetBackend (this.fetcher.RemoteUrl.AbsolutePath); SparkleConfig.DefaultConfig.AddFolder (target_folder_name, this.fetcher.RemoteUrl.ToString (), backend); - /* if (!string.IsNullOrEmpty (announcements_url)) { + if (FolderFetched != null) + FolderFetched (this.fetcher.RemoteUrl.ToString (), this.fetcher.Warnings.ToArray ()); + + /* TODO + if (!string.IsNullOrEmpty (announcements_url)) { SparkleConfig.DefaultConfig.SetFolderOptionalAttribute ( target_folder_name, "announcements_url", announcements_url); - } TODO - */ + */ lock (this.repo_lock) { AddRepository (target_folder_path); } - if (FolderFetched != null) - FolderFetched (this.fetcher.RemoteUrl.ToString (), this.fetcher.Warnings.ToArray ()); - if (FolderListChanged != null) FolderListChanged (); diff --git a/SparkleShare/Windows/SparkleAbout.cs b/SparkleShare/Windows/SparkleAbout.cs index b61ccbcd..bcbaf9a1 100644 --- a/SparkleShare/Windows/SparkleAbout.cs +++ b/SparkleShare/Windows/SparkleAbout.cs @@ -117,7 +117,7 @@ namespace SparkleShare { Foreground = new SolidColorBrush (Colors.White), Text = "Copyright © 2010–" + DateTime.Now.Year + " Hylke Bons and others.\n" + "\n" + - "SparkleShare is Free and Open Source Software. You are free to use, modify, " + + "SparkleShare is Open Source software. You are free to use, modify, " + "and redistribute it under the GNU General Public License version 3 or later.", TextWrapping = TextWrapping.Wrap, Width = 318