Add the field layout.shallow to the repo-info command. The data retrieved in this field is the same that currently is obtained by running `git rev-parse --is-shallow-repository`. Mentored-by: Karthik Nayak <karthik.188@xxxxxxxxx> Mentored-by Patrick Steinhardt <ps@xxxxxx> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@xxxxxxxxx> --- builtin/repo-info.c | 20 ++++++++++++++++-- t/t1518-repo-info.sh | 48 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/builtin/repo-info.c b/builtin/repo-info.c index bc25a0809f..d821292d78 100644 --- a/builtin/repo-info.c +++ b/builtin/repo-info.c @@ -6,6 +6,7 @@ #include "json-writer.h" #include "parse-options.h" #include "refs.h" +#include "shallow.h" enum output_format { FORMAT_PLAINTEXT, @@ -21,7 +22,9 @@ enum repo_info_references_field { FIELD_REFERENCES_FORMAT = 1 }; -enum repo_info_layout_field { FIELD_LAYOUT_BARE = 1 +enum repo_info_layout_field { + FIELD_LAYOUT_BARE = 1, + FIELD_LAYOUT_SHALLOW = 1 << 1 }; struct repo_info_field { @@ -41,7 +44,8 @@ struct repo_info { const char *default_fields[] = { "references.format", - "layout.bare" + "layout.bare", + "layout.shallow" }; static void repo_info_init(struct repo_info *repo_info, @@ -81,6 +85,10 @@ static void repo_info_init(struct repo_info *repo_info, field->category = CATEGORY_LAYOUT; field->field.layout = FIELD_LAYOUT_BARE; } + else if (!strcmp(arg, "layout.shallow")) { + field->category = CATEGORY_LAYOUT; + field->field.layout = FIELD_LAYOUT_SHALLOW; + } else { die("invalid field '%s'", arg); } @@ -110,6 +118,9 @@ static void repo_info_print_plaintext(struct repo_info *repo_info) { case FIELD_LAYOUT_BARE: puts(is_bare_repository() ? "true" : "false"); break; + case FIELD_LAYOUT_SHALLOW: + puts(is_repository_shallow(repo) ? "true" : "false"); + break; } break; } @@ -158,6 +169,11 @@ static void repo_info_print_json(struct repo_info *repo_info) jw_object_bool(&jw, "bare", is_bare_repository()); } + + if (layout_fields & FIELD_LAYOUT_SHALLOW) { + jw_object_bool(&jw, "shallow", + is_repository_shallow(repo)); + } jw_end(&jw); } jw_end(&jw); diff --git a/t/t1518-repo-info.sh b/t/t1518-repo-info.sh index 1831b74551..10de93219b 100755 --- a/t/t1518-repo-info.sh +++ b/t/t1518-repo-info.sh @@ -6,7 +6,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME . ./test-lib.sh -DEFAULT_NUMBER_OF_FIELDS=2 +DEFAULT_NUMBER_OF_FIELDS=3 parse_json () { tr '\n' ' ' | "$PERL_PATH" "$TEST_DIRECTORY/t0019/parse_json.perl" @@ -64,6 +64,51 @@ test_repo_info 'bare repository = true is retrieved correctly' \ '--bare' \ 'layout.bare' 'true' +test_repo_info 'shallow repository = false is retrieved correctly' \ + '' \ + 'layout.shallow' 'false' + +test_expect_success 'json: shallow repository = true is retrieved correctly' ' + test_when_finished "rm -rf repo" && + git init repo && + cd repo && + echo x >x && + git add x && + git commit -m x && + git clone --depth 1 "file://$PWD" cloned && + cd cloned && + echo 1 >expect && + git repo-info "layout.shallow" | parse_json >output && + grep -F "row[0].layout.shallow" output | cut -d " " -f 2 >actual && + cat actual > /dev/ttys001 && + test_cmp expect actual +' + +test_expect_success 'plaintext: shallow repository = true is retrieved correctly' ' + test_when_finished "rm -rf repo" && + git init repo && + cd repo && + echo x >x && + git add x && + git commit -m x && + test_commit "commit" && + git clone --depth=1 "file://$PWD" cloned && + cd cloned && + echo true >expect && + git repo-info --format=plaintext "layout.shallow" >actual && + test_cmp expect actual +' + +test_expect_success 'plaintext: output all default fields' " + git repo-info --format=plaintext >actual && + test_line_count = $DEFAULT_NUMBER_OF_FIELDS actual +" + +test_expect_success 'json: output all default fields' " + git repo-info --format=json | parse_json | grep '.*\..*\..*' >actual && + test_line_count = $DEFAULT_NUMBER_OF_FIELDS actual +" + test_expect_success 'plaintext: output all default fields' " git repo-info --format=plaintext >actual && test_line_count = $DEFAULT_NUMBER_OF_FIELDS actual @@ -74,4 +119,5 @@ test_expect_success 'json: output all default fields' " test_line_count = $DEFAULT_NUMBER_OF_FIELDS actual " + test_done -- 2.39.5 (Apple Git-154)