This repository was archived by the owner on Nov 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatchergit.go
More file actions
91 lines (78 loc) · 2.53 KB
/
watchergit.go
File metadata and controls
91 lines (78 loc) · 2.53 KB
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
/*
* Copyright (c) 2020-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package main
import (
"os"
"path"
gc "github.com/untillpro/gochips"
)
type watcherGit struct {
commitsTracker IGitTracker
lastCommitHashes map[string]string
}
func (w *watcherGit) Clean(repoPathsToClean []string) {
for _, repoPath := range repoPathsToClean {
gc.Info("watcherGit", "Resetting "+repoPath)
err := new(gc.PipedExec).
Command("git", "reset", "--hard").
WorkingDir(repoPath).
Run(os.Stdout, os.Stderr)
gc.PanicIfError(err)
// possible: module of wrong version is built within submodule. So it does not rebuilt on further push. Need to clean additionaly. Ask Yohanson555
gc.Info("watcherGit", "Cleaning "+repoPath)
err = new(gc.PipedExec).
Command("git", "clean", "-dxf").
WorkingDir(repoPath).
Run(os.Stdout, os.Stderr)
gc.PanicIfError(err)
}
}
func (w *watcherGit) Watch(repoURLs []string) (changedRepoPaths []string) {
defer func() {
if r := recover(); r != nil {
gc.Error("watcherGit: Recovered: ", r)
}
}()
// *************************************************
reposFolder := getReposFolder()
for _, repoURL := range repoURLs {
repoPath, repoFolder := getAbsRepoFolders(repoURL)
gc.Verbose("watcherGit", "repoPath, repoFolder=", repoPath, repoFolder)
gc.ExitIfError(os.MkdirAll(reposFolder, 0755))
if _, err := os.Stat(repoPath); os.IsNotExist(err) {
gc.Info("watcherGit", "Repo folder does not exist, will be cloned", repoPath, repoURL)
err := new(gc.PipedExec).
Command("git", "clone", "--recurse-submodules", repoURL).
WorkingDir(reposFolder).
Run(os.Stdout, os.Stderr)
gc.PanicIfError(err)
}
newHash, ok := w.commitsTracker.GetLastCommit(repoURL, repoPath)
if ok {
oldHash := w.lastCommitHashes[repoPath]
if oldHash == newHash {
continue
}
gc.Info("watcherGit", "Commit hash changed", repoURL, oldHash, newHash)
} else if _, ok := w.lastCommitHashes[repoPath]; ok {
// built once already -> skip
continue
}
gitModulesPath := path.Join(repoPath, ".gitmodules")
if _, err := os.Stat(gitModulesPath); err == nil {
gc.Doing("watcherGit: updating modules")
err = new(gc.PipedExec).
Command("git", "submodule", "update", "--init", "--recursive").
WorkingDir(repoPath).
Run(os.Stdout, os.Stderr)
gc.PanicIfError(err)
}
w.lastCommitHashes[repoPath] = newHash
changedRepoPaths = append(changedRepoPaths, repoPath)
}
return
}