Commits

Christian Specht committed 517ad92

add basic Git support (fixes #9)

Didn't find anything like Mercurial.Net for Git that worked
--> had to write simple Git wrapper myself

  • Participants
  • Parent commits f4dfbc9

Comments (0)

Files changed (7)

File src/BitbucketBackup/BitbucketBackup.csproj

     <Compile Include="BitbucketRequest.cs" />
     <Compile Include="ClientException.cs" />
     <Compile Include="Config.cs" />
+    <Compile Include="GitRepository.cs" />
+    <Compile Include="GitWrapper.cs" />
     <Compile Include="MercurialRepository.cs" />
     <Compile Include="RepositoryBase.cs" />
     <Compile Include="Program.cs" />

File src/BitbucketBackup/GitRepository.cs

+using System.IO;
+
+namespace BitbucketBackup
+{
+    /// <summary>
+    /// Creates and pulls from Git repositories.
+    /// </summary>
+    internal class GitRepository : RepositoryBase
+    {
+        private GitWrapper git;
+
+        public GitRepository(string remoteUri, string folder) : base(remoteUri, folder) { }
+
+        protected override void Init()
+        {
+            this.git = new GitWrapper(folder);
+
+            if (!Directory.Exists(Path.Combine(this.folder, ".git")))
+            {
+                this.git.Execute("init");
+            }
+        }
+
+        public override string PullingMessage
+        {
+            get { return Resources.PullingGit; }
+        }
+
+        public override void Pull()
+        {
+            this.git.Execute("pull " + this.remoteuri + " --bare");
+        }
+    }
+}

File src/BitbucketBackup/GitWrapper.cs

+using System.Diagnostics;
+
+namespace BitbucketBackup
+{
+    /// <summary>
+    /// Simple wrapper for Git (assumes that git.exe is in your %PATH%)
+    /// </summary>
+    internal class GitWrapper
+    {
+        private string folder;
+
+        /// <summary>
+        /// Creates a new GitWrapper instance.
+        /// </summary>
+        /// <param name="folder">The folder for the local repository.</param>
+        public GitWrapper(string folder)
+        {
+            this.folder = folder;
+        }
+
+        /// <summary>
+        /// Executes Git with the given command
+        /// </summary>
+        /// <param name="gitCommand">The command to execute, e.g. "init"</param>
+        public void Execute(string gitCommand)
+        {      
+            var info = new ProcessStartInfo();
+            info.FileName = "git.exe";
+            info.Arguments = gitCommand;
+            info.CreateNoWindow = true;
+            info.WorkingDirectory = this.folder;
+            info.UseShellExecute = false;
+
+            var git = new Process();
+            git.StartInfo = info;
+            git.Start();
+            git.WaitForExit();
+            git.Close();      
+        }
+    }
+}

File src/BitbucketBackup/Program.cs

 
                 foreach (var repo in repos)
                 {
-                    if (repo.Scm == "hg")
+                    var repoUri = new Uri(baseUri, repo.RepoName);
+                    string repoPath = Path.Combine(config.BackupFolder, repo.RepoName);
+
+                    var updater = new RepositoryUpdater(repo.Scm, repoUri, repoPath, config);
+                    updater.Update();
+
+                    if (repo.HasWiki)
                     {
-                        var repoUri = new Uri(baseUri, repo.RepoName);
-                        string repoPath = Path.Combine(config.BackupFolder, repo.RepoName);
+                        var wikiUri = new Uri(baseUri, repo.RepoName + "/wiki");
+                        string wikiPath = Path.Combine(config.BackupFolder, repo.RepoName + "-wiki");
 
-                        var updater = new RepositoryUpdater(repo.Scm, repoUri, repoPath, config);
-                        updater.Update();
-
-                        if (repo.HasWiki)
-                        {
-                            var wikiUri = new Uri(baseUri, repo.RepoName + "/wiki");
-                            string wikiPath = Path.Combine(config.BackupFolder, repo.RepoName + "-wiki");
-
-                            var wikiUpdater = new RepositoryUpdater(repo.Scm, wikiUri, wikiPath, config);
-                            wikiUpdater.Update();
-                        }
+                        var wikiUpdater = new RepositoryUpdater(repo.Scm, wikiUri, wikiPath, config);
+                        wikiUpdater.Update();
                     }
                 }
 

File src/BitbucketBackup/RepositoryFactory.cs

             {
                 case "hg":
                     return new MercurialRepository(remoteUri, localFolder);
+                case "git":
+                    return new GitRepository(remoteUri, localFolder);
                 default:
                     return null;
             }

File src/BitbucketBackup/Resources.Designer.cs

         }
         
         /// <summary>
+        ///   Sucht eine lokalisierte Zeichenfolge, die git pull: {0} ähnelt.
+        /// </summary>
+        internal static string PullingGit {
+            get {
+                return ResourceManager.GetString("PullingGit", resourceCulture);
+            }
+        }
+        
+        /// <summary>
         ///   Sucht eine lokalisierte Zeichenfolge, die hg pull: {0} ähnelt.
         /// </summary>
         internal static string PullingMercurial {

File src/BitbucketBackup/Resources.resx

     <value>Authentication failed.
 Please check if the password is valid!</value>
   </data>
+  <data name="PullingGit" xml:space="preserve">
+    <value>git pull: {0}</value>
+  </data>
 </root>