Update README.txt
[GitSharp.git] / GitSharp / Git.cs
blob5f3351e7068f11bd009d260da3107bf077facf07
1 /*
2 * Copyright (C) 2009, Henon <meinrad.recheis@gmail.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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.
38 using System;
39 using System.Collections.Generic;
40 using System.IO;
41 using System.Linq;
42 using System.Text;
43 using System.Reflection;
44 using GitSharp.Commands;
46 namespace GitSharp
48 /// <summary>
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.
50 /// </summary>
51 public static class Git
54 #region Version
57 /// <summary>
58 /// Returns the version of GitSharp.
59 /// </summary>
60 public static string Version
62 get
64 Assembly assembly = Assembly.Load("GitSharp");
66 Version version = assembly.GetName().Version;
67 if (version == null)
68 return null;
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(']');
90 #endregion
92 #region Defaults for git commands
95 /// <summary>
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.
98 /// </summary>
99 public static StreamWriter DefaultOutputStream
103 if (_output == null)
105 _output = new StreamWriter(Console.OpenStandardOutput());
106 Console.SetOut(_output);
108 return _output;
112 _output = value;
115 private static StreamWriter _output;
117 /// <summary>
118 /// Get or set the default git repository for all commands. A command can override this by
119 /// setting it's own Repository property.
120 ///
121 /// Note: Init and Clone do not respect Repository since they create a Repository as a result of Execute.
122 /// </summary>
123 public static Repository DefaultRepository { get; set; }
125 /// <summary>
126 /// Get or set the default git directory for all commands. A command can override this, however,
127 /// by setting its own GitDirectory property.
128 /// </summary>
129 public static string DefaultGitDirectory { get; set; }
132 #endregion
134 #region Clone
136 /// <summary>
137 /// Clone a repository and checkout the working directory.
138 /// </summary>
139 /// <param name="fromUrl"></param>
140 /// <param name="toPath"></param>
141 /// <returns></returns>
142 public static Repository Clone(string fromUrl, string toPath)
144 bool bare = false;
145 return Clone(fromUrl, toPath, bare);
148 /// <summary>
149 /// Clone a repository and checkout the working directory only if bare == false
150 /// </summary>
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()
159 Source = fromUrl,
160 Directory = toPath,
161 Bare = bare,
163 return Clone(cmd);
166 public static Repository Clone(CloneCommand command)
168 command.Execute();
169 return command.Repository;
173 #endregion
175 #region Init
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)
190 command.Execute();
194 #endregion
196 #region Merge
199 public static MergeResult Merge(MergeOptions options)
201 return MergeCommand.Execute(options);
205 #endregion
207 #region Status
209 public static StatusResults Status(StatusCommand command)
211 //Populate the command with the status results
212 command.Execute();
213 return command.Results;
216 #endregion