Skip to content

Commit 0df55da

Browse files
feat: added go git integration for templates check
1 parent 301a9f9 commit 0df55da

File tree

5 files changed

+97
-445
lines changed

5 files changed

+97
-445
lines changed

cmd/generate.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"errors"
5+
git_checker "github.com/intelops/compage/cmd/git-checker"
46
"github.com/intelops/compage/cmd/models"
57
"github.com/intelops/compage/internal/converter/cmd"
68
"github.com/intelops/compage/internal/handlers"
@@ -75,6 +77,20 @@ func GenerateCode() error {
7577
return err
7678
}
7779
log.Debugf("template pulled successfully for language %s", node.Language)
80+
81+
// check if the templates sha is matching
82+
repoPath := "/home/mahendrabagul/.compage/templates/compage-template-go"
83+
repoURL := "[email protected]:intelops/compage-template-go.git"
84+
branchName := "template-v8"
85+
commitSimilar, err := git_checker.CheckIfSHACommitSimilar(repoPath, repoURL, branchName)
86+
if err != nil {
87+
log.Errorf("error while checking the commit sha [" + err.Error() + "]")
88+
return err
89+
}
90+
if !commitSimilar {
91+
log.Errorf("the templates are not matching with the latest commit, please pull the latest templates")
92+
return errors.New("the templates are not matching with the latest commit, please pull the latest templates")
93+
}
7894
}
7995

8096
// triggers project generation, process the request

cmd/git-checker/git-commands.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package git_checker
2+
3+
import (
4+
"fmt"
5+
"github.com/go-git/go-git/v5"
6+
"github.com/go-git/go-git/v5/config"
7+
"github.com/go-git/go-git/v5/storage/memory"
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
func CheckIfSHACommitSimilar(repoPath, repoURL, branchName string) (bool, error) {
12+
// Get local commit SHA
13+
localSHA, err := GetLocalGitCommitSHA(repoPath)
14+
if err != nil {
15+
log.Error("Error fetching local Git commit SHA:", err)
16+
return false, err
17+
}
18+
19+
// Get remote commit SHA
20+
remoteSHA, err := GetRemoteGitCommitSHA(repoURL, branchName)
21+
if err != nil {
22+
log.Errorf("Error fetching remote Git commit SHA: %s", err)
23+
return false, err
24+
}
25+
26+
return localSHA == remoteSHA, nil
27+
}
28+
29+
func GetLocalGitCommitSHA(repoPath string) (string, error) {
30+
log.Debugf("repoPath: %s", repoPath)
31+
// Open the given repository
32+
r, err := git.PlainOpen(repoPath)
33+
if err != nil {
34+
log.Errorf("Error opening repository: %s", err)
35+
return "", err
36+
}
37+
38+
// Get the HEAD reference
39+
ref, err := r.Head()
40+
if err != nil {
41+
log.Errorf("Error getting HEAD reference: %s", err)
42+
return "", err
43+
}
44+
45+
// Get the commit SHA
46+
commitSHA := ref.Hash().String()
47+
48+
return commitSHA, nil
49+
}
50+
51+
func GetRemoteGitCommitSHA(repoURL, branchName string) (string, error) {
52+
// List remote references
53+
references, err := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
54+
Name: "origin",
55+
URLs: []string{repoURL},
56+
}).List(&git.ListOptions{})
57+
if err != nil {
58+
log.Errorf("Error listing remote references: %s", err)
59+
return "", err
60+
}
61+
62+
// Look for the branch reference
63+
for _, ref := range references {
64+
if ref.Name().String() == "refs/heads/"+branchName {
65+
return ref.Hash().String(), nil
66+
}
67+
}
68+
69+
return "", fmt.Errorf("branch %s not found", branchName)
70+
}

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/intelops/compage
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/gertd/go-pluralize v0.2.1
@@ -22,7 +22,6 @@ require (
2222
)
2323

2424
require (
25-
cloud.google.com/go/compute v1.23.3 // indirect
2625
dario.cat/mergo v1.0.0 // indirect
2726
github.com/Microsoft/go-winio v0.6.1 // indirect
2827
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c // indirect

0 commit comments

Comments
 (0)