2011-06-01 23:08:05 +00:00
|
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2011-05-16 20:19:58 +00:00
|
|
|
|
// 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
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Diagnostics;
|
2011-08-27 15:10:39 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
|
|
// Sets up a fetcher that can get remote folders
|
|
|
|
|
public class SparkleFetcherGit : SparkleFetcherBase {
|
|
|
|
|
|
2011-08-26 20:47:46 +00:00
|
|
|
|
private SparkleGit git;
|
|
|
|
|
|
|
|
|
|
|
2011-05-31 01:26:57 +00:00
|
|
|
|
public SparkleFetcherGit (string server, string remote_folder, string target_folder) :
|
|
|
|
|
base (server, remote_folder, target_folder)
|
|
|
|
|
{
|
|
|
|
|
remote_folder = remote_folder.Trim ("/".ToCharArray ());
|
|
|
|
|
|
2011-07-25 01:25:18 +00:00
|
|
|
|
if (server.StartsWith ("http")) {
|
2011-06-30 11:53:05 +00:00
|
|
|
|
base.target_folder = target_folder;
|
|
|
|
|
base.remote_url = server;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-31 01:26:57 +00:00
|
|
|
|
// Gitorious formatting
|
|
|
|
|
if (server.Contains ("gitorious.org")) {
|
|
|
|
|
server = "ssh://git@gitorious.org";
|
|
|
|
|
|
|
|
|
|
if (!remote_folder.EndsWith (".git")) {
|
|
|
|
|
|
|
|
|
|
if (!remote_folder.Contains ("/"))
|
|
|
|
|
remote_folder = remote_folder + "/" + remote_folder;
|
|
|
|
|
|
|
|
|
|
remote_folder += ".git";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (server.Contains ("github.com")) {
|
|
|
|
|
server = "ssh://git@github.com";
|
|
|
|
|
|
|
|
|
|
} else if (server.Contains ("gnome.org")) {
|
|
|
|
|
server = "ssh://git@gnome.org/git";
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
server = server.TrimEnd ("/".ToCharArray ());
|
|
|
|
|
|
2011-09-11 20:15:08 +00:00
|
|
|
|
string protocol = "ssh://";
|
2011-08-26 22:49:08 +00:00
|
|
|
|
|
2011-09-11 21:58:19 +00:00
|
|
|
|
if (server.StartsWith ("ssh://"))
|
2011-08-26 22:49:08 +00:00
|
|
|
|
server = server.Substring (6);
|
|
|
|
|
|
|
|
|
|
if (server.StartsWith ("git://")) {
|
2011-05-31 01:26:57 +00:00
|
|
|
|
server = server.Substring (6);
|
2011-08-26 22:49:08 +00:00
|
|
|
|
protocol = "git://";
|
2011-09-11 20:15:08 +00:00
|
|
|
|
}
|
2011-05-31 01:26:57 +00:00
|
|
|
|
|
2011-05-31 17:30:28 +00:00
|
|
|
|
if (!server.Contains ("@"))
|
2011-05-31 01:26:57 +00:00
|
|
|
|
server = "git@" + server;
|
|
|
|
|
|
2011-08-26 22:49:08 +00:00
|
|
|
|
server = protocol + server;
|
2011-05-31 01:26:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.target_folder = target_folder;
|
|
|
|
|
base.remote_url = server + "/" + remote_folder;
|
|
|
|
|
}
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Fetch ()
|
|
|
|
|
{
|
2011-12-24 17:04:27 +00:00
|
|
|
|
this.git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
|
2011-08-27 15:10:39 +00:00
|
|
|
|
"clone " +
|
2011-08-27 15:53:17 +00:00
|
|
|
|
"--progress " + // Redirects progress stats to standarderror
|
|
|
|
|
"\"" + base.remote_url + "\" " + "\"" + base.target_folder + "\"");
|
|
|
|
|
|
|
|
|
|
this.git.StartInfo.RedirectStandardError = true;
|
2011-08-26 20:47:46 +00:00
|
|
|
|
this.git.Start ();
|
2011-12-18 16:10:06 +00:00
|
|
|
|
|
2011-08-27 15:53:17 +00:00
|
|
|
|
double percentage = 1.0;
|
2011-08-27 15:10:39 +00:00
|
|
|
|
Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);
|
2011-12-30 00:44:35 +00:00
|
|
|
|
|
|
|
|
|
DateTime last_change = DateTime.Now;
|
|
|
|
|
TimeSpan change_interval = new TimeSpan (0, 0, 0, 1);
|
|
|
|
|
|
2011-08-27 15:53:17 +00:00
|
|
|
|
while (!this.git.StandardError.EndOfStream) {
|
|
|
|
|
string line = this.git.StandardError.ReadLine ();
|
|
|
|
|
Match match = progress_regex.Match (line);
|
|
|
|
|
|
|
|
|
|
double number = 0.0;
|
2011-08-27 15:10:39 +00:00
|
|
|
|
if (match.Success) {
|
|
|
|
|
number = double.Parse (match.Groups [1].Value);
|
2011-08-27 15:53:17 +00:00
|
|
|
|
|
|
|
|
|
// The cloning progress consists of two stages: the "Compressing
|
|
|
|
|
// objects" stage which we count as 20% of the total progress, and
|
|
|
|
|
// the "Receiving objects" stage which we count as the last 80%
|
|
|
|
|
if (line.Contains ("|"))
|
|
|
|
|
// "Receiving objects" stage
|
2011-12-30 00:44:35 +00:00
|
|
|
|
number = (number / 100 * 80 + 20);
|
2011-08-27 15:53:17 +00:00
|
|
|
|
else
|
|
|
|
|
// "Compressing objects" stage
|
|
|
|
|
number = (number / 100 * 20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (number >= percentage) {
|
|
|
|
|
percentage = number;
|
2011-12-30 00:44:35 +00:00
|
|
|
|
|
|
|
|
|
if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) {
|
|
|
|
|
base.OnProgressChanged (percentage);
|
|
|
|
|
last_change = DateTime.Now;
|
|
|
|
|
}
|
2011-08-27 15:53:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-26 20:47:46 +00:00
|
|
|
|
this.git.WaitForExit ();
|
|
|
|
|
SparkleHelpers.DebugInfo ("Git", "Exit code " + this.git.ExitCode.ToString ());
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
2011-12-30 00:44:35 +00:00
|
|
|
|
|
2011-08-26 20:47:46 +00:00
|
|
|
|
if (this.git.ExitCode != 0) {
|
2011-05-16 20:19:58 +00:00
|
|
|
|
return false;
|
2011-12-30 00:44:35 +00:00
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
InstallConfiguration ();
|
|
|
|
|
InstallExcludeRules ();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-11-28 13:40:34 +00:00
|
|
|
|
public override string [] Warnings {
|
|
|
|
|
get {
|
|
|
|
|
SparkleGit git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
|
|
|
|
|
"config --global core.excludesfile");
|
|
|
|
|
|
|
|
|
|
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 ().Trim ();
|
|
|
|
|
git.WaitForExit ();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty (output)) {
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return new string [] {
|
|
|
|
|
string.Format ("You seem to have configured a system ‘gitignore’ file. " +
|
|
|
|
|
"This may interfere with SparkleShare.\n({0})", output)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-26 20:47:46 +00:00
|
|
|
|
public override void Stop ()
|
|
|
|
|
{
|
2011-12-24 18:17:54 +00:00
|
|
|
|
if (this.git != null && !this.git.HasExited) {
|
2011-08-26 20:47:46 +00:00
|
|
|
|
this.git.Kill ();
|
|
|
|
|
this.git.Dispose ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.Stop ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
|
// Install the user's name and email and some config into
|
|
|
|
|
// the newly cloned repository
|
|
|
|
|
private void InstallConfiguration ()
|
|
|
|
|
{
|
|
|
|
|
string repo_config_file_path = SparkleHelpers.CombineMore (base.target_folder, ".git", "config");
|
|
|
|
|
string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path));
|
|
|
|
|
|
2011-07-29 22:03:45 +00:00
|
|
|
|
string n = Environment.NewLine;
|
|
|
|
|
|
|
|
|
|
// Show special characters in the logs
|
|
|
|
|
config = config.Replace ("[core]" + n,
|
|
|
|
|
"[core]" + n + "quotepath = false" + n);
|
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
|
// Be case sensitive explicitly to work on Mac
|
|
|
|
|
config = config.Replace ("ignorecase = true", "ignorecase = false");
|
|
|
|
|
|
|
|
|
|
// Ignore permission changes
|
|
|
|
|
config = config.Replace ("filemode = true", "filemode = false");
|
2011-07-29 22:03:45 +00:00
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
|
// Write the config to the file
|
|
|
|
|
TextWriter writer = new StreamWriter (repo_config_file_path);
|
|
|
|
|
writer.WriteLine (config);
|
|
|
|
|
writer.Close ();
|
|
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("Config", "Added configuration to '" + repo_config_file_path + "'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add a .gitignore file to the repo
|
|
|
|
|
private void InstallExcludeRules ()
|
|
|
|
|
{
|
2011-12-01 18:34:07 +00:00
|
|
|
|
DirectoryInfo info = Directory.CreateDirectory (SparkleHelpers.CombineMore (
|
|
|
|
|
this.target_folder, ".git", "info"));
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
2011-12-01 18:34:07 +00:00
|
|
|
|
string exlude_rules_file_path = Path.Combine (info.FullName, "exclude");
|
2011-05-16 20:19:58 +00:00
|
|
|
|
TextWriter writer = new StreamWriter (exlude_rules_file_path);
|
|
|
|
|
|
|
|
|
|
// gedit and emacs
|
|
|
|
|
writer.WriteLine ("*~");
|
|
|
|
|
|
2011-09-11 21:47:36 +00:00
|
|
|
|
// Firefox and Chromium temporary download files
|
2011-09-11 21:58:19 +00:00
|
|
|
|
writer.WriteLine ("*.part");
|
|
|
|
|
writer.WriteLine ("*.crdownload");
|
2011-09-11 21:47:36 +00:00
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
|
// 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");
|
|
|
|
|
|
2011-12-01 18:34:07 +00:00
|
|
|
|
// MS Office
|
2011-10-24 18:43:55 +00:00
|
|
|
|
writer.WriteLine ("~*.tmp");
|
|
|
|
|
writer.WriteLine ("~*.TMP");
|
|
|
|
|
writer.WriteLine ("*~*.tmp");
|
|
|
|
|
writer.WriteLine ("*~*.TMP");
|
2011-10-24 18:42:18 +00:00
|
|
|
|
writer.WriteLine ("~*.ppt");
|
|
|
|
|
writer.WriteLine ("~*.pptx");
|
|
|
|
|
writer.WriteLine ("~*.PPT");
|
|
|
|
|
writer.WriteLine ("~*.PPTX");
|
|
|
|
|
writer.WriteLine ("~*.xls");
|
|
|
|
|
writer.WriteLine ("~*.xlsx");
|
|
|
|
|
writer.WriteLine ("~*.XLS");
|
|
|
|
|
writer.WriteLine ("~*.XLSX");
|
|
|
|
|
writer.WriteLine ("~*.doc");
|
|
|
|
|
writer.WriteLine ("~*.docx");
|
|
|
|
|
writer.WriteLine ("~*.DOC");
|
|
|
|
|
writer.WriteLine ("~*.DOCX");
|
2011-10-23 12:39:00 +00:00
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
|
// CVS
|
|
|
|
|
writer.WriteLine ("*/CVS/*");
|
|
|
|
|
writer.WriteLine (".cvsignore");
|
|
|
|
|
writer.WriteLine ("*/.cvsignore");
|
|
|
|
|
|
|
|
|
|
// Subversion
|
|
|
|
|
writer.WriteLine ("/.svn/*");
|
|
|
|
|
writer.WriteLine ("*/.svn/*");
|
|
|
|
|
|
|
|
|
|
writer.Close ();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-20 00:55:49 +00:00
|
|
|
|
|
2011-12-18 16:10:06 +00:00
|
|
|
|
|
2011-05-20 00:55:49 +00:00
|
|
|
|
public class SparkleGit : Process {
|
|
|
|
|
|
|
|
|
|
public SparkleGit (string path, string args) : base ()
|
|
|
|
|
{
|
|
|
|
|
EnableRaisingEvents = true;
|
|
|
|
|
StartInfo.FileName = SparkleBackend.DefaultBackend.Path;
|
|
|
|
|
StartInfo.Arguments = args;
|
|
|
|
|
StartInfo.RedirectStandardOutput = true;
|
|
|
|
|
StartInfo.UseShellExecute = false;
|
|
|
|
|
StartInfo.WorkingDirectory = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new public void Start ()
|
|
|
|
|
{
|
|
|
|
|
SparkleHelpers.DebugInfo ("Cmd", StartInfo.FileName + " " + StartInfo.Arguments);
|
|
|
|
|
base.Start ();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-16 20:19:58 +00:00
|
|
|
|
}
|