2 * Copyright (C) 2010, Dominique van de Vorle <dvdvorle@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
;
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; }
64 /// Quiet, suppress feedback messages.
66 public bool Quiet { get; set; }
71 /// When switching branches, proceed even if the index or the
72 /// working tree differs from HEAD. This is used to throw away
75 /// When checking out paths from the index, do not fail upon unmerged
76 /// entries; instead, unmerged entries are ignored.
79 public bool Force { get; set; }
84 /// When checking out paths from the index, check out stage #2
85 /// ('ours') or #3 ('theirs') for unmerged paths.
88 public bool Ours { get; set; }
93 /// When checking out paths from the index, check out stage #2
94 /// ('ours') or #3 ('theirs') for unmerged paths.
97 public bool Theirs { get; set; }
102 /// Create a new branch named <new_branch> and start it at
103 /// <start_point>; see linkgit:git-branch[1] for details.
106 public string BranchCreate { get; set; }
111 /// When creating a new branch, set up "upstream" configuration. See
112 /// "--track" in linkgit:git-branch[1] for details.
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.
125 public bool Track { get; set; }
130 /// Do not set up "upstream" configuration, even if the
131 /// branch.autosetupmerge configuration variable is true.
134 public bool NoTrack { get; set; }
139 /// Create the new branch's reflog; see linkgit:git-branch[1] for
143 public bool RefLog { get; set; }
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.
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).
162 /// When checking out paths from the index, this option lets you recreate
163 /// the conflicted merge in the specified paths.
166 public bool Merge { get; set; }
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).
178 public string Conflict { get; set; }
183 /// Interactively select hunks in the difference between the
184 /// <tree-ish> (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 <tree-ish> was specified, the index).
188 /// This means that you can use `git checkout -p` to selectively discard
189 /// edits from your current working tree.
192 public bool Patch { get; set; }
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");
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]);
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
);
244 results
.FileNotFoundList
.Add(path
);
249 #region Checkout Results
251 public class CheckoutResults
253 public List
<string> FileNotFoundList
= new List
<string>();