diff options
author | Tim Clem <timothy.clem@gmail.com> | 2011-03-29 22:17:06 +0400 |
---|---|---|
committer | Tim Clem <timothy.clem@gmail.com> | 2011-03-30 20:31:56 +0400 |
commit | 2a8b0eaeac6815476d362f159dd5a0c797a24c37 (patch) | |
tree | 342222e9492105b2b7d3250826ffbb1c442d28c2 /LibGit2Sharp/BranchCollection.cs | |
parent | ca008e2d514dd02df4ab0c7a6f769b60e957f50c (diff) |
add concept of a branch to the API
This is really just a wrapper around refs, but you can refer to branches
in a much nicer manner ('master' or 'origin/master' for example)
Diffstat (limited to 'LibGit2Sharp/BranchCollection.cs')
-rw-r--r-- | LibGit2Sharp/BranchCollection.cs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/LibGit2Sharp/BranchCollection.cs b/LibGit2Sharp/BranchCollection.cs new file mode 100644 index 00000000..276a1b84 --- /dev/null +++ b/LibGit2Sharp/BranchCollection.cs @@ -0,0 +1,84 @@ +#region Copyright (c) 2011 LibGit2Sharp committers + +// The MIT License +// +// Copyright (c) 2011 LibGit2Sharp committers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#endregion + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; + +namespace LibGit2Sharp +{ + public class BranchCollection : IEnumerable<Branch> + { + private readonly Repository repo; + + public BranchCollection(Repository repo) + { + this.repo = repo; + } + + public Branch this[string name] + { + get + { + var tokens = name.Split('/'); + if (tokens.Length == 1) + { + return Branch.CreateBranchFromReference(repo.Refs[string.Format(CultureInfo.InvariantCulture, "refs/heads/{0}", name)]); + } + if (tokens.Length == 2) + { + return Branch.CreateBranchFromReference(repo.Refs[string.Format(CultureInfo.InvariantCulture, "refs/{0}", name)]); + } + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unable to parse branch name: {0}. Expecting local branches in the form <branchname> and remotes in the form <remote>/<branchname>.", name)); + } + } + + #region IEnumerable<Branch> Members + + public IEnumerator<Branch> GetEnumerator() + { + var list = repo.Refs + .Where(IsABranch) + .Select(Branch.CreateBranchFromReference); + return list.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + #endregion + + private static bool IsABranch(Reference reference) + { + return reference.Type == GitReferenceType.Oid + && !reference.Name.StartsWith("refs/tags/"); + } + } +}
\ No newline at end of file |