Hash :
bdc82e1c
Author :
Date :
2014-04-24T14:08:29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "git2/types.h"
#include "git2/oid.h"
#include "fetchhead.h"
#include "common.h"
#include "buffer.h"
#include "fileops.h"
#include "filebuf.h"
#include "refs.h"
#include "repository.h"
int git_fetchhead_ref_cmp(const void *a, const void *b)
{
const git_fetchhead_ref *one = (const git_fetchhead_ref *)a;
const git_fetchhead_ref *two = (const git_fetchhead_ref *)b;
if (one->is_merge && !two->is_merge)
return -1;
if (two->is_merge && !one->is_merge)
return 1;
if (one->ref_name && two->ref_name)
return strcmp(one->ref_name, two->ref_name);
else if (one->ref_name)
return -1;
else if (two->ref_name)
return 1;
return 0;
}
int git_fetchhead_ref_create(
git_fetchhead_ref **out,
git_oid *oid,
unsigned int is_merge,
const char *ref_name,
const char *remote_url)
{
git_fetchhead_ref *fetchhead_ref;
assert(out && oid);
*out = NULL;
fetchhead_ref = git__malloc(sizeof(git_fetchhead_ref));
GITERR_CHECK_ALLOC(fetchhead_ref);
memset(fetchhead_ref, 0x0, sizeof(git_fetchhead_ref));
git_oid_cpy(&fetchhead_ref->oid, oid);
fetchhead_ref->is_merge = is_merge;
if (ref_name)
fetchhead_ref->ref_name = git__strdup(ref_name);
if (remote_url)
fetchhead_ref->remote_url = git__strdup(remote_url);
*out = fetchhead_ref;
return 0;
}
static int fetchhead_ref_write(
git_filebuf *file,
git_fetchhead_ref *fetchhead_ref)
{
char oid[GIT_OID_HEXSZ + 1];
const char *type, *name;
int head = 0;
assert(file && fetchhead_ref);
git_oid_fmt(oid, &fetchhead_ref->oid);
oid[GIT_OID_HEXSZ] = '\0';
if (git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_HEADS_DIR) == 0) {
type = "branch ";
name = fetchhead_ref->ref_name + strlen(GIT_REFS_HEADS_DIR);
} else if(git__prefixcmp(fetchhead_ref->ref_name,
GIT_REFS_TAGS_DIR) == 0) {
type = "tag ";
name = fetchhead_ref->ref_name + strlen(GIT_REFS_TAGS_DIR);
} else if (!git__strcmp(fetchhead_ref->ref_name, GIT_HEAD_FILE)) {
head = 1;
} else {
type = "";
name = fetchhead_ref->ref_name;
}
if (head)
return git_filebuf_printf(file, "%s\t\t%s\n", oid, fetchhead_ref->remote_url);
return git_filebuf_printf(file, "%s\t%s\t%s'%s' of %s\n",
oid,
(fetchhead_ref->is_merge) ? "" : "not-for-merge",
type,
name,
fetchhead_ref->remote_url);
}
int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
{
git_filebuf file = GIT_FILEBUF_INIT;
git_buf path = GIT_BUF_INIT;
unsigned int i;
git_fetchhead_ref *fetchhead_ref;
assert(repo && fetchhead_refs);
if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
return -1;
if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE) < 0) {
git_buf_free(&path);
return -1;
}
git_buf_free(&path);
git_vector_sort(fetchhead_refs);
git_vector_foreach(fetchhead_refs, i, fetchhead_ref)
fetchhead_ref_write(&file, fetchhead_ref);
return git_filebuf_commit(&file);
}
static int fetchhead_ref_parse(
git_oid *oid,
unsigned int *is_merge,
git_buf *ref_name,
const char **remote_url,
char *line,
size_t line_num)
{
char *oid_str, *is_merge_str, *desc, *name = NULL;
const char *type = NULL;
int error = 0;
*remote_url = NULL;
if (!*line) {
giterr_set(GITERR_FETCHHEAD,
"Empty line in FETCH_HEAD line %d", line_num);
return -1;
}
/* Compat with old git clients that wrote FETCH_HEAD like a loose ref. */
if ((oid_str = git__strsep(&line, "\t")) == NULL) {
oid_str = line;
line += strlen(line);
*is_merge = 1;
}
if (strlen(oid_str) != GIT_OID_HEXSZ) {
giterr_set(GITERR_FETCHHEAD,
"Invalid object ID in FETCH_HEAD line %d", line_num);
return -1;
}
if (git_oid_fromstr(oid, oid_str) < 0) {
const git_error *oid_err = giterr_last();
const char *err_msg = oid_err ? oid_err->message : "Invalid object ID";
giterr_set(GITERR_FETCHHEAD, "%s in FETCH_HEAD line %d",
err_msg, line_num);
return -1;
}
/* Parse new data from newer git clients */
if (*line) {
if ((is_merge_str = git__strsep(&line, "\t")) == NULL) {
giterr_set(GITERR_FETCHHEAD,
"Invalid description data in FETCH_HEAD line %d", line_num);
return -1;
}
if (*is_merge_str == '\0')
*is_merge = 1;
else if (strcmp(is_merge_str, "not-for-merge") == 0)
*is_merge = 0;
else {
giterr_set(GITERR_FETCHHEAD,
"Invalid for-merge entry in FETCH_HEAD line %d", line_num);
return -1;
}
if ((desc = line) == NULL) {
giterr_set(GITERR_FETCHHEAD,
"Invalid description in FETCH_HEAD line %d", line_num);
return -1;
}
if (git__prefixcmp(desc, "branch '") == 0) {
type = GIT_REFS_HEADS_DIR;
name = desc + 8;
} else if (git__prefixcmp(desc, "tag '") == 0) {
type = GIT_REFS_TAGS_DIR;
name = desc + 5;
} else if (git__prefixcmp(desc, "'") == 0)
name = desc + 1;
if (name) {
if ((desc = strstr(name, "' ")) == NULL ||
git__prefixcmp(desc, "' of ") != 0) {
giterr_set(GITERR_FETCHHEAD,
"Invalid description in FETCH_HEAD line %d", line_num);
return -1;
}
*desc = '\0';
desc += 5;
}
*remote_url = desc;
}
git_buf_clear(ref_name);
if (type)
git_buf_join(ref_name, '/', type, name);
else if(name)
git_buf_puts(ref_name, name);
return error;
}
int git_repository_fetchhead_foreach(git_repository *repo,
git_repository_fetchhead_foreach_cb cb,
void *payload)
{
git_buf path = GIT_BUF_INIT, file = GIT_BUF_INIT, name = GIT_BUF_INIT;
const char *ref_name;
git_oid oid;
const char *remote_url;
unsigned int is_merge = 0;
char *buffer, *line;
size_t line_num = 0;
int error = 0;
assert(repo && cb);
if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
return -1;
if ((error = git_futils_readbuffer(&file, git_buf_cstr(&path))) < 0)
goto done;
buffer = file.ptr;
while ((line = git__strsep(&buffer, "\n")) != NULL) {
++line_num;
if ((error = fetchhead_ref_parse(
&oid, &is_merge, &name, &remote_url, line, line_num)) < 0)
goto done;
if (git_buf_len(&name) > 0)
ref_name = git_buf_cstr(&name);
else
ref_name = NULL;
error = cb(ref_name, remote_url, &oid, is_merge, payload);
if (error) {
giterr_set_after_callback(error);
goto done;
}
}
if (*buffer) {
giterr_set(GITERR_FETCHHEAD, "No EOL at line %d", line_num+1);
error = -1;
goto done;
}
done:
git_buf_free(&file);
git_buf_free(&path);
git_buf_free(&name);
return error;
}
void git_fetchhead_ref_free(git_fetchhead_ref *fetchhead_ref)
{
if (fetchhead_ref == NULL)
return;
git__free(fetchhead_ref->remote_url);
git__free(fetchhead_ref->ref_name);
git__free(fetchhead_ref);
}