Home

ugit @d87caf61fd67b5618db44968e560732f66890390 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / git / repo_utils.go
- raw
 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package git

import (
	"errors"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
)

// ListRepos returns all directory entries in the given directory
func ListRepos(dir string) ([]fs.DirEntry, error) {
	entries, err := os.ReadDir(dir)
	if err != nil {
		if errors.Is(err, fs.ErrNotExist) {
			return []fs.DirEntry{}, nil
		}
		return nil, err
	}
	return entries, nil
}

// DeleteRepo deletes a git repository from the filesystem
func DeleteRepo(repoPath string) error {
	return os.RemoveAll(repoPath)
}

// RenameRepo renames a git repository
func RenameRepo(repoDir, oldName, newName string) error {
	if !filepath.IsAbs(repoDir) {
		return errors.New("repository directory must be an absolute path")
	}

	if !filepath.IsAbs(oldName) && !filepath.IsAbs(newName) {
		oldPath := filepath.Join(repoDir, oldName)
		if !strings.HasSuffix(oldPath, ".git") {
			oldPath += ".git"
		}

		newPath := filepath.Join(repoDir, newName)
		if !strings.HasSuffix(newPath, ".git") {
			newPath += ".git"
		}

		return os.Rename(oldPath, newPath)
	}

	return errors.New("repository names should not be absolute paths")
}

// RepoPathExists checks if a path exists
func RepoPathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if errors.Is(err, fs.ErrNotExist) {
		return false, nil
	}
	return false, err
}