Skip to content

Commit 313908f

Browse files
committed
examples: normalize decls and usage of options structs
1 parent 4a4ad2b commit 313908f

File tree

12 files changed

+86
-89
lines changed

12 files changed

+86
-89
lines changed

examples/add.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct index_options {
4040
};
4141

4242
/* Forward declarations for helpers */
43-
static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args);
43+
static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args);
4444
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
4545

4646
int lg2_add(git_repository *repo, int argc, char **argv)
@@ -86,13 +86,13 @@ int lg2_add(git_repository *repo, int argc, char **argv)
8686
*/
8787
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
8888
{
89-
struct index_options options = *(struct index_options *)(payload);
89+
struct index_options *opts = (struct index_options *)(payload);
9090
int ret;
9191
unsigned status;
9292
(void)matched_pathspec;
9393

9494
/* Get the file status */
95-
if (git_status_file(&status, options.repo, path) < 0)
95+
if (git_status_file(&status, opts->repo, path) < 0)
9696
return -1;
9797

9898
if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
@@ -102,7 +102,7 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
102102
ret = 1;
103103
}
104104

105-
if (options.dry_run)
105+
if (opts->dry_run)
106106
ret = 1;
107107

108108
return ret;
@@ -132,7 +132,7 @@ void print_usage(void)
132132
exit(1);
133133
}
134134

135-
static void parse_opts(const char **repo_path, struct index_options *options, struct args_info *args)
135+
static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args)
136136
{
137137
if (args->argc <= 1)
138138
print_usage();
@@ -142,20 +142,20 @@ static void parse_opts(const char **repo_path, struct index_options *options, st
142142

143143
if (curr[0] != '-') {
144144
if (!strcmp("add", curr)) {
145-
options->mode = INDEX_ADD;
145+
opts->mode = INDEX_ADD;
146146
continue;
147-
} else if (options->mode == INDEX_NONE) {
147+
} else if (opts->mode == INDEX_NONE) {
148148
fprintf(stderr, "missing command: %s", curr);
149149
print_usage();
150150
break;
151151
} else {
152152
/* We might be looking at a filename */
153153
break;
154154
}
155-
} else if (match_bool_arg(&options->verbose, args, "--verbose") ||
156-
match_bool_arg(&options->dry_run, args, "--dry-run") ||
155+
} else if (match_bool_arg(&opts->verbose, args, "--verbose") ||
156+
match_bool_arg(&opts->dry_run, args, "--dry-run") ||
157157
match_str_arg(repo_path, args, "--git-dir") ||
158-
(options->mode == INDEX_ADD && match_bool_arg(&options->add_update, args, "--update"))) {
158+
(opts->mode == INDEX_ADD && match_bool_arg(&opts->add_update, args, "--update"))) {
159159
continue;
160160
} else if (match_bool_arg(NULL, args, "--help")) {
161161
print_usage();

examples/blame.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* simulate the output of `git blame` and a few of its command line arguments.
2020
*/
2121

22-
struct opts {
22+
struct blame_opts {
2323
char *path;
2424
char *commitspec;
2525
int C;
@@ -28,14 +28,14 @@ struct opts {
2828
int end_line;
2929
int F;
3030
};
31-
static void parse_opts(struct opts *o, int argc, char *argv[]);
31+
static void parse_opts(struct blame_opts *o, int argc, char *argv[]);
3232

3333
int lg2_blame(git_repository *repo, int argc, char *argv[])
3434
{
3535
int line, break_on_null_hunk;
3636
git_off_t i, rawsize;
3737
char spec[1024] = {0};
38-
struct opts o = {0};
38+
struct blame_opts o = {0};
3939
const char *rawdata;
4040
git_revspec revspec = {0};
4141
git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
@@ -143,7 +143,7 @@ static void usage(const char *msg, const char *arg)
143143
}
144144

145145
/** Parse the arguments. */
146-
static void parse_opts(struct opts *o, int argc, char *argv[])
146+
static void parse_opts(struct blame_opts *o, int argc, char *argv[])
147147
{
148148
int i;
149149
char *bare_args[3] = {0};

examples/cat-file.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,28 @@ static void show_tag(const git_tag *tag)
102102
printf("\n%s\n", git_tag_message(tag));
103103
}
104104

105-
enum {
105+
typedef enum {
106106
SHOW_TYPE = 1,
107107
SHOW_SIZE = 2,
108108
SHOW_NONE = 3,
109109
SHOW_PRETTY = 4
110-
};
110+
} catfile_mode;
111111

112112
/* Forward declarations for option-parsing helper */
113-
struct opts {
113+
struct catfile_options {
114114
const char *dir;
115115
const char *rev;
116-
int action;
116+
catfile_mode action;
117117
int verbose;
118118
};
119-
static void parse_opts(struct opts *o, int argc, char *argv[]);
119+
120+
static void parse_opts(struct catfile_options *o, int argc, char *argv[]);
120121

121122

122123
/** Entry point for this command */
123124
int lg2_cat_file(git_repository *repo, int argc, char *argv[])
124125
{
125-
struct opts o = { ".", NULL, 0, 0 };
126+
struct catfile_options o = { ".", NULL, 0, 0 };
126127
git_object *obj = NULL;
127128
char oidstr[GIT_OID_HEXSZ + 1];
128129

@@ -201,7 +202,7 @@ static void usage(const char *message, const char *arg)
201202
}
202203

203204
/** Parse the command-line options taken from git */
204-
static void parse_opts(struct opts *o, int argc, char *argv[])
205+
static void parse_opts(struct catfile_options *o, int argc, char *argv[])
205206
{
206207
struct args_info args = ARGS_INFO_INIT;
207208

examples/describe.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@
3737
*/
3838

3939
/** describe_options represents the parsed command line options */
40-
typedef struct {
40+
struct describe_options {
4141
const char **commits;
4242
size_t commit_count;
4343
git_describe_options describe_options;
4444
git_describe_format_options format_options;
45-
} describe_options;
45+
};
4646

47-
typedef struct args_info args_info;
48-
49-
static void opts_add_commit(describe_options *opts, const char *commit)
47+
static void opts_add_commit(struct describe_options *opts, const char *commit)
5048
{
5149
size_t sz;
5250

@@ -57,7 +55,7 @@ static void opts_add_commit(describe_options *opts, const char *commit)
5755
opts->commits[opts->commit_count - 1] = commit;
5856
}
5957

60-
static void do_describe_single(git_repository *repo, describe_options *opts, const char *rev)
58+
static void do_describe_single(git_repository *repo, struct describe_options *opts, const char *rev)
6159
{
6260
git_object *commit;
6361
git_describe_result *describe_result;
@@ -80,7 +78,7 @@ static void do_describe_single(git_repository *repo, describe_options *opts, con
8078
printf("%s\n", buf.ptr);
8179
}
8280

83-
static void do_describe(git_repository *repo, describe_options *opts)
81+
static void do_describe(git_repository *repo, struct describe_options *opts)
8482
{
8583
if (opts->commit_count == 0)
8684
do_describe_single(repo, opts, NULL);
@@ -99,9 +97,9 @@ static void print_usage(void)
9997
}
10098

10199
/** Parse command line arguments */
102-
static void parse_options(describe_options *opts, int argc, char **argv)
100+
static void parse_options(struct describe_options *opts, int argc, char **argv)
103101
{
104-
args_info args = ARGS_INFO_INIT;
102+
struct args_info args = ARGS_INFO_INIT;
105103

106104
for (args.pos = 1; args.pos < argc; ++args.pos) {
107105
const char *curr = argv[args.pos];
@@ -141,7 +139,7 @@ static void parse_options(describe_options *opts, int argc, char **argv)
141139
}
142140

143141
/** Initialize describe_options struct */
144-
static void describe_options_init(describe_options *opts)
142+
static void describe_options_init(struct describe_options *opts)
145143
{
146144
memset(opts, 0, sizeof(*opts));
147145

@@ -153,7 +151,7 @@ static void describe_options_init(describe_options *opts)
153151

154152
int lg2_describe(git_repository *repo, int argc, char **argv)
155153
{
156-
describe_options opts;
154+
struct describe_options opts;
157155

158156
describe_options_init(&opts);
159157
parse_options(&opts, argc, argv);

examples/diff.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ enum {
4747
CACHE_NONE = 2
4848
};
4949

50-
/** The 'opts' struct captures all the various parsed command line options. */
51-
struct opts {
50+
/** The 'diff_options' struct captures all the various parsed command line options. */
51+
struct diff_options {
5252
git_diff_options diffopts;
5353
git_diff_find_options findopts;
5454
int color;
@@ -63,18 +63,17 @@ struct opts {
6363

6464
/** These functions are implemented at the end */
6565
static void usage(const char *message, const char *arg);
66-
static void parse_opts(struct opts *o, int argc, char *argv[]);
66+
static void parse_opts(struct diff_options *o, int argc, char *argv[]);
6767
static int color_printer(
6868
const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
69-
static void diff_print_stats(git_diff *diff, struct opts *o);
70-
static void compute_diff_no_index(git_diff **diff, struct opts *o);
69+
static void diff_print_stats(git_diff *diff, struct diff_options *o);
70+
static void compute_diff_no_index(git_diff **diff, struct diff_options *o);
7171

7272
int lg2_diff(git_repository *repo, int argc, char *argv[])
7373
{
7474
git_tree *t1 = NULL, *t2 = NULL;
7575
git_diff *diff;
76-
77-
struct opts o = {
76+
struct diff_options o = {
7877
GIT_DIFF_OPTIONS_INIT, GIT_DIFF_FIND_OPTIONS_INIT,
7978
-1, -1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
8079
};
@@ -166,7 +165,7 @@ int lg2_diff(git_repository *repo, int argc, char *argv[])
166165
return 0;
167166
}
168167

169-
static void compute_diff_no_index(git_diff **diff, struct opts *o) {
168+
static void compute_diff_no_index(git_diff **diff, struct diff_options *o) {
170169
git_patch *patch = NULL;
171170
char *file1_str = NULL;
172171
char *file2_str = NULL;
@@ -242,11 +241,10 @@ static int color_printer(
242241
}
243242

244243
/** Parse arguments as copied from git-diff. */
245-
static void parse_opts(struct opts *o, int argc, char *argv[])
244+
static void parse_opts(struct diff_options *o, int argc, char *argv[])
246245
{
247246
struct args_info args = ARGS_INFO_INIT;
248247

249-
250248
for (args.pos = 1; args.pos < argc; ++args.pos) {
251249
const char *a = argv[args.pos];
252250

@@ -343,7 +341,7 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
343341
}
344342

345343
/** Display diff output with "--stat", "--numstat", or "--shortstat" */
346-
static void diff_print_stats(git_diff *diff, struct opts *o)
344+
static void diff_print_stats(git_diff *diff, struct diff_options *o)
347345
{
348346
git_diff_stats *stats;
349347
git_buf b = GIT_BUF_INIT_CONST(NULL, 0);

examples/init.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828

2929
/** Forward declarations of helpers */
30-
struct opts {
30+
struct init_opts {
3131
int no_options;
3232
int quiet;
3333
int bare;
@@ -38,11 +38,11 @@ struct opts {
3838
const char *dir;
3939
};
4040
static void create_initial_commit(git_repository *repo);
41-
static void parse_opts(struct opts *o, int argc, char *argv[]);
41+
static void parse_opts(struct init_opts *o, int argc, char *argv[]);
4242

4343
int lg2_init(git_repository *repo, int argc, char *argv[])
4444
{
45-
struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
45+
struct init_opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
4646

4747
parse_opts(&o, argc, argv);
4848

@@ -210,7 +210,7 @@ static uint32_t parse_shared(const char *shared)
210210
return 0;
211211
}
212212

213-
static void parse_opts(struct opts *o, int argc, char *argv[])
213+
static void parse_opts(struct init_opts *o, int argc, char *argv[])
214214
{
215215
struct args_info args = ARGS_INFO_INIT;
216216
const char *sharedarg;

examples/ls-files.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
* This currently supports the default behavior and the `--error-unmatch` option.
2626
*/
2727

28-
typedef struct {
28+
struct ls_options {
2929
int error_unmatch;
3030
char *files[1024];
3131
size_t file_count;
32-
} ls_options;
32+
};
3333

3434
static void usage(const char *message, const char *arg)
3535
{
@@ -41,12 +41,12 @@ static void usage(const char *message, const char *arg)
4141
exit(1);
4242
}
4343

44-
static int parse_options(ls_options *opts, int argc, char *argv[])
44+
static int parse_options(struct ls_options *opts, int argc, char *argv[])
4545
{
4646
int parsing_files = 0;
4747
int i;
4848

49-
memset(opts, 0, sizeof(ls_options));
49+
memset(opts, 0, sizeof(struct ls_options));
5050

5151
if (argc < 2)
5252
return 0;
@@ -78,7 +78,7 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
7878
return 0;
7979
}
8080

81-
static int print_paths(ls_options *opts, git_index *index)
81+
static int print_paths(struct ls_options *opts, git_index *index)
8282
{
8383
size_t i;
8484
const git_index_entry *entry;
@@ -113,7 +113,7 @@ static int print_paths(ls_options *opts, git_index *index)
113113
int lg2_ls_files(git_repository *repo, int argc, char *argv[])
114114
{
115115
git_index *index = NULL;
116-
ls_options opts;
116+
struct ls_options opts;
117117
int error;
118118

119119
if ((error = parse_options(&opts, argc, argv)) < 0)

0 commit comments

Comments
 (0)