display file paths in diffs instead of blob SHA1s
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
diff --git a/got/got.c b/got/got.c
index be58cf3..5467480 100644
--- a/got/got.c
+++ b/got/got.c
@@ -290,7 +290,7 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
return err;
}
- err = got_diff_tree(tree1, tree2, repo, stdout);
+ err = got_diff_tree(tree1, tree2, "", "", repo, stdout);
if (tree1)
got_object_tree_close(tree1);
got_object_tree_close(tree2);
@@ -717,10 +717,12 @@ 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, repo, stdout);
+ error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
+ repo, stdout);
break;
case GOT_OBJ_TYPE_TREE:
- error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
+ error = got_diff_objects_as_trees(obj1, obj2, "", "", repo,
+ stdout);
break;
case GOT_OBJ_TYPE_COMMIT:
error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
diff --git a/include/got_diff.h b/include/got_diff.h
index 0f04bd8..c92aa2f 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -25,24 +25,32 @@ const struct got_error *got_diff_blob(struct got_blob_object *,
/*
* Compute the differences between two trees and write unified diff text
- * to the provided output FILE.
+ * 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.
*/
const struct got_error *got_diff_tree(struct got_tree_object *,
- struct got_tree_object *, struct got_repository *, FILE *);
+ struct got_tree_object *, const char *label1, const char *label2,
+ struct got_repository *, FILE *);
/*
- * Diff two objects, assuming both objects are blobs.
+ * 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.
* Write unified diff text to the provided output FILE.
*/
const struct got_error *got_diff_objects_as_blobs(struct got_object *,
- struct got_object *, struct got_repository *, FILE *);
+ struct got_object *, const char *, const char *, struct got_repository *,
+ FILE *);
/*
- * Diff two objects, assuming both objects are trees.
+ * 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.
* Write unified diff text to the provided output FILE.
*/
const struct got_error *got_diff_objects_as_trees(struct got_object *,
- struct got_object *, struct got_repository *, FILE *);
+ struct got_object *, char *, char *, struct got_repository *, FILE *);
/*
* Diff two objects, assuming both objects are commits.
diff --git a/lib/diff.c b/lib/diff.c
index 2beea41..58ae938 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -163,8 +163,8 @@ match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
}
static const struct got_error *
-diff_added_blob(struct got_object_id *id, struct got_repository *repo,
- FILE *outfile)
+diff_added_blob(struct got_object_id *id, const char *label,
+ struct got_repository *repo, FILE *outfile)
{
const struct got_error *err;
struct got_blob_object *blob = NULL;
@@ -177,7 +177,7 @@ diff_added_blob(struct got_object_id *id, struct got_repository *repo,
err = got_object_blob_open(&blob, repo, obj, 8192);
if (err)
goto done;
- err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
+ err = got_diff_blob(NULL, blob, NULL, label, outfile);
done:
got_object_close(obj);
if (blob)
@@ -187,7 +187,8 @@ done:
static const struct got_error *
diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
- struct got_repository *repo, FILE *outfile)
+ const char *label1, const char *label2, struct got_repository *repo,
+ FILE *outfile)
{
const struct got_error *err;
struct got_object *obj1 = NULL;
@@ -219,7 +220,7 @@ diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
if (err)
goto done;
- err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
+ err = got_diff_blob(blob1, blob2, label1, label2, outfile);
done:
if (obj1)
@@ -234,8 +235,8 @@ done:
}
static const struct got_error *
-diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
- FILE *outfile)
+diff_deleted_blob(struct got_object_id *id, const char *label,
+ struct got_repository *repo, FILE *outfile)
{
const struct got_error *err;
struct got_blob_object *blob = NULL;
@@ -248,7 +249,7 @@ diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
err = got_object_blob_open(&blob, repo, obj, 8192);
if (err)
goto done;
- err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
+ err = got_diff_blob(blob, NULL, label, NULL, outfile);
done:
got_object_close(obj);
if (blob)
@@ -257,8 +258,8 @@ done:
}
static const struct got_error *
-diff_added_tree(struct got_object_id *id, struct got_repository *repo,
- FILE *outfile)
+diff_added_tree(struct got_object_id *id, const char *label,
+ struct got_repository *repo, FILE *outfile)
{
const struct got_error *err = NULL;
struct got_object *treeobj = NULL;
@@ -277,7 +278,7 @@ diff_added_tree(struct got_object_id *id, struct got_repository *repo,
if (err)
goto done;
- err = got_diff_tree(NULL, tree, repo, outfile);
+ err = got_diff_tree(NULL, tree, NULL, label, repo, outfile);
done:
if (tree)
@@ -289,9 +290,10 @@ done:
static const struct got_error *
diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
- struct got_repository *repo, FILE *outfile)
+ const char *label1, const char *label2, struct got_repository *repo,
+ FILE *outfile)
{
- const struct got_error *err = NULL;
+ const struct got_error *err;
struct got_object *treeobj1 = NULL;
struct got_object *treeobj2 = NULL;
struct got_tree_object *tree1 = NULL;
@@ -323,7 +325,7 @@ diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
if (err)
goto done;
- err = got_diff_tree(tree1, tree2, repo, outfile);
+ err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
done:
if (tree1)
@@ -338,9 +340,10 @@ done:
}
static const struct got_error *
-diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
+diff_deleted_tree(struct got_object_id *id, const char *label,
+ struct got_repository *repo, FILE *outfile)
{
- const struct got_error *err = NULL;
+ const struct got_error *err;
struct got_object *treeobj = NULL;
struct got_tree_object *tree = NULL;
@@ -357,8 +360,7 @@ diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *o
if (err)
goto done;
- err = got_diff_tree(tree, NULL, repo, outfile);
-
+ err = got_diff_tree(tree, NULL, label, NULL, repo, outfile);
done:
if (tree)
got_object_tree_close(tree);
@@ -369,90 +371,128 @@ done:
static const struct got_error *
diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
- FILE *outfile)
+ const char *label1, const char *label2, FILE *outfile)
{
/* XXX TODO */
return NULL;
}
static const struct got_error *
-diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
- struct got_repository *repo, FILE *outfile)
+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)
{
- struct got_tree_entry *te2 = NULL;
+ const struct got_error *err = NULL;
- if (tree2)
- te2 = match_entry_by_name(te1, tree2);
if (te2 == NULL) {
if (S_ISDIR(te1->mode))
- return diff_deleted_tree(te1->id, repo, outfile);
- return diff_deleted_blob(te1->id, repo, outfile);
+ err = diff_deleted_tree(te1->id, label1, repo, outfile);
+ else
+ err = diff_deleted_blob(te1->id, label1, 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, repo,
- outfile);
+ return diff_modified_tree(te1->id, te2->id,
+ label1, label2, 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, repo,
- outfile);
+ return diff_modified_blob(te1->id, te2->id,
+ label1, label2, repo, outfile);
}
- return diff_kind_mismatch(te1->id, te2->id, outfile);
+ if (got_object_id_cmp(te1->id, te2->id) == 0)
+ return NULL;
+
+ return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
}
static const struct got_error *
-diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
- struct got_repository *repo, FILE *outfile)
+diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
+ const char *label2, struct got_repository *repo, FILE *outfile)
{
- if (tree1) {
- struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
- if (te1 != NULL) /* handled by diff_entry_old_new() */
- return NULL;
- }
+ if (te1 != NULL) /* handled by diff_entry_old_new() */
+ return NULL;
if (S_ISDIR(te2->mode))
- return diff_added_tree(te2->id, repo, outfile);
- return diff_added_blob(te2->id, repo, outfile);
+ return diff_added_tree(te2->id, label2, repo, outfile);
+
+ return diff_added_blob(te2->id, label2, repo, outfile);
}
const struct got_error *
got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
- struct got_repository *repo, FILE *outfile)
+ const char *label1, const char *label2, struct got_repository *repo,
+ FILE *outfile)
{
const struct got_error *err = NULL;
struct got_tree_entry *te1 = NULL;
struct got_tree_entry *te2 = NULL;
+ char *l1 = NULL, *l2 = NULL;
if (tree1) {
const struct got_tree_entries *entries;
entries = got_object_tree_get_entries(tree1);
te1 = SIMPLEQ_FIRST(&entries->head);
+ if (asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
+ te1->name) == -1)
+ return got_error_from_errno();
}
if (tree2) {
const struct got_tree_entries *entries;
entries = got_object_tree_get_entries(tree2);
te2 = SIMPLEQ_FIRST(&entries->head);
+ if (asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
+ te2->name) == -1)
+ return got_error_from_errno();
}
do {
if (te1) {
- err = diff_entry_old_new(te1, tree2, repo, outfile);
+ struct got_tree_entry *te = NULL;
+ if (tree2)
+ te = match_entry_by_name(te1, tree2);
+ if (te) {
+ free(l2);
+ l2 = NULL;
+ if (te && asprintf(&l2, "%s%s%s", label2,
+ label2[0] ? "/" : "", te->name) == -1)
+ return got_error_from_errno();
+ }
+ err = diff_entry_old_new(te1, te, l1, l2, repo,
+ outfile);
if (err)
break;
}
if (te2) {
- err = diff_entry_new_old(te2, tree1, repo, outfile);
+ 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);
if (err)
break;
}
- if (te1)
+ free(l1);
+ l1 = NULL;
+ if (te1) {
te1 = SIMPLEQ_NEXT(te1, entry);
- if (te2)
+ if (te1 &&
+ asprintf(&l1, "%s%s%s", label1,
+ label1[0] ? "/" : "", te1->name) == -1)
+ return got_error_from_errno();
+ }
+ free(l2);
+ l2 = NULL;
+ if (te2) {
te2 = SIMPLEQ_NEXT(te2, entry);
+ if (te2 &&
+ asprintf(&l2, "%s%s%s", label2,
+ label2[0] ? "/" : "", te2->name) == -1)
+ return got_error_from_errno();
+ }
} while (te1 || te2);
return err;
@@ -460,7 +500,8 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
const struct got_error *
got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
- struct got_repository *repo, FILE *outfile)
+ const char *label1, const char *label2, struct got_repository *repo,
+ FILE *outfile)
{
const struct got_error *err;
struct got_blob_object *blob1 = NULL, *blob2 = NULL;
@@ -478,7 +519,7 @@ got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
if (err)
goto done;
}
- err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
+ err = got_diff_blob(blob1, blob2, label1, label2, outfile);
done:
if (blob1)
got_object_blob_close(blob1);
@@ -489,7 +530,7 @@ done:
const struct got_error *
got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
- struct got_repository *repo, FILE *outfile)
+ char *label1, char *label2, struct got_repository *repo, FILE *outfile)
{
const struct got_error *err;
struct got_tree_object *tree1 = NULL, *tree2 = NULL;
@@ -507,7 +548,7 @@ got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
if (err)
goto done;
}
- err = got_diff_tree(tree1, tree2, repo, outfile);
+ err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
done:
if (tree1)
got_object_tree_close(tree1);
@@ -582,7 +623,8 @@ got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
goto done;
}
- err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
+ err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", repo,
+ outfile);
done:
if (tree_obj1)
got_object_close(tree_obj1);
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index d5fe6d3..ed18000 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -427,7 +427,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, "", "", repo, outfile);
test_printf("\n");
got_object_tree_close(tree1);
diff --git a/tog/tog.c b/tog/tog.c
index b06d551..d7c5c68 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1590,10 +1590,11 @@ open_diff_view(struct tog_view *view, struct got_object *obj1,
switch (got_object_get_type(obj1 ? obj1 : obj2)) {
case GOT_OBJ_TYPE_BLOB:
- err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
+ err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
+ 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, "", "", repo, f);
break;
case GOT_OBJ_TYPE_COMMIT:
err = got_diff_objects_as_commits(obj1, obj2, repo, f);