Skip to content

Commit 18f1ad7

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 101dd4d commit 18f1ad7

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

revision.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,16 +1443,17 @@ static int dotdot_missing(const char *arg, char *dotdot,
14431443

14441444
static int handle_dotdot_1(const char *arg, char *dotdot,
14451445
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)
14471449
{
14481450
const char *a_name, *b_name;
14491451
struct object_id a_oid, b_oid;
14501452
struct object *a_obj, *b_obj;
1451-
struct object_context a_oc, b_oc;
14521453
unsigned int a_flags, b_flags;
14531454
int symmetric = 0;
14541455
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;
14561457

14571458
a_name = arg;
14581459
if (!*a_name)
@@ -1466,8 +1467,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
14661467
if (!*b_name)
14671468
b_name = "HEAD";
14681469

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))
14711472
return -1;
14721473

14731474
if (!cant_be_filename) {
@@ -1509,25 +1510,33 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
15091510
b_obj->flags |= b_flags;
15101511
add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
15111512
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);
15141515
return 0;
15151516
}
15161517

15171518
static int handle_dotdot(const char *arg,
15181519
struct rev_info *revs, int flags,
15191520
int cant_be_filename)
15201521
{
1522+
struct object_context a_oc, b_oc;
15211523
char *dotdot = strstr(arg, "..");
15221524
int ret;
15231525

15241526
if (!dotdot)
15251527
return -1;
15261528

1529+
memset(&a_oc, 0, sizeof(a_oc));
1530+
memset(&b_oc, 0, sizeof(b_oc));
1531+
15271532
*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);
15291535
*dotdot = '.';
15301536

1537+
free(a_oc.path);
1538+
free(b_oc.path);
1539+
15311540
return ret;
15321541
}
15331542

@@ -1540,7 +1549,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
15401549
int local_flags;
15411550
const char *arg = arg_;
15421551
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;
15441553

15451554
flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
15461555

@@ -1591,15 +1600,16 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
15911600
}
15921601

15931602
if (revarg_opt & REVARG_COMMITTISH)
1594-
get_sha1_flags = GET_SHA1_COMMITTISH;
1603+
get_sha1_flags |= GET_SHA1_COMMITTISH;
15951604

15961605
if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
15971606
return revs->ignore_missing ? 0 : -1;
15981607
if (!cant_be_filename)
15991608
verify_non_filename(revs->prefix, arg);
16001609
object = get_reference(revs, arg, sha1, flags ^ local_flags);
16011610
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);
16031613
return 0;
16041614
}
16051615

0 commit comments

Comments
 (0)