@@ -173,14 +173,49 @@ func CreateBranch(ctx *context.APIContext) {
173
173
return
174
174
}
175
175
176
- if len (opt .OldRefName ) == 0 {
177
- opt .OldRefName = ctx .Repo .Repository .DefaultBranch
178
- }
176
+ var oldCommit * git.Commit
177
+ var err error
179
178
180
- oldCommit , err := ctx .Repo .GitRepo .GetCommit (opt .OldRefName )
181
- if err != nil {
182
- ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
179
+ if len (opt .OldRefName ) > 0 && len (opt .OldBranchName ) > 0 {
180
+ ctx .Error (http .StatusInternalServerError , "" , "OldRefName And OldBranchName can not be both exist." )
183
181
return
182
+ } else if len (opt .OldRefName ) > 0 {
183
+ if ctx .Repo .GitRepo .IsBranchExist (opt .OldRefName ) {
184
+ oldCommit , err = ctx .Repo .GitRepo .GetBranchCommit (opt .OldRefName )
185
+ if err != nil {
186
+ ctx .Error (http .StatusInternalServerError , "GetBranchCommit" , err )
187
+ return
188
+ }
189
+ } else if ctx .Repo .GitRepo .IsTagExist (opt .OldRefName ) {
190
+ oldCommit , err = ctx .Repo .GitRepo .GetTagCommit (opt .OldRefName )
191
+ if err != nil {
192
+ ctx .Error (http .StatusInternalServerError , "GetTagCommit" , err )
193
+ return
194
+ }
195
+ } else if len (opt .OldRefName ) == git .SHAFullLength {
196
+ oldCommit , err = ctx .Repo .GitRepo .GetCommit (opt .OldRefName )
197
+ if err != nil {
198
+ ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
199
+ return
200
+ }
201
+ } else {
202
+ ctx .Error (http .StatusNotFound , "" , "OldRefName is not exits." )
203
+ return
204
+ }
205
+ } else if len (opt .OldBranchName ) > 0 {
206
+ if ctx .Repo .GitRepo .IsBranchExist (opt .OldBranchName ) {
207
+ oldCommit , err = ctx .Repo .GitRepo .GetBranchCommit (opt .OldBranchName )
208
+ if err != nil {
209
+ ctx .Error (http .StatusInternalServerError , "GetBranchCommit" , err )
210
+ return
211
+ }
212
+ }
213
+ } else {
214
+ oldCommit , err = ctx .Repo .GitRepo .GetBranchCommit (ctx .Repo .Repository .DefaultBranch )
215
+ if err != nil {
216
+ ctx .Error (http .StatusInternalServerError , "GetBranchCommit" , err )
217
+ return
218
+ }
184
219
}
185
220
186
221
err = repo_service .CreateNewBranchFromCommit (ctx , ctx .Doer , ctx .Repo .Repository , oldCommit .ID .String (), opt .BranchName )
0 commit comments