forked from yysun/Git-Source-Control-Provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitBash.cs
89 lines (76 loc) · 2.89 KB
/
GitBash.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Configuration;
using System.IO;
namespace GitScc
{
public abstract class GitBash
{
private static string gitExePath;
public static string GitExePath
{
set
{
try
{
gitExePath = value == null ? null : Path.Combine(Path.GetDirectoryName(value), "git.exe");
}
catch{}
}
}
public static bool Exists { get { return !string.IsNullOrWhiteSpace(gitExePath) &&
File.Exists(gitExePath); } }
public static string Run(string args, string workingDirectory)
{
if (string.IsNullOrWhiteSpace(gitExePath) || !File.Exists(gitExePath))
throw new Exception("Git Executable not found");
//Debug.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory));
var pinfo = new ProcessStartInfo(gitExePath)
{
Arguments = args,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WorkingDirectory = workingDirectory,
};
using (var process = Process.Start(pinfo))
{
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
//Debug.WriteLine(output);
if (!string.IsNullOrEmpty(error))
{
//Debug.WriteLine("STDERR: " + error);
throw new Exception(error);
}
return output;
}
}
public static void RunCmd(string args, string workingDirectory)
{
if (string.IsNullOrWhiteSpace(gitExePath) || !File.Exists(gitExePath))
throw new Exception("Git Executable not found");
Debug.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory));
var pinfo = new ProcessStartInfo("cmd.exe")
{
Arguments = "/C \"\"" + gitExePath + "\" " + args + "\"",
CreateNoWindow = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = workingDirectory,
};
using (var process = Process.Start(pinfo))
{
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (!string.IsNullOrEmpty(error))
throw new Exception(error);
}
}
}
}