Commits

Christian Specht committed 1cc236f

find git.exe, no matter if git.exe or git.cmd is in the %PATH%

  • Participants
  • Parent commits 9d2461a

Comments (0)

Files changed (2)

 Before you run Bitbucket Backup the first time, you need to edit **BitbucketBackup.exe.config** and provide your data.  
 
 Please note that Bitbucket Backup assumes that you have the Mercurial and Git executables in your **%PATH%** variable.  
-**ATTENTION:** At the moment, Git only works when you have the **bin** directory in your **%PATH%** variable.
+(depending on the version, Git may come with a **git.exe** AND a **git.cmd** - it doesn't matter which one is in the **%PATH%**, Bitbucket Backup will find both)
 
 ### Acknowledgements
 

File src/BitbucketBackup/GitWrapper.cs

-using System.Diagnostics;
+using System;
+using System.Diagnostics;
+using System.IO;
 
 namespace BitbucketBackup
 {
     /// <summary>
-    /// Simple wrapper for Git (assumes that git.exe is in your %PATH%)
+    /// Simple wrapper for Git (assumes that git.exe or git.cmd is in your %PATH%)
     /// </summary>
     internal class GitWrapper
     {
         private string folder;
+        private string executable;
 
         /// <summary>
         /// Creates a new GitWrapper instance.
         public GitWrapper(string folder)
         {
             this.folder = folder;
+            this.executable = this.FindExecutable();
         }
 
         /// <summary>
         /// </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.FileName = this.executable;
             info.Arguments = gitCommand;
             info.CreateNoWindow = true;
             info.WorkingDirectory = this.folder;
             git.WaitForExit();
             git.Close();      
         }
+
+        /// <summary>
+        /// Finds out the path to git.exe
+        /// </summary>
+        /// <returns>The complete path to git.exe</returns>
+        private string FindExecutable()
+        {
+            string pathVariable = Environment.GetEnvironmentVariable("path");
+
+            foreach (var value in pathVariable.Split(';'))
+            {
+                if (File.Exists(Path.Combine(value, "git.exe")))
+                {
+                    return Path.Combine(value, "git.exe");
+                }
+
+                if (File.Exists(Path.Combine(value, "git.cmd")))
+                {
+                    // Calling git.cmd with Process.Start doesn't work, so we always need to use git.exe.
+                    // git.cmd is in a folder called "cmd", and git.exe is in a folder called "bin", which is on the same level as "cmd".
+                    var exePath = Path.Combine(Directory.GetParent(value).ToString(), "bin");
+
+                    if (File.Exists(Path.Combine(exePath, "git.exe")))
+                    {
+                        return Path.Combine(exePath, "git.exe");
+                    }
+                }
+            }
+
+            throw new FileNotFoundException("No Git executable found!");
+        }
     }
 }