code.gitea.io/gitea@v1.19.3/modules/git/signature_gogit.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  //go:build gogit
     6  
     7  package git
     8  
     9  import (
    10  	"bytes"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  
    15  	"github.com/go-git/go-git/v5/plumbing/object"
    16  )
    17  
    18  // Signature represents the Author or Committer information.
    19  type Signature = object.Signature
    20  
    21  // Helper to get a signature from the commit line, which looks like these:
    22  //
    23  //	author Patrick Gundlach <gundlach@speedata.de> 1378823654 +0200
    24  //	author Patrick Gundlach <gundlach@speedata.de> Thu, 07 Apr 2005 22:13:13 +0200
    25  //
    26  // but without the "author " at the beginning (this method should)
    27  // be used for author and committer.
    28  //
    29  // FIXME: include timezone for timestamp!
    30  func newSignatureFromCommitline(line []byte) (_ *Signature, err error) {
    31  	sig := new(Signature)
    32  	emailStart := bytes.IndexByte(line, '<')
    33  	if emailStart > 0 { // Empty name has already occurred, even if it shouldn't
    34  		sig.Name = strings.TrimSpace(string(line[:emailStart-1]))
    35  	}
    36  	emailEnd := bytes.IndexByte(line, '>')
    37  	sig.Email = string(line[emailStart+1 : emailEnd])
    38  
    39  	// Check date format.
    40  	if len(line) > emailEnd+2 {
    41  		firstChar := line[emailEnd+2]
    42  		if firstChar >= 48 && firstChar <= 57 {
    43  			timestop := bytes.IndexByte(line[emailEnd+2:], ' ')
    44  			timestring := string(line[emailEnd+2 : emailEnd+2+timestop])
    45  			seconds, _ := strconv.ParseInt(timestring, 10, 64)
    46  			sig.When = time.Unix(seconds, 0)
    47  		} else {
    48  			sig.When, err = time.Parse(GitTimeLayout, string(line[emailEnd+2:]))
    49  			if err != nil {
    50  				return nil, err
    51  			}
    52  		}
    53  	} else {
    54  		// Fall back to unix 0 time
    55  		sig.When = time.Unix(0, 0)
    56  	}
    57  	return sig, nil
    58  }