aboutsummaryrefslogtreecommitdiff
path: root/ref/store/files/update_cleanup_parents.go
blob: c62681fa78b56b0d42224275582c8cb500e97ccf (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)
	}
}