2024-05-14 01:35:17 -04:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/mail"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrInvalidCommitTrailerValueSyntax = errors.New("syntax error occurred while parsing a commit trailer value")
|
|
|
|
|
|
|
|
// ParseCommitTrailerValueWithAuthor parses a commit trailer value that contains author data.
|
|
|
|
// Note that it only parses the value and does not consider the trailer key i.e. we just
|
|
|
|
// parse something like the following:
|
|
|
|
//
|
|
|
|
// Foo Bar <foobar@example.com>
|
|
|
|
func ParseCommitTrailerValueWithAuthor(value string) (name, email string, err error) {
|
2024-10-26 01:48:32 -04:00
|
|
|
addr, err := mail.ParseAddress(value)
|
|
|
|
if err != nil {
|
|
|
|
return name, email, err
|
2024-05-14 01:35:17 -04:00
|
|
|
}
|
|
|
|
|
2024-10-26 01:48:32 -04:00
|
|
|
if addr.Name == "" {
|
|
|
|
return name, email, errors.New("commit trailer missing name")
|
2024-05-14 01:35:17 -04:00
|
|
|
}
|
|
|
|
|
2024-10-26 01:48:32 -04:00
|
|
|
name = addr.Name
|
|
|
|
email = addr.Address
|
2024-05-14 01:35:17 -04:00
|
|
|
|
|
|
|
return name, email, nil
|
|
|
|
}
|