Refactor adding of (empty) repos. Fixes #773

This commit is contained in:
Hylke Bons 2012-06-10 23:56:39 +01:00
parent 19b7250b61
commit febd993f29
9 changed files with 57 additions and 57 deletions

View file

@ -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.");
}
*/ }
}
}

View file

@ -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<string> ExcludePaths {
get {
List<string> rules = new List<string> ();
@ -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<string> file_paths = new List<string> ();
@ -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);

View file

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

View file

@ -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 ("-", "");
}
}
}

View file

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

View file

@ -137,7 +137,7 @@ namespace SparkleShare {
LineWrap = true,
LineWrapMode = Pango.WrapMode.Word,
Markup = "<span font_size='small' fgcolor='white'>" +
"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." +
"</span>",
WidthRequest = 330,

View file

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

View file

@ -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 ();

View file

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