@@ -43,6 +43,16 @@ public bool IsBareRepository
4343 get => _repo . IsBare ;
4444 }
4545
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+
4656 public bool IsRecurseSubmoduleVisible
4757 {
4858 get => _repo . Submodules . Count > 0 ;
@@ -88,18 +98,24 @@ public CreateBranch(Repository repo, Models.Tag tag)
8898
8999 public static ValidationResult ValidateBranchName ( string name , ValidationContext ctx )
90100 {
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+ }
94112
95- var fixedName = creator . FixName ( name ) ;
96- foreach ( var b in creator . _repo . Branches )
113+ return ValidationResult . Success ;
114+ }
115+ else
97116 {
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!" ) ;
100118 }
101-
102- return ValidationResult . Success ;
103119 }
104120
105121 public override Task < bool > Sure ( )
@@ -119,7 +135,7 @@ public override Task<bool> Sure()
119135 var needPopStash = false ;
120136 if ( DiscardLocalChanges )
121137 {
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 ) ;
123139 }
124140 else
125141 {
@@ -137,7 +153,7 @@ public override Task<bool> Sure()
137153 needPopStash = true ;
138154 }
139155
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 ) ;
141157 }
142158
143159 if ( succ )
@@ -155,7 +171,7 @@ public override Task<bool> Sure()
155171 }
156172 else
157173 {
158- succ = Commands . Branch . Create ( _repo . FullPath , fixedName , _baseOnRevision , log ) ;
174+ succ = Commands . Branch . Create ( _repo . FullPath , fixedName , _baseOnRevision , _allowOverwrite , log ) ;
159175 }
160176
161177 log . Complete ( ) ;
@@ -201,5 +217,6 @@ private string FixName(string name)
201217 private readonly Repository _repo = null ;
202218 private string _name = null ;
203219 private readonly string _baseOnRevision = null ;
220+ private bool _allowOverwrite = false ;
204221 }
205222}
0 commit comments