1/*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <sys/queue.h>
18#include <sys/stat.h>
19
20#include <stdbool.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <limits.h>
25#include <sha1.h>
26#include <zlib.h>
27
28#include "got_object.h"
29#include "got_repository.h"
30#include "got_error.h"
31#include "got_diff.h"
32#include "got_opentemp.h"
33#include "got_path.h"
34#include "got_cancel.h"
35#include "got_worktree.h"
36
37#include "got_lib_diff.h"
38#include "got_lib_delta.h"
39#include "got_lib_inflate.h"
40#include "got_lib_object.h"
41
42static const struct got_error *
43add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
44{
45	off_t *p;
46
47	p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
48	if (p == NULL)
49		return got_error_from_errno("reallocarray");
50	*line_offsets = p;
51	(*line_offsets)[*nlines] = off;
52	(*nlines)++;
53	return NULL;
54}
55
56static const struct got_error *
57diff_blobs(off_t **line_offsets, size_t *nlines,
58    struct got_diffreg_result **resultp, struct got_blob_object *blob1,
59    struct got_blob_object *blob2,
60    const char *label1, const char *label2, mode_t mode1, mode_t mode2,
61    int diff_context, int ignore_whitespace, FILE *outfile)
62{
63	const struct got_error *err = NULL, *free_err;
64	FILE *f1 = NULL, *f2 = NULL;
65	char hex1[SHA1_DIGEST_STRING_LENGTH];
66	char hex2[SHA1_DIGEST_STRING_LENGTH];
67	char *idstr1 = NULL, *idstr2 = NULL;
68	size_t size1, size2;
69	struct got_diffreg_result *result;
70	off_t outoff = 0;
71	int n;
72
73	if (line_offsets && *line_offsets && *nlines > 0)
74		outoff = (*line_offsets)[*nlines - 1];
75
76	if (resultp)
77		*resultp = NULL;
78
79	if (blob1) {
80		f1 = got_opentemp();
81		if (f1 == NULL)
82			return got_error_from_errno("got_opentemp");
83	}
84
85	if (blob2) {
86		f2 = got_opentemp();
87		if (f2 == NULL) {
88			err = got_error_from_errno("got_opentemp");
89			fclose(f1);
90			return err;
91		}
92	}
93
94	size1 = 0;
95	if (blob1) {
96		idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97		err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
98		    blob1);
99		if (err)
100			goto done;
101	} else
102		idstr1 = "/dev/null";
103
104	size2 = 0;
105	if (blob2) {
106		idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107		err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
108		    blob2);
109		if (err)
110			goto done;
111	} else
112		idstr2 = "/dev/null";
113
114	if (outfile) {
115		char *modestr1 = NULL, *modestr2 = NULL;
116		int modebits;
117		if (mode1 && mode1 != mode2) {
118			if (S_ISLNK(mode1))
119				modebits = S_IFLNK;
120			else
121				modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
122			if (asprintf(&modestr1, " (mode %o)",
123			    mode1 & modebits) == -1) {
124				err = got_error_from_errno("asprintf");
125				goto done;
126			}
127		}
128		if (mode2 && mode1 != mode2) {
129			if (S_ISLNK(mode2))
130				modebits = S_IFLNK;
131			else
132				modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
133			if (asprintf(&modestr2, " (mode %o)",
134			    mode2 & modebits) == -1) {
135				err = got_error_from_errno("asprintf");
136				goto done;
137			}
138		}
139		n = fprintf(outfile, "blob - %s%s\n", idstr1,
140		    modestr1 ? modestr1 : "");
141		if (n < 0) {
142			err = got_error_from_errno("fprintf");
143			goto done;
144		}
145		outoff += n;
146		if (line_offsets) {
147			err = add_line_offset(line_offsets, nlines, outoff);
148			if (err)
149				goto done;
150		}
151
152		n = fprintf(outfile, "blob + %s%s\n", idstr2,
153		    modestr2 ? modestr2 : "");
154		if (n < 0) {
155			err = got_error_from_errno("fprintf");
156			goto done;
157		}
158		outoff += n;
159		if (line_offsets) {
160			err = add_line_offset(line_offsets, nlines, outoff);
161			if (err)
162				goto done;
163		}
164
165		free(modestr1);
166		free(modestr2);
167	}
168	err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
169	    ignore_whitespace);
170	if (err)
171		goto done;
172
173	if (outfile) {
174		err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
175		    label1 ? label1 : idstr1,
176		    label2 ? label2 : idstr2,
177		    GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
178		if (err)
179			goto done;
180	}
181
182	if (resultp && err == NULL)
183		*resultp = result;
184	else {
185		free_err = got_diffreg_result_free(result);
186		if (free_err && err == NULL)
187			err = free_err;
188	}
189done:
190	if (f1 && fclose(f1) != 0 && err == NULL)
191		err = got_error_from_errno("fclose");
192	if (f2 && fclose(f2) != 0 && err == NULL)
193		err = got_error_from_errno("fclose");
194	return err;
195}
196
197const struct got_error *
198got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
199    struct got_blob_object *blob2, struct got_object_id *id1,
200    struct got_object_id *id2, const char *label1, const char *label2,
201    mode_t mode1, mode_t mode2, struct got_repository *repo)
202{
203	struct got_diff_blob_output_unidiff_arg *a = arg;
204
205	return diff_blobs(&a->line_offsets, &a->nlines, NULL,
206	    blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
207	    a->ignore_whitespace, a->outfile);
208}
209
210const struct got_error *
211got_diff_blob(off_t **line_offsets, size_t *nlines,
212    struct got_blob_object *blob1, struct got_blob_object *blob2,
213    const char *label1, const char *label2, int diff_context,
214    int ignore_whitespace, FILE *outfile)
215{
216	return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
217	    label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
218}
219
220static const struct got_error *
221diff_blob_file(struct got_diffreg_result **resultp,
222    struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
223    const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
224{
225	const struct got_error *err = NULL, *free_err;
226	FILE *f1 = NULL;
227	char hex1[SHA1_DIGEST_STRING_LENGTH];
228	char *idstr1 = NULL;
229	size_t size1;
230	struct got_diffreg_result *result = NULL;
231
232	if (resultp)
233		*resultp = NULL;
234
235	size1 = 0;
236	if (blob1) {
237		f1 = got_opentemp();
238		if (f1 == NULL)
239			return got_error_from_errno("got_opentemp");
240		idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
241		err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
242		    blob1);
243		if (err)
244			goto done;
245	} else {
246		idstr1 = "/dev/null";
247	}
248
249	if (outfile) {
250		fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
251		fprintf(outfile, "file + %s\n",
252		    f2 == NULL ? "/dev/null" : label2);
253	}
254
255	err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
256	    ignore_whitespace);
257	if (err)
258		goto done;
259
260	if (outfile) {
261		err = got_diffreg_output(NULL, NULL, result, f1, f2,
262		    label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
263		    outfile);
264		if (err)
265			goto done;
266	}
267
268	if (resultp && err == NULL)
269		*resultp = result;
270	else if (result) {
271		free_err = got_diffreg_result_free(result);
272		if (free_err && err == NULL)
273			err = free_err;
274	}
275done:
276	if (f1 && fclose(f1) != 0 && err == NULL)
277		err = got_error_from_errno("fclose");
278	return err;
279}
280
281const struct got_error *
282got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
283    FILE *f2, size_t size2, const char *label2, int diff_context,
284    int ignore_whitespace, FILE *outfile)
285{
286	return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
287	    diff_context, ignore_whitespace, outfile);
288}
289
290const struct got_error *
291got_diff_blob_file_lines_changed(struct got_diffreg_result **result,
292    struct got_blob_object *blob1, FILE *f2, size_t size2)
293{
294	return diff_blob_file(result, blob1, NULL, f2, size2, NULL,
295	    0, 0, NULL);
296}
297
298const struct got_error *
299got_diff_blob_lines_changed(struct got_diffreg_result **result,
300    struct got_blob_object *blob1, struct got_blob_object *blob2)
301{
302	const struct got_error *err = NULL;
303
304	err = diff_blobs(NULL, NULL, result, blob1, blob2,
305	    NULL, NULL, 0, 0, 3, 0, NULL);
306	if (err) {
307		got_diffreg_result_free(*result);
308		*result = NULL;
309	}
310	return err;
311}
312
313static const struct got_error *
314diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
315    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
316{
317	const struct got_error *err;
318	struct got_blob_object  *blob = NULL;
319	struct got_object *obj = NULL;
320
321	err = got_object_open(&obj, repo, id);
322	if (err)
323		return err;
324
325	err = got_object_blob_open(&blob, repo, obj, 8192);
326	if (err)
327		goto done;
328	err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
329done:
330	got_object_close(obj);
331	if (blob)
332		got_object_blob_close(blob);
333	return err;
334}
335
336static const struct got_error *
337diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
338    const char *label1, const char *label2, mode_t mode1, mode_t mode2,
339    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
340{
341	const struct got_error *err;
342	struct got_object *obj1 = NULL;
343	struct got_object *obj2 = NULL;
344	struct got_blob_object *blob1 = NULL;
345	struct got_blob_object *blob2 = NULL;
346
347	err = got_object_open(&obj1, repo, id1);
348	if (err)
349		return err;
350	if (obj1->type != GOT_OBJ_TYPE_BLOB) {
351		err = got_error(GOT_ERR_OBJ_TYPE);
352		goto done;
353	}
354
355	err = got_object_open(&obj2, repo, id2);
356	if (err)
357		goto done;
358	if (obj2->type != GOT_OBJ_TYPE_BLOB) {
359		err = got_error(GOT_ERR_BAD_OBJ_DATA);
360		goto done;
361	}
362
363	err = got_object_blob_open(&blob1, repo, obj1, 8192);
364	if (err)
365		goto done;
366
367	err = got_object_blob_open(&blob2, repo, obj2, 8192);
368	if (err)
369		goto done;
370
371	err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
372	    repo);
373done:
374	if (obj1)
375		got_object_close(obj1);
376	if (obj2)
377		got_object_close(obj2);
378	if (blob1)
379		got_object_blob_close(blob1);
380	if (blob2)
381		got_object_blob_close(blob2);
382	return err;
383}
384
385static const struct got_error *
386diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
387    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
388{
389	const struct got_error *err;
390	struct got_blob_object  *blob = NULL;
391	struct got_object *obj = NULL;
392
393	err = got_object_open(&obj, repo, id);
394	if (err)
395		return err;
396
397	err = got_object_blob_open(&blob, repo, obj, 8192);
398	if (err)
399		goto done;
400	err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
401done:
402	got_object_close(obj);
403	if (blob)
404		got_object_blob_close(blob);
405	return err;
406}
407
408static const struct got_error *
409diff_added_tree(struct got_object_id *id, const char *label,
410    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
411    int diff_content)
412{
413	const struct got_error *err = NULL;
414	struct got_object *treeobj = NULL;
415	struct got_tree_object *tree = NULL;
416
417	err = got_object_open(&treeobj, repo, id);
418	if (err)
419		goto done;
420
421	if (treeobj->type != GOT_OBJ_TYPE_TREE) {
422		err = got_error(GOT_ERR_OBJ_TYPE);
423		goto done;
424	}
425
426	err = got_object_tree_open(&tree, repo, treeobj);
427	if (err)
428		goto done;
429
430	err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
431	    diff_content);
432done:
433	if (tree)
434		got_object_tree_close(tree);
435	if (treeobj)
436		got_object_close(treeobj);
437	return err;
438}
439
440static const struct got_error *
441diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
442    const char *label1, const char *label2, struct got_repository *repo,
443    got_diff_blob_cb cb, void *cb_arg, int diff_content)
444{
445	const struct got_error *err;
446	struct got_object *treeobj1 = NULL;
447	struct got_object *treeobj2 = NULL;
448	struct got_tree_object *tree1 = NULL;
449	struct got_tree_object *tree2 = NULL;
450
451	err = got_object_open(&treeobj1, repo, id1);
452	if (err)
453		goto done;
454
455	if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
456		err = got_error(GOT_ERR_OBJ_TYPE);
457		goto done;
458	}
459
460	err = got_object_open(&treeobj2, repo, id2);
461	if (err)
462		goto done;
463
464	if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
465		err = got_error(GOT_ERR_OBJ_TYPE);
466		goto done;
467	}
468
469	err = got_object_tree_open(&tree1, repo, treeobj1);
470	if (err)
471		goto done;
472
473	err = got_object_tree_open(&tree2, repo, treeobj2);
474	if (err)
475		goto done;
476
477	err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
478	    diff_content);
479
480done:
481	if (tree1)
482		got_object_tree_close(tree1);
483	if (tree2)
484		got_object_tree_close(tree2);
485	if (treeobj1)
486		got_object_close(treeobj1);
487	if (treeobj2)
488		got_object_close(treeobj2);
489	return err;
490}
491
492static const struct got_error *
493diff_deleted_tree(struct got_object_id *id, const char *label,
494    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
495    int diff_content)
496{
497	const struct got_error *err;
498	struct got_object *treeobj = NULL;
499	struct got_tree_object *tree = NULL;
500
501	err = got_object_open(&treeobj, repo, id);
502	if (err)
503		goto done;
504
505	if (treeobj->type != GOT_OBJ_TYPE_TREE) {
506		err = got_error(GOT_ERR_OBJ_TYPE);
507		goto done;
508	}
509
510	err = got_object_tree_open(&tree, repo, treeobj);
511	if (err)
512		goto done;
513
514	err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
515	    diff_content);
516done:
517	if (tree)
518		got_object_tree_close(tree);
519	if (treeobj)
520		got_object_close(treeobj);
521	return err;
522}
523
524static const struct got_error *
525diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
526    const char *label1, const char *label2, struct got_repository *repo,
527    got_diff_blob_cb cb, void *cb_arg)
528{
529	/* XXX TODO */
530	return NULL;
531}
532
533static const struct got_error *
534diff_entry_old_new(struct got_tree_entry *te1,
535    struct got_tree_entry *te2, const char *label1, const char *label2,
536    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
537    int diff_content)
538{
539	const struct got_error *err = NULL;
540	int id_match;
541
542	if (got_object_tree_entry_is_submodule(te1))
543		return NULL;
544
545	if (te2 == NULL) {
546		if (S_ISDIR(te1->mode))
547			err = diff_deleted_tree(&te1->id, label1, repo,
548			    cb, cb_arg, diff_content);
549		else {
550			if (diff_content)
551				err = diff_deleted_blob(&te1->id, label1,
552				    te1->mode, repo, cb, cb_arg);
553			else
554				err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
555				    label1, NULL, te1->mode, 0, repo);
556		}
557		return err;
558	} else if (got_object_tree_entry_is_submodule(te2))
559		return NULL;
560
561	id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
562	if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
563		if (!id_match)
564			return diff_modified_tree(&te1->id, &te2->id,
565			    label1, label2, repo, cb, cb_arg, diff_content);
566	} else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
567	    (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
568		if (!id_match ||
569		    ((te1->mode & (S_IFLNK | S_IXUSR))) !=
570		    (te2->mode & (S_IFLNK | S_IXUSR))) {
571			if (diff_content)
572				return diff_modified_blob(&te1->id, &te2->id,
573				    label1, label2, te1->mode, te2->mode,
574				    repo, cb, cb_arg);
575			else
576				return cb(cb_arg, NULL, NULL, &te1->id,
577				    &te2->id, label1, label2, te1->mode,
578				    te2->mode, repo);
579		}
580	}
581
582	if (id_match)
583		return NULL;
584
585	return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
586	    cb, cb_arg);
587}
588
589static const struct got_error *
590diff_entry_new_old(struct got_tree_entry *te2,
591    struct got_tree_entry *te1, const char *label2,
592    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
593    int diff_content)
594{
595	if (te1 != NULL) /* handled by diff_entry_old_new() */
596		return NULL;
597
598	if (got_object_tree_entry_is_submodule(te2))
599		return NULL;
600
601	if (S_ISDIR(te2->mode))
602		return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
603		    diff_content);
604
605	if (diff_content)
606		return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
607		    cb_arg);
608
609	return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
610	    te2->mode, repo);
611}
612
613const struct got_error *
614got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
615    struct got_blob_object *blob2, struct got_object_id *id1,
616    struct got_object_id *id2, const char *label1, const char *label2,
617    mode_t mode1, mode_t mode2, struct got_repository *repo)
618{
619	const struct got_error *err = NULL;
620	struct got_pathlist_head *paths = arg;
621	struct got_diff_changed_path *change = NULL;
622	char *path = NULL;
623
624	path = strdup(label2 ? label2 : label1);
625	if (path == NULL)
626		return got_error_from_errno("malloc");
627
628	change = malloc(sizeof(*change));
629	if (change == NULL) {
630		err = got_error_from_errno("malloc");
631		goto done;
632	}
633
634	change->status = GOT_STATUS_NO_CHANGE;
635	if (id1 == NULL)
636		change->status = GOT_STATUS_ADD;
637	else if (id2 == NULL)
638		change->status = GOT_STATUS_DELETE;
639	else {
640		if (got_object_id_cmp(id1, id2) != 0)
641			change->status = GOT_STATUS_MODIFY;
642		else if (mode1 != mode2)
643			change->status = GOT_STATUS_MODE_CHANGE;
644	}
645
646	err = got_pathlist_insert(NULL, paths, path, change);
647done:
648	if (err) {
649		free(path);
650		free(change);
651	}
652	return err;
653}
654
655const struct got_error *
656got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
657    const char *label1, const char *label2, struct got_repository *repo,
658    got_diff_blob_cb cb, void *cb_arg, int diff_content)
659{
660	const struct got_error *err = NULL;
661	struct got_tree_entry *te1 = NULL;
662	struct got_tree_entry *te2 = NULL;
663	char *l1 = NULL, *l2 = NULL;
664	int tidx1 = 0, tidx2 = 0;
665
666	if (tree1) {
667		te1 = got_object_tree_get_entry(tree1, 0);
668		if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
669		    te1->name) == -1)
670			return got_error_from_errno("asprintf");
671	}
672	if (tree2) {
673		te2 = got_object_tree_get_entry(tree2, 0);
674		if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
675		    te2->name) == -1)
676			return got_error_from_errno("asprintf");
677	}
678
679	do {
680		if (te1) {
681			struct got_tree_entry *te = NULL;
682			if (tree2)
683				te = got_object_tree_find_entry(tree2,
684				    te1->name);
685			if (te) {
686				free(l2);
687				l2 = NULL;
688				if (te && asprintf(&l2, "%s%s%s", label2,
689				    label2[0] ? "/" : "", te->name) == -1)
690					return
691					    got_error_from_errno("asprintf");
692			}
693			err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
694			    cb_arg, diff_content);
695			if (err)
696				break;
697		}
698
699		if (te2) {
700			struct got_tree_entry *te = NULL;
701			if (tree1)
702				te = got_object_tree_find_entry(tree1,
703				    te2->name);
704			free(l2);
705			if (te) {
706				if (asprintf(&l2, "%s%s%s", label2,
707				    label2[0] ? "/" : "", te->name) == -1)
708					return
709					    got_error_from_errno("asprintf");
710			} else {
711				if (asprintf(&l2, "%s%s%s", label2,
712				    label2[0] ? "/" : "", te2->name) == -1)
713					return
714					    got_error_from_errno("asprintf");
715			}
716			err = diff_entry_new_old(te2, te, l2, repo,
717			    cb, cb_arg, diff_content);
718			if (err)
719				break;
720		}
721
722		free(l1);
723		l1 = NULL;
724		if (te1) {
725			tidx1++;
726			te1 = got_object_tree_get_entry(tree1, tidx1);
727			if (te1 &&
728			    asprintf(&l1, "%s%s%s", label1,
729			    label1[0] ? "/" : "", te1->name) == -1)
730				return got_error_from_errno("asprintf");
731		}
732		free(l2);
733		l2 = NULL;
734		if (te2) {
735			tidx2++;
736			te2 = got_object_tree_get_entry(tree2, tidx2);
737			if (te2 &&
738			    asprintf(&l2, "%s%s%s", label2,
739			        label2[0] ? "/" : "", te2->name) == -1)
740				return got_error_from_errno("asprintf");
741		}
742	} while (te1 || te2);
743
744	return err;
745}
746
747const struct got_error *
748got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
749    struct got_object_id *id1, struct got_object_id *id2,
750    const char *label1, const char *label2, int diff_context,
751    int ignore_whitespace, struct got_repository *repo, FILE *outfile)
752{
753	const struct got_error *err;
754	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
755
756	if (id1 == NULL && id2 == NULL)
757		return got_error(GOT_ERR_NO_OBJ);
758
759	if (id1) {
760		err = got_object_open_as_blob(&blob1, repo, id1, 8192);
761		if (err)
762			goto done;
763	}
764	if (id2) {
765		err = got_object_open_as_blob(&blob2, repo, id2, 8192);
766		if (err)
767			goto done;
768	}
769	err = got_diff_blob(line_offsets, nlines, blob1, blob2,
770	    label1, label2, diff_context, ignore_whitespace, outfile);
771done:
772	if (blob1)
773		got_object_blob_close(blob1);
774	if (blob2)
775		got_object_blob_close(blob2);
776	return err;
777}
778
779const struct got_error *
780got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
781    struct got_object_id *id1, struct got_object_id *id2,
782    char *label1, char *label2, int diff_context, int ignore_whitespace,
783    struct got_repository *repo, FILE *outfile)
784{
785	const struct got_error *err;
786	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
787	struct got_diff_blob_output_unidiff_arg arg;
788	int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
789
790	if (id1 == NULL && id2 == NULL)
791		return got_error(GOT_ERR_NO_OBJ);
792
793	if (id1) {
794		err = got_object_open_as_tree(&tree1, repo, id1);
795		if (err)
796			goto done;
797	}
798	if (id2) {
799		err = got_object_open_as_tree(&tree2, repo, id2);
800		if (err)
801			goto done;
802	}
803	arg.diff_context = diff_context;
804	arg.ignore_whitespace = ignore_whitespace;
805	arg.outfile = outfile;
806	if (want_lineoffsets) {
807		arg.line_offsets = *line_offsets;
808		arg.nlines = *nlines;
809	} else {
810		arg.line_offsets = NULL;
811		arg.nlines = 0;
812	}
813	err = got_diff_tree(tree1, tree2, label1, label2, repo,
814	    got_diff_blob_output_unidiff, &arg, 1);
815
816	if (want_lineoffsets) {
817		*line_offsets = arg.line_offsets; /* was likely re-allocated */
818		*nlines = arg.nlines;
819	}
820done:
821	if (tree1)
822		got_object_tree_close(tree1);
823	if (tree2)
824		got_object_tree_close(tree2);
825	return err;
826}
827
828const struct got_error *
829got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
830    struct got_object_id *id1, struct got_object_id *id2,
831    int diff_context, int ignore_whitespace,
832    struct got_repository *repo, FILE *outfile)
833{
834	const struct got_error *err;
835	struct got_commit_object *commit1 = NULL, *commit2 = NULL;
836
837	if (id2 == NULL)
838		return got_error(GOT_ERR_NO_OBJ);
839
840	if (id1) {
841		err = got_object_open_as_commit(&commit1, repo, id1);
842		if (err)
843			goto done;
844	}
845
846	err = got_object_open_as_commit(&commit2, repo, id2);
847	if (err)
848		goto done;
849
850	err = got_diff_objects_as_trees(line_offsets, nlines,
851	    commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
852	    got_object_commit_get_tree_id(commit2), "", "", diff_context,
853	    ignore_whitespace, repo, outfile);
854done:
855	if (commit1)
856		got_object_commit_close(commit1);
857	if (commit2)
858		got_object_commit_close(commit2);
859	return err;
860}
861
862const struct got_error *
863got_diff_files(struct got_diffreg_result **resultp,
864    FILE *f1, const char *label1, FILE *f2, const char *label2,
865    int diff_context, int ignore_whitespace, FILE *outfile)
866{
867	const struct got_error *err = NULL;
868	struct got_diffreg_result *diffreg_result = NULL;
869
870	if (resultp)
871		*resultp = NULL;
872
873	if (outfile) {
874		fprintf(outfile, "file - %s\n",
875		    f1 == NULL ? "/dev/null" : label1);
876		fprintf(outfile, "file + %s\n",
877		    f2 == NULL ? "/dev/null" : label2);
878	}
879
880	err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
881	    ignore_whitespace);
882	if (err)
883		goto done;
884
885	if (outfile) {
886		err = got_diffreg_output(NULL, NULL, diffreg_result,
887		    f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
888		    diff_context, outfile);
889		if (err)
890			goto done;
891	}
892
893done:
894	if (resultp && err == NULL)
895		*resultp = diffreg_result;
896	else if (diffreg_result) {
897		const struct got_error *free_err;
898		free_err = got_diffreg_result_free(diffreg_result);
899		if (free_err && err == NULL)
900			err = free_err;
901	}
902
903	return err;
904}
905