1
1
#include "builtin.h"
2
2
#include "json-writer.h"
3
3
#include "parse-options.h"
4
+ #include "quote.h"
5
+ #include "refs.h"
4
6
5
7
enum output_format {
6
8
FORMAT_JSON ,
7
9
FORMAT_PLAINTEXT
8
10
};
9
11
12
+ enum repo_info_category {
13
+ CATEGORY_REFERENCES = 1 << 0
14
+ };
15
+
16
+ enum repo_info_references_field {
17
+ FIELD_REFERENCES_FORMAT = 1 << 0
18
+ };
19
+
20
+ struct repo_info_field {
21
+ enum repo_info_category category ;
22
+ union {
23
+ enum repo_info_references_field references ;
24
+ } field ;
25
+ };
26
+
10
27
struct repo_info {
11
28
struct repository * repo ;
12
29
enum output_format format ;
30
+ int n_fields ;
31
+ struct repo_info_field * fields ;
13
32
};
14
33
34
+ static struct repo_info_field default_fields [] = {
35
+ {
36
+ .category = CATEGORY_REFERENCES ,
37
+ .field .references = FIELD_REFERENCES_FORMAT
38
+ }
39
+ };
40
+
41
+ static void print_key_value (const char * key , const char * value ) {
42
+ printf ("%s=" , key );
43
+ quote_c_style (value , NULL , stdout , 0 );
44
+ putchar ('\n' );
45
+ }
46
+
15
47
static void repo_info_init (struct repo_info * repo_info ,
16
48
struct repository * repo ,
17
- char * format )
49
+ char * format ,
50
+ int allow_empty ,
51
+ int argc , const char * * argv )
18
52
{
53
+ int i ;
19
54
repo_info -> repo = repo ;
20
55
21
56
if (format == NULL || !strcmp (format , "json" ))
@@ -24,19 +59,83 @@ static void repo_info_init(struct repo_info *repo_info,
24
59
repo_info -> format = FORMAT_PLAINTEXT ;
25
60
else
26
61
die ("invalid format %s" , format );
62
+
63
+ if (argc == 0 && !allow_empty ) {
64
+ repo_info -> n_fields = ARRAY_SIZE (default_fields );
65
+ repo_info -> fields = default_fields ;
66
+ } else {
67
+ repo_info -> n_fields = argc ;
68
+ ALLOC_ARRAY (repo_info -> fields , argc );
69
+
70
+ for (i = 0 ; i < argc ; i ++ ) {
71
+ const char * arg = argv [i ];
72
+ struct repo_info_field * field = repo_info -> fields + i ;
73
+
74
+ if (!strcmp (arg , "references.format" )) {
75
+ field -> category = CATEGORY_REFERENCES ;
76
+ field -> field .references = FIELD_REFERENCES_FORMAT ;
77
+ } else {
78
+ die ("invalid field '%s'" , arg );
79
+ }
80
+ }
81
+ }
27
82
}
28
83
29
- static void repo_info_print_plaintext (struct repo_info * repo_info UNUSED )
84
+ static void repo_info_release (struct repo_info * repo_info )
30
85
{
86
+ if (repo_info -> fields != default_fields ) free (repo_info -> fields );
31
87
}
32
88
33
- static void repo_info_print_json (struct repo_info * repo_info UNUSED )
89
+ static void repo_info_print_plaintext (struct repo_info * repo_info ) {
90
+ struct repository * repo = repo_info -> repo ;
91
+ int i ;
92
+ for (i = 0 ; i < repo_info -> n_fields ; i ++ ) {
93
+ struct repo_info_field * field = & repo_info -> fields [i ];
94
+ switch (field -> category ) {
95
+ case CATEGORY_REFERENCES :
96
+ switch (field -> field .references ) {
97
+ case FIELD_REFERENCES_FORMAT :
98
+ print_key_value ("references.format" ,
99
+ ref_storage_format_to_name (
100
+ repo -> ref_storage_format ));
101
+ break ;
102
+ }
103
+ break ;
104
+ }
105
+ }
106
+ }
107
+
108
+ static void repo_info_print_json (struct repo_info * repo_info )
34
109
{
35
110
struct json_writer jw ;
111
+ int i ;
112
+ unsigned int categories = 0 ;
113
+ unsigned int references_fields = 0 ;
114
+ struct repository * repo = repo_info -> repo ;
115
+
116
+ for (i = 0 ; i < repo_info -> n_fields ; i ++ ) {
117
+ struct repo_info_field * field = repo_info -> fields + i ;
118
+ categories |= field -> category ;
119
+ switch (field -> category ) {
120
+ case CATEGORY_REFERENCES :
121
+ references_fields |= field -> field .references ;
122
+ break ;
123
+ }
124
+ }
36
125
37
126
jw_init (& jw );
38
127
39
128
jw_object_begin (& jw , 1 );
129
+
130
+ if (categories & CATEGORY_REFERENCES ) {
131
+ jw_object_inline_begin_object (& jw , "references" );
132
+ if (references_fields & FIELD_REFERENCES_FORMAT ) {
133
+ const char * format_name = ref_storage_format_to_name (
134
+ repo -> ref_storage_format );
135
+ jw_object_string (& jw , "format" , format_name );
136
+ }
137
+ jw_end (& jw );
138
+ }
40
139
jw_end (& jw );
41
140
42
141
puts (jw .json .buf );
@@ -79,8 +178,9 @@ int cmd_repo_info(int argc,
79
178
80
179
argc = parse_options (argc , argv , prefix , options , repo_info_usage ,
81
180
PARSE_OPT_KEEP_UNKNOWN_OPT );
82
- repo_info_init (& repo_info , repo , format );
181
+ repo_info_init (& repo_info , repo , format , allow_empty , argc , argv );
83
182
repo_info_print (& repo_info );
183
+ repo_info_release (& repo_info );
84
184
85
185
return 0 ;
86
186
}
0 commit comments