@@ -27,19 +27,7 @@ public BranchCollection(Repository repo)
27
27
/// </summary>
28
28
public Branch this [ string name ]
29
29
{
30
- get
31
- {
32
- var tokens = name . Split ( '/' ) ;
33
- if ( tokens . Length == 1 )
34
- {
35
- return Branch . CreateBranchFromReference ( repo . Refs [ string . Format ( CultureInfo . InvariantCulture , "refs/heads/{0}" , name ) ] , repo ) ;
36
- }
37
- if ( tokens . Length == 2 )
38
- {
39
- return Branch . CreateBranchFromReference ( repo . Refs [ string . Format ( CultureInfo . InvariantCulture , "refs/{0}" , name ) ] , repo ) ;
40
- }
41
- 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 ) ) ;
42
- }
30
+ get { return Branch . CreateBranchFromReference ( repo . Refs [ ParseName ( name ) ] , repo ) ; }
43
31
}
44
32
45
33
#region IEnumerable<Branch> Members
@@ -59,10 +47,70 @@ IEnumerator IEnumerable.GetEnumerator()
59
47
60
48
#endregion
61
49
50
+ /// <summary>
51
+ /// Create a new local branch with the specified name
52
+ /// </summary>
53
+ /// <param name = "name">The name of the branch.</param>
54
+ /// <param name = "target">The target sha, ref or branch name.</param>
55
+ /// <returns></returns>
56
+ public Branch Create ( string name , string target )
57
+ {
58
+ Ensure . ArgumentNotNullOrEmptyString ( target , "target" ) ;
59
+
60
+ GitOid oid ;
61
+ if ( NativeMethods . git_oid_mkstr ( out oid , target ) == ( int ) GitErrorCode . GIT_SUCCESS )
62
+ {
63
+ return Create ( name , new ObjectId ( oid ) ) ;
64
+ }
65
+
66
+ var reference = repo . Refs . Create ( EnsureValidBranchName ( name ) , ParseName ( target ) ) ;
67
+ return Branch . CreateBranchFromReference ( reference , repo ) ;
68
+ }
69
+
70
+ /// <summary>
71
+ /// Create a new local branch with the specified name.
72
+ /// </summary>
73
+ /// <param name = "name">The name of the branch.</param>
74
+ /// <param name = "target">The target.</param>
75
+ /// <returns></returns>
76
+ public Branch Create ( string name , ObjectId target )
77
+ {
78
+ Ensure . ArgumentNotNull ( target , "target" ) ;
79
+
80
+ var reference = repo . Refs . Create ( EnsureValidBranchName ( name ) , target ) ;
81
+ return Branch . CreateBranchFromReference ( reference , repo ) ;
82
+ }
83
+
84
+ private static string EnsureValidBranchName ( string name )
85
+ {
86
+ Ensure . ArgumentNotNullOrEmptyString ( name , "name" ) ;
87
+
88
+ if ( name . Contains ( "/" ) )
89
+ {
90
+ throw new ArgumentException ( "Branch names cannot contain the character '/'." ) ;
91
+ }
92
+
93
+ return string . Format ( CultureInfo . InvariantCulture , "refs/heads/{0}" , name ) ;
94
+ }
95
+
62
96
private static bool IsABranch ( Reference reference )
63
97
{
64
- return reference . Type == GitReferenceType . Oid
65
- && ! reference . Name . StartsWith ( "refs/tags/" ) ;
98
+ return /*reference.Type == GitReferenceType.Oid
99
+ &&*/ ! reference . Name . StartsWith ( "refs/tags/" ) ;
100
+ }
101
+
102
+ private static string ParseName ( string name )
103
+ {
104
+ var tokens = name . Split ( '/' ) ;
105
+ if ( tokens . Length == 1 )
106
+ {
107
+ return string . Format ( CultureInfo . InvariantCulture , "refs/heads/{0}" , name ) ;
108
+ }
109
+ if ( tokens . Length == 2 )
110
+ {
111
+ return string . Format ( CultureInfo . InvariantCulture , "refs/{0}" , name ) ;
112
+ }
113
+ 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 ) ) ;
66
114
}
67
115
}
68
116
}
0 commit comments