Skip to content

Commit 19214f2

Browse files
feat: added go git based git dirty check
1 parent 0df55da commit 19214f2

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,27 @@ compage
1717
dist/
1818
/cosign.key
1919
/cosign.pub
20+
/myproject/.github/workflows/user-service-ci.yml
21+
/myproject/.github/workflows/user-service-release.yml
22+
/myproject/user-service/.devcontainer/devcontainer.json
23+
/myproject/user-service/.devcontainer/Dockerfile
24+
/myproject/user-service/config/rest-opentel-config.go
25+
/myproject/user-service/kubernetes/deployment.yaml
26+
/myproject/user-service/kubernetes/service.yaml
27+
/myproject/user-service/pkg/rest/server/controllers/user-controller.go
28+
/myproject/user-service/pkg/rest/server/daos/clients/sqls/map.go
29+
/myproject/user-service/pkg/rest/server/daos/user-dao.go
30+
/myproject/user-service/pkg/rest/server/models/user-model.go
31+
/myproject/user-service/pkg/rest/server/services/user-service.go
32+
/myproject/user-service/.gitignore
33+
/myproject/user-service/devspace.yaml
34+
/myproject/user-service/devspace_start.sh
35+
/myproject/user-service/Dockerfile
36+
/myproject/user-service/go.mod
37+
/myproject/user-service/go.sum
38+
/myproject/user-service/main.go
39+
/myproject/user-service/README.md
40+
/myproject/user-service/useful-commands
41+
/myproject/.deepsource.toml
42+
/myproject/LICENSE
43+
/myproject/README.md

cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func GenerateCode() error {
7979
log.Debugf("template pulled successfully for language %s", node.Language)
8080

8181
// check if the templates sha is matching
82-
repoPath := "/home/mahendrabagul/.compage/templates/compage-template-go"
82+
repoPath := "/Users/mahendrabagul/.compage/templates/compage-template-go"
8383
repoURL := "[email protected]:intelops/compage-template-go.git"
8484
branchName := "template-v8"
8585
commitSimilar, err := git_checker.CheckIfSHACommitSimilar(repoPath, repoURL, branchName)

cmd/git-checker/git-commands.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git_checker
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/go-git/go-git/v5"
67
"github.com/go-git/go-git/v5/config"
@@ -23,6 +24,16 @@ func CheckIfSHACommitSimilar(repoPath, repoURL, branchName string) (bool, error)
2324
return false, err
2425
}
2526

27+
isDirty, err := IsRepoDirty(repoPath)
28+
if err != nil {
29+
log.Errorf("Error checking if repository is dirty: %s", err)
30+
return false, err
31+
}
32+
if isDirty {
33+
log.Errorf("Repository is dirty")
34+
return false, errors.New("repository is dirty")
35+
}
36+
2637
return localSHA == remoteSHA, nil
2738
}
2839

@@ -68,3 +79,29 @@ func GetRemoteGitCommitSHA(repoURL, branchName string) (string, error) {
6879

6980
return "", fmt.Errorf("branch %s not found", branchName)
7081
}
82+
83+
func IsRepoDirty(repoPath string) (bool, error) {
84+
// Open the given repository
85+
repo, err := git.PlainOpen(repoPath)
86+
if err != nil {
87+
log.Errorf("Error opening repository: %s", err)
88+
return false, err
89+
}
90+
91+
// Get the working directory for the repository
92+
worktree, err := repo.Worktree()
93+
if err != nil {
94+
log.Errorf("Error getting working directory: %s", err)
95+
return false, err
96+
}
97+
98+
// Check the status of the working directory
99+
status, err := worktree.Status()
100+
if err != nil {
101+
log.Errorf("Error getting working directory status: %s", err)
102+
return false, err
103+
}
104+
105+
// The repository is dirty if there are any changes in the status
106+
return !status.IsClean(), nil
107+
}

0 commit comments

Comments
 (0)