handle_revision_arg: record paths for pending objects

If the revision parser sees an argument like tree:path, we parse it down to the correct blob (or tree), but throw away the "path" portion. Let's ask get_sha1_with_context() to record it, and pass it along in the pending array. This will let programs like git-diff which rely on the revision-parser show more accurate paths. Note that the implementation is a little tricky; we have to make sure we free oc.path in all code paths. For handle_dotdot(), we can piggy-back on the existing cleanup-wrapper pattern. The real work happens in handle_dotdot_1(), but the handle_dotdot() wrapper makes sure that the path is freed no matter how we exit the function (and for that reason we make sure that the object_context struct is zero'd, so if we fail to even get to the get_sha1_with_context() call, we just end up calling free(NULL)). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 19, 2017 at 08:55 UTC 18f1ad76392e60cfaf68c3f1e49a4c91bd415d0d
1 file changed +21 -11
revision.c
+21 -11
@@ -1443,16 +1443,17 @@ static int dotdot_missing(const char *arg, char *dotdot,
1443
1444 static int handle_dotdot_1(const char *arg, char *dotdot,
1445 struct rev_info *revs, int flags,
1446 - int cant_be_filename)
1446 + int cant_be_filename,
1447 + struct object_context *a_oc,
1448 + struct object_context *b_oc)
1449 {
1450 const char *a_name, *b_name;
1451 struct object_id a_oid, b_oid;
1452 struct object *a_obj, *b_obj;
1451 - struct object_context a_oc, b_oc;
1453 unsigned int a_flags, b_flags;
1454 int symmetric = 0;
1455 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1455 - unsigned int oc_flags = GET_SHA1_COMMITTISH;
1456 + unsigned int oc_flags = GET_SHA1_COMMITTISH | GET_SHA1_RECORD_PATH;
1457
1458 a_name = arg;
1459 if (!*a_name)
@@ -1466,8 +1467,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
1467 if (!*b_name)
1468 b_name = "HEAD";
1469
1469 - if (get_sha1_with_context(a_name, oc_flags, a_oid.hash, &a_oc) ||
1470 - get_sha1_with_context(b_name, oc_flags, b_oid.hash, &b_oc))
1470 + if (get_sha1_with_context(a_name, oc_flags, a_oid.hash, a_oc) ||
1471 + get_sha1_with_context(b_name, oc_flags, b_oid.hash, b_oc))
1472 return -1;
1473
1474 if (!cant_be_filename) {
@@ -1509,8 +1510,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
1510 b_obj->flags |= b_flags;
1511 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1512 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1512 - add_pending_object_with_mode(revs, a_obj, a_name, a_oc.mode);
1513 - add_pending_object_with_mode(revs, b_obj, b_name, b_oc.mode);
1513 + add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1514 + add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1515 return 0;
1516 }
1517
@@ -1518,16 +1519,24 @@ static int handle_dotdot(const char *arg,
1519 struct rev_info *revs, int flags,
1520 int cant_be_filename)
1521 {
1522 + struct object_context a_oc, b_oc;
1523 char *dotdot = strstr(arg, "..");
1524 int ret;
1525
1526 if (!dotdot)
1527 return -1;
1528
1529 + memset(&a_oc, 0, sizeof(a_oc));
1530 + memset(&b_oc, 0, sizeof(b_oc));
1531 +
1532 *dotdot = '\0';
1528 - ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename);
1533 + ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1534 + &a_oc, &b_oc);
1535 *dotdot = '.';
1536
1537 + free(a_oc.path);
1538 + free(b_oc.path);
1539 +
1540 return ret;
1541 }
1542
@@ -1540,7 +1549,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
1549 int local_flags;
1550 const char *arg = arg_;
1551 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1543 - unsigned get_sha1_flags = 0;
1552 + unsigned get_sha1_flags = GET_SHA1_RECORD_PATH;
1553
1554 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1555
@@ -1591,7 +1600,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
1600 }
1601
1602 if (revarg_opt & REVARG_COMMITTISH)
1594 - get_sha1_flags = GET_SHA1_COMMITTISH;
1603 + get_sha1_flags |= GET_SHA1_COMMITTISH;
1604
1605 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1606 return revs->ignore_missing ? 0 : -1;
@@ -1599,7 +1608,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
1608 verify_non_filename(revs->prefix, arg);
1609 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1610 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1602 - add_pending_object_with_mode(revs, object, arg, oc.mode);
1611 + add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1612 + free(oc.path);
1613 return 0;
1614 }
1615