code.gitea.io/gitea@v1.22.3/modules/git/signature.go (about)

     1  // Copyright 2015 The Gogs Authors. All rights reserved.
     2  // Copyright 2019 The Gitea Authors. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package git
     6  
     7  import (
     8  	"strconv"
     9  	"strings"
    10  	"time"
    11  
    12  	"code.gitea.io/gitea/modules/log"
    13  )
    14  
    15  // Helper to get a signature from the commit line, which looks like:
    16  //
    17  //	full name <user@example.com> 1378823654 +0200
    18  //
    19  // Haven't found the official reference for the standard format yet.
    20  // This function never fails, if the "line" can't be parsed, it returns a default Signature with "zero" time.
    21  func parseSignatureFromCommitLine(line string) *Signature {
    22  	sig := &Signature{}
    23  	s1, sx, ok1 := strings.Cut(line, " <")
    24  	s2, s3, ok2 := strings.Cut(sx, "> ")
    25  	if !ok1 || !ok2 {
    26  		sig.Name = line
    27  		return sig
    28  	}
    29  	sig.Name, sig.Email = s1, s2
    30  
    31  	if strings.Count(s3, " ") == 1 {
    32  		ts, tz, _ := strings.Cut(s3, " ")
    33  		seconds, _ := strconv.ParseInt(ts, 10, 64)
    34  		if tzTime, err := time.Parse("-0700", tz); err == nil {
    35  			sig.When = time.Unix(seconds, 0).In(tzTime.Location())
    36  		}
    37  	} else {
    38  		// the old gitea code tried to parse the date in a few different formats, but it's not clear why.
    39  		// according to public document, only the standard format "timestamp timezone" could be found, so drop other formats.
    40  		log.Error("suspicious commit line format: %q", line)
    41  		for _, fmt := range []string{ /*"Mon Jan _2 15:04:05 2006 -0700"*/ } {
    42  			if t, err := time.Parse(fmt, s3); err == nil {
    43  				sig.When = t
    44  				break
    45  			}
    46  		}
    47  	}
    48  	return sig
    49  }