commit df2871d297805326f5d359be74d6b309537f768b from: Stefan Sperling date: Thu Oct 18 09:45:20 2018 UTC make amount of diff context configurable at API level commit - 5c60c32a7d03db0967a09aee2994a92589d31a8e commit + df2871d297805326f5d359be74d6b309537f768b blob - 5cf3fa097e58978530f6814cb80c561bca32714e blob + 27bc2f2aa2ebdeb9408b5858a0639cb23c761177 --- got/got.c +++ got/got.c @@ -290,7 +290,7 @@ print_patch(struct got_commit_object *commit, struct g return err; } - err = got_diff_tree(tree1, tree2, "", "", repo, stdout); + err = got_diff_tree(tree1, tree2, "", "", 3, repo, stdout); if (tree1) got_object_tree_close(tree1); got_object_tree_close(tree2); @@ -639,15 +639,15 @@ cmd_diff(int argc, char *argv[]) switch (got_object_get_type(obj1)) { case GOT_OBJ_TYPE_BLOB: - error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL, + error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL, 3, repo, stdout); break; case GOT_OBJ_TYPE_TREE: - error = got_diff_objects_as_trees(obj1, obj2, "", "", repo, + error = got_diff_objects_as_trees(obj1, obj2, "", "", 3, repo, stdout); break; case GOT_OBJ_TYPE_COMMIT: - error = got_diff_objects_as_commits(obj1, obj2, repo, stdout); + error = got_diff_objects_as_commits(obj1, obj2, 3, repo, stdout); break; default: error = got_error(GOT_ERR_OBJ_TYPE); blob - c92aa2f05fa3e54c44c247060cc5cfb9aa5690cb blob + 2d8ae7f5341ad85ed33cb1c568a4692af5d57f25 --- include/got_diff.h +++ include/got_diff.h @@ -19,42 +19,47 @@ * to the provided output FILE. Two const char * diff header labels may * be provided which will be used to identify each blob in the diff output. * If a label is NULL, use the blob's SHA1 checksum instead. + * The number of context lines to show in the diff must be specified as well. */ const struct got_error *got_diff_blob(struct got_blob_object *, - struct got_blob_object *, const char *, const char *, FILE *); + struct got_blob_object *, const char *, const char *, int, FILE *); /* * Compute the differences between two trees and write unified diff text * to the provided output FILE. Two const char * diff header labels may * be provided which will be used to identify each blob in the diff output. * If a label is NULL, use the blob's SHA1 checksum instead. + * The number of context lines to show in the diff must be specified as well. */ const struct got_error *got_diff_tree(struct got_tree_object *, struct got_tree_object *, const char *label1, const char *label2, - struct got_repository *, FILE *); + int, struct got_repository *, FILE *); /* * Diff two objects, assuming both objects are blobs. Two const char * diff * header labels may be provided which will be used to identify each blob in * the diff output. If a label is NULL, use the blob's SHA1 checksum instead. + * The number of context lines to show in the diff must be specified as well. * Write unified diff text to the provided output FILE. */ const struct got_error *got_diff_objects_as_blobs(struct got_object *, - struct got_object *, const char *, const char *, struct got_repository *, - FILE *); + struct got_object *, const char *, const char *, int, + struct got_repository *, FILE *); /* * Diff two objects, assuming both objects are trees. Two const char * diff * header labels may be provided which will be used to identify each blob in * the trees. If a label is NULL, use the blob's SHA1 checksum instead. + * The number of context lines to show in diffs must be specified. * Write unified diff text to the provided output FILE. */ const struct got_error *got_diff_objects_as_trees(struct got_object *, - struct got_object *, char *, char *, struct got_repository *, FILE *); + struct got_object *, char *, char *, int, struct got_repository *, FILE *); /* * Diff two objects, assuming both objects are commits. + * The number of context lines to show in diffs must be specified. * Write unified diff text to the provided output FILE. */ const struct got_error *got_diff_objects_as_commits(struct got_object *, - struct got_object *, struct got_repository *, FILE *); + struct got_object *, int, struct got_repository *, FILE *); blob - c412692a0b16da6620136e1f5667f7aede401bd6 blob + 66dfd0373a32ff14bcf5f7b355f5b47d3c7339a9 --- lib/diff.c +++ lib/diff.c @@ -35,7 +35,7 @@ static const struct got_error * diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2, - const char *label1, const char *label2, FILE *outfile, + const char *label1, const char *label2, int diff_context, FILE *outfile, struct got_diff_changes *changes) { struct got_diff_state ds; @@ -98,7 +98,7 @@ diff_blobs(struct got_blob_object *blob1, struct got_b args.diff_format = D_UNIFIED; args.label[0] = label1 ? label1 : idstr1; args.label[1] = label2 ? label2 : idstr2; - args.diff_context = 3; + args.diff_context = diff_context; flags |= D_PROTOTYPE; if (label1 && strcmp(label1, idstr1) != 0) @@ -117,9 +117,10 @@ done: const struct got_error * got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2, - const char *label1, const char *label2, FILE *outfile) + const char *label1, const char *label2, int diff_context, FILE *outfile) { - return diff_blobs(blob1, blob2, label1, label2, outfile, NULL); + return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile, + NULL); } const struct got_error * @@ -133,7 +134,7 @@ got_diff_blob_lines_changed(struct got_diff_changes ** return got_error_from_errno(); SIMPLEQ_INIT(&(*changes)->entries); - err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes); + err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes); if (err) { got_diff_free_changes(*changes); *changes = NULL; @@ -169,7 +170,7 @@ match_entry_by_name(struct got_tree_entry *te1, struct static const struct got_error * diff_added_blob(struct got_object_id *id, const char *label, - struct got_repository *repo, FILE *outfile) + int diff_context, struct got_repository *repo, FILE *outfile) { const struct got_error *err; struct got_blob_object *blob = NULL; @@ -182,7 +183,7 @@ diff_added_blob(struct got_object_id *id, const char * err = got_object_blob_open(&blob, repo, obj, 8192); if (err) goto done; - err = got_diff_blob(NULL, blob, NULL, label, outfile); + err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile); done: got_object_close(obj); if (blob) @@ -192,8 +193,8 @@ done: static const struct got_error * diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2, - const char *label1, const char *label2, struct got_repository *repo, - FILE *outfile) + const char *label1, const char *label2, int diff_context, + struct got_repository *repo, FILE *outfile) { const struct got_error *err; struct got_object *obj1 = NULL; @@ -225,7 +226,8 @@ diff_modified_blob(struct got_object_id *id1, struct g if (err) goto done; - err = got_diff_blob(blob1, blob2, label1, label2, outfile); + err = got_diff_blob(blob1, blob2, label1, label2, diff_context, + outfile); done: if (obj1) @@ -241,7 +243,7 @@ done: static const struct got_error * diff_deleted_blob(struct got_object_id *id, const char *label, - struct got_repository *repo, FILE *outfile) + int diff_context, struct got_repository *repo, FILE *outfile) { const struct got_error *err; struct got_blob_object *blob = NULL; @@ -254,7 +256,7 @@ diff_deleted_blob(struct got_object_id *id, const char err = got_object_blob_open(&blob, repo, obj, 8192); if (err) goto done; - err = got_diff_blob(blob, NULL, label, NULL, outfile); + err = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile); done: got_object_close(obj); if (blob) @@ -264,7 +266,7 @@ done: static const struct got_error * diff_added_tree(struct got_object_id *id, const char *label, - struct got_repository *repo, FILE *outfile) + int diff_context, struct got_repository *repo, FILE *outfile) { const struct got_error *err = NULL; struct got_object *treeobj = NULL; @@ -283,7 +285,8 @@ diff_added_tree(struct got_object_id *id, const char * if (err) goto done; - err = got_diff_tree(NULL, tree, NULL, label, repo, outfile); + err = got_diff_tree(NULL, tree, NULL, label, diff_context, repo, + outfile); done: if (tree) @@ -295,8 +298,8 @@ done: static const struct got_error * diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2, - const char *label1, const char *label2, struct got_repository *repo, - FILE *outfile) + const char *label1, const char *label2, int diff_context, + struct got_repository *repo, FILE *outfile) { const struct got_error *err; struct got_object *treeobj1 = NULL; @@ -330,7 +333,8 @@ diff_modified_tree(struct got_object_id *id1, struct g if (err) goto done; - err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile); + err = got_diff_tree(tree1, tree2, label1, label2, diff_context, repo, + outfile); done: if (tree1) @@ -346,7 +350,7 @@ done: static const struct got_error * diff_deleted_tree(struct got_object_id *id, const char *label, - struct got_repository *repo, FILE *outfile) + int diff_context, struct got_repository *repo, FILE *outfile) { const struct got_error *err; struct got_object *treeobj = NULL; @@ -365,7 +369,8 @@ diff_deleted_tree(struct got_object_id *id, const char if (err) goto done; - err = got_diff_tree(tree, NULL, label, NULL, repo, outfile); + err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo, + outfile); done: if (tree) got_object_tree_close(tree); @@ -384,27 +389,29 @@ diff_kind_mismatch(struct got_object_id *id1, struct g static const struct got_error * diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2, - const char *label1, const char *label2, struct got_repository *repo, - FILE *outfile) + const char *label1, const char *label2, int diff_context, + struct got_repository *repo, FILE *outfile) { const struct got_error *err = NULL; if (te2 == NULL) { if (S_ISDIR(te1->mode)) - err = diff_deleted_tree(te1->id, label1, repo, outfile); + err = diff_deleted_tree(te1->id, label1, diff_context, + repo, outfile); else - err = diff_deleted_blob(te1->id, label1, repo, outfile); + err = diff_deleted_blob(te1->id, label1, diff_context, + repo, outfile); return err; } if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) { if (got_object_id_cmp(te1->id, te2->id) != 0) return diff_modified_tree(te1->id, te2->id, - label1, label2, repo, outfile); + label1, label2, diff_context, repo, outfile); } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) { if (got_object_id_cmp(te1->id, te2->id) != 0) return diff_modified_blob(te1->id, te2->id, - label1, label2, repo, outfile); + label1, label2, diff_context, repo, outfile); } if (got_object_id_cmp(te1->id, te2->id) == 0) @@ -415,21 +422,23 @@ diff_entry_old_new(struct got_tree_entry *te1, struct static const struct got_error * diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1, - const char *label2, struct got_repository *repo, FILE *outfile) + const char *label2, int diff_context, struct got_repository *repo, + FILE *outfile) { if (te1 != NULL) /* handled by diff_entry_old_new() */ return NULL; if (S_ISDIR(te2->mode)) - return diff_added_tree(te2->id, label2, repo, outfile); + return diff_added_tree(te2->id, label2, diff_context, repo, + outfile); - return diff_added_blob(te2->id, label2, repo, outfile); + return diff_added_blob(te2->id, label2, diff_context, repo, outfile); } const struct got_error * got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2, - const char *label1, const char *label2, struct got_repository *repo, - FILE *outfile) + const char *label1, const char *label2, int diff_context, + struct got_repository *repo, FILE *outfile) { const struct got_error *err = NULL; struct got_tree_entry *te1 = NULL; @@ -465,8 +474,8 @@ got_diff_tree(struct got_tree_object *tree1, struct go label2[0] ? "/" : "", te->name) == -1) return got_error_from_errno(); } - err = diff_entry_old_new(te1, te, l1, l2, repo, - outfile); + err = diff_entry_old_new(te1, te, l1, l2, diff_context, + repo, outfile); if (err) break; } @@ -475,7 +484,8 @@ got_diff_tree(struct got_tree_object *tree1, struct go struct got_tree_entry *te = NULL; if (tree1) te = match_entry_by_name(te2, tree1); - err = diff_entry_new_old(te2, te, l2, repo, outfile); + err = diff_entry_new_old(te2, te, l2, diff_context, + repo, outfile); if (err) break; } @@ -505,8 +515,8 @@ got_diff_tree(struct got_tree_object *tree1, struct go const struct got_error * got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2, - const char *label1, const char *label2, struct got_repository *repo, - FILE *outfile) + const char *label1, const char *label2, int diff_context, + struct got_repository *repo, FILE *outfile) { const struct got_error *err; struct got_blob_object *blob1 = NULL, *blob2 = NULL; @@ -524,7 +534,8 @@ got_diff_objects_as_blobs(struct got_object *obj1, str if (err) goto done; } - err = got_diff_blob(blob1, blob2, label1, label2, outfile); + err = got_diff_blob(blob1, blob2, label1, label2, diff_context, + outfile); done: if (blob1) got_object_blob_close(blob1); @@ -535,7 +546,8 @@ done: const struct got_error * got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2, - char *label1, char *label2, struct got_repository *repo, FILE *outfile) + char *label1, char *label2, int diff_context, struct got_repository *repo, + FILE *outfile) { const struct got_error *err; struct got_tree_object *tree1 = NULL, *tree2 = NULL; @@ -553,7 +565,8 @@ got_diff_objects_as_trees(struct got_object *obj1, str if (err) goto done; } - err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile); + err = got_diff_tree(tree1, tree2, label1, label2, diff_context, + repo, outfile); done: if (tree1) got_object_tree_close(tree1); @@ -574,7 +587,7 @@ get_datestr(time_t *time, char *datebuf) const struct got_error * got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2, - struct got_repository *repo, FILE *outfile) + int diff_context, struct got_repository *repo, FILE *outfile) { const struct got_error *err; struct got_commit_object *commit1 = NULL, *commit2 = NULL; @@ -630,8 +643,8 @@ got_diff_objects_as_commits(struct got_object *obj1, s goto done; } - err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", repo, - outfile); + err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", + diff_context, repo, outfile); done: if (tree_obj1) got_object_close(tree_obj1); blob - 7d05f1a2cc249d41a0e063d1cdc0a1c2efa80e2d blob + faf7e0d4c42ae9b366451dd0c514260ed7b37585 --- regress/repository/repository_test.c +++ regress/repository/repository_test.c @@ -355,7 +355,7 @@ repo_diff_blob(const char *repo_path) outfile = got_opentemp(); if (outfile == NULL) return 0; - got_diff_blob(blob1, blob2, NULL, NULL, outfile); + got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile); rewind(outfile); i = 0; while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) { @@ -428,7 +428,7 @@ repo_diff_tree(const char *repo_path) } else outfile = stdout; test_printf("\n"); - got_diff_tree(tree1, tree2, "", "", repo, outfile); + got_diff_tree(tree1, tree2, "", "", 3, repo, outfile); test_printf("\n"); got_object_tree_close(tree1); blob - faaa00b87b84667bb9b053f2e40a63d9b2cb7eac blob + d57ef707fcf7b7f170f55f83b1be9883fb3f3a6e --- tog/tog.c +++ tog/tog.c @@ -1625,14 +1625,14 @@ open_diff_view(struct tog_view *view, struct got_objec switch (got_object_get_type(obj1 ? obj1 : obj2)) { case GOT_OBJ_TYPE_BLOB: - err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL, + err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL, 3, repo, f); break; case GOT_OBJ_TYPE_TREE: - err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f); + err = got_diff_objects_as_trees(obj1, obj2, "", "", 3, repo, f); break; case GOT_OBJ_TYPE_COMMIT: - err = got_diff_objects_as_commits(obj1, obj2, repo, f); + err = got_diff_objects_as_commits(obj1, obj2, 3, repo, f); break; default: return got_error(GOT_ERR_OBJ_TYPE);