mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add support for Git 2.45+ iso-strict date format
Git 2.45+ introduced a backward incompatible change in the iso-strict date format, showing time in the Zulu timezone with Z suffix instead of +00:00. This commit adds parsing for the new date format before falling back to the old format. Ignore errors for old git versions where %cI is not defined. Revise GetCGRVersion error messages. Revise GetCGRVersion unit test.
This commit is contained in:
committed by
Dan Christian Bogos
parent
13a0e86a0e
commit
4d3868aad0
@@ -758,23 +758,41 @@ func GetCGRVersion() (vers string, err error) {
|
||||
if GitCommitDate == "" || GitCommitHash == "" {
|
||||
return vers, nil
|
||||
}
|
||||
var commitHash string
|
||||
var commitDate time.Time
|
||||
var matched bool
|
||||
|
||||
commitDate, _ = time.Parse("2006-01-02T15:04:05-07:00", strings.TrimSpace(GitCommitDate))
|
||||
//ignoring err temporarily until a future fix since we dont get correct date format in older linux distributions
|
||||
//if err != nil {
|
||||
//return vers, fmt.Errorf("Building version - error: <%s> compiling commit date", err.Error())
|
||||
//}
|
||||
/*
|
||||
Git v2.45 relevant release note:
|
||||
* The output format for dates "iso-strict" has been tweaked to show
|
||||
a time in the Zulu timezone with "Z" suffix, instead of "+00:00".
|
||||
*/
|
||||
|
||||
// Parse the git commit date, which might be in different formats depending on the git version
|
||||
trimmedCommitDate := strings.TrimSpace(GitCommitDate)
|
||||
commitDate, err := time.Parse("2006-01-02T15:04:05Z", trimmedCommitDate)
|
||||
if err != nil {
|
||||
// Failed to parse iso-strict date format for git version 2.45+. Try to parse with the previous format.
|
||||
var fallbackErr error
|
||||
commitDate, fallbackErr = time.Parse("2006-01-02T15:04:05-07:00", trimmedCommitDate)
|
||||
if fallbackErr != nil {
|
||||
// Both parsing attempts failed, group the errors together.
|
||||
err = fmt.Errorf(
|
||||
"failed to parse date:\ngit2.45+ iso-strict format: %w\nprevious iso-strict format: %w",
|
||||
err, fallbackErr)
|
||||
} else {
|
||||
err = nil // successfully parsed with fallback format
|
||||
}
|
||||
}
|
||||
if err != nil && GitCommitDate != "%cI" { // ignore error for old git versions where %cI is not defined
|
||||
return vers, fmt.Errorf("version build error: %w", err)
|
||||
}
|
||||
matched, err = regexp.MatchString("^[0-9a-f]{12,}$", GitCommitHash)
|
||||
if err != nil {
|
||||
return vers, fmt.Errorf("Building version - error: <%s> compiling commit hash", err.Error())
|
||||
return vers, fmt.Errorf("version build error: commit hash compilation failed: %v", err)
|
||||
} else if !matched {
|
||||
return vers, fmt.Errorf("Building version - error: <%s> compiling commit hash", "Regex not matched")
|
||||
return vers, fmt.Errorf("version build error: commit hash does not match expected format")
|
||||
}
|
||||
commitHash = GitCommitHash
|
||||
//CGRateS@v0.11.0~dev-20200110075344-7572e7b11e00
|
||||
commitHash := GitCommitHash
|
||||
//CGRateS@v0.10.1~dev-20200110075344-7572e7b11e00
|
||||
return fmt.Sprintf("%s@%s-%s-%s", CGRateS, Version, commitDate.UTC().Format("20060102150405"), commitHash[:12]), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user