@@ -43,6 +43,16 @@ public bool IsBareRepository
43
43
get => _repo . IsBare ;
44
44
}
45
45
46
+ public bool AllowOverwrite
47
+ {
48
+ get => _allowOverwrite ;
49
+ set
50
+ {
51
+ if ( SetProperty ( ref _allowOverwrite , value ) )
52
+ ValidateProperty ( _name , nameof ( Name ) ) ;
53
+ }
54
+ }
55
+
46
56
public bool IsRecurseSubmoduleVisible
47
57
{
48
58
get => _repo . Submodules . Count > 0 ;
@@ -88,18 +98,24 @@ public CreateBranch(Repository repo, Models.Tag tag)
88
98
89
99
public static ValidationResult ValidateBranchName ( string name , ValidationContext ctx )
90
100
{
91
- var creator = ctx . ObjectInstance as CreateBranch ;
92
- if ( creator == null )
93
- return new ValidationResult ( "Missing runtime context to create branch!" ) ;
101
+ if ( ctx . ObjectInstance is CreateBranch creator )
102
+ {
103
+ if ( ! creator . _allowOverwrite )
104
+ {
105
+ var fixedName = creator . FixName ( name ) ;
106
+ foreach ( var b in creator . _repo . Branches )
107
+ {
108
+ if ( b . FriendlyName == fixedName )
109
+ return new ValidationResult ( "A branch with same name already exists!" ) ;
110
+ }
111
+ }
94
112
95
- var fixedName = creator . FixName ( name ) ;
96
- foreach ( var b in creator . _repo . Branches )
113
+ return ValidationResult . Success ;
114
+ }
115
+ else
97
116
{
98
- if ( b . FriendlyName == fixedName )
99
- return new ValidationResult ( "A branch with same name already exists!" ) ;
117
+ return new ValidationResult ( "Missing runtime context to create branch!" ) ;
100
118
}
101
-
102
- return ValidationResult . Success ;
103
119
}
104
120
105
121
public override Task < bool > Sure ( )
@@ -119,7 +135,7 @@ public override Task<bool> Sure()
119
135
var needPopStash = false ;
120
136
if ( DiscardLocalChanges )
121
137
{
122
- succ = new Commands . Checkout ( _repo . FullPath ) . Use ( log ) . Branch ( fixedName , _baseOnRevision , true ) ;
138
+ succ = new Commands . Checkout ( _repo . FullPath ) . Use ( log ) . Branch ( fixedName , _baseOnRevision , true , _allowOverwrite ) ;
123
139
}
124
140
else
125
141
{
@@ -137,7 +153,7 @@ public override Task<bool> Sure()
137
153
needPopStash = true ;
138
154
}
139
155
140
- succ = new Commands . Checkout ( _repo . FullPath ) . Use ( log ) . Branch ( fixedName , _baseOnRevision , false ) ;
156
+ succ = new Commands . Checkout ( _repo . FullPath ) . Use ( log ) . Branch ( fixedName , _baseOnRevision , false , _allowOverwrite ) ;
141
157
}
142
158
143
159
if ( succ )
@@ -155,7 +171,7 @@ public override Task<bool> Sure()
155
171
}
156
172
else
157
173
{
158
- succ = Commands . Branch . Create ( _repo . FullPath , fixedName , _baseOnRevision , log ) ;
174
+ succ = Commands . Branch . Create ( _repo . FullPath , fixedName , _baseOnRevision , _allowOverwrite , log ) ;
159
175
}
160
176
161
177
log . Complete ( ) ;
@@ -201,5 +217,6 @@ private string FixName(string name)
201
217
private readonly Repository _repo = null ;
202
218
private string _name = null ;
203
219
private readonly string _baseOnRevision = null ;
220
+ private bool _allowOverwrite = false ;
204
221
}
205
222
}
0 commit comments