summaryrefslogtreecommitdiff
path: root/types.go
blob: 2edaad8ddb09a1d2d264b38461e7c66d712a87e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2017-2018, 2020  Luke Shumaker <lukeshu@lukeshu.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package libfastimport

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	"github.com/pkg/errors"
)

// Ident is a tuple of a commiter's (or author's) name, email, and a
// timestamp with timezone.
//
// BUG(lukeshu): Ident (and ParseIdent) only supports the "raw" date
// format (not "rfc2822" or "now")
type Ident struct {
	Name  string
	Email string
	Time  time.Time
}

func (ut Ident) String() string {
	if ut.Name == "" {
		return fmt.Sprintf("<%s> %d %s",
			ut.Email,
			ut.Time.Unix(),
			ut.Time.Format("-0700"))
	} else {
		return fmt.Sprintf("%s <%s> %d %s",
			ut.Name,
			ut.Email,
			ut.Time.Unix(),
			ut.Time.Format("-0700"))
	}
}

// ParseIdent parses a string containing an Ident.
//
// The format of this string is
//
//     <name> SP LT <email> GT SP <time> SP <offutc>
//
// Where <name> may contain a space, but not "<" or ">"; <time> is an
// integer number of seconds since the UNIX epoch (UTC); <offutc> is
// positive or negative 4-digit offset from UTC (for example, EST
// would be "-0500").
func ParseIdent(str string) (Ident, error) {
	ret := Ident{}
	lt := strings.IndexAny(str, "<>")
	if lt < 0 || str[lt] != '<' {
		return ret, errors.Errorf("Missing < in ident string: %v", str)
	}
	if lt > 0 {
		if str[lt-1] != ' ' {
			return ret, errors.Errorf("Missing space before < in ident string: %v", str)
		}
		ret.Name = str[:lt-1]
	}
	gt := lt + 1 + strings.IndexAny(str[lt+1:], "<>")
	if gt < lt+1 || str[gt] != '>' {
		return ret, errors.Errorf("Missing > in ident string: %v", str)
	}
	if str[gt+1] != ' ' {
		return ret, errors.Errorf("Missing space after > in ident string: %v", str)
	}
	ret.Email = str[lt+1 : gt]

	strWhen := str[gt+2:]
	sp := strings.IndexByte(strWhen, ' ')
	if sp < 0 {
		return ret, errors.Errorf("missing time zone in when: %v", str)
	}
	sec, err := strconv.ParseInt(strWhen[:sp], 10, 64)
	if err != nil {
		return ret, err
	}
	tzt, err := time.Parse("-0700", strWhen[sp+1:])
	if err != nil {
		return ret, err
	}
	ret.Time = time.Unix(sec, 0).In(tzt.Location())

	return ret, nil
}

// Mode is a file mode as seen by git.
type Mode uint32 // 18 bits

const (
	ModeFil = Mode(0100644) // A regular file
	ModeExe = Mode(0100755) // An executable file
	ModeSym = Mode(0120000) // A symbolic link
	ModeGit = Mode(0160000) // A nested git repository (e.g. submodule)
	ModeDir = Mode(0040000) // A directory
)

func (m Mode) String() string {
	return fmt.Sprintf("%06o", m)
}

func (m Mode) GoString() string {
	return fmt.Sprintf("%07o", m)
}

// Path is a string storing a git path.
type Path string

// PathEscape escapes a path in case it contains special characters.
func PathEscape(path Path) string {
	if strings.HasPrefix(string(path), "\"") || strings.ContainsRune(string(path), '\n') {
		return "\"" + strings.Replace(strings.Replace(strings.Replace(string(path), "\\", "\\\\", -1), "\"", "\\\"", -1), "\n", "\\n", -1) + "\""
	} else {
		return string(path)
	}
}

// PathUnescape unescapes a quoted path.
func PathUnescape(epath string) Path {
	if strings.HasPrefix(epath, "\"") {
		return Path(strings.Replace(strings.Replace(strings.Replace(epath[1:len(epath)-1], "\\n", "\n", -1), "\\\"", "\"", -1), "\\\\", "\\", -1))
	} else {
		return Path(epath)
	}
}

// String calls PathEscape on the Path.
func (p Path) String() string {
	return PathEscape(p)
}