refs: add refs_head_ref()

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 Aug 23, 2017 at 19:36 UTC 62f0b399e00f38751834e688677ab49fcccd28fe
2 files changed +15 -10
refs.c
+13 -10
@@ -1248,27 +1248,30 @@ int refs_rename_ref_available(struct ref_store *refs,
1248 return ok;
1249 }
1250
1251 -int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1251 +int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1252 {
1253 struct object_id oid;
1254 int flag;
1255
1256 - if (submodule) {
1257 - if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1258 - return fn("HEAD", &oid, 0, cb_data);
1259 -
1260 - return 0;
1261 - }
1262 -
1263 - if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1256 + if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING,
1257 + oid.hash, &flag))
1258 return fn("HEAD", &oid, flag, cb_data);
1259
1260 return 0;
1261 }
1262
1263 +int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1264 +{
1265 + struct ref_store *refs = get_submodule_ref_store(submodule);
1266 +
1267 + if (!refs)
1268 + return -1;
1269 + return refs_head_ref(refs, fn, cb_data);
1270 +}
1271 +
1272 int head_ref(each_ref_fn fn, void *cb_data)
1273 {
1271 - return head_ref_submodule(NULL, fn, cb_data);
1274 + return refs_head_ref(get_main_ref_store(), fn, cb_data);
1275 }
1276
1277 struct ref_iterator *refs_ref_iterator_begin(
refs.h
+2
@@ -275,6 +275,8 @@ typedef int each_ref_fn(const char *refname,
275 * modifies the reference also returns a nonzero value to immediately
276 * stop the iteration. Returned references are sorted.
277 */
278 +int refs_head_ref(struct ref_store *refs,
279 + each_ref_fn fn, void *cb_data);
280 int refs_for_each_ref(struct ref_store *refs,
281 each_ref_fn fn, void *cb_data);
282 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,