SparkleShare/SparkleLib/Git/SparkleFetcherGit.cs

429 lines
16 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
// 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.Diagnostics;
2012-06-17 20:56:27 +00:00
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using SparkleLib;
namespace SparkleLib.Git {
// Sets up a fetcher that can get remote folders
public class SparkleFetcher : SparkleFetcherBase {
private SparkleGit git;
private bool use_git_bin;
private string cached_salt;
private string crypto_salt {
get {
if (!string.IsNullOrEmpty (this.cached_salt))
return this.cached_salt;
// Check if the repo's salt is stored in a branch...
SparkleGit git = new SparkleGit (TargetFolder, "branch -a");
string [] branches = git.StartAndReadStandardOutput ().Split (Environment.NewLine.ToCharArray ());
// TODO double check env.newline ^
foreach (string branch in branches) {
if (branch.StartsWith (" remotes/origin/salt-")) {
this.cached_salt = branch.Substring (22);
break;
}
}
// ...if not, create a new salt for the repo
if (string.IsNullOrEmpty (this.cached_salt)) {
this.cached_salt = GenerateCryptoSalt ();
string salt_file_path = new string [] { TargetFolder, ".git", "salt" }.Combine ();
// Temporarily store the salt in a file, so the Repo can
// push it to a branch on the host later
File.WriteAllText (salt_file_path, this.cached_salt);
}
return this.cached_salt;
}
}
public SparkleFetcher (string server, string required_fingerprint, string remote_path,
string target_folder, bool fetch_prior_history) : base (server, required_fingerprint,
remote_path, target_folder, fetch_prior_history)
2011-05-31 01:26:57 +00:00
{
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")) {
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 = new Uri ("ssh://git@github.com" + uri.AbsolutePath);
} else if (uri.Host.Equals ("bitbucket.org")) {
// Nothing really
} else if (uri.Host.Equals ("gnome.org")) {
uri = new Uri ("ssh://git@gnome.org/git" + uri.AbsolutePath);
2011-05-31 01:26:57 +00:00
} else {
2012-04-26 12:58:05 +00:00
if (string.IsNullOrEmpty (uri.UserInfo) &&
!uri.Scheme.Equals ("https") &&
!uri.Scheme.Equals ("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);
}
2012-07-22 18:59:51 +00:00
this.use_git_bin = false; // TODO
2011-05-31 01:26:57 +00:00
}
2012-02-09 01:46:25 +00:00
TargetFolder = target_folder;
RemoteUrl = uri;
2011-05-31 01:26:57 +00:00
}
public override bool Fetch ()
{
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;
Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);
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;
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 ("|"))
2012-07-22 20:54:59 +00:00
number = (number / 100 * 80 + 20); // "Receiving objects" stage
2011-08-27 15:53:17 +00:00
else
2012-07-22 20:54:59 +00:00
number = (number / 100 * 20); // "Compressing objects" stage
} else {
2012-07-28 13:58:09 +00:00
SparkleLogger.LogInfo ("Fetcher", line);
line = line.Trim (new char [] {' ', '@'});
if (line.StartsWith ("fatal:", StringComparison.InvariantCultureIgnoreCase) ||
line.StartsWith ("error:", StringComparison.InvariantCultureIgnoreCase)) {
2012-06-17 20:56:27 +00:00
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!");
}
2011-08-27 15:53:17 +00:00
}
2011-08-27 15:53:17 +00:00
if (number >= percentage) {
percentage = number;
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
}
}
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);
}
2012-06-17 20:56:27 +00:00
base.OnProgressChanged (100);
InstallConfiguration ();
InstallExcludeRules ();
2012-06-29 13:00:25 +00:00
InstallAttributeRules ();
2012-06-17 20:56:27 +00:00
2012-02-09 01:46:25 +00:00
AddWarnings ();
return true;
} else {
return false;
}
}
public override bool IsFetchedRepoEmpty {
get {
SparkleGit git = new SparkleGit (TargetFolder, "rev-parse HEAD");
git.StartInfo.RedirectStandardError = true;
git.Start ();
// Reading the standard output HAS to go before
// WaitForExit, or it will hang forever on output > 4096 bytes
git.StandardOutput.ReadToEnd ();
git.StandardError.ReadToEnd ();
git.WaitForExit ();
return (git.ExitCode != 0);
}
}
public override void EnableFetchedRepoCrypto (string password)
2012-02-09 01:46:25 +00:00
{
// Define the crypto filter in the config
2012-07-28 13:58:09 +00:00
string repo_config_file_path = new string [] { TargetFolder, ".git", "config" }.Combine ();
string config = File.ReadAllText (repo_config_file_path);
string n = Environment.NewLine;
config += "[filter \"crypto\"]" + n +
"\tsmudge = openssl enc -d -aes-256-cbc -base64 -S " + this.crypto_salt + " -pass file:.git/password" + n +
"\tclean = openssl enc -e -aes-256-cbc -base64 -S " + this.crypto_salt + " -pass file:.git/password" + n;
File.WriteAllText (repo_config_file_path, config);
// Pass all files through the crypto filter
2012-07-28 13:58:09 +00:00
string git_attributes_file_path = new string [] { TargetFolder, ".git", "info", "attributes" }.Combine ();
File.AppendAllText (git_attributes_file_path, "\n* filter=crypto");
// Store the password
2012-07-28 13:58:09 +00:00
string password_file_path = new string [] { TargetFolder, ".git", "password" }.Combine ();
File.WriteAllText (password_file_path, password.Trim ());
}
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");
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 ();
2012-07-22 20:54:59 +00:00
if (git.ExitCode != 0)
return false;
2012-07-22 20:54:59 +00:00
else
File.WriteAllText (password_check_file_path, output);
}
Process process = new Process () {
EnableRaisingEvents = true
};
process.StartInfo.WorkingDirectory = TargetFolder;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "openssl";
process.StartInfo.Arguments = "enc -d -aes-256-cbc -base64 -S " + this.crypto_salt +
" -pass pass:\"" + password + "\" -in " + password_check_file_path;
process.Start ();
process.WaitForExit ();
if (process.ExitCode == 0) {
File.Delete (password_check_file_path);
return true;
} else {
return false;
}
}
2012-02-09 01:46:25 +00:00
public override void Stop ()
{
2012-04-11 10:51:53 +00:00
try {
2012-04-11 11:04:42 +00:00
this.git.Close ();
this.git.Kill ();
this.git.Dispose ();
2012-04-11 10:51:53 +00:00
} catch (Exception e) {
2012-07-28 13:58:09 +00:00
SparkleLogger.LogInfo ("Fetcher", "Failed to dispose properly: " + e.Message);
}
}
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 ();
}
private void InstallConfiguration ()
{
string [] settings = new string [] {
"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
"core.autocrlf false", // Don't change file line endings
"core.precomposedunicode true", // Use the same Unicode form on all filesystems
"core.safecrlf false",
"core.packedGitLimit 128m", // Some memory limiting options
"core.packedGitWindowSize 128m",
"pack.deltaCacheSize 128m",
"pack.packSizeLimit 128m",
"pack.windowMemory 128m"
};
foreach (string setting in settings) {
SparkleGit git_config = new SparkleGit (TargetFolder, "config " + setting);
2012-07-26 10:12:14 +00:00
git_config.StartAndWaitForExit ();
}
if (this.use_git_bin)
InstallGitBinConfiguration ();
}
public void InstallGitBinConfiguration ()
{
string [] settings = new string [] {
2012-06-29 13:00:25 +00:00
"core.bigFileThreshold 8g",
"filter.bin.clean \"git bin clean %f\"",
"filter.bin.smudge \"git bin smudge\"",
2012-06-29 13:00:25 +00:00
"git-bin.chunkSize 1m",
"git-bin.s3bucket \"your bucket name\"",
"git-bin.s3key \"your key\"",
2012-06-29 13:00:25 +00:00
"git-bin.s3secretKey \"your secret key\""
};
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
private 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
}
2012-06-29 13:00:25 +00:00
private void InstallAttributeRules ()
{
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);
if (this.use_git_bin) {
writer.WriteLine ("* filter=bin binary");
} else {
2012-06-29 13:00:25 +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", "tar", "iso" // Archives
};
foreach (string extension in extensions) {
writer.WriteLine ("*." + extension + " -delta");
writer.WriteLine ("*." + extension.ToUpper () + " -delta");
}
}
writer.Close ();
}
private void AddWarnings ()
{
2012-06-17 20:56:27 +00:00
/*
SparkleGit git = new SparkleGit (TargetFolder,
"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;
else
this.warnings.Add ("You seem to have a system wide gitignore file, this may affect SparkleShare files.");
2012-06-17 20:56:27 +00:00
*/
}
}
}