ref-filter: Do not abruptly die when using the 'lstrip=<N>' option
authorKarthik Nayak <karthik.188@gmail.com>
Tue, 10 Jan 2017 08:49:47 +0000 (14:19 +0530)
committerJunio C Hamano <gitster@pobox.com>
Tue, 10 Jan 2017 20:44:31 +0000 (12:44 -0800)
Currently when we use the 'lstrip=<N>' option, if 'N' is greater than
the number of components available in the refname, we abruptly end
program execution by calling die().

This behavior is undesired since a single refname with few components
could end program execution. To avoid this, return an empty string
whenever the value 'N' is greater than the number of components
available, instead of calling die().

Signed-off-by: Karthik Nayak <Karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-for-each-ref.txt
ref-filter.c
t/t6300-for-each-ref.sh
index b0d94deea6c7361e30d8a1d3ed86f7b8d14a6e00..04ffc35524288eb251576e9db4df41157e75eb87 100644 (file)
@@ -98,8 +98,7 @@ refname::
        abbreviation mode. If `lstrip=<N>` is appended, strips `<N>`
        slash-separated path components from the front of the refname
        (e.g., `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo`.
-       `<N>` must be a positive integer.  If a displayed ref has fewer
-       components than `<N>`, the command aborts with an error.
+       `<N>` must be a positive integer.
 
 objecttype::
        The type of the object (`blob`, `tree`, `commit`, `tag`).
index cccd86f574dad26c663ecb5eb722c77d8c55b792..4fd6ef186cc430b34be9f15335e5af72e67bbc1c 100644 (file)
@@ -1099,8 +1099,7 @@ static const char *lstrip_ref_components(const char *refname, unsigned int len)
        while (remaining) {
                switch (*start++) {
                case '\0':
-                       die(_("ref '%s' does not have %ud components to :lstrip"),
-                           refname, len);
+                       return "";
                case '/':
                        remaining--;
                        break;
index 5eb013ca2146d152be86362a921a2183396c0aaf..d3d1a97db9709c29b829cf64df4fa6cce04ed6fe 100755 (executable)
@@ -147,10 +147,6 @@ test_expect_success 'arguments to :lstrip must be positive integers' '
        test_must_fail git for-each-ref --format="%(refname:lstrip=foo)"
 '
 
-test_expect_success 'stripping refnames too far gives an error' '
-       test_must_fail git for-each-ref --format="%(refname:lstrip=3)"
-'
-
 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
        git for-each-ref --format="%(authordate)" refs/heads &&
        git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&