(ctx context.Context, srcObj, dstDir model.Obj)
| 245 | } |
| 246 | |
| 247 | func (d *Github) Move(ctx context.Context, srcObj, dstDir model.Obj) error { |
| 248 | if !d.isOnBranch { |
| 249 | return errors.New("cannot write to non-branch reference") |
| 250 | } |
| 251 | if strings.HasPrefix(dstDir.GetPath(), srcObj.GetPath()) { |
| 252 | return errors.New("cannot move parent dir to child") |
| 253 | } |
| 254 | d.commitMutex.Lock() |
| 255 | defer d.commitMutex.Unlock() |
| 256 | |
| 257 | var rootSha string |
| 258 | if strings.HasPrefix(dstDir.GetPath(), stdpath.Dir(srcObj.GetPath())) { // /aa/1 -> /aa/bb/ |
| 259 | dstOldSha, dstNewSha, ancestorOldSha, srcParentTree, err := d.copyWithoutRenewTree(srcObj, dstDir) |
| 260 | if err != nil { |
| 261 | return err |
| 262 | } |
| 263 | |
| 264 | srcParentPath := stdpath.Dir(srcObj.GetPath()) |
| 265 | dstRest := dstDir.GetPath()[len(srcParentPath):] |
| 266 | if dstRest[0] == '/' { |
| 267 | dstRest = dstRest[1:] |
| 268 | } |
| 269 | dstNextName, _, _ := strings.Cut(dstRest, "/") |
| 270 | dstNextPath := stdpath.Join(srcParentPath, dstNextName) |
| 271 | dstNextTreeSha, err := d.renewParentTrees(dstDir.GetPath(), dstOldSha, dstNewSha, dstNextPath) |
| 272 | if err != nil { |
| 273 | return err |
| 274 | } |
| 275 | var delSrc, dstNextTree *TreeObjReq = nil, nil |
| 276 | for _, t := range srcParentTree.Trees { |
| 277 | if t.Path == dstNextName { |
| 278 | dstNextTree = &t.TreeObjReq |
| 279 | dstNextTree.Sha = dstNextTreeSha |
| 280 | } |
| 281 | if t.Path == srcObj.GetName() { |
| 282 | delSrc = &t.TreeObjReq |
| 283 | delSrc.Sha = nil |
| 284 | } |
| 285 | if delSrc != nil && dstNextTree != nil { |
| 286 | break |
| 287 | } |
| 288 | } |
| 289 | if delSrc == nil || dstNextTree == nil { |
| 290 | return errs.ObjectNotFound |
| 291 | } |
| 292 | ancestorNewSha, err := d.newTree(ancestorOldSha, []interface{}{*delSrc, *dstNextTree}) |
| 293 | if err != nil { |
| 294 | return err |
| 295 | } |
| 296 | rootSha, err = d.renewParentTrees(srcParentPath, ancestorOldSha, ancestorNewSha, "/") |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | } else if strings.HasPrefix(srcObj.GetPath(), dstDir.GetPath()) { // /aa/bb/1 -> /aa/ |
| 301 | srcParentPath := stdpath.Dir(srcObj.GetPath()) |
| 302 | srcParentTree, srcParentOldSha, err := d.getTreeDirectly(srcParentPath) |
| 303 | if err != nil { |
| 304 | return err |
nothing calls this directly
no test coverage detected