summaryrefslogtreecommitdiff
path: root/forgeDir.go
blob: 7d5a2a5e9436f7d1147231a808ba5e5bf4b7b683 (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
package main

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

	"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)
	bytes, err := os.ReadFile(filepath.Join(fullpath, "git.pb"))
	if err != nil {
		log.Info("todo: git.pb non-existant:", fullpath)
		return nil, err
	}
	newr := new(gitpb.Repo)
	if err := newr.Unmarshal(bytes); err != nil {
		log.Info("todo: unmarshal failed for git.pb:", fullpath)
		return nil, fmt.Errorf("todo: 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
			fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
			return err
		}

		if d.IsDir() {
			// log.Info("path is dir", 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
		}

		gitdir := filepath.Join(path, "git.pb")
		_, err2 := os.Stat(gitdir)
		if !os.IsNotExist(err2) {
			all = append(all, path)
			return filepath.SkipDir
		}
		// todo: check if dir is empty here and delete dir?
		return nil
	})

	// probably always leave this here forever
	// this check, along with CheckDirty() makes sure you can safely delete ~/go/src or the go.work directory
	// because everything is either checked in or deleted. An important thing to know!
	if trip {
		log.Info("WARNING: junk in", srcDir)
	}
	return all, err
}