Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BranchCollectionExtensions.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07061430024fd1d78c9892042431bfbf88250063 (plain)
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
using LibGit2Sharp.Core;

namespace LibGit2Sharp
{
    /// <summary>
    /// Provides helper overloads to a <see cref="BranchCollection"/>.
    /// </summary>
    public static class BranchCollectionExtensions
    {
        /// <summary>
        /// Create a new local branch with the specified name
        /// </summary>
        /// <param name="name">The name of the branch.</param>
        /// <param name="committish">Revparse spec for the target commit.</param>
        /// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
        /// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
        /// <returns>A new <see cref="Branch"/>.</returns>
        public static Branch Add(this BranchCollection branches, string name, string committish, bool allowOverwrite = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            Ensure.ArgumentNotNullOrEmptyString(committish, "committish");

            var commit = branches.repo.LookupCommit(committish);

            var createdFrom = committish != "HEAD"
                                  ? committish
                                  : branches.repo.Info.IsHeadDetached
                                        ? commit.Sha
                                        : branches.repo.Head.Name;

            return branches.Add(name, commit, allowOverwrite, "branch: Created from " + createdFrom);
        }

        /// <summary>
        /// Deletes the branch with the specified name.
        /// </summary>
        /// <param name="name">The name of the branch to delete.</param>
        /// <param name="isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
        /// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
        public static void Remove(this BranchCollection branches, string name, bool isRemote = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");

            string branchName = isRemote ? Reference.RemoteTrackingBranchPrefix + name : name;

            Branch branch = branches[branchName];

            if (branch == null)
            {
                return;
            }

            branches.Remove(branch);
        }

        /// <summary>
        /// Renames an existing local branch with a new name.
        /// </summary>
        /// <param name="currentName">The current branch name.</param>
        /// <param name="newName">The new name the existing branch should bear.</param>
        /// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
        /// <param name="branches">The <see cref="BranchCollection"/> being worked with.</param>
        /// <returns>A new <see cref="Branch"/>.</returns>
        public static Branch Move(this BranchCollection branches, string currentName, string newName, bool allowOverwrite = false)
        {
            Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
            Ensure.ArgumentNotNullOrEmptyString(newName, "newName");

            Branch branch = branches[currentName];

            if (branch == null)
            {
                throw new LibGit2SharpException("No branch named '{0}' exists in the repository.");
            }

            return branches.Move(branch, newName, allowOverwrite);
        }
    }
}