1
1
using System ;
2
2
using System . Diagnostics ;
3
+ using System . IO ;
3
4
4
5
namespace Skarp . Version . Cli . Vcs . Git
5
6
{
@@ -10,14 +11,15 @@ public class GitVcs : IVcs
10
11
/// </summary>
11
12
/// <param name="csProjFilePath">Path to the cs project file that was version updated</param>
12
13
/// <param name="message">The message to include in the commit</param>
13
- public void Commit ( string csProjFilePath , string message )
14
+ /// <param name="cwd"></param>
15
+ public void Commit ( string csProjFilePath , string message , string cwd = null )
14
16
{
15
- if ( ! LaunchGitWithArgs ( $ "add \" { csProjFilePath } \" ") )
17
+ if ( ! LaunchGitWithArgs ( $ "add \" { csProjFilePath } \" ", cwd : cwd ) )
16
18
{
17
19
throw new OperationCanceledException ( $ "Unable to add cs proj file { csProjFilePath } to git index") ;
18
20
}
19
21
20
- if ( ! LaunchGitWithArgs ( $ "commit -m \" { message } \" ") )
22
+ if ( ! LaunchGitWithArgs ( $ "commit -m \" { message } \" ", cwd : cwd ) )
21
23
{
22
24
throw new OperationCanceledException ( "Unable to commit" ) ;
23
25
}
@@ -27,42 +29,54 @@ public void Commit(string csProjFilePath, string message)
27
29
/// Determines whether the current repository is clean.
28
30
/// </summary>
29
31
/// <returns></returns>
30
- public bool IsRepositoryClean ( )
32
+ public bool IsRepositoryClean ( string cwd = null )
31
33
{
32
- return LaunchGitWithArgs ( "diff-index --quiet HEAD --" ) ;
34
+ return LaunchGitWithArgs ( "diff-index HEAD --" , cwd : cwd ) ;
33
35
}
34
36
35
37
/// <summary>
36
38
/// Determines whether git is present in PATH on the current computer
37
39
/// </summary>
38
40
/// <returns></returns>
39
- public bool IsVcsToolPresent ( )
41
+ public bool IsVcsToolPresent ( string cwd = null )
40
42
{
41
43
// launching `git --help` returns exit code 0 where as `git` returns 1 as git wants a cmd line argument
42
- return LaunchGitWithArgs ( "--help" ) ;
44
+ return LaunchGitWithArgs ( "--help" , cwd : cwd ) ;
43
45
}
44
46
45
47
/// <summary>
46
48
/// Creates a new tag
47
49
/// </summary>
48
50
/// <param name="tagName">Name of the tag</param>
49
- public void Tag ( string tagName )
51
+ /// <param name="cwd"></param>
52
+ public void Tag ( string tagName , string cwd = null )
50
53
{
51
- if ( ! LaunchGitWithArgs ( $ "tag { tagName } ") )
54
+ if ( ! LaunchGitWithArgs ( $ "tag { tagName } ", cwd : cwd ) )
52
55
{
53
56
throw new OperationCanceledException ( "Unable to create tag" ) ;
54
57
}
55
58
}
56
59
57
- private static bool LaunchGitWithArgs ( string args , int waitForExitTimeMs = 1000 , int exitCode = 0 )
60
+ /// <summary>
61
+ /// Helper method for launching git with different arguments while returning just a boolean of whether the
62
+ /// "command" was successful
63
+ /// </summary>
64
+ /// <param name="args">The args to pass onto git, e.g `diff` to launch `git diff`</param>
65
+ /// <param name="waitForExitTimeMs">How long to wait for the git operation to complete</param>
66
+ /// <param name="exitCode">The expected exit code</param>
67
+ /// <param name="cwd">The working directory to change into, if any. Leave null for "current directory" </param>
68
+ /// <returns></returns>
69
+ internal static bool LaunchGitWithArgs (
70
+ string args ,
71
+ int waitForExitTimeMs = 1000 ,
72
+ int exitCode = 0 ,
73
+ string cwd = null
74
+ )
58
75
{
59
76
try
60
77
{
61
- var startInfo = CreateGitShellStartInfo ( args ) ;
62
- var proc = Process . Start ( startInfo ) ;
63
- proc . WaitForExit ( waitForExitTimeMs ) ;
64
-
65
- return proc . ExitCode == exitCode ;
78
+ var ( procExitCode , stdOut , stdErr ) = LaunchGitWithArgsInner ( args , waitForExitTimeMs , cwd ) ;
79
+ return procExitCode == exitCode ;
66
80
}
67
81
catch ( Exception ex )
68
82
{
@@ -71,15 +85,36 @@ private static bool LaunchGitWithArgs(string args, int waitForExitTimeMs = 1000,
71
85
}
72
86
}
73
87
74
- private static ProcessStartInfo CreateGitShellStartInfo ( string args )
88
+ internal static ( int ExitCode , string stdOut , string stdErr ) LaunchGitWithArgsInner (
89
+ string args ,
90
+ int waitForExitTimeMs ,
91
+ string cwd = null
92
+ )
75
93
{
76
- return new ProcessStartInfo ( "git" )
94
+ var startInfo = CreateGitShellStartInfo ( args , cwd ) ;
95
+ var proc = Process . Start ( startInfo ) ;
96
+ proc . WaitForExit ( waitForExitTimeMs ) ;
97
+
98
+ var stdOut = proc . StandardOutput . ReadToEnd ( ) ;
99
+ var stdErr = proc . StandardError . ReadToEnd ( ) ;
100
+ return ( proc . ExitCode , stdOut , stdErr ) ;
101
+ }
102
+
103
+ internal static ProcessStartInfo CreateGitShellStartInfo ( string args , string cwd = null )
104
+ {
105
+ var procInfo = new ProcessStartInfo ( "git" )
77
106
{
78
107
Arguments = args ,
79
108
RedirectStandardError = true ,
80
109
RedirectStandardInput = true ,
81
110
RedirectStandardOutput = true ,
82
111
} ;
112
+
113
+ if ( ! string . IsNullOrWhiteSpace ( cwd ) )
114
+ {
115
+ procInfo . WorkingDirectory = cwd ;
116
+ }
117
+ return procInfo ;
83
118
}
84
119
85
120
public string ToolName ( )
0 commit comments