summaryrefslogtreecommitdiff
path: root/doSync.go
blob: 08d23fc70db84203d57cd6e730e3857043f06911 (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
// 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"
	"path/filepath"
	"time"

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

// trys to figure out if there is still something to update

func doSync() error {
	if argv.Pull.Sync.Clean != nil {
		return doSyncClean()
	}
	if argv.Pull.Sync.User != nil {
		return doSyncUser()
	}

	return fmt.Errorf("nothing to do")
}

func doSyncClean() error {
	if err := doAllCheckoutMaster(); err != nil {
		return err
	}

	if _, _, _, err := IsEverythingOnMaster(); err != nil {
		log.Info("Not all repos are on the master branch")
		return err
	}

	// force everything
	argv.Force = true

	now := time.Now()
	pullcount := me.forge.RillFuncError(rillPull)
	count := me.forge.RillReload()
	if count != 0 {
		me.forge.ConfigSave()
	}

	total, count, nope, _ := IsEverythingOnMaster()
	log.Printf("doSyncClean() ok. %d total repos. (%d git pulled) (%d not on master branch) (%s) git pull total=%d\n", total, count, nope, shell.FormatDuration(time.Since(now)), pullcount)

	return nil
}

func doSyncUser() error {
	me.forge.ConfigRill(10, 20)
	if allerr := me.forge.RillRepos(syncDevelBranch); len(allerr) != 0 {
		log.Info("RillFunc() failed", allerr)
		return fmt.Errorf("Rill doSyncUser() error count = %d", len(allerr))
	} else {
		log.Info("Rill syncDevelBranch() ok count =", len(allerr))
	}

	argv.Force = true
	if err := doAllCheckoutUser(); err != nil {
		return err
	}

	return nil
}

func syncDevelBranch(repo *gitpb.Repo) error {
	branch := repo.GetDevelBranchName()
	if repo.Exists(filepath.Join(".git/refs/heads", branch)) {
		return nil
	}
	if repo.Exists(filepath.Join(".git/refs/remotes/origin", branch)) {
		cmd := []string{"git", "checkout", branch}
		err := repo.RunVerbose(cmd)
		return err
	}
	cmd := []string{"git", "branch", branch}
	repo.RunVerbose(cmd)
	cmd = []string{"git", "checkout", branch}
	err := repo.RunVerbose(cmd)
	return err
}

func syncDevelBranches() error {
	all := me.forge.Repos.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		branch := repo.GetDevelBranchName()
		if repo.Exists(filepath.Join(".git/refs/heads", branch)) {
			continue
		}
		if repo.Exists(filepath.Join(".git/refs/remotes/origin", branch)) {
			cmd := []string{"git", "checkout", branch}
			repo.RunVerbose(cmd)
			continue
		}
		cmd := []string{"git", "branch", branch}
		repo.RunVerbose(cmd)
		cmd = []string{"git", "checkout", branch}
		repo.RunVerbose(cmd)
	}
	return nil
}