Skip to contentSkip to conversation

rev-parse: rev-parse: add --is-shallow-repository

PATCH In review Øystein Walle 3 participants 4 messages Sep 18, 2017 – Sep 19, 2017 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rev-parse: rev-parse: add --is-shallow-repository

Running git fetch --unshallow on a repo that is not in fact shallow produces a fatal error message. Add a helper to rev-parse that scripters can use to determine whether a repo is shallow or not.

---
 Documentation/git-rev-parse.txt |  3 +++
 builtin/rev-parse.c             |  5 +++++
 t/t1500-rev-parse.sh            | 15 +++++++++++++++
 3 files changed, 23 insertions(+)
Documentation/git-rev-parse.txt +3 -0
index b1293f24b..0917b8207 100644
@@ -235,6 +235,9 @@ print a message to stderr and exit with nonzero status.
 --is-bare-repository::
 	When the repository is bare print "true", otherwise "false".
 
+--is-shallow-repository::
+	When the repository is shallow print "true", otherwise "false".
+
 --resolve-git-dir <path>::
 	Check if <path> is a valid repository or a gitfile that
 	points at a valid repository, and print the location of the
builtin/rev-parse.c +5 -0
index 2bd28d3c0..c923207f2 100644
@@ -868,6 +868,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 						: "false");
 				continue;
 			}
+			if (!strcmp(arg, "--is-shallow-repository")) {
+				printf("%s\n", is_repository_shallow() ? "true"
+						: "false");
+				continue;
+			}
 			if (!strcmp(arg, "--shared-index-path")) {
 				if (read_cache() < 0)
 					die(_("Could not read the index"));
t/t1500-rev-parse.sh +15 -0
index 03d3c7f6d..9d3433a30 100755
@@ -116,6 +116,21 @@ test_expect_success 'git-path inside sub-dir' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git-path shallow repository' '
+	test_commit test_commit &&
+	echo true >expect &&
+	git clone --depth 1 --no-local . shallow &&
+	test_when_finished "rm -rf shallow" &&
+	git -C shallow rev-parse --is-shallow-repository >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'git-path notshallow repository' '
+	echo false >expect &&
+	git rev-parse --is-shallow-repository >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'showing the superproject correctly' '
 	git rev-parse --show-superproject-working-tree >out &&
 	test_must_be_empty out &&
Signature · 1 line
2.11.0.485.g4e59582
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 2 lines · Øystein Walle

Running git fetch --unshallow on a repo that is not in fact shallow produces a fatal error message.

Hm, can you say more about the context? From a certain point of view, it might make sense for that command to succeed instead: if the repo is already unshallow, then why should't "fetch --unshallow" complain instead of declaring victory?

Quoted 9 lines

Add a helper to rev-parse that scripters
can use to determine whether a repo is shallow or not.

---
 Documentation/git-rev-parse.txt |  3 +++
 builtin/rev-parse.c             |  5 +++++
 t/t1500-rev-parse.sh            | 15 +++++++++++++++
 3 files changed, 23 insertions(+)

Regardless, this new rev-parse --is-shallow helper looks like a good feature.

[...]

builtin/rev-parse.c int cmd_rev_parse(int argc, const char **argv, const char *prefix) · line 875

Quoted 11 lines
builtin/rev-parse.c +5 -0
@@ -868,6 +868,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 						: "false");
 				continue;
 			}
+			if (!strcmp(arg, "--is-shallow-repository")) {
+				printf("%s\n", is_repository_shallow() ? "true"
+						: "false");
+				continue;
+			}

The implementation is straightforward and correct.

[...]

t/t1500-rev-parse.sh

Quoted 2 lines
t/t1500-rev-parse.sh +0 -0

Thanks for writing tests. \o/

t/t1500-rev-parse.sh test_expect_success 'git-path inside sub-dir' ' · line 119

Quoted 5 lines
t/t1500-rev-parse.sh +1 -0
@@ -116,6 +116,21 @@ test_expect_success 'git-path inside sub-dir' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git-path shallow repository' '

What does git-path mean here? I wonder if it's a copy/paste error. Did you mean something like

test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '

?

t/t1500-rev-parse.sh test_expect_success 'git-path inside sub-dir' ' · line 128

Quoted 9 lines
t/t1500-rev-parse.sh +9 -0
+	test_commit test_commit &&
+	echo true >expect &&
+	git clone --depth 1 --no-local . shallow &&
+	test_when_finished "rm -rf shallow" &&
+	git -C shallow rev-parse --is-shallow-repository >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'git-path notshallow repository' '

Likewise: should this be

test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '

?

t/t1500-rev-parse.sh test_expect_success 'git-path inside sub-dir' ' · line 134

Quoted 6 lines
t/t1500-rev-parse.sh +5 -0
+	echo false >expect &&
+	git rev-parse --is-shallow-repository >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'showing the superproject correctly' '

With the two tweaks mentioned above,

Thanks.

#2 lore raw
Quoted 4 lines · Jonathan Nieder
Quoted 1 line

test_expect_success 'showing the superproject correctly' '

With the two tweaks mentioned above,

I agree with the fixes to the test titles suggested, so I'll queue the patch with the fixes squashed in. Hearing "yeah, the titles were copy-pasted without adjusting, thanks for fixing, Jonathan!" sent by Øystein would be super nice.

Thanks, both.

#3 lore raw
Quoted 4 lines

Hm, can you say more about the context? From a certain point of view, it might make sense for that command to succeed instead: if the repo is already unshallow, then why should't "fetch --unshallow" complain instead of declaring victory?

A fellow in #git on Freenode was writing a script for automation and encountered this error, and asked how to find out whether a repo was shallow. My *first instinct* was to check if rev-parse had a flag for it; I wouldn't have been surprised if it did.

I agree that treating it as a fatal error is a bit much in the first place, but I also think having a way to check can be useful. I also wonder if a lot of the stuff rev-parse is used for now should be moved to some sort of git misc command, but that's a different can of worms, so into rev-parse a new flag went.

Quoted 3 lines

What does git-path mean here? I wonder if it's a copy/paste error.
...

Yeah, the titles were copy-pasted without adjusting, thanks for fixing, Jonathan! ;)

Quoted 4 lines

I agree with the fixes to the test titles suggested, so I'll queue the patch with the fixes squashed in. Hearing "yeah, the titles were copy-pasted without adjusting, thanks for fixing, Jonathan!" sent by =C3=98ystein would be super nice.

Sounds good. Thanks for queueing my patch. My fourth!

�sse