1
1
package git_checker
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"github.com/go-git/go-git/v5"
6
7
"github.com/go-git/go-git/v5/config"
@@ -23,6 +24,16 @@ func CheckIfSHACommitSimilar(repoPath, repoURL, branchName string) (bool, error)
23
24
return false , err
24
25
}
25
26
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
+
26
37
return localSHA == remoteSHA , nil
27
38
}
28
39
@@ -68,3 +79,29 @@ func GetRemoteGitCommitSHA(repoURL, branchName string) (string, error) {
68
79
69
80
return "" , fmt .Errorf ("branch %s not found" , branchName )
70
81
}
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