Update README.txt
[GitSharp.git] / GitSharp / Stubs / CheckoutCommand.cs
blob44d5584023aa775950b637a313634fc826851aee
1 /*
2 * Copyright (C) 2010, Dominique van de Vorle <dvdvorle@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;
44 namespace GitSharp.Commands
46 public class CheckoutCommand : AbstractCommand
48 private CheckoutResults results = new CheckoutResults();
50 public CheckoutCommand() {
53 public CheckoutResults Results
55 get { return results; }
56 private set { results = value; }
59 // note: the naming of command parameters is not following .NET conventions in favour of git command line parameter naming conventions.
61 #region Properties / Options
62 public List<string> Arguments { get; set; }
63 /// <summary>
64 /// Quiet, suppress feedback messages.
65 /// </summary>
66 public bool Quiet { get; set; }
68 /// <summary>
69 /// Not implemented
70 ///
71 /// When switching branches, proceed even if the index or the
72 /// working tree differs from HEAD. This is used to throw away
73 /// local changes.
74 /// +
75 /// When checking out paths from the index, do not fail upon unmerged
76 /// entries; instead, unmerged entries are ignored.
77 ///
78 /// </summary>
79 public bool Force { get; set; }
81 /// <summary>
82 /// Not implemented
83 ///
84 /// When checking out paths from the index, check out stage #2
85 /// ('ours') or #3 ('theirs') for unmerged paths.
86 ///
87 /// </summary>
88 public bool Ours { get; set; }
90 /// <summary>
91 /// Not implemented
92 ///
93 /// When checking out paths from the index, check out stage #2
94 /// ('ours') or #3 ('theirs') for unmerged paths.
95 ///
96 /// </summary>
97 public bool Theirs { get; set; }
99 /// <summary>
100 /// Not implemented
101 ///
102 /// Create a new branch named &lt;new_branch&gt; and start it at
103 /// &lt;start_point&gt;; see linkgit:git-branch[1] for details.
104 ///
105 /// </summary>
106 public string BranchCreate { get; set; }
108 /// <summary>
109 /// Not implemented
110 ///
111 /// When creating a new branch, set up "upstream" configuration. See
112 /// "--track" in linkgit:git-branch[1] for details.
113 /// +
114 /// If no '-b' option is given, the name of the new branch will be
115 /// derived from the remote branch. If "remotes/" or "refs/remotes/"
116 /// is prefixed it is stripped away, and then the part up to the
117 /// next slash (which would be the nickname of the remote) is removed.
118 /// This would tell us to use "hack" as the local branch when branching
119 /// off of "origin/hack" (or "remotes/origin/hack", or even
120 /// "refs/remotes/origin/hack"). If the given name has no slash, or the above
121 /// guessing results in an empty name, the guessing is aborted. You can
122 /// explicitly give a name with '-b' in such a case.
123 ///
124 /// </summary>
125 public bool Track { get; set; }
127 /// <summary>
128 /// Not implemented
129 ///
130 /// Do not set up "upstream" configuration, even if the
131 /// branch.autosetupmerge configuration variable is true.
132 ///
133 /// </summary>
134 public bool NoTrack { get; set; }
136 /// <summary>
137 /// Not implemented
138 ///
139 /// Create the new branch's reflog; see linkgit:git-branch[1] for
140 /// details.
141 ///
142 /// </summary>
143 public bool RefLog { get; set; }
145 /// <summary>
146 /// Not implemented
147 ///
148 /// When switching branches,
149 /// if you have local modifications to one or more files that
150 /// are different between the current branch and the branch to
151 /// which you are switching, the command refuses to switch
152 /// branches in order to preserve your modifications in context.
153 /// However, with this option, a three-way merge between the current
154 /// branch, your working tree contents, and the new branch
155 /// is done, and you will be on the new branch.
156 /// +
157 /// When a merge conflict happens, the index entries for conflicting
158 /// paths are left unmerged, and you need to resolve the conflicts
159 /// and mark the resolved paths with `git add` (or `git rm` if the merge
160 /// should result in deletion of the path).
161 /// +
162 /// When checking out paths from the index, this option lets you recreate
163 /// the conflicted merge in the specified paths.
164 ///
165 /// </summary>
166 public bool Merge { get; set; }
168 /// <summary>
169 /// Not implemented
170 ///
171 /// The same as --merge option above, but changes the way the
172 /// conflicting hunks are presented, overriding the
173 /// merge.conflictstyle configuration variable. Possible values are
174 /// "merge" (default) and "diff3" (in addition to what is shown by
175 /// "merge" style, shows the original contents).
176 ///
177 /// </summary>
178 public string Conflict { get; set; }
180 /// <summary>
181 /// Not implemented
182 ///
183 /// Interactively select hunks in the difference between the
184 /// &lt;tree-ish&gt; (or the index, if unspecified) and the working
185 /// tree. The chosen hunks are then applied in reverse to the
186 /// working tree (and if a &lt;tree-ish&gt; was specified, the index).
187 /// +
188 /// This means that you can use `git checkout -p` to selectively discard
189 /// edits from your current working tree.
190 ///
191 /// </summary>
192 public bool Patch { get; set; }
194 #endregion
196 public override void Execute()
199 if (Patch && (Track || BranchCreate.Length > 0 || RefLog || Merge || Force))
200 throw new ArgumentException("--patch is incompatible with all other options");
202 if (Force && Merge)
203 throw new ArgumentException("git checkout: -f and -m are incompatible");
205 if (Track && !(BranchCreate.Length > 0))
206 throw new ArgumentException("Missing branch name; try -b");
208 if (Arguments.Count == 0)
209 Arguments.Add("HEAD");
211 //Create a new branch and reassign the Repository to the new location before checkout.
212 if (BranchCreate.Length > 0)
214 Branch.Create(Repository, BranchCreate);
215 Arguments[0] = BranchCreate;
218 if (Arguments.Count == 1)
220 //Checkout the branch using the SHA1 or the name such as HEAD or master/HEAD
221 Branch b = new Branch(Repository, Arguments[0]);
222 if (b != null)
224 b.Checkout();
225 return;
229 //Todo: Add FileNameMatcher support. To be added when fnmatch is completed.
230 //For now, pattern matching is not allowed. Please specify the files only.
232 //Gain access to the Git index using the repository determined before command execution
233 Index index = new Index(Repository);
235 foreach (string arg in Arguments)
237 //Use full paths only to eliminate platform-based directory differences
238 string path = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), arg));
240 //Perform the validity tests outside of the index to handle the error messages
241 if ((new FileInfo(path).Exists) || (new DirectoryInfo(path).Exists))
242 index.Checkout(path);
243 else
244 results.FileNotFoundList.Add(path);
249 #region Checkout Results
251 public class CheckoutResults
253 public List<string> FileNotFoundList = new List<string>();
256 #endregion