Skip to content

Commit 32acbd9

Browse files
lucasoshirogitster
authored andcommitted
repo-info: add field layout.bare
Add the field layout.bare to the repo-info command. The data retrieved in this field is the same that currently is obtained by running `git rev-parse --is-bare-repository`. Mentored-by: Karthik Nayak <[email protected]> Mentored-by Patrick Steinhardt <[email protected]> Signed-off-by: Lucas Seiki Oshiro <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3a3722e commit 32acbd9

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

builtin/repo-info.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
13
#include "builtin.h"
4+
#include "environment.h"
5+
#include "hash.h"
26
#include "json-writer.h"
37
#include "parse-options.h"
48
#include "quote.h"
@@ -10,17 +14,23 @@ enum output_format {
1014
};
1115

1216
enum repo_info_category {
13-
CATEGORY_REFERENCES = 1 << 0
17+
CATEGORY_REFERENCES = 1 << 0,
18+
CATEGORY_LAYOUT = 1 << 1
1419
};
1520

1621
enum repo_info_references_field {
1722
FIELD_REFERENCES_FORMAT = 1 << 0
1823
};
1924

25+
enum repo_info_layout_field {
26+
FIELD_LAYOUT_BARE = 1 << 0
27+
};
28+
2029
struct repo_info_field {
2130
enum repo_info_category category;
2231
union {
2332
enum repo_info_references_field references;
33+
enum repo_info_layout_field layout;
2434
} field;
2535
};
2636

@@ -35,6 +45,10 @@ static struct repo_info_field default_fields[] = {
3545
{
3646
.category = CATEGORY_REFERENCES,
3747
.field.references = FIELD_REFERENCES_FORMAT
48+
},
49+
{
50+
.category = CATEGORY_LAYOUT,
51+
.field.layout = FIELD_LAYOUT_BARE
3852
}
3953
};
4054

@@ -74,6 +88,9 @@ static void repo_info_init(struct repo_info *repo_info,
7488
if (!strcmp(arg, "references.format")) {
7589
field->category = CATEGORY_REFERENCES;
7690
field->field.references = FIELD_REFERENCES_FORMAT;
91+
} else if (!strcmp(arg, "layout.bare")) {
92+
field->category = CATEGORY_LAYOUT;
93+
field->field.layout = FIELD_LAYOUT_BARE;
7794
} else {
7895
die("invalid field '%s'", arg);
7996
}
@@ -101,6 +118,15 @@ static void repo_info_print_plaintext(struct repo_info *repo_info) {
101118
break;
102119
}
103120
break;
121+
case CATEGORY_LAYOUT:
122+
switch (field->field.layout) {
123+
case FIELD_LAYOUT_BARE:
124+
print_key_value("layout.bare",
125+
is_bare_repository() ?
126+
"true" : "false");
127+
break;
128+
}
129+
break;
104130
}
105131
}
106132
}
@@ -111,6 +137,7 @@ static void repo_info_print_json(struct repo_info *repo_info)
111137
int i;
112138
unsigned int categories = 0;
113139
unsigned int references_fields = 0;
140+
unsigned int layout_fields = 0;
114141
struct repository *repo = repo_info->repo;
115142

116143
for (i = 0; i < repo_info->n_fields; i++) {
@@ -120,6 +147,9 @@ static void repo_info_print_json(struct repo_info *repo_info)
120147
case CATEGORY_REFERENCES:
121148
references_fields |= field->field.references;
122149
break;
150+
case CATEGORY_LAYOUT:
151+
layout_fields |= field->field.layout;
152+
break;
123153
}
124154
}
125155

@@ -136,6 +166,15 @@ static void repo_info_print_json(struct repo_info *repo_info)
136166
}
137167
jw_end(&jw);
138168
}
169+
170+
if (categories & CATEGORY_LAYOUT) {
171+
jw_object_inline_begin_object(&jw, "layout");
172+
if (layout_fields & FIELD_LAYOUT_BARE) {
173+
jw_object_bool(&jw, "bare",
174+
is_bare_repository());
175+
}
176+
jw_end(&jw);
177+
}
139178
jw_end(&jw);
140179

141180
puts(jw.json.buf);

t/t1900-repo-info.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

77
. ./test-lib.sh
88

9-
DEFAULT_NUMBER_OF_FIELDS=1
9+
DEFAULT_NUMBER_OF_FIELDS=2
1010

1111
parse_json () {
1212
tr '\n' ' ' | "$PERL_PATH" "$TEST_DIRECTORY/t0019/parse_json.perl"
@@ -71,6 +71,12 @@ test_repo_info 'ref format files is retrieved correctly' '
7171
test_repo_info 'ref format reftable is retrieved correctly' '
7272
git init --ref-format=reftable repo' 'references.format' 'reftable'
7373

74+
test_repo_info 'bare repository = false is retrieved correctly' '
75+
git init repo' 'layout.bare' 'false'
76+
77+
test_repo_info 'bare repository = true is retrieved correctly' '
78+
git init --bare repo' 'layout.bare' 'true'
79+
7480
test_expect_success 'plaintext: output all default fields' "
7581
git repo-info --format=plaintext >actual &&
7682
test_line_count = $DEFAULT_NUMBER_OF_FIELDS actual

0 commit comments

Comments
 (0)