| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include <git2.h> |
| 2 |
|
|
#include <glib.h> |
| 3 |
|
|
#include <stdbool.h> |
| 4 |
|
|
#include <stdio.h> |
| 5 |
|
|
#include <stdlib.h> |
| 6 |
|
|
#include "init/init.h" |
| 7 |
|
|
|
| 8 |
|
|
/** |
| 9 |
|
|
* @brief Setup GitTor specific configurations. |
| 10 |
|
|
* |
| 11 |
|
|
* @param repo The repository to configure |
| 12 |
|
|
* @return int error code |
| 13 |
|
|
*/ |
| 14 |
|
4 |
static int repo_config(git_repository* repo) { |
| 15 |
|
4 |
int error = 0; |
| 16 |
|
4 |
git_config* cfg = NULL; |
| 17 |
|
|
|
| 18 |
|
|
// Initialize libgit2 |
| 19 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
int ret = git_libgit2_init(); |
| 20 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 |
if (ret < 0) { |
| 21 |
|
✗ |
error = ret; |
| 22 |
|
|
} |
| 23 |
|
|
|
| 24 |
|
|
// Open the configurations file |
| 25 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
if (!error) { |
| 26 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
error = git_repository_config(&cfg, repo); |
| 27 |
|
|
} |
| 28 |
|
|
|
| 29 |
|
|
// Configure seed |
| 30 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
if (!error) { |
| 31 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
error = git_config_set_string(cfg, "alias.seed", "!gittor seed"); |
| 32 |
|
|
} |
| 33 |
|
|
|
| 34 |
|
|
// Configure leech |
| 35 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
if (!error) { |
| 36 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
error = git_config_set_string(cfg, "alias.leech", "!gittor leech"); |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
git_config_free(cfg); |
| 40 |
1/1
✓ Branch 1 taken 4 times.
|
4 |
git_libgit2_shutdown(); |
| 41 |
|
4 |
return error; |
| 42 |
|
|
} |
| 43 |
|
|
|
| 44 |
|
2 |
extern int create_bare_repo(char url[FILE_URL_MAX]) { |
| 45 |
|
2 |
int error = 0; |
| 46 |
|
2 |
const git_error* e = NULL; |
| 47 |
|
2 |
git_treebuilder* builder = NULL; |
| 48 |
|
2 |
git_tree* tree = NULL; |
| 49 |
|
2 |
git_signature* me = NULL; |
| 50 |
|
2 |
git_repository* repo = NULL; |
| 51 |
|
|
|
| 52 |
|
|
// Create temporary directory for repo |
| 53 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
gchar* dir = g_dir_make_tmp("gittor-XXXXXX", NULL); |
| 54 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (dir == NULL) { |
| 55 |
|
✗ |
g_error("Failed to create temporary directory"); |
| 56 |
|
2 |
return 1; |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
// Initialize libgit2 |
| 60 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
int ret = git_libgit2_init(); |
| 61 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (ret < 0) { |
| 62 |
|
✗ |
error = ret; |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
|
|
// Initialize a new bare repo |
| 66 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 67 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = git_repository_init(&repo, dir, true); |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
|
// Load up a new git tree |
| 71 |
|
|
git_oid tree_oid; |
| 72 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 73 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = git_treebuilder_new(&builder, repo, NULL); |
| 74 |
|
|
} |
| 75 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 76 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = git_treebuilder_write(&tree_oid, builder); |
| 77 |
|
|
} |
| 78 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 79 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = git_tree_lookup(&tree, repo, &tree_oid); |
| 80 |
|
|
} |
| 81 |
|
|
|
| 82 |
|
|
// Load up users personal info |
| 83 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 84 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = git_signature_default(&me, repo); |
| 85 |
|
|
|
| 86 |
|
|
// If they have no default author set, use these |
| 87 |
3/5
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
|
2 |
if (error == -3 && git_error_last()->klass == 7) { |
| 88 |
1/1
✓ Branch 2 taken 2 times.
|
2 |
error = git_signature_new(&me, "Default User", |
| 89 |
|
|
"default@example.com", time(NULL), 0); |
| 90 |
|
|
} |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
// Create an intial commit with users info |
| 94 |
|
|
git_oid commit_oid; |
| 95 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 96 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = git_commit_create(&commit_oid, repo, "HEAD", me, me, "UTF-8", |
| 97 |
|
|
"init", tree, 0, NULL); |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
// Configure repo |
| 101 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 102 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = repo_config(repo); |
| 103 |
|
|
} |
| 104 |
|
|
|
| 105 |
|
|
// Close the repository |
| 106 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_repository_free(repo); |
| 107 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_treebuilder_free(builder); |
| 108 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_tree_free(tree); |
| 109 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_signature_free(me); |
| 110 |
|
|
|
| 111 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 112 |
|
|
// Get the initial commit hash |
| 113 |
|
|
char oid_str[GIT_OID_HEXSZ + 1]; |
| 114 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_oid_tostr(oid_str, sizeof(oid_str), &commit_oid); |
| 115 |
|
|
|
| 116 |
|
|
// Build the permanent path to the repo |
| 117 |
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 |
gchar* new_dir = g_build_filename(g_get_user_config_dir(), "gittor", |
| 118 |
|
|
"repos", oid_str, NULL); |
| 119 |
|
|
|
| 120 |
|
|
// Make sure parent directories exist |
| 121 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
gchar* parent = g_path_get_dirname(new_dir); |
| 122 |
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
|
2 |
if (g_mkdir_with_parents(parent, 0700) != 0) { |
| 123 |
|
✗ |
g_error("Failed to create parent directories"); |
| 124 |
|
|
error = 2; |
| 125 |
|
|
} |
| 126 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
g_free(parent); |
| 127 |
|
|
|
| 128 |
|
|
// Move repo to permanent path |
| 129 |
|
2 |
rename(dir, new_dir); |
| 130 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
g_snprintf(url, FILE_URL_MAX, "file://%s", new_dir); |
| 131 |
|
2 |
free(new_dir); |
| 132 |
|
|
} |
| 133 |
|
|
|
| 134 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (error < 0) { |
| 135 |
|
✗ |
e = git_error_last(); |
| 136 |
|
✗ |
printf("Error %d/%d: %s\n", error, e->klass, e->message); |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
2 |
free(dir); |
| 140 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_libgit2_shutdown(); |
| 141 |
|
2 |
return error; |
| 142 |
|
|
} |
| 143 |
|
|
|
| 144 |
|
2 |
extern int clone_bare_repo(char url[FILE_URL_MAX], char path[PATH_MAX]) { |
| 145 |
|
2 |
int error = 0; |
| 146 |
|
2 |
const git_error* e = NULL; |
| 147 |
|
2 |
git_repository* repo = NULL; |
| 148 |
|
|
|
| 149 |
|
|
// Initialize libgit2 |
| 150 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
int ret = git_libgit2_init(); |
| 151 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (ret < 0) { |
| 152 |
|
✗ |
error = ret; |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
|
|
// Clone bare repository |
| 156 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 157 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = git_clone(&repo, url, path, NULL); |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
// Configure repo |
| 161 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (!error) { |
| 162 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
error = repo_config(repo); |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (error < 0) { |
| 166 |
|
✗ |
e = git_error_last(); |
| 167 |
|
✗ |
printf("Error %d/%d: %s\n", error, e->klass, e->message); |
| 168 |
|
|
} |
| 169 |
|
|
|
| 170 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_repository_free(repo); |
| 171 |
1/1
✓ Branch 1 taken 2 times.
|
2 |
git_libgit2_shutdown(); |
| 172 |
|
2 |
return error; |
| 173 |
|
|
} |
| 174 |
|
|
|