summaryrefslogtreecommitdiff
path: root/doClone.go
blob: d2e5ffc99a0eb59f870176e44bd048b4eb40bb0a (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
117
118
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"fmt"
	"strings"

	"go.wit.com/lib/config"
	"go.wit.com/lib/fhelp"
	"go.wit.com/log"
)

//
// go-clone everything from wit
//

func doClone() error {
	initForge()

	data, err := resources.ReadFile("resources/repomap")
	if err != nil {
		log.Info("open repomap failed", err)
		return err
	}
	if len(data) == 0 {
		log.Info("resources/repomap is empty")
		return config.ErrEmpty
	}
	if argv.Clone.Repomap != nil {
		makeNewRepomap(string(data))
		return nil
	}

	for _, line := range strings.Split(string(data), "\n") {
		if line == "" {
			continue
		}
		if strings.HasPrefix(line, "#") {
			continue
		}
		parts := strings.Fields(line)
		namespace := parts[0]
		repo := me.forge.Repos.FindByNamespace(namespace)
		if argv.Clone.Check != nil {
			if repo == nil {
				log.Info("don't have repo", namespace)
				continue
			}
			if repo.URL == parts[1] {
				continue
			}
			log.Info("url wrong?", repo.URL, parts[1])
			continue
		}
		if argv.Clone.Fix != nil {
			if repo == nil {
				log.Info("don't have repo", namespace)
				continue
			}
			if repo.URL == parts[1] {
				continue
			}
			cmd := []string{"git", "remote", "set-url", "origin", parts[1]}
			log.Info("url wrong?", cmd)
			repo.RunVerbose(cmd)
			continue
		}
		if repo != nil {
			if argv.Verbose {
				log.Info("already have", repo.FullPath)
			}
			continue
		}
		var cmd []string
		cmd = []string{"go-clone", "--non-recursive", namespace}
		if argv.DryRun {
			log.Info("run:", cmd)
		} else {
			log.Info("should run:", cmd)
			if _, err := fhelp.RunRealtimeError(cmd); err != nil {
				if !argv.Force {
					msg := fmt.Sprintf("failed: %v", cmd)
					me.argv.BadExit(msg, err)
				}
			}
		}
	}
	return nil
}

func makeNewRepomap(lines string) {
	for _, line := range strings.Split(lines, "\n") {
		if line == "" {
			log.Info(line)
			continue
		}
		if strings.HasPrefix(line, "#") {
			log.Info(line)
			continue
		}
		parts := strings.Fields(line)
		namespace := parts[0]
		repo := me.forge.Repos.FindByNamespace(namespace)
		if repo == nil {
			log.Info(line)
			continue
		}
		newparts := []string{namespace}
		newparts = append(newparts, repo.URL)
		if len(parts) > 2 {
			newparts = append(newparts, parts[2:]...)
		}
		newline := strings.Join(newparts, " ")
		log.Info(newline)
	}
}