2010-07-31 19:13:01 +00:00
|
|
|
// SparkleShare, an instant update workflow to Git.
|
|
|
|
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-11-21 12:33:24 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-07-31 19:13:01 +00:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-03-04 13:30:11 +00:00
|
|
|
|
2010-07-31 19:13:01 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Diagnostics;
|
2011-05-08 17:25:33 +00:00
|
|
|
using System.Threading;
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2010-08-02 15:42:42 +00:00
|
|
|
namespace SparkleLib {
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-08 17:25:33 +00:00
|
|
|
// Sets up a fetcher that can get remote folders
|
2011-05-01 13:40:46 +00:00
|
|
|
public class SparkleFetcher {
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-03 17:30:39 +00:00
|
|
|
public delegate void StartedEventHandler (object o, SparkleEventArgs args);
|
|
|
|
public delegate void FinishedEventHandler (object o, SparkleEventArgs args);
|
|
|
|
public delegate void FailedEventHandler (object o, SparkleEventArgs args);
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-03 17:30:39 +00:00
|
|
|
public event StartedEventHandler Started;
|
|
|
|
public event FinishedEventHandler Finished;
|
|
|
|
public event FailedEventHandler Failed;
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-08 17:25:33 +00:00
|
|
|
private string target_folder;
|
|
|
|
private string remote_url;
|
2011-05-08 19:35:26 +00:00
|
|
|
private Thread thread;
|
2010-10-28 11:09:52 +00:00
|
|
|
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-08 17:25:33 +00:00
|
|
|
public SparkleFetcher (string remote_url, string target_folder)
|
2011-05-01 13:40:46 +00:00
|
|
|
{
|
2011-05-08 17:25:33 +00:00
|
|
|
this.target_folder = target_folder;
|
|
|
|
this.remote_url = remote_url;
|
2011-05-01 13:40:46 +00:00
|
|
|
}
|
2010-07-31 19:13:01 +00:00
|
|
|
|
|
|
|
|
2011-05-01 13:40:46 +00:00
|
|
|
// Clones the remote repository
|
|
|
|
public void Start ()
|
|
|
|
{
|
2011-05-08 17:25:33 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder...");
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-03 17:30:39 +00:00
|
|
|
if (Started != null)
|
|
|
|
Started (this, new SparkleEventArgs ("Started"));
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-08 17:25:33 +00:00
|
|
|
if (Directory.Exists (this.target_folder))
|
|
|
|
Directory.Delete (this.target_folder, true);
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-08 19:35:26 +00:00
|
|
|
this.thread = new Thread (new ThreadStart (delegate {
|
2011-05-08 17:25:33 +00:00
|
|
|
if (Fetch ()) {
|
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching finished");
|
2010-11-27 21:23:44 +00:00
|
|
|
|
2011-05-03 17:30:39 +00:00
|
|
|
if (Finished != null)
|
|
|
|
Finished (this, new SparkleEventArgs ("Finished"));
|
2011-05-08 17:25:33 +00:00
|
|
|
} else {
|
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching failed");
|
|
|
|
|
|
|
|
if (Failed != null)
|
|
|
|
Failed (this, new SparkleEventArgs ("Failed"));
|
2011-05-01 13:40:46 +00:00
|
|
|
}
|
2011-05-08 17:25:33 +00:00
|
|
|
}));
|
|
|
|
|
2011-05-08 19:35:26 +00:00
|
|
|
this.thread.Start ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
this.thread.Abort ();
|
|
|
|
this.thread.Join ();
|
2011-05-08 17:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: abstract -> override
|
|
|
|
public virtual bool Fetch ()
|
|
|
|
{
|
|
|
|
SparkleGit git = new SparkleGit (SparklePaths.SparkleTmpPath,
|
|
|
|
"clone \"" + this.remote_url + "\" " + "\"" + this.target_folder + "\"");
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2011-05-01 13:40:46 +00:00
|
|
|
git.Start ();
|
2011-05-08 17:25:33 +00:00
|
|
|
git.WaitForExit ();
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("Git", "Exit code " + git.ExitCode.ToString ());
|
|
|
|
|
|
|
|
if (git.ExitCode != 0) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
InstallConfiguration ();
|
|
|
|
InstallExcludeRules ();
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-01 13:40:46 +00:00
|
|
|
}
|
2010-07-31 19:13:01 +00:00
|
|
|
|
2010-08-01 12:31:11 +00:00
|
|
|
|
2011-05-01 13:40:46 +00:00
|
|
|
// Install the user's name and email and some config into
|
|
|
|
// the newly cloned repository
|
|
|
|
private void InstallConfiguration ()
|
|
|
|
{
|
|
|
|
string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config");
|
2010-08-01 17:12:02 +00:00
|
|
|
|
2011-03-12 17:53:49 +00:00
|
|
|
if (File.Exists (global_config_file_path)) {
|
|
|
|
StreamReader reader = new StreamReader (global_config_file_path);
|
|
|
|
string user_info = reader.ReadToEnd ();
|
|
|
|
reader.Close ();
|
2010-08-01 17:12:02 +00:00
|
|
|
|
2011-05-08 17:25:33 +00:00
|
|
|
string repo_config_file_path = SparkleHelpers.CombineMore (this.target_folder, ".git", "config");
|
2011-03-12 17:53:49 +00:00
|
|
|
string config = String.Join ("\n", File.ReadAllLines (repo_config_file_path));
|
2011-05-01 13:40:46 +00:00
|
|
|
|
|
|
|
// Be case sensitive explicitly to work on Mac
|
2011-03-12 17:53:49 +00:00
|
|
|
config = config.Replace ("ignorecase = true", "ignorecase = false");
|
2011-05-01 15:25:42 +00:00
|
|
|
|
|
|
|
// Ignore permission changes
|
|
|
|
config = config.Replace ("filemode = true", "filemode = false");
|
|
|
|
|
|
|
|
// Add user info
|
2011-03-12 17:53:49 +00:00
|
|
|
config += Environment.NewLine + user_info;
|
2011-03-12 15:38:20 +00:00
|
|
|
|
2011-03-12 17:53:49 +00:00
|
|
|
TextWriter writer = new StreamWriter (repo_config_file_path);
|
|
|
|
writer.WriteLine (config);
|
2011-03-12 15:38:20 +00:00
|
|
|
writer.Close ();
|
|
|
|
|
2011-03-12 17:53:49 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Config", "Added configuration to '" + repo_config_file_path + "'");
|
|
|
|
}
|
|
|
|
}
|
2010-08-01 12:31:11 +00:00
|
|
|
|
|
|
|
|
2011-05-01 13:40:46 +00:00
|
|
|
// Add a .gitignore file to the repo
|
|
|
|
private void InstallExcludeRules ()
|
|
|
|
{
|
|
|
|
string exlude_rules_file_path = SparkleHelpers.CombineMore (
|
2011-05-08 17:25:33 +00:00
|
|
|
this.target_folder, ".git", "info", "exclude");
|
2011-05-01 13:40:46 +00:00
|
|
|
|
|
|
|
TextWriter writer = new StreamWriter (exlude_rules_file_path);
|
|
|
|
|
|
|
|
// gedit and emacs
|
|
|
|
writer.WriteLine ("*~");
|
|
|
|
|
|
|
|
// vi(m)
|
|
|
|
writer.WriteLine (".*.sw[a-z]");
|
|
|
|
writer.WriteLine ("*.un~");
|
|
|
|
writer.WriteLine ("*.swp");
|
|
|
|
writer.WriteLine ("*.swo");
|
|
|
|
|
|
|
|
// KDE
|
|
|
|
writer.WriteLine (".directory");
|
|
|
|
|
|
|
|
// Mac OSX
|
|
|
|
writer.WriteLine (".DS_Store");
|
|
|
|
writer.WriteLine ("Icon?");
|
|
|
|
writer.WriteLine ("._*");
|
|
|
|
writer.WriteLine (".Spotlight-V100");
|
|
|
|
writer.WriteLine (".Trashes");
|
|
|
|
|
|
|
|
// Mac OSX
|
|
|
|
writer.WriteLine ("*(Autosaved).graffle");
|
|
|
|
|
|
|
|
// Windows
|
|
|
|
writer.WriteLine ("Thumbs.db");
|
|
|
|
writer.WriteLine ("Desktop.ini");
|
|
|
|
|
|
|
|
// CVS
|
|
|
|
writer.WriteLine ("*/CVS/*");
|
|
|
|
writer.WriteLine (".cvsignore");
|
|
|
|
writer.WriteLine ("*/.cvsignore");
|
|
|
|
|
|
|
|
// Subversion
|
|
|
|
writer.WriteLine ("/.svn/*");
|
|
|
|
writer.WriteLine ("*/.svn/*");
|
|
|
|
|
|
|
|
writer.Close ();
|
|
|
|
}
|
|
|
|
}
|
2010-07-31 19:13:01 +00:00
|
|
|
}
|