| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include <glib.h> |
| 2 |
|
|
#include "config/config.h" |
| 3 |
|
|
|
| 4 |
|
11 |
static gchar* local_config_path() { |
| 5 |
|
11 |
gchar* cwd = g_get_current_dir(); |
| 6 |
|
11 |
gchar* path = g_build_filename(cwd, ".gittorconfig", NULL); |
| 7 |
|
11 |
g_free(cwd); |
| 8 |
|
11 |
return path; |
| 9 |
|
|
} |
| 10 |
|
|
|
| 11 |
|
7 |
extern gchar* global_config_path() { |
| 12 |
|
7 |
const gchar* home = g_get_home_dir(); |
| 13 |
|
7 |
gchar* config_path = g_build_filename(home, ".gittorconfig", NULL); |
| 14 |
|
7 |
return config_path; |
| 15 |
|
|
} |
| 16 |
|
|
|
| 17 |
|
8 |
extern char* config_get(config_scope_e scope, |
| 18 |
|
|
const config_id_t* id, |
| 19 |
|
|
const gchar* default_value) { |
| 20 |
|
8 |
GError* error = NULL; |
| 21 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
GKeyFile* keyfile = g_key_file_new(); |
| 22 |
|
8 |
gchar* value = NULL; |
| 23 |
|
|
|
| 24 |
|
|
// Try local config first |
| 25 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 |
if (scope == CONFIG_SCOPE_LOCAL) { |
| 26 |
1/1
✓ Branch 1 taken 6 times.
|
6 |
gchar* local_path = local_config_path(); |
| 27 |
3/3
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 2 times.
|
6 |
if (g_file_test(local_path, G_FILE_TEST_EXISTS)) { |
| 28 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
g_key_file_load_from_file(keyfile, local_path, G_KEY_FILE_NONE, |
| 29 |
|
|
&error); |
| 30 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
if (!error) { |
| 31 |
|
|
value = |
| 32 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
g_key_file_get_string(keyfile, id->group, id->key, NULL); |
| 33 |
|
|
} |
| 34 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
g_clear_error(&error); |
| 35 |
|
|
} |
| 36 |
1/1
✓ Branch 1 taken 6 times.
|
6 |
g_free(local_path); |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
|
|
// If not found, try global config |
| 40 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 |
if (!value) { |
| 41 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
gchar* global_path = global_config_path(); |
| 42 |
3/3
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 times.
|
4 |
if (g_file_test(global_path, G_FILE_TEST_EXISTS)) { |
| 43 |
1/1
✓ Branch 1 taken 3 times.
|
3 |
g_key_file_load_from_file(keyfile, global_path, G_KEY_FILE_NONE, |
| 44 |
|
|
&error); |
| 45 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
if (!error) { |
| 46 |
|
|
value = |
| 47 |
1/1
✓ Branch 1 taken 3 times.
|
3 |
g_key_file_get_string(keyfile, id->group, id->key, NULL); |
| 48 |
|
|
} |
| 49 |
1/1
✓ Branch 1 taken 3 times.
|
3 |
g_clear_error(&error); |
| 50 |
|
|
} |
| 51 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
g_free(global_path); |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
|
|
// If still not found, use default |
| 55 |
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
8 |
if (!value && default_value) { |
| 56 |
|
✗ |
value = g_strdup(default_value); |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_key_file_unref(keyfile); |
| 60 |
|
8 |
return value; |
| 61 |
|
|
} |
| 62 |
|
|
|
| 63 |
|
8 |
extern int config_set(config_scope_e scope, |
| 64 |
|
|
const config_id_t* id, |
| 65 |
|
|
const gchar* value) { |
| 66 |
|
8 |
GError* error = NULL; |
| 67 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
GKeyFile* keyfile = g_key_file_new(); |
| 68 |
|
|
|
| 69 |
1/1
✓ Branch 1 taken 5 times.
|
5 |
gchar* path = (scope == CONFIG_SCOPE_LOCAL) ? local_config_path() |
| 70 |
3/3
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
|
8 |
: global_config_path(); |
| 71 |
|
|
|
| 72 |
|
8 |
GKeyFileFlags flags = |
| 73 |
|
|
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS; |
| 74 |
|
|
|
| 75 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_key_file_load_from_file(keyfile, path, flags, &error); |
| 76 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_clear_error(&error); |
| 77 |
|
|
|
| 78 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_key_file_set_string(keyfile, id->group, id->key, value); |
| 79 |
|
|
|
| 80 |
|
|
gsize length; |
| 81 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
gchar* data = g_key_file_to_data(keyfile, &length, &error); |
| 82 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 |
if (error) { |
| 83 |
|
✗ |
g_printerr("Error exporting config: %s\n", error->message); |
| 84 |
|
✗ |
g_clear_error(&error); |
| 85 |
|
✗ |
g_key_file_unref(keyfile); |
| 86 |
|
✗ |
g_free(path); |
| 87 |
|
8 |
return -1; |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_file_set_contents(path, data, (gssize)length, &error); |
| 91 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 |
if (error) { |
| 92 |
|
✗ |
g_printerr("Error saving config file: %s\n", error->message); |
| 93 |
|
✗ |
g_clear_error(&error); |
| 94 |
|
✗ |
g_free(data); |
| 95 |
|
✗ |
g_key_file_unref(keyfile); |
| 96 |
|
✗ |
g_free(path); |
| 97 |
|
✗ |
return -1; |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_free(data); |
| 101 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_free(path); |
| 102 |
1/1
✓ Branch 1 taken 8 times.
|
8 |
g_key_file_unref(keyfile); |
| 103 |
|
8 |
return 0; |
| 104 |
|
|
} |
| 105 |
|
|
|