gitech
1// Copyright 2015 The Gogs Authors. All rights reserved.
2// Copyright 2019 The Gitea Authors. All rights reserved.
3// SPDX-License-Identifier: MIT
4
5package git6
7import (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.
21func parseSignatureFromCommitLine(line string) *Signature {22sig := &Signature{}23s1, sx, ok1 := strings.Cut(line, " <")24s2, s3, ok2 := strings.Cut(sx, "> ")25if !ok1 || !ok2 {26sig.Name = line27return sig28}29sig.Name, sig.Email = s1, s230
31if strings.Count(s3, " ") == 1 {32ts, tz, _ := strings.Cut(s3, " ")33seconds, _ := strconv.ParseInt(ts, 10, 64)34if tzTime, err := time.Parse("-0700", tz); err == nil {35sig.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.40log.Error("suspicious commit line format: %q", line)41for _, fmt := range []string{ /*"Mon Jan _2 15:04:05 2006 -0700"*/ } {42if t, err := time.Parse(fmt, s3); err == nil {43sig.When = t44break45}46}47}48return sig49}
50