rerere: explain the rerere I/O abstraction

Explain the internals of rerere as in-code comments. This one covers our thin I/O abstraction to read from either a file or a memory while optionally writing out to a file. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 30, 2015 at 22:33 UTC a96847cc1691840bd95cc56549d7c00b35f6d5a0
1 file changed +31 -7
rerere.c
+31 -7
@@ -83,6 +83,21 @@ static int write_rr(struct string_list *rr, int out_fd)
83 return 0;
84 }
85
86 +/*
87 + * "rerere" interacts with conflicted file contents using this I/O
88 + * abstraction. It reads a conflicted contents from one place via
89 + * "getline()" method, and optionally can write it out after
90 + * normalizing the conflicted hunks to the "output". Subclasses of
91 + * rerere_io embed this structure at the beginning of their own
92 + * rerere_io object.
93 + */
94 +struct rerere_io {
95 + int (*getline)(struct strbuf *, struct rerere_io *);
96 + FILE *output;
97 + int wrerror;
98 + /* some more stuff */
99 +};
100 +
101 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
102 {
103 if (!count || *err)
@@ -96,19 +111,15 @@ static inline void ferr_puts(const char *s, FILE *fp, int *err)
111 ferr_write(s, strlen(s), fp, err);
112 }
113
99 -struct rerere_io {
100 - int (*getline)(struct strbuf *, struct rerere_io *);
101 - FILE *output;
102 - int wrerror;
103 - /* some more stuff */
104 -};
105 -
114 static void rerere_io_putstr(const char *str, struct rerere_io *io)
115 {
116 if (io->output)
117 ferr_puts(str, io->output, &io->wrerror);
118 }
119
120 +/*
121 + * Write a conflict marker to io->output (if defined).
122 + */
123 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
124 {
125 char buf[64];
@@ -137,11 +148,17 @@ static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
148 ferr_write(mem, sz, io->output, &io->wrerror);
149 }
150
151 +/*
152 + * Subclass of rerere_io that reads from an on-disk file
153 + */
154 struct rerere_io_file {
155 struct rerere_io io;
156 FILE *input;
157 };
158
159 +/*
160 + * ... and its getline() method implementation
161 + */
162 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
163 {
164 struct rerere_io_file *io = (struct rerere_io_file *)io_;
@@ -286,11 +303,18 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
303 return hunk_no;
304 }
305
306 +/*
307 + * Subclass of rerere_io that reads from an in-core buffer that is a
308 + * strbuf
309 + */
310 struct rerere_io_mem {
311 struct rerere_io io;
312 struct strbuf input;
313 };
314
315 +/*
316 + * ... and its getline() method implementation
317 + */
318 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
319 {
320 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;