summaryrefslogtreecommitdiff
path: root/scanReposDir.go
blob: 9c62e55ca0805762f06cee8c7389ff78947ecc6b (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func readGitPB(fullpath string) (*gitpb.Repo, error) {
	// fmt.Fprintln(w, "repo:", repo.FullPath, repo.Namespace)
	gitfile := filepath.Join(fullpath, "git.pb")
	bytes, err := os.ReadFile(gitfile)
	if err != nil {
		log.Info("todo: git.pb non-existant:", gitfile)
		return nil, err
	}
	newr := new(gitpb.Repo)
	if err := newr.Unmarshal(bytes); err != nil {
		log.Info("unmarshal failed: NEED TO DELETE:", gitfile)
		return nil, fmt.Errorf("todo: re-generate git.pb for " + fullpath)
	}
	return newr, nil
}

// doesn't enter the directory any further when it finds a .git/
// not stupid like my old version
func scanForgedDir(srcDir string) ([]string, error) {
	var all []string
	var trip bool
	err := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, err error) error {
		if err != nil {
			// Handle possible errors, like permission issues
			log.Infof("error accessing path %q: %v\n", path, err)
			return err
		}

		if d.IsDir() {
			// log.Info("WHAT IS THIS?", path)
		} else {
			_, fname := filepath.Split(path)
			switch fname {
			case "repos.pb":
			case "git.pb":
			case "go.work.last":
			case "go.work.sum":
			default:
				// todo: figure out a way to do padding for init()
				if trip == false {
					log.Info("WARNING:")
				}
				log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
				trip = true
			}
			return nil
		}

		gitfile := filepath.Join(path, "git.pb")
		_, err2 := os.Stat(gitfile)
		if !os.IsNotExist(err2) {
			// log.Info("IS THIS THE ONE?", path)
			if ok, err := addGitRepoDir(path); ok {
				log.Info("added", path)
			} else {
				if err != nil {
					log.Info("add failed", path, err)
				}
			}
			return filepath.SkipDir
		}

		// todo: check if dir is empty here and delete dir?
		return nil
	})

	if trip {
		log.Info("WARNING: junk in", srcDir)
	}
	return all, err
}

// must be "/foo/bar/" + "git.clone/" and then have a .git/ dir
func addGitRepoDir(dir string) (bool, error) {
	fullpath := filepath.Join(dir, "git.clone")
	if check := me.forge.Repos.FindByFullPath(fullpath); check != nil {
		// log.Info(oldr.Namespace, fullpath, "already added")
		return false, nil
	}

	var namespace string
	if oldr, err := readGitPB(dir); err == nil {
		namespace = oldr.Namespace
	} else {
		log.Info("readGitPB() failed", dir, err)
		namespace = strings.TrimPrefix(dir, "/home/repos")
	}
	namespace = strings.Trim(namespace, "/")

	// check to see if 'git clone' has already been run
	_, err := os.Stat(fullpath)
	if os.IsNotExist(err) {
		log.Info("repo needs cloning:", namespace, dir)
		return false, err
	}
	if repo, err := me.forge.AddNamespaceDir(namespace, fullpath); err == nil {
		log.Info("repo added", repo.FullPath)
		return true, nil
	} else {
		log.Info("repo add failed", repo.FullPath, err)
		return false, err
	}
	return false, err
}