@@ -790,7 +790,7 @@ func (m *nativeGitClient) RevisionMetadata(revision string) (*RevisionMetadata,
790790 if err != nil {
791791 return nil , fmt .Errorf ("failed to interpret trailers for revision %q in repo %q: %w" , revision , m .repoURL , err )
792792 }
793- relatedCommits := getReferences (log .WithFields (log.Fields {"repo" : m .repoURL , "revision" : revision }), out )
793+ relatedCommits , _ := GetReferences (log .WithFields (log.Fields {"repo" : m .repoURL , "revision" : revision }), out )
794794
795795 out , err = m .runCmd ("tag" , "--points-at" , revision )
796796 if err != nil {
@@ -816,65 +816,23 @@ func truncate(str string) string {
816816
817817var shaRegex = regexp .MustCompile (`^[0-9a-f]{5,40}$` )
818818
819- // getReferences extracts related commit metadata from the commit message trailers. If referenced commit
819+ // GetReferences extracts related commit metadata from the commit message trailers. If referenced commit
820820// metadata is present, we return a slice containing a single metadata object. If no related commit metadata is found,
821821// we return a nil slice.
822822//
823823// If a trailer fails validation, we log an error and skip that trailer. We truncate the trailer values to 100
824824// characters to avoid excessively long log messages.
825- func getReferences (logCtx * log.Entry , commitMessageBody string ) []RevisionReference {
825+ //
826+ // We also return the commit message body with all valid Argocd-reference-commit-* trailers removed.
827+ func GetReferences (logCtx * log.Entry , commitMessageBody string ) ([]RevisionReference , string ) {
828+ unrelatedLines := strings.Builder {}
826829 var relatedCommit CommitMetadata
827830 scanner := bufio .NewScanner (strings .NewReader (commitMessageBody ))
828831 for scanner .Scan () {
829832 line := scanner .Text ()
830- if ! strings .HasPrefix (line , "Argocd-reference-commit-" ) {
831- continue
832- }
833- parts := strings .SplitN (line , ": " , 2 )
834- if len (parts ) != 2 {
835- continue
836- }
837- trailerKey := parts [0 ]
838- trailerValue := parts [1 ]
839- switch trailerKey {
840- case "Argocd-reference-commit-repourl" :
841- _ , err := url .Parse (trailerValue )
842- if err != nil {
843- logCtx .Errorf ("failed to parse repo URL %q: %v" , truncate (trailerValue ), err )
844- continue
845- }
846- relatedCommit .RepoURL = trailerValue
847- case "Argocd-reference-commit-author" :
848- address , err := mail .ParseAddress (trailerValue )
849- if err != nil || address == nil {
850- logCtx .Errorf ("failed to parse author email %q: %v" , truncate (trailerValue ), err )
851- continue
852- }
853- relatedCommit .Author = * address
854- case "Argocd-reference-commit-date" :
855- // Validate that it's the correct date format.
856- t , err := time .Parse (time .RFC3339 , trailerValue )
857- if err != nil {
858- logCtx .Errorf ("failed to parse date %q with RFC3339 format: %v" , truncate (trailerValue ), err )
859- continue
860- }
861- relatedCommit .Date = t .Format (time .RFC3339 )
862- case "Argocd-reference-commit-subject" :
863- relatedCommit .Subject = trailerValue
864- case "Argocd-reference-commit-body" :
865- body := ""
866- err := json .Unmarshal ([]byte (trailerValue ), & body )
867- if err != nil {
868- logCtx .Errorf ("failed to parse body %q as JSON: %v" , truncate (trailerValue ), err )
869- continue
870- }
871- relatedCommit .Body = body
872- case "Argocd-reference-commit-sha" :
873- if ! shaRegex .MatchString (trailerValue ) {
874- logCtx .Errorf ("invalid commit SHA %q in trailer %s: must be a lowercase hex string 5-40 characters long" , truncate (trailerValue ), trailerKey )
875- continue
876- }
877- relatedCommit .SHA = trailerValue
833+ updated := updateCommitMetadata (logCtx , & relatedCommit , line )
834+ if ! updated {
835+ unrelatedLines .WriteString (line + "\n " )
878836 }
879837 }
880838 var relatedCommits []RevisionReference
@@ -883,7 +841,64 @@ func getReferences(logCtx *log.Entry, commitMessageBody string) []RevisionRefere
883841 Commit : & relatedCommit ,
884842 })
885843 }
886- return relatedCommits
844+ return relatedCommits , unrelatedLines .String ()
845+ }
846+
847+ // updateCommitMetadata checks if the line is a valid Argocd-reference-commit-* trailer. If so, it updates
848+ // the relatedCommit object and returns true. If the line is not a valid trailer, it returns false.
849+ func updateCommitMetadata (logCtx * log.Entry , relatedCommit * CommitMetadata , line string ) bool {
850+ if ! strings .HasPrefix (line , "Argocd-reference-commit-" ) {
851+ return false
852+ }
853+ parts := strings .SplitN (line , ": " , 2 )
854+ if len (parts ) != 2 {
855+ return false
856+ }
857+ trailerKey := parts [0 ]
858+ trailerValue := parts [1 ]
859+ switch trailerKey {
860+ case "Argocd-reference-commit-repourl" :
861+ _ , err := url .Parse (trailerValue )
862+ if err != nil {
863+ logCtx .Errorf ("failed to parse repo URL %q: %v" , truncate (trailerValue ), err )
864+ return false
865+ }
866+ relatedCommit .RepoURL = trailerValue
867+ case "Argocd-reference-commit-author" :
868+ address , err := mail .ParseAddress (trailerValue )
869+ if err != nil || address == nil {
870+ logCtx .Errorf ("failed to parse author email %q: %v" , truncate (trailerValue ), err )
871+ return false
872+ }
873+ relatedCommit .Author = * address
874+ case "Argocd-reference-commit-date" :
875+ // Validate that it's the correct date format.
876+ t , err := time .Parse (time .RFC3339 , trailerValue )
877+ if err != nil {
878+ logCtx .Errorf ("failed to parse date %q with RFC3339 format: %v" , truncate (trailerValue ), err )
879+ return false
880+ }
881+ relatedCommit .Date = t .Format (time .RFC3339 )
882+ case "Argocd-reference-commit-subject" :
883+ relatedCommit .Subject = trailerValue
884+ case "Argocd-reference-commit-body" :
885+ body := ""
886+ err := json .Unmarshal ([]byte (trailerValue ), & body )
887+ if err != nil {
888+ logCtx .Errorf ("failed to parse body %q as JSON: %v" , truncate (trailerValue ), err )
889+ return false
890+ }
891+ relatedCommit .Body = body
892+ case "Argocd-reference-commit-sha" :
893+ if ! shaRegex .MatchString (trailerValue ) {
894+ logCtx .Errorf ("invalid commit SHA %q in trailer %s: must be a lowercase hex string 5-40 characters long" , truncate (trailerValue ), trailerKey )
895+ return false
896+ }
897+ relatedCommit .SHA = trailerValue
898+ default :
899+ return false
900+ }
901+ return true
887902}
888903
889904// VerifyCommitSignature Runs verify-commit on a given revision and returns the output
0 commit comments