From 209509d68842002031dd87f8ab19123711345316 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Wed, 25 Jan 2012 15:27:21 +0100 Subject: [PATCH] git for-each-ref: allow general objects Introduce an `--objects` option which makes git-for-each-ref into git-for-each-object, i.e. the non-option arguments are treated as general revision arguments specifying any object. In this mode, the refname output field reproduces the revision argument: git for-each-ref --objects v1.7.9-rc2 v1.7.9-rc2^{commit} v1.7.9-rc2^{tree} eab05abaeb51531e11835aaa4c26564a1babebac tag v1.7.9-rc2 bddcefc6380bd6629f3f12b5ffd856ec436c6abd commit v1.7.9-rc2^{commit} 3382700b2f37c2e6e243c04b8c87bab6f125ca98 tree v1.7.9-rc2^{tree} `git for-each-ref --objects` can be aliased to `git for-each-object` internally or by the user later on. --- Documentation/git-for-each-ref.txt | 5 ++++- builtin/for-each-ref.c | 30 ++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt index 7f8d9a5b5f..e5eeb401d8 100644 --- a/Documentation/git-for-each-ref.txt +++ b/Documentation/git-for-each-ref.txt @@ -9,7 +9,7 @@ SYNOPSIS -------- [verse] 'git for-each-ref' [--count=] [--shell|--perl|--python|--tcl] - [(--sort=)...] [--format=] [...] + [(--sort=)...] [--format=] [--objects] [...] DESCRIPTION ----------- @@ -62,6 +62,9 @@ OPTIONS the specified host language. This is meant to produce a scriptlet that can directly be `eval`ed. +--objects:: + Interpret the `` arguments as general revision parameters + (see linkgit:gitrevisions[1]) rather than refname patterns. FIELD NAMES ----------- diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index cb7db230d3..64e44938c7 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -1089,7 +1089,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) int i, num_refs; const char *format = "%(objectname) %(objecttype)\t%(refname)"; struct ref_sort *sort = NULL, **sort_tail = &sort; - int maxcount = 0, quote_style = 0; + int maxcount = 0, quote_style = 0, objects = 0; struct refinfo **refs; struct grab_ref_cbdata cbdata; @@ -1108,10 +1108,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")), OPT_CALLBACK(0 , "sort", sort_tail, N_("key"), N_("field name to sort on"), &opt_parse_sort), + OPT_BOOL( 0 , "objects", &objects, "arguments are objects, not ref patterns"), OPT_END(), }; - parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0); + argc = parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0); if (maxcount < 0) { error("invalid --count argument: `%d'", maxcount); usage_with_options(for_each_ref_usage, opts); @@ -1129,12 +1130,25 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) /* for warn_ambiguous_refs */ git_config(git_default_config, NULL); - memset(&cbdata, 0, sizeof(cbdata)); - cbdata.grab_pattern = argv; - for_each_rawref(grab_single_ref, &cbdata); - refs = cbdata.grab_array; - num_refs = cbdata.grab_cnt; - + if (objects) { + unsigned char sha1[20]; + refs = xcalloc(argc, sizeof(*refs)); + for (i = 0, num_refs = 0; i < argc; i++) { + if (get_sha1(argv[i], sha1)) + continue; /* or warn or die */ + refs[num_refs] = xcalloc(1, sizeof(**refs)); + /* ref->refname = xstrdup(sha1_to_hex(sha1)); */ + refs[num_refs]->refname = xstrdup(argv[i]); + hashcpy(refs[num_refs]->objectname, sha1); + num_refs++; + } + } else { + memset(&cbdata, 0, sizeof(cbdata)); + cbdata.grab_pattern = argv; + for_each_rawref(grab_single_ref, &cbdata); + refs = cbdata.grab_array; + num_refs = cbdata.grab_cnt; + } sort_refs(sort, refs, num_refs); if (!maxcount || num_refs < maxcount) -- 2.11.4.GIT