2 * Copyright (C) 2009, Henon <meinrad.recheis@gmail.com>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 using System
.Collections
.Generic
;
43 using System
.Reflection
;
44 using GitSharp
.Commands
;
49 /// The static class Git provides everything to interact with git itself, such as the command line interface commands, the git configuration or properties that are affecting git globally.
51 public static class Git
58 /// Returns the version of GitSharp.
60 public static string Version
64 Assembly assembly
= Assembly
.Load("GitSharp");
66 Version version
= assembly
.GetName().Version
;
70 object[] attributes
= assembly
.GetCustomAttributes(typeof(AssemblyProductAttribute
), false);
71 if (attributes
.Length
== 0)
73 // No AssemblyProduct attribute to parse, no commit hash to extract
74 return version
.ToString();
77 string commitHash
= ExtractCommitHashFrom(((AssemblyProductAttribute
)attributes
[0]).Product
);
78 return string.Format("{0}.{1}.{2}.{3}", version
.Major
, version
.Minor
, version
.Build
, commitHash
);
82 private static string ExtractCommitHashFrom(string product
)
84 // TODO: Maybe should we switch to a regEx capture ?
85 string[] parts
= product
.Split(new[] { '[' }
);
86 return parts
[1].TrimEnd(']');
92 #region Defaults for git commands
96 /// Get or set the default output stream that all git commands are writing to. Per default this returns a StreamWriter wrapping the standard output stream.
97 /// By setting your own Streamwriter one can capture the output of the commands.
99 public static StreamWriter DefaultOutputStream
105 _output
= new StreamWriter(Console
.OpenStandardOutput());
106 Console
.SetOut(_output
);
115 private static StreamWriter _output
;
118 /// Get or set the default git repository for all commands. A command can override this by
119 /// setting it's own Repository property.
121 /// Note: Init and Clone do not respect Repository since they create a Repository as a result of Execute.
123 public static Repository DefaultRepository { get; set; }
126 /// Get or set the default git directory for all commands. A command can override this, however,
127 /// by setting its own GitDirectory property.
129 public static string DefaultGitDirectory { get; set; }
137 /// Clone a repository and checkout the working directory.
139 /// <param name="fromUrl"></param>
140 /// <param name="toPath"></param>
141 /// <returns></returns>
142 public static Repository
Clone(string fromUrl
, string toPath
)
145 return Clone(fromUrl
, toPath
, bare
);
149 /// Clone a repository and checkout the working directory only if bare == false
151 /// <param name="fromUrl"></param>
152 /// <param name="toPath"></param>
153 /// <param name="bare"></param>
154 /// <returns></returns>
155 public static Repository
Clone(string fromUrl
, string toPath
, bool bare
)
157 CloneCommand cmd
= new CloneCommand()
166 public static Repository
Clone(CloneCommand command
)
169 return command
.Repository
;
178 public static void Init(string path
)
180 Repository
.Init(path
);
183 public static void Init(string path
, bool bare
)
185 Repository
.Init(path
, bare
);
188 public static void Init(InitCommand command
)
199 public static MergeResult
Merge(MergeOptions options
)
201 return MergeCommand
.Execute(options
);
209 public static StatusResults
Status(StatusCommand command
)
211 //Populate the command with the status results
213 return command
.Results
;