diff --git a/SparkleLib/Git/SparkleFetcherGit.cs b/SparkleLib/Git/SparkleFetcherGit.cs index 2d7d4ed8..105f994a 100644 --- a/SparkleLib/Git/SparkleFetcherGit.cs +++ b/SparkleLib/Git/SparkleFetcherGit.cs @@ -25,8 +25,44 @@ namespace SparkleLib { // Sets up a fetcher that can get remote folders public class SparkleFetcherGit : SparkleFetcherBase { - public SparkleFetcherGit (string remote_url, string target_folder) : - base (remote_url, target_folder) { } + public SparkleFetcherGit (string server, string remote_folder, string target_folder) : + base (server, remote_folder, target_folder) + { + remote_folder = remote_folder.Trim ("/".ToCharArray ()); + + // 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 ()); + + if (server.StartsWith ("ssh://")) + server = server.Substring (6); + + if (!server.StartsWith ("@")) + server = "git@" + server; + + server = "ssh://" + server; + } + + base.target_folder = target_folder; + base.remote_url = server + "/" + remote_folder; + } public override bool Fetch () diff --git a/SparkleLib/Hg/SparkleFetcherHg.cs b/SparkleLib/Hg/SparkleFetcherHg.cs index 9e926ccc..fcdc7d43 100644 --- a/SparkleLib/Hg/SparkleFetcherHg.cs +++ b/SparkleLib/Hg/SparkleFetcherHg.cs @@ -25,8 +25,8 @@ namespace SparkleLib { // Sets up a fetcher that can get remote folders public class SparkleFetcherHg : SparkleFetcherBase { - public SparkleFetcherHg (string remote_url, string target_folder) : - base (remote_url, target_folder) { } + public SparkleFetcherHg (string server, string remote_folder, string target_folder) : + base (server, remote_folder, target_folder) { } public override bool Fetch () diff --git a/SparkleLib/Scp/SparkleFetcherScp.cs b/SparkleLib/Scp/SparkleFetcherScp.cs index 37cdf4df..4284f4aa 100644 --- a/SparkleLib/Scp/SparkleFetcherScp.cs +++ b/SparkleLib/Scp/SparkleFetcherScp.cs @@ -25,8 +25,8 @@ namespace SparkleLib { // Sets up a fetcher that can get remote folders public class SparkleFetcherScp : SparkleFetcherBase { - public SparkleFetcherScp (string remote_url, string target_folder) : - base (remote_url, target_folder) { } + public SparkleFetcherScp (string server, string remote_folder, string target_folder) : + base (server, remote_folder, target_folder) { } public override bool Fetch () diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs index 6df608ea..e092d728 100644 --- a/SparkleLib/SparkleFetcherBase.cs +++ b/SparkleLib/SparkleFetcherBase.cs @@ -18,8 +18,11 @@ using System; using System.IO; using System.Diagnostics; +using System.Text.RegularExpressions; using System.Threading; +using Mono.Unix; + namespace SparkleLib { // Sets up a fetcher that can get remote folders @@ -38,17 +41,17 @@ namespace SparkleLib { private Thread thread; - public SparkleFetcherBase (string remote_url, string target_folder) + public SparkleFetcherBase (string server, string remote_folder, string target_folder) { this.target_folder = target_folder; - this.remote_url = remote_url; + this.remote_url = server + "/" + remote_folder; } // Clones the remote repository public void Start () { - SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder..."); + SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder: " + this.remote_url); if (Started != null) Started (); @@ -56,14 +59,30 @@ namespace SparkleLib { if (Directory.Exists (this.target_folder)) Directory.Delete (this.target_folder, true); + string host = GetHost (this.remote_url); + + if (String.IsNullOrEmpty (host)) { + if (Failed != null) + Failed (); + + return; + } + + DisableHostKeyCheckingForHost (host); + this.thread = new Thread (new ThreadStart (delegate { if (Fetch ()) { - SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching finished"); + SparkleHelpers.DebugInfo ("Fetcher", "Finished"); + + EnableHostKeyCheckingForHost (host); if (Finished != null) Finished (); + } else { - SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching failed"); + SparkleHelpers.DebugInfo ("Fetcher", "Failed"); + + EnableHostKeyCheckingForHost (host); if (Failed != null) Failed (); @@ -74,6 +93,13 @@ namespace SparkleLib { } + public string RemoteUrl { + get { + return this.remote_url; + } + } + + public void Dispose () { this.thread.Abort (); @@ -82,5 +108,79 @@ namespace SparkleLib { public abstract bool Fetch (); + + + private void DisableHostKeyCheckingForHost (string host) + { + string ssh_config_file_path = SparkleHelpers.CombineMore ( + SparklePaths.HomePath, ".ssh", "config"); + + string ssh_config = Environment.NewLine + "Host " + host + + Environment.NewLine + "\tStrictHostKeyChecking no"; + + if (File.Exists (ssh_config_file_path)) { + TextWriter writer = File.AppendText (ssh_config_file_path); + writer.WriteLine (ssh_config); + writer.Close (); + + } else { + TextWriter writer = new StreamWriter (ssh_config_file_path); + writer.WriteLine (ssh_config); + writer.Close (); + } + + UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path); + file_info.FileAccessPermissions = (FileAccessPermissions.UserRead | + FileAccessPermissions.UserWrite); + + SparkleHelpers.DebugInfo ("Fetcher", "Disabled host key checking"); + } + + + private void EnableHostKeyCheckingForHost (string host) + { + string ssh_config_file_path = SparkleHelpers.CombineMore ( + SparklePaths.HomePath, ".ssh", "config"); + + string ssh_config = Environment.NewLine + "Host " + host + + Environment.NewLine + "\tStrictHostKeyChecking no"; + + if (File.Exists (ssh_config_file_path)) { + StreamReader reader = new StreamReader (ssh_config_file_path); + string current_ssh_config = reader.ReadToEnd (); + reader.Close (); + + current_ssh_config = current_ssh_config.Remove ( + current_ssh_config.IndexOf (ssh_config), ssh_config.Length); + + bool has_some_ssh_config = new Regex (@"[a-z]").IsMatch (current_ssh_config); + if (!has_some_ssh_config) { + File.Delete (ssh_config_file_path); + + } else { + TextWriter writer = new StreamWriter (ssh_config_file_path); + writer.WriteLine (current_ssh_config); + writer.Close (); + + UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path); + file_info.FileAccessPermissions = (FileAccessPermissions.UserRead | + FileAccessPermissions.UserWrite); + } + } + + SparkleHelpers.DebugInfo ("Fetcher", "Enabled host key checking"); + } + + + private string GetHost (string url) + { + Regex regex = new Regex (@"(@|://)([a-z0-9\.]+)(/|:)"); + Match match = regex.Match (url); + + if (match.Success) + return match.Groups [2].Value; + else + return null; + } } } diff --git a/SparkleShare/Mac/SparkleIntro.cs b/SparkleShare/Mac/SparkleIntro.cs index 84aaf76c..884c10c3 100644 --- a/SparkleShare/Mac/SparkleIntro.cs +++ b/SparkleShare/Mac/SparkleIntro.cs @@ -279,57 +279,9 @@ namespace SparkleShare { }; SyncButton.Activated += delegate { - string name = FolderNameTextField.StringValue; - - // Remove the starting slash if there is one - if (name.StartsWith ("/")) - name = name.Substring (1); - - string server = AddressTextField.StringValue; - - if (name.EndsWith ("/")) - name = name.TrimEnd ("/".ToCharArray ()); - - if (name.StartsWith ("/")) - name = name.TrimStart ("/".ToCharArray ()); - - if (server.StartsWith ("ssh://")) - server = server.Substring (6); - - if (ServerType == 0) { - - // Use the default user 'git' if no username is specified - if (!server.Contains ("@")) - server = "git@" + server; - - // Prepend the Secure Shell protocol when it isn't specified - if (!server.StartsWith ("ssh://")) - server = "ssh://" + server; - - // Remove the trailing slash if there is one - if (server.EndsWith ("/")) - server = server.TrimEnd ("/".ToCharArray ()); - } - - if (ServerType == 2) { - server = "ssh://git@gitorious.org"; - - if (!name.EndsWith (".git")) { - if (!name.Contains ("/")) - name = name + "/" + name; - - name += ".git"; - } - } - - if (ServerType == 1) - server = "ssh://git@github.com"; - - if (ServerType == 3) - server = "ssh://git@gnome.org/git/"; - - string url = server + "/" + name; - string canonical_name = Path.GetFileNameWithoutExtension (name); + string folder_name = FolderNameTextField.StringValue; + string server = AddressTextField.StringValue; + string canonical_name = Path.GetFileNameWithoutExtension (folder_name); ShowSyncingPage (canonical_name); @@ -345,7 +297,7 @@ namespace SparkleShare { }); }; - SparkleShare.Controller.FetchFolder (url, name); + SparkleShare.Controller.FetchFolder (server, folder_name); }; Buttons.Add (SyncButton); diff --git a/SparkleShare/SparkleController.cs b/SparkleShare/SparkleController.cs index 38f0bbde..920eae98 100644 --- a/SparkleShare/SparkleController.cs +++ b/SparkleShare/SparkleController.cs @@ -782,68 +782,6 @@ namespace SparkleShare { } } - - private void DisableHostKeyCheckingForHost (string host) - { - string ssh_config_file_path = SparkleHelpers.CombineMore ( - SparklePaths.HomePath, ".ssh", "config"); - - string ssh_config = Environment.NewLine + "Host " + host + - Environment.NewLine + "\tStrictHostKeyChecking no"; - - if (File.Exists (ssh_config_file_path)) { - TextWriter writer = File.AppendText (ssh_config_file_path); - writer.WriteLine (ssh_config); - writer.Close (); - - } else { - TextWriter writer = new StreamWriter (ssh_config_file_path); - writer.WriteLine (ssh_config); - writer.Close (); - } - - UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path); - file_info.FileAccessPermissions = (FileAccessPermissions.UserRead | - FileAccessPermissions.UserWrite); - - SparkleHelpers.DebugInfo ("Controller", "Disabled host key checking"); - } - - - private void EnableHostKeyCheckingForHost (string host) - { - string ssh_config_file_path = SparkleHelpers.CombineMore ( - SparklePaths.HomePath, ".ssh", "config"); - - string ssh_config = Environment.NewLine + "Host " + host + - Environment.NewLine + "\tStrictHostKeyChecking no"; - - if (File.Exists (ssh_config_file_path)) { - StreamReader reader = new StreamReader (ssh_config_file_path); - string current_ssh_config = reader.ReadToEnd (); - reader.Close (); - - current_ssh_config = current_ssh_config.Remove ( - current_ssh_config.IndexOf (ssh_config), ssh_config.Length); - - bool has_some_ssh_config = new Regex (@"[a-z]").IsMatch (current_ssh_config); - if (!has_some_ssh_config) { - File.Delete (ssh_config_file_path); - - } else { - TextWriter writer = new StreamWriter (ssh_config_file_path); - writer.WriteLine (current_ssh_config); - writer.Close (); - - UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path); - file_info.FileAccessPermissions = (FileAccessPermissions.UserRead | - FileAccessPermissions.UserWrite); - } - } - - SparkleHelpers.DebugInfo ("Controller", "Enabled host key checking"); - } - // Gets the avatar for a specific email address and size public string GetAvatar (string email, int size) @@ -895,43 +833,30 @@ namespace SparkleShare { } - public void FetchFolder (string url, string name) + public void FetchFolder (string server, string remote_folder) { if (!Directory.Exists (SparklePaths.SparkleTmpPath)) Directory.CreateDirectory (SparklePaths.SparkleTmpPath); - SparkleHelpers.DebugInfo ("Controller", "Formed URL: " + url); - - string host = GetHost (url); - - if (String.IsNullOrEmpty (host)) { - if (FolderFetchError != null) - FolderFetchError (); - - return; - } - - DisableHostKeyCheckingForHost (host); - // Strip the '.git' from the name - string canonical_name = Path.GetFileNameWithoutExtension (name); + string canonical_name = Path.GetFileNameWithoutExtension (remote_folder); string tmp_folder = Path.Combine (SparklePaths.SparkleTmpPath, canonical_name); SparkleFetcherBase fetcher = null; string backend = null; - if (url.EndsWith (".hg")) { - url = url.Substring (0, (url.Length - 3)); - fetcher = new SparkleFetcherHg (url, tmp_folder); - backend = "Hg"; + if (remote_folder.EndsWith (".hg")) { + remote_folder = remote_folder.Substring (0, (remote_folder.Length - 3)); + fetcher = new SparkleFetcherHg (server, remote_folder, tmp_folder); + backend = "Hg"; - } else if (url.EndsWith (".scp")) { - url = url.Substring (0, (url.Length - 4)); - fetcher = new SparkleFetcherScp (url, tmp_folder); + } else if (remote_folder.EndsWith (".scp")) { + remote_folder = remote_folder.Substring (0, (remote_folder.Length - 4)); + fetcher = new SparkleFetcherScp (server, remote_folder, tmp_folder); backend = "Scp"; } else { - fetcher = new SparkleFetcherGit (url, tmp_folder); + fetcher = new SparkleFetcherGit (server, remote_folder, tmp_folder); backend = "Git"; } @@ -951,7 +876,6 @@ namespace SparkleShare { target_folder_name += " (" + i + ")"; fetcher.Finished += delegate { - EnableHostKeyCheckingForHost (host); // Needed to do the moving SparkleHelpers.ClearAttributes (tmp_folder); @@ -963,7 +887,7 @@ namespace SparkleShare { SparkleHelpers.DebugInfo ("Controller", "Error moving folder: " + e.Message); } - SparkleConfig.DefaultConfig.AddFolder (target_folder_name, url, backend); + SparkleConfig.DefaultConfig.AddFolder (target_folder_name, fetcher.RemoteUrl, backend); AddRepository (target_folder_path); if (FolderFetched != null) @@ -985,7 +909,6 @@ namespace SparkleShare { fetcher.Failed += delegate { - EnableHostKeyCheckingForHost (host); if (FolderFetchError != null) FolderFetchError (); @@ -1011,18 +934,6 @@ namespace SparkleShare { } - private string GetHost (string url) - { - Regex regex = new Regex (@"(@|://)([a-z0-9\.]+)(/|:)"); - Match match = regex.Match (url); - - if (match.Success) - return match.Groups [2].Value; - else - return null; - } - - // Checks whether there are any folders syncing and // quits if safe public void TryQuit () diff --git a/SparkleShare/SparkleIntro.cs b/SparkleShare/SparkleIntro.cs index 045f6c4d..ff399a89 100644 --- a/SparkleShare/SparkleIntro.cs +++ b/SparkleShare/SparkleIntro.cs @@ -269,59 +269,18 @@ namespace SparkleShare { SyncButton = new Button (_("Sync")); SyncButton.Clicked += delegate { - string name = FolderEntry.Text; - - // Remove the starting slash if there is one - if (name.StartsWith ("/")) - name = name.Substring (1); - - string server = ServerEntry.Text; - - if (name.EndsWith ("/")) - name = name.TrimEnd ("/".ToCharArray ()); - - if (name.StartsWith ("/")) - name = name.TrimStart ("/".ToCharArray ()); - - if (server.StartsWith ("ssh://")) - server = server.Substring (6); - - if (radio_button.Active) { - - // Use the default user 'git' if no username is specified - if (!server.Contains ("@")) - server = "git@" + server; - - // Prepend the Secure Shell protocol when it isn't specified - if (!server.StartsWith ("ssh://")) - server = "ssh://" + server; - - // Remove the trailing slash if there is one - if (server.EndsWith ("/")) - server = server.TrimEnd ("/".ToCharArray ()); - } + string folder_name = FolderEntry.Text; + string server = ServerEntry.Text; + string canonical_name = System.IO.Path.GetFileNameWithoutExtension (name); if (radio_button_gitorious.Active) { - server = "ssh://git@gitorious.org"; - - if (!name.EndsWith (".git")) { - if (!name.Contains ("/")) - name = name + "/" + name; - - name += ".git"; - } - } + server = "gitorious.org"; if (radio_button_github.Active) - server = "ssh://git@github.com"; + server = "github.com"; if (radio_button_gnome.Active) - server = "ssh://git@gnome.org/git/"; - - string url = server + "/" + name; - Console.WriteLine ("View", "[" + name + "] Formed URL: " + url); - - string canonical_name = System.IO.Path.GetFileNameWithoutExtension (name); + server = "gnome.org"; ShowSyncingPage (canonical_name); @@ -339,7 +298,7 @@ namespace SparkleShare { }); }; - SparkleShare.Controller.FetchFolder (url, name); + SparkleShare.Controller.FetchFolder (server, folder_name); };