sha1-name.c: remove implicit dependency on the_index

This kills the_index dependency in get_oid_with_context() but for get_oid() and friends, they still assume the_repository (which also means the_index). Unfortunately the widespread use of get_oid() will make it hard to make the conversion now. We probably will add repo_get_oid() at some point and limit the use of get_oid() in builtin/ instead of forcing all get_oid() call sites to carry struct repository. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 12, 2019 at 09:13 UTC 3a7a698e93e1031c322ab20c0e336e205514c058
8 files changed +50 -33
builtin/cat-file.c
+4 -2
@@ -73,7 +73,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
73 if (unknown_type)
74 flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
75
76 - if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
76 + if (get_oid_with_context(the_repository, obj_name,
77 + GET_OID_RECORD_PATH,
78 &oid, &obj_context))
79 die("Not a valid object name %s", obj_name);
80
@@ -382,7 +383,8 @@ static void batch_one_object(const char *obj_name,
383 int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
384 enum follow_symlinks_result result;
385
385 - result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
386 + result = get_oid_with_context(the_repository, obj_name,
387 + flags, &data->oid, &ctx);
388 if (result != FOUND) {
389 switch (result) {
390 case MISSING_OBJECT:
builtin/grep.c
+2 -1
@@ -1021,7 +1021,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1021 break;
1022 }
1023
1024 - if (get_oid_with_context(arg, GET_OID_RECORD_PATH,
1024 + if (get_oid_with_context(the_repository, arg,
1025 + GET_OID_RECORD_PATH,
1026 &oid, &oc)) {
1027 if (seen_dashdash)
1028 die(_("unable to resolve revision: %s"), arg);
builtin/log.c
+2 -1
@@ -508,7 +508,8 @@ static int show_blob_object(const struct object_id *oid, struct rev_info *rev, c
508 !rev->diffopt.flags.allow_textconv)
509 return stream_blob_to_fd(1, oid, NULL, 0);
510
511 - if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
511 + if (get_oid_with_context(the_repository, obj_name,
512 + GET_OID_RECORD_PATH,
513 &oidc, &obj_context))
514 die(_("Not a valid object name %s"), obj_name);
515 if (!obj_context.path ||
builtin/rev-parse.c
+2 -1
@@ -933,7 +933,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
933 name++;
934 type = REVERSED;
935 }
936 - if (!get_oid_with_context(name, flags, &oid, &unused)) {
936 + if (!get_oid_with_context(the_repository, name,
937 + flags, &oid, &unused)) {
938 if (verify)
939 revs_count++;
940 else
cache.h
+3 -1
@@ -1328,7 +1328,9 @@ extern int get_oid_tree(const char *str, struct object_id *oid);
1328 extern int get_oid_treeish(const char *str, struct object_id *oid);
1329 extern int get_oid_blob(const char *str, struct object_id *oid);
1330 extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
1331 -extern int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc);
1331 +extern int get_oid_with_context(struct repository *repo, const char *str,
1332 + unsigned flags, struct object_id *oid,
1333 + struct object_context *oc);
1334
1335
1336 typedef int each_abbrev_fn(const struct object_id *oid, void *);
list-objects-filter-options.c
+1 -1
@@ -71,7 +71,7 @@ static int gently_parse_list_objects_filter(
71 * command, but DO NOT complain if we don't have the blob or
72 * ref locally.
73 */
74 - if (!get_oid_with_context(v0, GET_OID_BLOB,
74 + if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
75 &sparse_oid, &oc))
76 filter_options->sparse_oid_value = oiddup(&sparse_oid);
77 filter_options->choice = LOFC_SPARSE_OID;
revision.c
+4 -4
@@ -1589,8 +1589,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
1589 if (!*b_name)
1590 b_name = "HEAD";
1591
1592 - if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1593 - get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
1592 + if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1593 + get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1594 return -1;
1595
1596 if (!cant_be_filename) {
@@ -1724,7 +1724,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
1724 if (revarg_opt & REVARG_COMMITTISH)
1725 get_sha1_flags |= GET_OID_COMMITTISH;
1726
1727 - if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
1727 + if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
1728 return revs->ignore_missing ? 0 : -1;
1729 if (!cant_be_filename)
1730 verify_non_filename(revs->prefix, arg);
@@ -2453,7 +2453,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
2453 struct object_id oid;
2454 struct object *object;
2455 struct object_context oc;
2456 - if (get_oid_with_context(revs->def, 0, &oid, &oc))
2456 + if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2457 diagnose_missing_default(revs->def);
2458 object = get_reference(revs, revs->def, &oid, 0);
2459 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
sha1-name.c
+32 -22
@@ -1513,7 +1513,7 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1513 int get_oid(const char *name, struct object_id *oid)
1514 {
1515 struct object_context unused;
1516 - return get_oid_with_context(name, 0, oid, &unused);
1516 + return get_oid_with_context(the_repository, name, 0, oid, &unused);
1517 }
1518
1519
@@ -1530,35 +1530,40 @@ int get_oid(const char *name, struct object_id *oid)
1530 int get_oid_committish(const char *name, struct object_id *oid)
1531 {
1532 struct object_context unused;
1533 - return get_oid_with_context(name, GET_OID_COMMITTISH,
1533 + return get_oid_with_context(the_repository,
1534 + name, GET_OID_COMMITTISH,
1535 oid, &unused);
1536 }
1537
1538 int get_oid_treeish(const char *name, struct object_id *oid)
1539 {
1540 struct object_context unused;
1540 - return get_oid_with_context(name, GET_OID_TREEISH,
1541 + return get_oid_with_context(the_repository,
1542 + name, GET_OID_TREEISH,
1543 oid, &unused);
1544 }
1545
1546 int get_oid_commit(const char *name, struct object_id *oid)
1547 {
1548 struct object_context unused;
1547 - return get_oid_with_context(name, GET_OID_COMMIT,
1549 + return get_oid_with_context(the_repository,
1550 + name, GET_OID_COMMIT,
1551 oid, &unused);
1552 }
1553
1554 int get_oid_tree(const char *name, struct object_id *oid)
1555 {
1556 struct object_context unused;
1554 - return get_oid_with_context(name, GET_OID_TREE,
1557 + return get_oid_with_context(the_repository,
1558 + name, GET_OID_TREE,
1559 oid, &unused);
1560 }
1561
1562 int get_oid_blob(const char *name, struct object_id *oid)
1563 {
1564 struct object_context unused;
1561 - return get_oid_with_context(name, GET_OID_BLOB,
1565 + return get_oid_with_context(the_repository,
1566 + name, GET_OID_BLOB,
1567 oid, &unused);
1568 }
1569
@@ -1597,7 +1602,8 @@ static void diagnose_invalid_oid_path(const char *prefix,
1602 }
1603
1604 /* Must be called only when :stage:filename doesn't exist. */
1600 -static void diagnose_invalid_index_path(int stage,
1605 +static void diagnose_invalid_index_path(struct index_state *istate,
1606 + int stage,
1607 const char *prefix,
1608 const char *filename)
1609 {
@@ -1610,11 +1616,11 @@ static void diagnose_invalid_index_path(int stage,
1616 prefix = "";
1617
1618 /* Wrong stage number? */
1613 - pos = cache_name_pos(filename, namelen);
1619 + pos = index_name_pos(istate, filename, namelen);
1620 if (pos < 0)
1621 pos = -pos - 1;
1616 - if (pos < active_nr) {
1617 - ce = active_cache[pos];
1622 + if (pos < istate->cache_nr) {
1623 + ce = istate->cache[pos];
1624 if (ce_namelen(ce) == namelen &&
1625 !memcmp(ce->name, filename, namelen))
1626 die("Path '%s' is in the index, but not at stage %d.\n"
@@ -1626,11 +1632,11 @@ static void diagnose_invalid_index_path(int stage,
1632 /* Confusion between relative and absolute filenames? */
1633 strbuf_addstr(&fullname, prefix);
1634 strbuf_addstr(&fullname, filename);
1629 - pos = cache_name_pos(fullname.buf, fullname.len);
1635 + pos = index_name_pos(istate, fullname.buf, fullname.len);
1636 if (pos < 0)
1637 pos = -pos - 1;
1632 - if (pos < active_nr) {
1633 - ce = active_cache[pos];
1638 + if (pos < istate->cache_nr) {
1639 + ce = istate->cache[pos];
1640 if (ce_namelen(ce) == fullname.len &&
1641 !memcmp(ce->name, fullname.buf, fullname.len))
1642 die("Path '%s' is in the index, but not '%s'.\n"
@@ -1664,7 +1670,8 @@ static char *resolve_relative_path(const char *rel)
1670 rel);
1671 }
1672
1667 -static int get_oid_with_context_1(const char *name,
1673 +static int get_oid_with_context_1(struct repository *repo,
1674 + const char *name,
1675 unsigned flags,
1676 const char *prefix,
1677 struct object_id *oid,
@@ -1723,13 +1730,13 @@ static int get_oid_with_context_1(const char *name,
1730 if (flags & GET_OID_RECORD_PATH)
1731 oc->path = xstrdup(cp);
1732
1726 - if (!the_index.cache)
1733 + if (!repo->index->cache)
1734 repo_read_index(the_repository);
1728 - pos = index_name_pos(&the_index, cp, namelen);
1735 + pos = index_name_pos(repo->index, cp, namelen);
1736 if (pos < 0)
1737 pos = -pos - 1;
1731 - while (pos < active_nr) {
1732 - ce = active_cache[pos];
1738 + while (pos < repo->index->cache_nr) {
1739 + ce = repo->index->cache[pos];
1740 if (ce_namelen(ce) != namelen ||
1741 memcmp(ce->name, cp, namelen))
1742 break;
@@ -1742,7 +1749,7 @@ static int get_oid_with_context_1(const char *name,
1749 pos++;
1750 }
1751 if (only_to_die && name[1] && name[1] != '/')
1745 - diagnose_invalid_index_path(stage, prefix, cp);
1752 + diagnose_invalid_index_path(repo->index, stage, prefix, cp);
1753 free(new_path);
1754 return -1;
1755 }
@@ -1807,12 +1814,15 @@ void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
1814 {
1815 struct object_context oc;
1816 struct object_id oid;
1810 - get_oid_with_context_1(name, GET_OID_ONLY_TO_DIE, prefix, &oid, &oc);
1817 + get_oid_with_context_1(the_repository, name, GET_OID_ONLY_TO_DIE,
1818 + prefix, &oid, &oc);
1819 }
1820
1813 -int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc)
1821 +int get_oid_with_context(struct repository *repo, const char *str,
1822 + unsigned flags, struct object_id *oid,
1823 + struct object_context *oc)
1824 {
1825 if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
1826 BUG("incompatible flags for get_sha1_with_context");
1817 - return get_oid_with_context_1(str, flags, NULL, oid, oc);
1827 + return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);
1828 }