GCC Code Coverage Report


src/
File: src/init/init_cmd.c
Date: 2026-03-31 17:48:22
Lines:
43/43
100.0%
Functions:
2/2
100.0%
Branches:
17/19
89.5%

Line Branch Exec Source
1 #include <assert.h>
2 #include <glib.h>
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "cmd/cmd.h"
8 #include "init/init.h"
9
10 #define KEY_USAGE 1
11
12 static error_t parse_opt(int key, char* arg, struct argp_state* state);
13
14 struct init_arguments {
15 struct global_arguments* global;
16 char* dir;
17 };
18
19 static struct argp_option options[] = {
20 {"help", '?', NULL, 0, "Give this help list", -2},
21 {"usage", KEY_USAGE, NULL, 0, "Give a short usage message", -1},
22 {NULL}};
23
24 static char doc[] =
25 "Initializes a new GitTor repository in the current directory.";
26
27 static struct argp argp = {options, parse_opt, "[DIRECTORY]", doc,
28 NULL, NULL, NULL};
29
30 static bool helped;
31 34 static error_t parse_opt(int key, char* arg, struct argp_state* state) {
32 34 struct init_arguments* args = state->input;
33 size_t arglen;
34
35
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 29 times.
34 switch (key) {
36 3 case '?':
37 3 argp_help(&argp, stdout, ARGP_HELP_STD_HELP, state->name);
38 3 helped = true;
39 3 break;
40
41 1 case KEY_USAGE:
42 1 argp_help(&argp, stdout, ARGP_HELP_STD_USAGE, state->name);
43 1 helped = true;
44 1 break;
45
46 1 case ARGP_KEY_ARG:
47 1 arglen = strlen(arg);
48 1 args->dir = malloc(arglen + 1);
49 1 g_snprintf(args->dir, arglen + 1, "%s", arg);
50 1 break;
51
52 29 default:
53 29 return ARGP_ERR_UNKNOWN;
54 }
55
56 5 return 0;
57 }
58
59 6 extern int gittor_init(struct argp_state* state) {
60 // Set defaults arguments
61 6 struct init_arguments args = {.dir = NULL};
62 6 helped = false;
63
64 // Change the arguments array for just init
65 6 int argc = state->argc - state->next + 1;
66 6 char** argv = &state->argv[state->next - 1];
67 6 args.global = state->input;
68
69 // Change the command name to gittor init
70 6 const char name[] = "init";
71 6 size_t argv0len = strlen(state->name) + sizeof(name) + 1;
72 6 char* argv0 = argv[0];
73 6 argv[0] = malloc(argv0len);
74
1/1
✓ Branch 1 taken 6 times.
6 g_snprintf(argv[0], argv0len, "%s %s", state->name, name);
75
76 // Parse arguments
77
1/1
✓ Branch 1 taken 6 times.
6 int err = argp_parse(&argp, argc, argv, ARGP_NO_EXIT, &argc, &args);
78
79 // Create bare repository
80 char bare_repo[FILE_URL_MAX];
81
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
6 if (!err && !helped) {
82
1/1
✓ Branch 1 taken 2 times.
2 err = create_bare_repo(bare_repo);
83 }
84
85 // Clone bare repository
86
1/1
✓ Branch 1 taken 6 times.
6 gchar* path = g_build_filename(args.global->path, args.dir, NULL);
87
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
6 if (!err && !helped) {
88
1/1
✓ Branch 1 taken 2 times.
2 err = clone_bare_repo(bare_repo, path);
89 }
90 6 free(path);
91
92 // Reset back to global
93 6 free(argv[0]);
94 6 argv[0] = argv0;
95 6 state->next += argc - 1;
96
97
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (args.dir) {
98 1 free(args.dir);
99 }
100 6 return err;
101 }
102