From ee394bd376e833d8e9e38f81c57f6b4a370e8a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:36:50 +0700 Subject: refs.c: use is_dir_sep() in resolve_gitlink_ref() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "submodule" argument in this function is a path, which can have either '/' or '\\' as a separator. Use is_dir_sep() to support both. Noticed-by: Johannes Sixt Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 3d549a8970..dec899a57a 100644 --- a/refs.c +++ b/refs.c @@ -1507,7 +1507,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname, struct ref_store *refs; int flags; - while (len && submodule[len - 1] == '/') + while (len && is_dir_sep(submodule[len - 1])) len--; if (!len) -- cgit v1.2.1 From 2c616c172d5ae052600006e942ff34136e7c534e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:36:53 +0700 Subject: refs.c: refactor get_submodule_ref_store(), share common free block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index dec899a57a..522c4ab74f 100644 --- a/refs.c +++ b/refs.c @@ -1636,7 +1636,6 @@ struct ref_store *get_submodule_ref_store(const char *submodule) { struct strbuf submodule_sb = STRBUF_INIT; struct ref_store *refs; - int ret; if (!submodule || !*submodule) { /* @@ -1648,19 +1647,14 @@ struct ref_store *get_submodule_ref_store(const char *submodule) refs = lookup_ref_store_map(&submodule_ref_stores, submodule); if (refs) - return refs; + goto done; strbuf_addstr(&submodule_sb, submodule); - ret = is_nonbare_repository_dir(&submodule_sb); - strbuf_release(&submodule_sb); - if (!ret) - return NULL; + if (!is_nonbare_repository_dir(&submodule_sb)) + goto done; - ret = submodule_to_gitdir(&submodule_sb, submodule); - if (ret) { - strbuf_release(&submodule_sb); - return NULL; - } + if (submodule_to_gitdir(&submodule_sb, submodule)) + goto done; /* assume that add_submodule_odb() has been called */ refs = ref_store_init(submodule_sb.buf, @@ -1668,6 +1662,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule) register_ref_store_map(&submodule_ref_stores, "submodule", refs, submodule); +done: strbuf_release(&submodule_sb); return refs; } -- cgit v1.2.1 From 29babbeeb32fb4e8b892940e69207ec7de2e7a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:36:54 +0700 Subject: refs: move submodule slash stripping code to get_submodule_ref_store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a better place that will benefit all submodule callers instead of just resolve_gitlink_ref() Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 522c4ab74f..ea8e6b9f42 100644 --- a/refs.c +++ b/refs.c @@ -1503,25 +1503,10 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, int resolve_gitlink_ref(const char *submodule, const char *refname, unsigned char *sha1) { - size_t len = strlen(submodule); struct ref_store *refs; int flags; - while (len && is_dir_sep(submodule[len - 1])) - len--; - - if (!len) - return -1; - - if (submodule[len]) { - /* We need to strip off one or more trailing slashes */ - char *stripped = xmemdupz(submodule, len); - - refs = get_submodule_ref_store(stripped); - free(stripped); - } else { - refs = get_submodule_ref_store(submodule); - } + refs = get_submodule_ref_store(submodule); if (!refs) return -1; @@ -1636,6 +1621,16 @@ struct ref_store *get_submodule_ref_store(const char *submodule) { struct strbuf submodule_sb = STRBUF_INIT; struct ref_store *refs; + char *to_free = NULL; + size_t len; + + if (submodule) { + len = strlen(submodule); + while (len && is_dir_sep(submodule[len - 1])) + len--; + if (!len) + return NULL; + } if (!submodule || !*submodule) { /* @@ -1645,6 +1640,10 @@ struct ref_store *get_submodule_ref_store(const char *submodule) return get_main_ref_store(); } + if (submodule[len]) + /* We need to strip off one or more trailing slashes */ + submodule = to_free = xmemdupz(submodule, len); + refs = lookup_ref_store_map(&submodule_ref_stores, submodule); if (refs) goto done; @@ -1664,6 +1663,8 @@ struct ref_store *get_submodule_ref_store(const char *submodule) done: strbuf_release(&submodule_sb); + free(to_free); + return refs; } -- cgit v1.2.1 From 62f0b399e00f38751834e688677ab49fcccd28fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:36:55 +0700 Subject: refs: add refs_head_ref() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index ea8e6b9f42..b3a0a24469 100644 --- a/refs.c +++ b/refs.c @@ -1248,27 +1248,30 @@ int refs_rename_ref_available(struct ref_store *refs, return ok; } -int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) +int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) { struct object_id oid; int flag; - if (submodule) { - if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0) - return fn("HEAD", &oid, 0, cb_data); - - return 0; - } - - if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag)) + if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING, + oid.hash, &flag)) return fn("HEAD", &oid, flag, cb_data); return 0; } +int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) +{ + struct ref_store *refs = get_submodule_ref_store(submodule); + + if (!refs) + return -1; + return refs_head_ref(refs, fn, cb_data); +} + int head_ref(each_ref_fn fn, void *cb_data) { - return head_ref_submodule(NULL, fn, cb_data); + return refs_head_ref(get_main_ref_store(), fn, cb_data); } struct ref_iterator *refs_ref_iterator_begin( -- cgit v1.2.1 From 073cf63c523e3e88a8e002fbc04fc1876f074aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:36:56 +0700 Subject: revision.c: use refs_for_each*() instead of for_each_*_submodule() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index b3a0a24469..cd61509bc8 100644 --- a/refs.c +++ b/refs.c @@ -1362,16 +1362,15 @@ int for_each_ref_in_submodule(const char *submodule, const char *prefix, prefix, fn, cb_data); } -int for_each_fullref_in_submodule(const char *submodule, const char *prefix, - each_ref_fn fn, void *cb_data, - unsigned int broken) +int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, + each_ref_fn fn, void *cb_data, + unsigned int broken) { unsigned int flag = 0; if (broken) flag = DO_FOR_EACH_INCLUDE_BROKEN; - return do_for_each_ref(get_submodule_ref_store(submodule), - prefix, fn, 0, flag, cb_data); + return do_for_each_ref(refs, prefix, fn, 0, flag, cb_data); } int for_each_replace_ref(each_ref_fn fn, void *cb_data) -- cgit v1.2.1 From 2e2d4040bd1c26eccfe0e54b3ab73e13d4bf45e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:36:57 +0700 Subject: refs.c: move for_each_remote_ref_submodule() to submodule.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index cd61509bc8..7fa19e9309 100644 --- a/refs.c +++ b/refs.c @@ -368,12 +368,6 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data) return refs_for_each_remote_ref(get_main_ref_store(), fn, cb_data); } -int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) -{ - return refs_for_each_remote_ref(get_submodule_ref_store(submodule), - fn, cb_data); -} - int head_ref_namespaced(each_ref_fn fn, void *cb_data) { struct strbuf buf = STRBUF_INIT; -- cgit v1.2.1 From 419221c1065981311b1a0f4a469d4d8a9ea09f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:36:58 +0700 Subject: refs: remove dead for_each_*_submodule() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These are used in revision.c. After the last patch they are replaced with the refs_ version. Delete them. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 33 --------------------------------- 1 file changed, 33 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 7fa19e9309..8c989ffec7 100644 --- a/refs.c +++ b/refs.c @@ -336,12 +336,6 @@ int for_each_tag_ref(each_ref_fn fn, void *cb_data) return refs_for_each_tag_ref(get_main_ref_store(), fn, cb_data); } -int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) -{ - return refs_for_each_tag_ref(get_submodule_ref_store(submodule), - fn, cb_data); -} - int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) { return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data); @@ -352,12 +346,6 @@ int for_each_branch_ref(each_ref_fn fn, void *cb_data) return refs_for_each_branch_ref(get_main_ref_store(), fn, cb_data); } -int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) -{ - return refs_for_each_branch_ref(get_submodule_ref_store(submodule), - fn, cb_data); -} - int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) { return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data); @@ -1254,15 +1242,6 @@ int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) return 0; } -int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) -{ - struct ref_store *refs = get_submodule_ref_store(submodule); - - if (!refs) - return -1; - return refs_head_ref(refs, fn, cb_data); -} - int head_ref(each_ref_fn fn, void *cb_data) { return refs_head_ref(get_main_ref_store(), fn, cb_data); @@ -1323,11 +1302,6 @@ int for_each_ref(each_ref_fn fn, void *cb_data) return refs_for_each_ref(get_main_ref_store(), fn, cb_data); } -int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) -{ - return refs_for_each_ref(get_submodule_ref_store(submodule), fn, cb_data); -} - int refs_for_each_ref_in(struct ref_store *refs, const char *prefix, each_ref_fn fn, void *cb_data) { @@ -1349,13 +1323,6 @@ int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsig prefix, fn, 0, flag, cb_data); } -int for_each_ref_in_submodule(const char *submodule, const char *prefix, - each_ref_fn fn, void *cb_data) -{ - return refs_for_each_ref_in(get_submodule_ref_store(submodule), - prefix, fn, cb_data); -} - int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken) -- cgit v1.2.1 From 82a150f27a16cd1425d4c78904470e4b7b8c4cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:37:03 +0700 Subject: refs.c: remove fallback-to-main-store code get_submodule_ref_store() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At this state, there are three get_submodule_ref_store() callers: - for_each_remote_ref_submodule() - handle_revision_pseudo_opt() - resolve_gitlink_ref() The first two deal explicitly with submodules (and we should never fall back to the main ref store as a result). They are only called from submodule.c: - find_first_merges() - submodule_needs_pushing() - push_submodule() The last one, as its name implies, deals only with submodules too, and the "submodule" (path) argument must be a non-NULL, non-empty string. So, this "if NULL or empty string" code block should never ever trigger. And it's wrong to fall back to the main ref store anyway. Delete it. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 8c989ffec7..a0c5078901 100644 --- a/refs.c +++ b/refs.c @@ -1587,6 +1587,9 @@ struct ref_store *get_submodule_ref_store(const char *submodule) char *to_free = NULL; size_t len; + if (!submodule) + return NULL; + if (submodule) { len = strlen(submodule); while (len && is_dir_sep(submodule[len - 1])) @@ -1595,14 +1598,6 @@ struct ref_store *get_submodule_ref_store(const char *submodule) return NULL; } - if (!submodule || !*submodule) { - /* - * FIXME: This case is ideally not allowed. But that - * can't happen until we clean up all the callers. - */ - return get_main_ref_store(); - } - if (submodule[len]) /* We need to strip off one or more trailing slashes */ submodule = to_free = xmemdupz(submodule, len); -- cgit v1.2.1 From 873ea90d61fa45ec91af3864c2e336bfe489d181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 23 Aug 2017 19:37:04 +0700 Subject: refs.c: reindent get_submodule_ref_store() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the new "if (!submodule) return NULL;" code added in the previous commit, we don't need to check if submodule is not NULL anymore. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index a0c5078901..206af61d62 100644 --- a/refs.c +++ b/refs.c @@ -1590,13 +1590,11 @@ struct ref_store *get_submodule_ref_store(const char *submodule) if (!submodule) return NULL; - if (submodule) { - len = strlen(submodule); - while (len && is_dir_sep(submodule[len - 1])) - len--; - if (!len) - return NULL; - } + len = strlen(submodule); + while (len && is_dir_sep(submodule[len - 1])) + len--; + if (!len) + return NULL; if (submodule[len]) /* We need to strip off one or more trailing slashes */ -- cgit v1.2.1