@@ -10,6 +10,7 @@ import (
1010 artclientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
1111 clientutils "github.com/jfrog/jfrog-client-go/utils"
1212 "github.com/jfrog/jfrog-client-go/utils/errorutils"
13+ "github.com/jfrog/jfrog-client-go/utils/io/fileutils"
1314 "github.com/jfrog/jfrog-client-go/utils/log"
1415 "io"
1516 "os"
@@ -62,21 +63,33 @@ func getLatestBuildInfo(serverDetails *utilsconfig.ServerDetails, buildConfigura
6263}
6364
6465type GitParsingDetails struct {
65- DotGitPath string
66- VcsUrl string
6766 LogLimit int
6867 PrettyFormat string
68+ // Optional
69+ DotGitPath string
6970}
7071
72+ // Parses git logs from the last build's VCS revision.
73+ // Calls git log with a custom format, and parses each line of the output with regexp. logRegExp is used to parse the log lines.
7174func ParseGitLogsFromLastBuild (serverDetails * utilsconfig.ServerDetails , buildConfiguration * build.BuildConfiguration , gitDetails GitParsingDetails , logRegExp * gofrogcmd.CmdOutputPattern ) error {
7275 // Check that git exists in path.
7376 _ , err := exec .LookPath ("git" )
7477 if err != nil {
7578 return errorutils .CheckError (err )
7679 }
7780
81+ gitDetails .DotGitPath , err = GetDotGit (gitDetails .DotGitPath )
82+ if err != nil {
83+ return err
84+ }
85+
86+ vcsUrl , err := getVcsUrl (gitDetails .DotGitPath )
87+ if err != nil {
88+ return err
89+ }
90+
7891 // Get latest build's VCS revision from Artifactory.
79- lastVcsRevision , err := getLatestVcsRevision (serverDetails , buildConfiguration , gitDetails . VcsUrl )
92+ lastVcsRevision , err := getLatestVcsRevision (serverDetails , buildConfiguration , vcsUrl )
8093 if err != nil {
8194 return err
8295 }
@@ -143,6 +156,30 @@ func createErrRegExpHandler(lastVcsRevision string) (*gofrogcmd.CmdOutputPattern
143156 return & errRegExp , nil
144157}
145158
159+ // Looks for the .git directory in the current directory and its parents.
160+ func GetDotGit (providedDotGitPath string ) (string , error ) {
161+ if providedDotGitPath != "" {
162+ return providedDotGitPath , nil
163+ }
164+ dotGitPath , exists , err := fileutils .FindUpstream (".git" , fileutils .Any )
165+ if err != nil {
166+ return "" , err
167+ }
168+ if ! exists {
169+ return "" , errorutils .CheckErrorf ("Could not find .git" )
170+ }
171+ return dotGitPath , nil
172+
173+ }
174+
175+ func getVcsUrl (dotGitPath string ) (string , error ) {
176+ gitManager := clientutils .NewGitManager (dotGitPath )
177+ if err := gitManager .ReadConfig (); err != nil {
178+ return "" , err
179+ }
180+ return gitManager .GetUrl (), nil
181+ }
182+
146183type LogCmd struct {
147184 logLimit int
148185 lastVcsRevision string
0 commit comments