summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8db9a4b)
raw | patch | inline | side by side (parent: 8db9a4b)
author | Jeff King <peff@peff.net> | |
Tue, 7 Apr 2009 07:09:39 +0000 (03:09 -0400) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Wed, 8 Apr 2009 06:22:15 +0000 (23:22 -0700) |
The logic for determining the upstream ref of a branch is
somewhat complex to perform in a shell script. This patch
provides a plumbing mechanism for scripts to access the C
logic used internally by git-status, git-branch, etc.
For example:
$ git for-each-ref \
--format='%(refname:short) %(upstream:short)' \
refs/heads/
master origin/master
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
somewhat complex to perform in a shell script. This patch
provides a plumbing mechanism for scripts to access the C
logic used internally by git-status, git-branch, etc.
For example:
$ git for-each-ref \
--format='%(refname:short) %(upstream:short)' \
refs/heads/
master origin/master
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-for-each-ref.txt | patch | blob | history | |
builtin-for-each-ref.c | patch | blob | history | |
t/t6300-for-each-ref.sh | patch | blob | history |
index 5061d3e4e7b8a888093c5e8c7b4cb03509391756..b362e9ed12fb8c45802d488eb5685f8842e0607d 100644 (file)
objectname::
The object name (aka SHA-1).
+upstream::
+ The name of a local ref which can be considered ``upstream''
+ from the displayed ref. Respects `:short` in the same way as
+ `refname` above.
+
In addition to the above, for commit and tag objects, the header
field names (`tree`, `parent`, `object`, `type`, and `tag`) can
be used to specify the value in the header field.
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index 653dca9a5765d9a5873f5ac1b7345d2182ad7d01..8796352eb673ff1f52df13329ee99530eaa98c39 100644 (file)
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
#include "blob.h"
#include "quote.h"
#include "parse-options.h"
+#include "remote.h"
/* Quoting styles */
#define QUOTE_NONE 0
{ "subject" },
{ "body" },
{ "contents" },
+ { "upstream" },
};
/*
if (!prefixcmp(name, "refname"))
refname = ref->refname;
+ else if(!prefixcmp(name, "upstream")) {
+ struct branch *branch;
+ /* only local branches may have an upstream */
+ if (prefixcmp(ref->refname, "refs/heads/"))
+ continue;
+ branch = branch_get(ref->refname + 11);
+
+ if (!branch || !branch->merge || !branch->merge[0] ||
+ !branch->merge[0]->dst)
+ continue;
+ refname = branch->merge[0]->dst;
+ }
else
continue;
index 8bfae44a8392898453785f43c7e35b65e9c1f017..daf02d5c103a55575edeefc53643a5b3794de121 100755 (executable)
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
git tag -a -m "Tagging at $datestamp" testtag
'
+test_expect_success 'Create upstream config' '
+ git update-ref refs/remotes/origin/master master &&
+ git remote add origin nowhere &&
+ git config branch.master.remote origin &&
+ git config branch.master.merge refs/heads/master
+'
+
test_atom() {
case "$1" in
head) ref=refs/heads/master ;;
}
test_atom head refname refs/heads/master
+test_atom head upstream refs/remotes/origin/master
test_atom head objecttype commit
test_atom head objectsize 171
test_atom head objectname 67a36f10722846e891fbada1ba48ed035de75581
'
test_atom tag refname refs/tags/testtag
+test_atom tag upstream ''
test_atom tag objecttype tag
test_atom tag objectsize 154
test_atom tag objectname 98b46b1d36e5b07909de1b3886224e3e81e87322
cat >expected <<\EOF
refs/heads/master
+refs/remotes/origin/master
refs/tags/testtag
EOF
cat >expected <<\EOF
refs/tags/testtag
+refs/remotes/origin/master
refs/heads/master
EOF
cat >expected <<\EOF
'refs/heads/master'
+'refs/remotes/origin/master'
'refs/tags/testtag'
EOF
cat >expected <<\EOF
"refs/heads/master"
+"refs/remotes/origin/master"
"refs/tags/testtag"
EOF
test_cmp expected actual
'
+cat >expected <<EOF
+origin/master
+EOF
+
+test_expect_success 'Check short upstream format' '
+ git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
+ test_cmp expected actual
+'
+
test_expect_success 'Check for invalid refname format' '
test_must_fail git for-each-ref --format="%(refname:INVALID)"
'