SparkleShare/SparkleLib/Git/SparkleFetcherGit.cs

369 lines
14 KiB
C#
Raw Normal View History

// SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
2015-01-13 22:20:50 +00:00
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
2013-10-11 15:13:46 +00:00
// 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
2013-10-11 15:13:46 +00:00
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
namespace SparkleLib.Git {
public class SparkleFetcher : SparkleFetcherSSH {
2016-03-28 17:14:21 +00:00
SparkleGit git;
2016-03-28 17:14:21 +00:00
Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);
Regex speed_regex = new Regex (@"([0-9\.]+) ([KM])iB/s", RegexOptions.Compiled);
2016-03-28 17:14:21 +00:00
string password_salt = "662282447f6bbb8c8e15fb32dd09e3e708c32bc8";
2016-03-28 17:14:21 +00:00
public override bool IsFetchedRepoEmpty {
get {
2016-03-28 17:14:21 +00:00
SparkleGit git = new SparkleGit (TargetFolder, "rev-parse HEAD");
git.StartAndWaitForExit ();
2016-03-28 17:14:21 +00:00
return (git.ExitCode != 0);
}
}
public SparkleFetcher (SparkleFetcherInfo info) : base (info)
2011-05-31 01:26:57 +00:00
{
if (RemoteUrl.ToString ().StartsWith ("ssh+"))
RemoteUrl = new Uri ("ssh" + RemoteUrl.ToString ().Substring (RemoteUrl.ToString ().IndexOf ("://")));
Uri uri = RemoteUrl;
2011-05-31 01:26:57 +00:00
2012-07-22 20:54:59 +00:00
if (!uri.Scheme.Equals ("ssh") && !uri.Scheme.Equals ("https") &&
!uri.Scheme.Equals ("http") && !uri.Scheme.Equals ("git")) {
2012-04-11 21:10:02 +00:00
uri = new Uri ("ssh://" + uri);
}
2011-05-31 01:26:57 +00:00
if (uri.Host.Equals ("gitorious.org") && !uri.Scheme.StartsWith ("http")) {
if (!uri.AbsolutePath.Equals ("/") &&
!uri.AbsolutePath.EndsWith (".git")) {
uri = new Uri ("ssh://git@gitorious.org" + uri.AbsolutePath + ".git");
} else {
uri = new Uri ("ssh://git@gitorious.org" + uri.AbsolutePath);
}
2011-05-31 01:26:57 +00:00
} else if (uri.Host.Equals ("github.com") && !uri.Scheme.StartsWith ("http")) {
uri = new Uri ("ssh://git@github.com" + uri.AbsolutePath);
} else if (uri.Host.Equals ("bitbucket.org") && !uri.Scheme.StartsWith ("http")) {
// Nothing really
} else {
if (string.IsNullOrEmpty (uri.UserInfo) && !uri.Scheme.StartsWith ("http")) {
if (uri.Port == -1)
uri = new Uri (uri.Scheme + "://storage@" + uri.Host + uri.AbsolutePath);
else
uri = new Uri (uri.Scheme + "://storage@" + uri.Host + ":" + uri.Port + uri.AbsolutePath);
}
2011-05-31 01:26:57 +00:00
}
RemoteUrl = uri;
2011-05-31 01:26:57 +00:00
}
public override bool Fetch ()
{
if (!base.Fetch ())
return false;
if (FetchPriorHistory) {
this.git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
2012-07-22 20:54:59 +00:00
"clone --progress --no-checkout \"" + RemoteUrl + "\" \"" + TargetFolder + "\"");
} else {
this.git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
2012-07-22 20:54:59 +00:00
"clone --progress --no-checkout --depth=1 \"" + RemoteUrl + "\" \"" + TargetFolder + "\"");
}
2011-08-27 15:53:17 +00:00
this.git.StartInfo.RedirectStandardError = true;
this.git.Start ();
2011-08-27 15:53:17 +00:00
double percentage = 1.0;
DateTime last_change = DateTime.Now;
TimeSpan change_interval = new TimeSpan (0, 0, 0, 1);
try {
while (!this.git.StandardError.EndOfStream) {
string line = this.git.StandardError.ReadLine ();
Match match = this.progress_regex.Match (line);
2015-01-13 22:20:50 +00:00
double number = 0.0;
2015-01-13 22:20:50 +00:00
double speed = 0.0;
if (match.Success) {
try {
number = double.Parse (match.Groups [1].Value, new CultureInfo ("en-US"));
2015-01-13 22:20:50 +00:00
} catch (FormatException) {
SparkleLogger.LogInfo ("Git", "Error parsing progress: \"" + match.Groups [1] + "\"");
}
2015-01-13 22:20:50 +00:00
// The pushing progress consists of two stages: the "Compressing
// objects" stage which we count as 20% of the total progress, and
// the "Writing objects" stage which we count as the last 80%
if (line.Contains ("Compressing")) {
// "Compressing objects" stage
number = (number / 100 * 20);
2015-01-13 22:20:50 +00:00
} else {
// "Writing objects" stage
number = (number / 100 * 80 + 20);
Match speed_match = this.speed_regex.Match (line);
2015-01-13 22:20:50 +00:00
if (speed_match.Success) {
try {
speed = double.Parse (speed_match.Groups [1].Value, new CultureInfo ("en-US")) * 1024;
} catch (FormatException) {
SparkleLogger.LogInfo ("Git", "Error parsing speed: \"" + speed_match.Groups [1] + "\"");
}
2015-01-13 22:20:50 +00:00
if (speed_match.Groups [2].Value.Equals ("M"))
speed = speed * 1024;
2015-01-13 22:20:50 +00:00
}
}
} else {
SparkleLogger.LogInfo ("Fetcher", line);
line = line.Trim (new char [] {' ', '@'});
if (line.StartsWith ("fatal:", StringComparison.InvariantCultureIgnoreCase) ||
line.StartsWith ("error:", StringComparison.InvariantCultureIgnoreCase)) {
base.errors.Add (line);
} else if (line.StartsWith ("WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!")) {
base.errors.Add ("warning: Remote host identification has changed!");
} else if (line.StartsWith ("WARNING: POSSIBLE DNS SPOOFING DETECTED!")) {
base.errors.Add ("warning: Possible DNS spoofing detected!");
}
}
if (number >= percentage) {
percentage = number;
if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) {
base.OnProgressChanged (percentage, speed);
last_change = DateTime.Now;
}
}
2011-08-27 15:53:17 +00:00
}
2015-01-13 22:20:50 +00:00
2013-06-26 09:11:03 +00:00
} catch (Exception) {
IsActive = false;
return false;
}
this.git.WaitForExit ();
2012-06-17 20:56:27 +00:00
if (this.git.ExitCode == 0) {
while (percentage < 100) {
percentage += 25;
2012-06-17 20:56:27 +00:00
if (percentage >= 100)
break;
Thread.Sleep (500);
base.OnProgressChanged (percentage, 0);
}
2012-06-17 20:56:27 +00:00
base.OnProgressChanged (100, 0);
InstallConfiguration ();
InstallExcludeRules ();
2012-06-29 13:00:25 +00:00
InstallAttributeRules ();
2012-06-17 20:56:27 +00:00
return true;
} else {
return false;
}
}
public override void Stop ()
{
2012-04-11 10:51:53 +00:00
try {
if (this.git != null && !this.git.HasExited) {
this.git.Kill ();
this.git.Dispose ();
}
2012-04-11 10:51:53 +00:00
} catch (Exception e) {
2012-11-22 12:31:48 +00:00
SparkleLogger.LogInfo ("Fetcher", "Failed to dispose properly", e);
}
if (Directory.Exists (TargetFolder)) {
try {
Directory.Delete (TargetFolder, true /* Recursive */ );
SparkleLogger.LogInfo ("Fetcher", "Deleted '" + TargetFolder + "'");
2015-01-13 22:20:50 +00:00
} catch (Exception e) {
SparkleLogger.LogInfo ("Fetcher", "Failed to delete '" + TargetFolder + "'", e);
}
}
}
public override void Complete ()
{
if (!IsFetchedRepoEmpty) {
SparkleGit git = new SparkleGit (TargetFolder, "checkout --quiet HEAD");
2012-07-26 10:12:14 +00:00
git.StartAndWaitForExit ();
}
base.Complete ();
}
2016-03-28 17:14:21 +00:00
void InstallConfiguration ()
{
string [] settings = new string [] {
"core.autocrlf input",
"core.quotepath false", // Don't quote "unusual" characters in path names
"core.ignorecase false", // Be case sensitive explicitly to work on Mac
"core.filemode false", // Ignore permission changes
2012-11-08 16:02:55 +00:00
"core.precomposeunicode true", // Use the same Unicode form on all filesystems
"core.safecrlf false",
"core.excludesfile \"\"",
"core.packedGitLimit 128m", // Some memory limiting options
"core.packedGitWindowSize 128m",
"pack.deltaCacheSize 128m",
"pack.packSizeLimit 128m",
"pack.windowMemory 128m",
"push.default matching"
};
if (SparkleBackend.Platform == PlatformID.Win32NT)
settings [0] = "core.autocrlf true";
foreach (string setting in settings) {
SparkleGit git_config = new SparkleGit (TargetFolder, "config " + setting);
2012-07-26 10:12:14 +00:00
git_config.StartAndWaitForExit ();
}
}
// Add a .gitignore file to the repo
2016-03-28 17:14:21 +00:00
void InstallExcludeRules ()
{
string git_info_path = new string [] { TargetFolder, ".git", "info" }.Combine ();
if (!Directory.Exists (git_info_path))
Directory.CreateDirectory (git_info_path);
2012-07-22 20:54:59 +00:00
string exclude_rules = string.Join (Environment.NewLine, ExcludeRules);
string exclude_rules_file_path = new string [] { git_info_path, "exclude" }.Combine ();
2012-07-22 20:54:59 +00:00
File.WriteAllText (exclude_rules_file_path, exclude_rules);
2012-06-29 13:00:25 +00:00
}
2016-03-28 17:14:21 +00:00
void InstallAttributeRules ()
2012-06-29 13:00:25 +00:00
{
2012-07-28 13:58:09 +00:00
string attribute_rules_file_path = new string [] { TargetFolder, ".git", "info", "attributes" }.Combine ();
2012-07-22 20:54:59 +00:00
TextWriter writer = new StreamWriter (attribute_rules_file_path);
2016-03-26 10:40:52 +00:00
// Compile a list of files we don't want Git to compress.
// Not compressing already compressed files decreases memory usage and increases speed
string [] extensions = new string [] {
"jpg", "jpeg", "png", "tiff", "gif", // Images
"flac", "mp3", "ogg", "oga", // Audio
"avi", "mov", "mpg", "mpeg", "mkv", "ogv", "ogx", "webm", // Video
"zip", "gz", "bz", "bz2", "rpm", "deb", "tgz", "rar", "ace", "7z", "pak", "tc", "iso", ".dmg" // Archives
};
2015-01-13 22:20:50 +00:00
2016-03-26 10:40:52 +00:00
foreach (string extension in extensions) {
writer.WriteLine ("*." + extension + " -delta");
writer.WriteLine ("*." + extension.ToUpper () + " -delta");
}
2016-03-26 10:40:52 +00:00
writer.WriteLine ("*.txt text");
writer.WriteLine ("*.TXT text");
writer.Close ();
}
2016-03-28 17:14:21 +00:00
public override void EnableFetchedRepoCrypto (string password)
{
// Set up the encryption filter
SparkleGit git_config_smudge = new SparkleGit (TargetFolder,
2016-03-28 18:53:45 +00:00
"config filter.encryption.smudge \"openssl enc -d -aes-256-cbc -base64" + " " +
2016-03-28 19:37:31 +00:00
"-S " + password.SHA256 (this.password_salt).Substring (0, 16) + " " +
2016-03-28 17:14:21 +00:00
"-pass file:.git/info/encryption_password\"");
SparkleGit git_config_clean = new SparkleGit (TargetFolder,
2016-03-28 18:53:45 +00:00
"config filter.encryption.clean \"openssl enc -e -aes-256-cbc -base64" + " " +
2016-03-28 19:37:31 +00:00
"-S " + password.SHA256 (this.password_salt).Substring (0, 16) + " " +
2016-03-28 17:14:21 +00:00
"-pass file:.git/info/encryption_password\"");
git_config_smudge.StartAndWaitForExit ();
git_config_clean.StartAndWaitForExit ();
// Pass all files through the encryption filter
string git_attributes_file_path = new string [] { TargetFolder, ".git", "info", "attributes" }.Combine ();
File.WriteAllText (git_attributes_file_path, "* filter=encryption");
// Store the password
string password_file_path = new string [] { TargetFolder, ".git", "info", "encryption_password" }.Combine ();
File.WriteAllText (password_file_path, password.SHA256 (this.password_salt));
}
public override bool IsFetchedRepoPasswordCorrect (string password)
{
string password_check_file_path = Path.Combine (TargetFolder, ".sparkleshare");
if (!File.Exists (password_check_file_path)) {
SparkleGit git = new SparkleGit (TargetFolder, "show HEAD:.sparkleshare");
string output = git.StartAndReadStandardOutput ();
if (git.ExitCode == 0)
File.WriteAllText (password_check_file_path, output);
else
return false;
}
string args = "enc -d -aes-256-cbc -base64 -salt -pass pass:" + password.SHA256 (this.password_salt) + " " +
"-in \"" + password_check_file_path + "\"";
var process = new SparkleProcess ("openssl", args);
2016-03-28 18:36:19 +00:00
process.StartInfo.WorkingDirectory = TargetFolder;
2016-03-28 17:14:21 +00:00
process.StartAndWaitForExit ();
if (process.ExitCode == 0) {
File.Delete (password_check_file_path);
return true;
}
return false;
}
}
}