shithub: furgit

ref: e7ce17381f525328073577d60583447fc9412c18
dir: /refstore/files/update_cleanup_parents.go/

View raw version
package files

import (
	"errors"
	"os"
	"path"
)

func (executor *refUpdateExecutor) tryRemoveEmptyParents(name string) {
	loc := executor.store.loosePath(name)
	executor.tryRemoveEmptyParentPaths(loc.root, loc.path)
}

func (executor *refUpdateExecutor) tryRemoveEmptyParentPaths(kind rootKind, name string) {
	root := executor.store.rootFor(kind)
	dir := path.Dir(name)

	for dir != "." && dir != "/" {
		err := root.Remove(dir)
		if err != nil {
			if errors.Is(err, os.ErrNotExist) {
				return
			}

			var pathErr *os.PathError
			if errors.As(err, &pathErr) {
				return
			}

			return
		}

		dir = path.Dir(dir)
	}
}