forked from libgit2/libgit2sharp
/
BranchCollection.cs
141 lines (119 loc) · 5.04 KB
/
BranchCollection.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// The collection of Branches in a <see cref = "Repository" />
/// </summary>
public class BranchCollection : IEnumerable<Branch>
{
private readonly Repository repo;
/// <summary>
/// Initializes a new instance of the <see cref = "BranchCollection" /> class.
/// </summary>
/// <param name = "repo">The repo.</param>
internal BranchCollection(Repository repo)
{
this.repo = repo;
}
/// <summary>
/// Gets the <see cref = "LibGit2Sharp.Branch" /> with the specified name.
/// </summary>
public Branch this[string name]
{
get { return repo.Refs.Resolve<Branch>(NormalizeToCanonicalName(name)); }
}
#region IEnumerable<Branch> Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public IEnumerator<Branch> GetEnumerator()
{
return Libgit2UnsafeHelper.ListAllReferenceNames(repo.Handle, GitReferenceType.ListAll)
.Where(LooksLikeABranchName)
.Select(n => this[n])
.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
/// <summary>
/// Checkout the branch with the specified by name.
/// </summary>
/// <param name = "name">The name of the branch to checkout.</param>
/// <returns></returns>
public Branch Checkout(string name)
{
// TODO: Allow checkout of an arbitrary commit, thus putting HEAD in detached state.
// TODO: This does not yet checkout (write) the working directory
Ensure.ArgumentNotNullOrEmptyString(name, "name");
repo.Refs.UpdateTarget("HEAD", this[name].CanonicalName);
return this[name];
}
/// <summary>
/// Create a new local branch with the specified name
/// </summary>
/// <param name = "name">The name of the branch.</param>
/// <param name = "target">The target sha or branch name.</param>
/// <returns></returns>
public Branch Create(string name, string target)
{
ObjectId id;
if (!ObjectId.TryParse(target, out id))
{
var reference = repo.Refs[NormalizeToCanonicalName(target)].ResolveToDirectReference();
target = reference.TargetIdentifier;
}
repo.Refs.Create(NormalizeToCanonicalName(name), target);
return this[name];
}
/// <summary>
/// Deletes the branch with the specified name.
/// </summary>
/// <param name = "name">The name of the branch to delete.</param>
public void Delete(string name)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
repo.Refs.Delete(NormalizeToCanonicalName(name)); //TODO: To be replaced by native libgit2 git_branch_delete() when available.
}
///<summary>
/// Rename an existing branch with a new name.
///</summary>
///<param name="currentName">The current branch name.</param>
///<param name="newName">The new name of the existing branch should bear.</param>
///<param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
///<returns></returns>
public Branch Move(string currentName, string newName, bool allowOverwrite = false)
{
Ensure.ArgumentNotNullOrEmptyString(currentName, "name");
Ensure.ArgumentNotNullOrEmptyString(newName, "name");
Reference reference = repo.Refs.Move(NormalizeToCanonicalName(currentName), NormalizeToCanonicalName(newName),
allowOverwrite);
return this[reference.CanonicalName];
}
private static bool LooksLikeABranchName(string referenceName)
{
return referenceName.StartsWith("refs/heads/", StringComparison.Ordinal) || referenceName.StartsWith("refs/remotes/", StringComparison.Ordinal);
}
private static string NormalizeToCanonicalName(string name)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
if (LooksLikeABranchName(name))
{
return name;
}
return string.Format(CultureInfo.InvariantCulture, "refs/heads/{0}", name);
}
}
}