summaryrefslogtreecommitdiff
path: root/cmd_commit.go
blob: 1e2672d4f01a5f65bbaf15744ff825c1a089d541 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package libfastimport

import (
	"fmt"
	"strconv"
	"strings"

	"git.lukeshu.com/go/libfastimport/textproto"
)

// M ///////////////////////////////////////////////////////////////////////////

type FileModify struct {
	Mode    textproto.Mode
	Path    textproto.Path
	DataRef string
}

func (o FileModify) fiCmdClass() cmdClass { return cmdClassCommit }
func (o FileModify) fiCmdWrite(fiw fiWriter) error {
	return fiw.WriteLine("M", o.Mode, o.DataRef, o.Path)
}
func init() { parser_registerCmd("M ", FileModify{}) }
func (FileModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
	// NB: This parses both FileModify and FileModifyInline
	line, err := fir.ReadLine()
	if err != nil {
		return nil, err
	}
	str := trimLinePrefix(line, "M ")
	fields := strings.SplitN(str, " ", 3)
	if len(fields) != 3 {
		return nil, fmt.Errorf("commit: malformed modify command: %v", line)
	}

	nMode, err := strconv.ParseUint(fields[0], 8, 18)
	if err != nil {
		return nil, err
	}

	ref := fields[1]
	path := textproto.PathUnescape(fields[2])

	if ref == "inline" {
		line, err = fir.ReadLine()
		if err != nil {
			return nil, err
		}
		data, err := parse_data(line)
		if err != nil {
			return nil, err
		}
		return FileModifyInline{
			Mode: textproto.Mode(nMode),
			Path: path,
			Data: data,
		}, nil
	} else {
		return FileModify{
			Mode:    textproto.Mode(nMode),
			Path:    path,
			DataRef: ref,
		}, nil
	}
}

type FileModifyInline struct {
	Mode textproto.Mode
	Path textproto.Path
	Data string
}

func (o FileModifyInline) fiCmdClass() cmdClass { return cmdClassCommit }
func (o FileModifyInline) fiCmdWrite(fiw fiWriter) error {
	ez := &ezfiw{fiw: fiw}
	ez.WriteLine("M", o.Mode, "inline", o.Path)
	ez.WriteData(o.Data)
	return ez.err
}
func (FileModifyInline) fiCmdRead(fiReader) (Cmd, error) { panic("not reached") }

// D ///////////////////////////////////////////////////////////////////////////

type FileDelete struct {
	Path textproto.Path
}

func (o FileDelete) fiCmdClass() cmdClass { return cmdClassCommit }
func (o FileDelete) fiCmdWrite(fiw fiWriter) error {
	return fiw.WriteLine("D", o.Path)
}
func init() { parser_registerCmd("D ", FileDelete{}) }
func (FileDelete) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
	line, err := fir.ReadLine()
	if err != nil {
		return nil, err
	}
	return FileDelete{Path: textproto.PathUnescape(trimLinePrefix(line, "D "))}, nil
}

// C ///////////////////////////////////////////////////////////////////////////

type FileCopy struct {
	Src textproto.Path
	Dst textproto.Path
}

func (o FileCopy) fiCmdClass() cmdClass { return cmdClassCommit }
func (o FileCopy) fiCmdWrite(fiw fiWriter) error {
	return fiw.WriteLine("C", o.Src, o.Dst)
}
func init() { parser_registerCmd("C ", FileDelete{}) }
func (FileCopy) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
	// BUG(lukeshu): TODO: commit C not implemented
	panic("TODO: commit C not implemented")
}

// R ///////////////////////////////////////////////////////////////////////////

type FileRename struct {
	Src string
	Dst string
}

func (o FileRename) fiCmdClass() cmdClass { return cmdClassCommit }
func (o FileRename) fiCmdWrite(fiw fiWriter) error {
	return fiw.WriteLine("R", o.Src, o.Dst)
}
func init() { parser_registerCmd("R ", FileDelete{}) }
func (FileRename) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
	// BUG(lukeshu): TODO: commit R not implemented
	panic("TODO: commit R not implemented")
}

// deleteall ///////////////////////////////////////////////////////////////////

type FileDeleteAll struct{}

func (o FileDeleteAll) fiCmdClass() cmdClass { return cmdClassCommit }
func (o FileDeleteAll) fiCmdWrite(fiw fiWriter) error {
	return fiw.WriteLine("deleteall")
}
func init() { parser_registerCmd("deleteall\n", FileDeleteAll{}) }
func (FileDeleteAll) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
	_, err = fir.ReadLine()
	if err != nil {
		return nil, err
	}
	return FileDeleteAll{}, nil
}

// N ///////////////////////////////////////////////////////////////////////////

type NoteModify struct {
	CommitIsh string
	DataRef   string
}

func (o NoteModify) fiCmdClass() cmdClass { return cmdClassCommit }
func (o NoteModify) fiCmdWrite(fiw fiWriter) error {
	return fiw.WriteLine("N", o.DataRef, o.CommitIsh)
}
func init() { parser_registerCmd("N ", NoteModify{}) }
func (NoteModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
	// NB: This parses both NoteModify and NoteModifyInline
	line, err := fir.ReadLine()
	if err != nil {
		return nil, err
	}
	str := trimLinePrefix(line, "N ")
	sp := strings.IndexByte(str, ' ')
	if sp < 0 {
		return nil, fmt.Errorf("commit: malformed notemodify command: %v", line)
	}

	ref := str[:sp]
	commitish := str[sp+1:]

	if ref == "inline" {
		line, err = fir.ReadLine()
		if err != nil {
			return nil, err
		}
		data, err := parse_data(line)
		if err != nil {
			return nil, err
		}
		return NoteModifyInline{
			CommitIsh: commitish,
			Data:      data,
		}, nil
	} else {
		return NoteModify{
			CommitIsh: commitish,
			DataRef:   ref,
		}, nil
	}
}

type NoteModifyInline struct {
	CommitIsh string
	Data      string
}

func (o NoteModifyInline) fiCmdClass() cmdClass { return cmdClassCommit }
func (o NoteModifyInline) fiCmdWrite(fiw fiWriter) error {
	ez := &ezfiw{fiw: fiw}
	ez.WriteLine("N", "inline", o.CommitIsh)
	ez.WriteData(o.Data)
	return ez.err
}
func (NoteModifyInline) fiCmdRead(fiReader) (Cmd, error) { panic("not reached") }