int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt) { struct read_ref_at_cb cb; memset(&cb, 0, sizeof(cb)); cb.refname = refname; cb.at_time = at_time; cb.cnt = cnt; cb.msg = msg; cb.cutoff_time = cutoff_time; cb.cutoff_tz = cutoff_tz; cb.cutoff_cnt = cutoff_cnt; cb.sha1 = sha1; for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb); if (!cb.reccnt) { if (flags & GET_SHA1_QUIETLY) exit(128); else die("Log for %s is empty.", refname); } if (cb.found_it) return 0; for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb); return 1; }
static void wt_status_get_detached_from(struct wt_status_state *state) { struct grab_1st_switch_cbdata cb; struct commit *commit; unsigned char sha1[20]; char *ref = NULL; strbuf_init(&cb.buf, 0); if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) { strbuf_release(&cb.buf); return; } if (dwim_ref(cb.buf.buf, cb.buf.len, sha1, &ref) == 1 && /* sha1 is a commit? match without further lookup */ (!hashcmp(cb.nsha1, sha1) || /* perhaps sha1 is a tag, try to dereference to a commit */ ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL && !hashcmp(cb.nsha1, commit->object.sha1)))) { int ofs; if (!prefixcmp(ref, "refs/tags/")) ofs = strlen("refs/tags/"); else if (!prefixcmp(ref, "refs/remotes/")) ofs = strlen("refs/remotes/"); else ofs = 0; state->detached_from = xstrdup(ref + ofs); } else state->detached_from = xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV)); hashcpy(state->detached_sha1, cb.nsha1); free(ref); strbuf_release(&cb.buf); }