1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
using LibGit2Sharp.Core.Handles;
using LibGit2Sharp.Handlers;
namespace LibGit2Sharp
{
/// <summary>
/// A Repository is the primary interface into a git repository
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Repository : IRepository
{
private readonly bool isBare;
private readonly BranchCollection branches;
private readonly CommitLog commits;
private readonly Lazy<Configuration> config;
private readonly RepositorySafeHandle handle;
private readonly Index index;
private readonly ConflictCollection conflicts;
private readonly ReferenceCollection refs;
private readonly TagCollection tags;
private readonly StashCollection stashes;
private readonly Lazy<RepositoryInformation> info;
private readonly Diff diff;
private readonly NoteCollection notes;
private readonly Lazy<ObjectDatabase> odb;
private readonly Lazy<Network> network;
private readonly Stack<IDisposable> toCleanup = new Stack<IDisposable>();
private readonly Ignore ignore;
private readonly SubmoduleCollection submodules;
private static readonly Lazy<string> versionRetriever = new Lazy<string>(RetrieveVersion);
private readonly Lazy<PathCase> pathCase;
/// <summary>
/// Initializes a new instance of the <see cref = "Repository" /> class, providing ooptional behavioral overrides through <paramref name="options"/> parameter.
/// <para>For a standard repository, <paramref name = "path" /> should either point to the ".git" folder or to the working directory. For a bare repository, <paramref name = "path" /> should directly point to the repository folder.</para>
/// </summary>
/// <param name = "path">
/// The path to the git repository to open, can be either the path to the git directory (for non-bare repositories this
/// would be the ".git" folder inside the working directory) or the path to the working directory.
/// </param>
/// <param name="options">
/// Overrides to the way a repository is opened.
/// </param>
public Repository(string path, RepositoryOptions options = null)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");
try
{
handle = Proxy.git_repository_open(path);
RegisterForCleanup(handle);
isBare = Proxy.git_repository_is_bare(handle);
Func<Index> indexBuilder = () => new Index(this);
string configurationGlobalFilePath = null;
string configurationXDGFilePath = null;
string configurationSystemFilePath = null;
if (options != null)
{
bool isWorkDirNull = string.IsNullOrEmpty(options.WorkingDirectoryPath);
bool isIndexNull = string.IsNullOrEmpty(options.IndexPath);
if (isBare && (isWorkDirNull ^ isIndexNull))
{
throw new ArgumentException(
"When overriding the opening of a bare repository, both RepositoryOptions.WorkingDirectoryPath an RepositoryOptions.IndexPath have to be provided.");
}
isBare = false;
if (!isIndexNull)
{
indexBuilder = () => new Index(this, options.IndexPath);
}
if (!isWorkDirNull)
{
Proxy.git_repository_set_workdir(handle, options.WorkingDirectoryPath);
}
configurationGlobalFilePath = options.GlobalConfigurationLocation;
configurationXDGFilePath = options.XdgConfigurationLocation;
configurationSystemFilePath = options.SystemConfigurationLocation;
}
if (!isBare)
{
index = indexBuilder();
conflicts = new ConflictCollection(this);
}
commits = new CommitLog(this);
refs = new ReferenceCollection(this);
branches = new BranchCollection(this);
tags = new TagCollection(this);
stashes = new StashCollection(this);
info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
config =
new Lazy<Configuration>(
() =>
RegisterForCleanup(new Configuration(this, configurationGlobalFilePath, configurationXDGFilePath,
configurationSystemFilePath)));
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
diff = new Diff(this);
notes = new NoteCollection(this);
ignore = new Ignore(this);
network = new Lazy<Network>(() => new Network(this));
pathCase = new Lazy<PathCase>(() => new PathCase(this));
submodules = new SubmoduleCollection(this);
EagerlyLoadTheConfigIfAnyPathHaveBeenPassed(options);
}
catch
{
CleanupDisposableDependencies();
throw;
}
}
/// <summary>
/// Check if parameter <paramref name="path"/> leads to a valid git repository.
/// </summary>
/// <param name = "path">
/// The path to the git repository to check, can be either the path to the git directory (for non-bare repositories this
/// would be the ".git" folder inside the working directory) or the path to the working directory.
/// </param>
/// <returns>True if a repository can be resolved through this path; false otherwise</returns>
static public bool IsValid(string path)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");
try
{
Proxy.git_repository_open_ext(path, RepositoryOpenFlags.NoSearch, null);
}
catch (RepositoryNotFoundException)
{
return false;
}
return true;
}
private void EagerlyLoadTheConfigIfAnyPathHaveBeenPassed(RepositoryOptions options)
{
if (options == null)
{
return;
}
if (options.GlobalConfigurationLocation == null &&
options.XdgConfigurationLocation == null &&
options.SystemConfigurationLocation == null)
{
return;
}
// Dirty hack to force the eager load of the configuration
// without Resharper pestering about useless code
if (!Config.HasConfig(ConfigurationLevel.Local))
{
throw new InvalidOperationException("Unexpected state.");
}
}
internal RepositorySafeHandle Handle
{
get { return handle; }
}
/// <summary>
/// Shortcut to return the branch pointed to by HEAD
/// </summary>
/// <returns></returns>
public Branch Head
{
get
{
Reference reference = Refs.Head;
if (reference == null)
{
throw new LibGit2SharpException("Corrupt repository. The 'HEAD' reference is missing.");
}
if (reference is SymbolicReference)
{
return new Branch(this, reference);
}
return new DetachedHead(this, reference);
}
}
/// <summary>
/// Provides access to the configuration settings for this repository.
/// </summary>
public Configuration Config
{
get { return config.Value; }
}
/// <summary>
/// Gets the index.
/// </summary>
public Index Index
{
get
{
if (isBare)
{
throw new BareRepositoryException("Index is not available in a bare repository.");
}
return index;
}
}
/// <summary>
/// Gets the conflicts that exist.
/// </summary>
public ConflictCollection Conflicts
{
get
{
if (isBare)
{
throw new BareRepositoryException("Conflicts are not available in a bare repository.");
}
return conflicts;
}
}
/// <summary>
/// Manipulate the currently ignored files.
/// </summary>
public Ignore Ignore
{
get
{
return ignore;
}
}
/// <summary>
/// Provides access to network functionality for a repository.
/// </summary>
public Network Network
{
get
{
return network.Value;
}
}
/// <summary>
/// Gets the database.
/// </summary>
public ObjectDatabase ObjectDatabase
{
get
{
return odb.Value;
}
}
/// <summary>
/// Lookup and enumerate references in the repository.
/// </summary>
public ReferenceCollection Refs
{
get { return refs; }
}
/// <summary>
/// Lookup and enumerate commits in the repository.
/// Iterating this collection directly starts walking from the HEAD.
/// </summary>
public IQueryableCommitLog Commits
{
get { return commits; }
}
/// <summary>
/// Lookup and enumerate branches in the repository.
/// </summary>
public BranchCollection Branches
{
get { return branches; }
}
/// <summary>
/// Lookup and enumerate tags in the repository.
/// </summary>
public TagCollection Tags
{
get { return tags; }
}
///<summary>
/// Lookup and enumerate stashes in the repository.
///</summary>
public StashCollection Stashes
{
get { return stashes; }
}
/// <summary>
/// Provides high level information about this repository.
/// </summary>
public RepositoryInformation Info
{
get { return info.Value; }
}
/// <summary>
/// Provides access to diffing functionalities to show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.
/// </summary>
public Diff Diff
{
get { return diff; }
}
/// <summary>
/// Lookup notes in the repository.
/// </summary>
public NoteCollection Notes
{
get { return notes; }
}
/// <summary>
/// Submodules in the repository.
/// </summary>
public SubmoduleCollection Submodules
{
get { return submodules; }
}
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
protected virtual void Dispose(bool disposing)
{
CleanupDisposableDependencies();
}
#endregion
/// <summary>
/// Initialize a repository at the specified <paramref name = "path" />.
/// </summary>
/// <param name = "path">The path to the working folder when initializing a standard ".git" repository. Otherwise, when initializing a bare repository, the path to the expected location of this later.</param>
/// <param name = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param>
/// <param name="options">Overrides to the way a repository is opened.</param>
/// <returns> a new instance of the <see cref = "Repository" /> class. The client code is responsible for calling <see cref = "Dispose()" /> on this instance.</returns>
public static Repository Init(string path, bool isBare = false, RepositoryOptions options = null)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");
using (RepositorySafeHandle repo = Proxy.git_repository_init(path, isBare))
{
FilePath repoPath = Proxy.git_repository_path(repo);
return new Repository(repoPath.Native, options);
}
}
/// <summary>
/// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
/// </summary>
/// <param name = "id">The id to lookup.</param>
/// <param name = "type">The kind of GitObject being looked up</param>
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
public GitObject Lookup(ObjectId id, GitObjectType type = GitObjectType.Any)
{
return LookupInternal(id, type, null);
}
internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath knownPath)
{
Ensure.ArgumentNotNull(id, "id");
GitObjectSafeHandle obj = null;
try
{
obj = Proxy.git_object_lookup(handle, id, type);
if (obj == null)
{
return null;
}
return GitObject.BuildFrom(this, id, Proxy.git_object_type(obj), knownPath);
}
finally
{
obj.SafeDispose();
}
}
/// <summary>
/// Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
/// </summary>
/// <param name = "objectish">A revparse spec for the object to lookup.</param>
/// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
public GitObject Lookup(string objectish, GitObjectType type = GitObjectType.Any)
{
return Lookup(objectish, type, LookUpOptions.None);
}
private static string PathFromRevparseSpec(string spec)
{
if (spec.StartsWith(":/", StringComparison.Ordinal))
{
return null;
}
if (Regex.IsMatch(spec, @"^:.*:"))
{
return null;
}
var m = Regex.Match(spec, @"[^@^ ]*:(.*)");
return (m.Groups.Count > 1) ? m.Groups[1].Value : null;
}
internal GitObject Lookup(string objectish, GitObjectType type, LookUpOptions lookUpOptions)
{
Ensure.ArgumentNotNullOrEmptyString(objectish, "commitOrBranchSpec");
GitObject obj;
using (GitObjectSafeHandle sh = Proxy.git_revparse_single(handle, objectish))
{
if (sh == null)
{
if (lookUpOptions.HasFlag(LookUpOptions.ThrowWhenNoGitObjectHasBeenFound))
{
Ensure.GitObjectIsNotNull(null, objectish);
}
return null;
}
GitObjectType objType = Proxy.git_object_type(sh);
if (type != GitObjectType.Any && objType != type)
{
return null;
}
obj = GitObject.BuildFrom(this, Proxy.git_object_id(sh), objType, PathFromRevparseSpec(objectish));
}
if (lookUpOptions.HasFlag(LookUpOptions.DereferenceResultToCommit))
{
return obj.DereferenceToCommit(
lookUpOptions.HasFlag(LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit));
}
return obj;
}
/// <summary>
/// Lookup a commit by its SHA or name, or throw if a commit is not found.
/// </summary>
/// <param name="committish">A revparse spec for the commit.</param>
/// <returns>The commit.</returns>
internal Commit LookupCommit(string committish)
{
return (Commit)Lookup(committish, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound | LookUpOptions.DereferenceResultToCommit | LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit);
}
/// <summary>
/// Probe for a git repository.
/// <para>The lookup start from <paramref name = "startingPath" /> and walk upward parent directories if nothing has been found.</para>
/// </summary>
/// <param name = "startingPath">The base path where the lookup starts.</param>
/// <returns>The path to the git repository.</returns>
public static string Discover(string startingPath)
{
FilePath discoveredPath = Proxy.git_repository_discover(startingPath);
if (discoveredPath == null)
{
return null;
}
return discoveredPath.Native;
}
/// <summary>
/// Clone with specified options.
/// </summary>
/// <param name="sourceUrl">URI for the remote repository</param>
/// <param name="workdirPath">Local path to clone into</param>
/// <param name="bare">True will result in a bare clone, false a full clone.</param>
/// <param name="checkout">If true, the origin's HEAD will be checked out. This only applies
/// to non-bare repositories.</param>
/// <param name="onTransferProgress">Handler for network transfer and indexing progress information</param>
/// <param name="onCheckoutProgress">Handler for checkout progress information</param>
/// <param name="options">Overrides to the way a repository is opened.</param>
/// <param name="credentials">Credentials to use for user/pass authentication</param>
/// <returns></returns>
public static Repository Clone(string sourceUrl, string workdirPath,
bool bare = false,
bool checkout = true,
TransferProgressHandler onTransferProgress = null,
CheckoutProgressHandler onCheckoutProgress = null,
RepositoryOptions options = null,
Credentials credentials = null)
{
var cloneOpts = new GitCloneOptions
{
Bare = bare ? 1 : 0,
TransferProgressCallback = TransferCallbacks.GenerateCallback(onTransferProgress),
CheckoutOpts =
{
version = 1,
progress_cb =
CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress),
checkout_strategy = checkout
? CheckoutStrategy.GIT_CHECKOUT_SAFE_CREATE
: CheckoutStrategy.GIT_CHECKOUT_NONE
},
};
if (credentials != null)
{
cloneOpts.CredAcquireCallback =
(out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
}
using(Proxy.git_clone(sourceUrl, workdirPath, cloneOpts)) {}
// To be safe, make sure the credential callback is kept until
// alive until at least this point.
GC.KeepAlive(cloneOpts.CredAcquireCallback);
return new Repository(workdirPath, options);
}
/// <summary>
/// Checkout the specified <see cref = "Branch" />, reference or SHA.
/// </summary>
/// <param name = "committishOrBranchSpec">A revparse spec for the commit or branch to checkout.</param>
/// <param name="checkoutOptions"><see cref = "CheckoutOptions" /> controlling checkout behavior.</param>
/// <param name="onCheckoutProgress"><see cref = "CheckoutProgressHandler" /> that checkout progress is reported through.</param>
/// <returns>The <see cref = "Branch" /> that was checked out.</returns>
public Branch Checkout(string committishOrBranchSpec, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress)
{
Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, "committishOrBranchSpec");
Branch branch = TryResolveBranch(committishOrBranchSpec);
if (branch != null)
{
return Checkout(branch, checkoutOptions, onCheckoutProgress);
}
var previousHeadName = Info.IsHeadDetached ? Head.Tip.Sha : Head.Name;
Commit commit = LookupCommit(committishOrBranchSpec);
CheckoutTree(commit.Tree, checkoutOptions, onCheckoutProgress);
// Update HEAD.
Refs.UpdateTarget("HEAD", commit.Id.Sha);
if (committishOrBranchSpec != "HEAD")
{
LogCheckout(previousHeadName, commit.Id, committishOrBranchSpec);
}
return Head;
}
private Branch TryResolveBranch(string committishOrBranchSpec)
{
if (committishOrBranchSpec == "HEAD")
{
return Head;
}
try
{
return Branches[committishOrBranchSpec];
}
catch (InvalidSpecificationException)
{
return null;
}
}
/// <summary>
/// Checkout the tip commit of the specified <see cref = "Branch" /> object. If this commit is the
/// current tip of the branch, will checkout the named branch. Otherwise, will checkout the tip commit
/// as a detached HEAD.
/// </summary>
/// <param name="branch">The <see cref = "Branch" /> to check out. </param>
/// <param name="checkoutOptions"><see cref = "CheckoutOptions" /> controlling checkout behavior.</param>
/// <param name="onCheckoutProgress"><see cref = "CheckoutProgressHandler" /> that checkout progress is reported through.</param>
/// <returns>The <see cref = "Branch" /> that was checked out.</returns>
public Branch Checkout(Branch branch, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress)
{
Ensure.ArgumentNotNull(branch, "branch");
// Make sure this is not an unborn branch.
if (branch.Tip == null)
{
throw new OrphanedHeadException(
string.Format(CultureInfo.InvariantCulture,
"The tip of branch '{0}' is null. There's nothing to checkout.", branch.Name));
}
var branchIsCurrentRepositoryHead = branch.IsCurrentRepositoryHead;
var previousHeadName = Info.IsHeadDetached ? Head.Tip.Sha : Head.Name;
CheckoutTree(branch.Tip.Tree, checkoutOptions, onCheckoutProgress);
// Update HEAD.
if (!branch.IsRemote && !(branch is DetachedHead) &&
string.Equals(Refs[branch.CanonicalName].TargetIdentifier, branch.Tip.Id.Sha,
StringComparison.OrdinalIgnoreCase))
{
Refs.UpdateTarget("HEAD", branch.CanonicalName);
}
else
{
Refs.UpdateTarget("HEAD", branch.Tip.Id.Sha);
}
if (!branchIsCurrentRepositoryHead)
{
LogCheckout(previousHeadName, branch);
}
return Head;
}
private void LogCheckout(string previousHeadName, Branch newHead)
{
LogCheckout(previousHeadName, newHead.Tip.Id, newHead.Name);
}
private void LogCheckout(string previousHeadName, ObjectId newHeadTip, string newHeadSpec)
{
// Compute reflog message
string reflogMessage = string.Format("checkout: moving from {0} to {1}", previousHeadName, newHeadSpec);
// Log checkout
Signature author = Config.BuildSignatureFromGlobalConfiguration(DateTimeOffset.Now, false);
Refs.Log(Refs.Head).Append(newHeadTip, author, reflogMessage);
}
/// <summary>
/// Internal implementation of Checkout that expects the ID of the checkout target
/// to already be in the form of a canonical branch name or a commit ID.
/// </summary>
/// <param name="tree">The <see cref="Tree"/> to checkout.</param>
/// <param name="checkoutOptions"><see cref = "CheckoutOptions" /> controlling checkout behavior.</param>
/// <param name="onCheckoutProgress"><see cref = "CheckoutProgressHandler" /> that checkout progress is reported through.</param>
private void CheckoutTree(Tree tree, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress)
{
GitCheckoutOpts options = new GitCheckoutOpts
{
version = 1,
checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_SAFE,
progress_cb = CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress)
};
if (checkoutOptions.HasFlag(CheckoutOptions.Force))
{
options.checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_FORCE;
}
Proxy.git_checkout_tree(this.Handle, tree.Id, ref options);
}
/// <summary>
/// Sets the current <see cref = "Head" /> to the specified commit and optionally resets the <see cref = "Index" /> and
/// the content of the working tree to match.
/// </summary>
/// <param name = "resetOptions">Flavor of reset operation to perform.</param>
/// <param name = "commit">The target commit object.</param>
public void Reset(ResetOptions resetOptions, Commit commit)
{
Ensure.ArgumentNotNull(commit, "commit");
Proxy.git_reset(handle, commit.Id, resetOptions);
}
/// <summary>
/// Replaces entries in the <see cref="Repository.Index"/> with entries from the specified commit.
/// </summary>
/// <param name = "commit">The target commit object.</param>
/// <param name = "paths">The list of paths (either files or directories) that should be considered.</param>
/// <param name = "explicitPathsOptions">
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
public void Reset(Commit commit, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
{
if (Info.IsBare)
{
throw new BareRepositoryException("Reset is not allowed in a bare repository");
}
Ensure.ArgumentNotNull(commit, "commit");
TreeChanges changes = Diff.Compare(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions);
Index.Reset(changes);
}
/// <summary>
/// Stores the content of the <see cref = "Repository.Index" /> as a new <see cref = "Commit" /> into the repository.
/// The tip of the <see cref = "Repository.Head"/> will be used as the parent of this new Commit.
/// Once the commit is created, the <see cref = "Repository.Head"/> will move forward to point at it.
/// </summary>
/// <param name = "message">The description of why a change was made to the repository.</param>
/// <param name = "author">The <see cref = "Signature" /> of who made the change.</param>
/// <param name = "committer">The <see cref = "Signature" /> of who added the change to the repository.</param>
/// <param name = "amendPreviousCommit">True to amend the current <see cref = "Commit"/> pointed at by <see cref = "Repository.Head"/>, false otherwise.</param>
/// <returns>The generated <see cref = "Commit" />.</returns>
public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false)
{
bool isHeadOrphaned = Info.IsHeadOrphaned;
if (amendPreviousCommit && isHeadOrphaned)
{
throw new OrphanedHeadException("Can not amend anything. The Head doesn't point at any commit.");
}
var treeId = Proxy.git_tree_create_fromindex(Index);
var tree = this.Lookup<Tree>(treeId);
var parents = RetrieveParentsOfTheCommitBeingCreated(amendPreviousCommit);
Commit result = ObjectDatabase.CreateCommit(message, author, committer, tree, parents, "HEAD");
Proxy.git_repository_merge_cleanup(handle);
// Insert reflog entry
LogCommit(result, amendPreviousCommit, isHeadOrphaned, parents.Count() > 1);
return result;
}
private void LogCommit(Commit commit, bool amendPreviousCommit, bool isHeadOrphaned, bool isMergeCommit)
{
// Compute reflog message
string reflogMessage = "commit";
if (isHeadOrphaned)
{
reflogMessage += " (initial)";
}
else if(amendPreviousCommit)
{
reflogMessage += " (amend)";
}
else if(isMergeCommit)
{
reflogMessage += " (merge)";
}
reflogMessage = string.Format("{0}: {1}", reflogMessage, commit.MessageShort);
var headRef = Refs.Head;
// in case HEAD targets a symbolic reference, log commit on the targeted direct reference
if(headRef is SymbolicReference)
{
Refs.Log(headRef.ResolveToDirectReference()).Append(commit.Id, commit.Committer, reflogMessage);
}
// Log commit on HEAD
Refs.Log(headRef).Append(commit.Id, commit.Committer, reflogMessage);
}
private IEnumerable<Commit> RetrieveParentsOfTheCommitBeingCreated(bool amendPreviousCommit)
{
if (amendPreviousCommit)
{
return Head.Tip.Parents;
}
if (Info.IsHeadOrphaned)
{
return Enumerable.Empty<Commit>();
}
var parents = new List<Commit> { Head.Tip };
if (Info.CurrentOperation == CurrentOperation.Merge)
{
parents.AddRange(MergeHeads.Select(mh => mh.Tip));
}
return parents;
}
/// <summary>
/// Clean the working tree by removing files that are not under version control.
/// </summary>
public virtual void RemoveUntrackedFiles()
{
var options = new GitCheckoutOpts
{
version = 1,
checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_REMOVE_UNTRACKED
| CheckoutStrategy.GIT_CHECKOUT_ALLOW_CONFLICTS,
};
Proxy.git_checkout_index(Handle, new NullGitObjectSafeHandle(), ref options);
}
private void CleanupDisposableDependencies()
{
while (toCleanup.Count > 0)
{
toCleanup.Pop().SafeDispose();
}
}
internal T RegisterForCleanup<T>(T disposable) where T : IDisposable
{
toCleanup.Push(disposable);
return disposable;
}
/// <summary>
/// Gets the current LibGit2Sharp version.
/// <para>
/// The format of the version number is as follows:
/// <para>Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64)</para>
/// </para>
/// </summary>
public static string Version
{
get { return versionRetriever.Value; }
}
private static string RetrieveVersion()
{
Assembly assembly = typeof(Repository).Assembly;
Version version = assembly.GetName().Version;
string libgit2Hash = ReadContentFromResource(assembly, "libgit2_hash.txt");
string libgit2sharpHash = ReadContentFromResource(assembly, "libgit2sharp_hash.txt");
return string.Format(
CultureInfo.InvariantCulture,
"{0}-{1}-{2} ({3})",
version.ToString(3),
libgit2sharpHash.Substring(0, 7),
libgit2Hash.Substring(0, 7),
NativeMethods.ProcessorArchitecture
);
}
private static string ReadContentFromResource(Assembly assembly, string partialResourceName)
{
string name = string.Format(CultureInfo.InvariantCulture, "LibGit2Sharp.{0}", partialResourceName);
using (var sr = new StreamReader(assembly.GetManifestResourceStream(name)))
{
return sr.ReadLine();
}
}
/// <summary>
/// Gets the references to the tips that are currently being merged.
/// </summary>
public virtual IEnumerable<MergeHead> MergeHeads
{
get
{
int i = 0;
return Proxy.git_repository_mergehead_foreach(Handle,
commitId => new MergeHead(this, commitId, i++));
}
}
internal StringComparer PathComparer
{
get { return pathCase.Value.Comparer; }
}
internal bool PathStartsWith(string path, string value)
{
return pathCase.Value.StartsWith(path, value);
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"{0} = \"{1}\"",
Info.IsBare ? "Gitdir" : "Workdir",
Info.IsBare ? Info.Path : Info.WorkingDirectory);
}
}
}
}
|