github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/signature_gogit.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Copyright 2015 The Gogs Authors. All rights reserved.
     7  
     8  //go:build gogit
     9  
    10  package git
    11  
    12  import (
    13  	"bytes"
    14  	"strconv"
    15  	"time"
    16  
    17  	"github.com/go-git/go-git/v5/plumbing/object"
    18  )
    19  
    20  // Signature represents the Author or Committer information.
    21  type Signature = object.Signature
    22  
    23  // Helper to get a signature from the commit line, which looks like these:
    24  //
    25  //	author Patrick Gundlach <gundlach@speedata.de> 1378823654 +0200
    26  //	author Patrick Gundlach <gundlach@speedata.de> Thu, 07 Apr 2005 22:13:13 +0200
    27  //
    28  // but without the "author " at the beginning (this method should)
    29  // be used for author and committer.
    30  //
    31  // FIXME: include timezone for timestamp!
    32  func newSignatureFromCommitline(line []byte) (_ *Signature, err error) {
    33  	sig := new(Signature)
    34  	emailStart := bytes.IndexByte(line, '<')
    35  	sig.Name = string(line[:emailStart-1])
    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  }