summaryrefslogtreecommitdiff
path: root/types.go
blob: 1ce0741ce348a0398b186c8760c928b1c2e519bb (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
package libfastimport

import (
	"fmt"
	"strings"
	"time"
)

type UserTime struct {
	Name  string
	Email string
	Time  time.Time
}

func (ut UserTime) String() string {
	if ut.Name == "" {
		return fmt.Sprintf("<%s> %d %s",
			ut.Name,
			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"))
	}
}

type Mode uint32 // 18 bits

var (
	ModeFil = Mode(0100644)
	ModeExe = Mode(0100755)
	ModeSym = Mode(0120000)
	ModeGit = Mode(0160000)
	ModeDir = Mode(0040000)
)

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

func PathEscape(path string) string {
	if strings.HasPrefix(path, "\"") || strings.ContainsRune(path, '\n') {
		return "\"" + strings.Replace(strings.Replace(strings.Replace(path, "\\", "\\\\", -1), "\"", "\\\"", -1), "\n", "\\n", -1) + "\""
	} else {
		return path
	}
}

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

type Path string

func (p Path) String() string {
	return PathEscape(string(p))
}