reflog.c 11 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10 11 12
 */

#include "reflog.h"
#include "repository.h"
#include "filebuf.h"
#include "signature.h"

13
static int reflog_init(git_reflog **reflog, const git_reference *ref)
14 15 16 17 18
{
	git_reflog *log;

	*reflog = NULL;

19 20
	log = git__calloc(1, sizeof(git_reflog));
	GITERR_CHECK_ALLOC(log);
21 22

	log->ref_name = git__strdup(ref->name);
23
	GITERR_CHECK_ALLOC(log->ref_name);
24 25

	if (git_vector_init(&log->entries, 0, NULL) < 0) {
26 27
		git__free(log->ref_name);
		git__free(log);
28
		return -1;
29 30
	}

31
	log->owner = git_reference_owner(ref);
32 33
	*reflog = log;

34
	return 0;
35 36
}

37 38 39 40 41 42
static int serialize_reflog_entry(
	git_buf *buf,
	const git_oid *oid_old,
	const git_oid *oid_new,
	const git_signature *committer,
	const char *msg)
43
{
44 45
	char raw_old[GIT_OID_HEXSZ+1];
	char raw_new[GIT_OID_HEXSZ+1];
46

47 48 49 50 51 52 53 54 55 56 57 58 59
	git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
	git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);

	git_buf_clear(buf);

	git_buf_puts(buf, raw_old);
	git_buf_putc(buf, ' ');
	git_buf_puts(buf, raw_new);

	git_signature__writebuf(buf, " ", committer);

	/* drop trailing LF */
	git_buf_rtrim(buf);
60

61
	if (msg) {
62 63
		git_buf_putc(buf, '\t');
		git_buf_puts(buf, msg);
64 65
	}

66
	git_buf_putc(buf, '\n');
67

68 69
	return git_buf_oom(buf);
}
70

71
static int reflog_entry_new(git_reflog_entry **entry)
72
{
73
	git_reflog_entry *e;
74

75
	assert(entry);
76

77 78
	e = git__malloc(sizeof(git_reflog_entry));
	GITERR_CHECK_ALLOC(e);
79

80
	memset(e, 0, sizeof(git_reflog_entry));
81

82
	*entry = e;
83

84 85
	return 0;
}
86

87 88 89
static void reflog_entry_free(git_reflog_entry *entry)
{
	git_signature_free(entry->committer);
90

91 92
	git__free(entry->msg);
	git__free(entry);
93 94 95 96 97 98 99
}

static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
{
	const char *ptr;
	git_reflog_entry *entry;

100
#define seek_forward(_increase) do { \
schu committed
101
	if (_increase >= buf_size) { \
102 103
		giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
		goto fail; \
schu committed
104
	} \
105 106
	buf += _increase; \
	buf_size -= _increase; \
107
	} while (0)
108 109

	while (buf_size > GIT_REFLOG_SIZE_MIN) {
110 111
		if (reflog_entry_new(&entry) < 0)
			return -1;
112

113 114 115 116 117
		entry->committer = git__malloc(sizeof(git_signature));
		GITERR_CHECK_ALLOC(entry->committer);

		if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0)
			goto fail;
118
		seek_forward(GIT_OID_HEXSZ + 1);
119

120 121
		if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0)
			goto fail;
122
		seek_forward(GIT_OID_HEXSZ + 1);
123 124 125 126 127 128 129

		ptr = buf;

		/* Seek forward to the end of the signature. */
		while (*buf && *buf != '\t' && *buf != '\n')
			seek_forward(1);

130 131
		if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0)
			goto fail;
132 133 134 135

		if (*buf == '\t') {
			/* We got a message. Read everything till we reach LF. */
			seek_forward(1);
136
			ptr = buf;
137 138 139 140

			while (*buf && *buf != '\n')
				seek_forward(1);

141
			entry->msg = git__strndup(ptr, buf - ptr);
142
			GITERR_CHECK_ALLOC(entry->msg);
143 144 145 146 147 148
		} else
			entry->msg = NULL;

		while (*buf && *buf == '\n' && buf_size > 1)
			seek_forward(1);

149 150
		if (git_vector_insert(&log->entries, entry) < 0)
			goto fail;
151 152
	}

153 154
	return 0;

155 156
#undef seek_forward

157
fail:
158 159
	if (entry)
		reflog_entry_free(entry);
160

161
	return -1;
162 163
}

164 165
void git_reflog_free(git_reflog *reflog)
{
166
	size_t i;
167 168
	git_reflog_entry *entry;

169 170 171
	if (reflog == NULL)
		return;

172 173 174
	for (i=0; i < reflog->entries.length; i++) {
		entry = git_vector_get(&reflog->entries, i);

175
		reflog_entry_free(entry);
176 177 178
	}

	git_vector_free(&reflog->entries);
179 180
	git__free(reflog->ref_name);
	git__free(reflog);
181 182
}

183
static int retrieve_reflog_path(git_buf *path, const git_reference *ref)
184 185 186 187 188
{
	return git_buf_join_n(path, '/', 3,
		git_reference_owner(ref)->path_repository, GIT_REFLOG_DIR, ref->name);
}

189
static int create_new_reflog_file(const char *filepath)
190
{
191 192 193 194
	int fd, error;

	if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
		return error;
195 196 197 198 199 200 201 202 203

	if ((fd = p_open(filepath,
			O_WRONLY | O_CREAT | O_TRUNC,
			GIT_REFLOG_FILE_MODE)) < 0)
		return -1;

	return p_close(fd);
}

204
int git_reflog_read(git_reflog **reflog, const git_reference *ref)
205
{
206
	int error = -1;
207
	git_buf log_path = GIT_BUF_INIT;
208
	git_buf log_file = GIT_BUF_INIT;
209 210
	git_reflog *log = NULL;

211 212
	assert(reflog && ref);

213 214
	*reflog = NULL;

215 216
	if (reflog_init(&log, ref) < 0)
		return -1;
217

218 219
	if (retrieve_reflog_path(&log_path, ref) < 0)
		goto cleanup;
220

221 222 223
	error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path));
	if (error < 0 && error != GIT_ENOTFOUND)
		goto cleanup;
224

225 226 227 228
	if ((error == GIT_ENOTFOUND) &&
		((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0))
		goto cleanup;

229 230 231
	if ((error = reflog_parse(log,
		git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0)
		goto cleanup;
232

233 234 235 236 237
	*reflog = log;
	goto success;

cleanup:
	git_reflog_free(log);
238

239
success:
240
	git_buf_free(&log_file);
241
	git_buf_free(&log_path);
242

243
	return error;
244 245
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
int git_reflog_write(git_reflog *reflog)
{
	int error = -1;
	unsigned int i;
	git_reflog_entry *entry;
	git_buf log_path = GIT_BUF_INIT;
	git_buf log = GIT_BUF_INIT;
	git_filebuf fbuf = GIT_FILEBUF_INIT;

	assert(reflog);

	if (git_buf_join_n(&log_path, '/', 3,
		git_repository_path(reflog->owner), GIT_REFLOG_DIR, reflog->ref_name) < 0)
		return -1;

261 262 263 264 265 266
	if (!git_path_isfile(git_buf_cstr(&log_path))) {
		giterr_set(GITERR_INVALID,
			"Log file for reference '%s' doesn't exist.", reflog->ref_name);
		goto cleanup;
	}

267 268 269 270 271 272 273 274 275 276
	if ((error = git_filebuf_open(&fbuf, git_buf_cstr(&log_path), 0)) < 0)
		goto cleanup;

	git_vector_foreach(&reflog->entries, i, entry) {
		if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
			goto cleanup;

		if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
			goto cleanup;
	}
277

278 279 280 281 282 283 284 285 286 287 288 289
	error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE);
	goto success;

cleanup:
	git_filebuf_cleanup(&fbuf);

success:
	git_buf_free(&log);
	git_buf_free(&log_path);
	return error;
}

290
int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
Vicent Marti committed
291
				const git_signature *committer, const char *msg)
292
{
293
	git_reflog_entry *entry;
294
	const git_reflog_entry *previous;
295
	const char *newline;
296

297 298 299
	assert(reflog && new_oid && committer);

	if (reflog_entry_new(&entry) < 0)
300
		return -1;
301

302
	if ((entry->committer = git_signature_dup(committer)) == NULL)
303
		goto cleanup;
304

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	if (msg != NULL) {
		if ((entry->msg = git__strdup(msg)) == NULL)
			goto cleanup;

		newline = strchr(msg, '\n');

		if (newline) {
			if (newline[1] != '\0') {
				giterr_set(GITERR_INVALID, "Reflog message cannot contain newline");
				goto cleanup;
			}

			entry->msg[newline - msg] = '\0';
		}
	}

321
	previous = git_reflog_entry_byindex(reflog, 0);
322

323
	if (previous == NULL)
324
		git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO);
325
	else
326 327 328 329 330
		git_oid_cpy(&entry->oid_old, &previous->oid_cur);

	git_oid_cpy(&entry->oid_cur, new_oid);

	if (git_vector_insert(&reflog->entries, entry) < 0)
331 332
		goto cleanup;

333
	return 0;
334

335
cleanup:
336 337
	reflog_entry_free(entry);
	return -1;
338 339
}

340 341
int git_reflog_rename(git_reference *ref, const char *new_name)
{
342
	int error = 0, fd;
343 344
	git_buf old_path = GIT_BUF_INIT;
	git_buf new_path = GIT_BUF_INIT;
345
	git_buf temp_path = GIT_BUF_INIT;
346
	git_buf normalized = GIT_BUF_INIT;
347

348 349
	assert(ref && new_name);

350
	if ((error = git_reference__normalize_name(
351
		&normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0)
352
			return error;
353

354
	if (git_buf_joinpath(&temp_path, git_reference_owner(ref)->path_repository, GIT_REFLOG_DIR) < 0)
355
		return -1;
356 357

	if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), ref->name) < 0)
358
		return -1;
359

360 361
	if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0)
		return -1;
362 363 364 365 366 367 368 369 370

	/*
	 * Move the reflog to a temporary place. This two-phase renaming is required
	 * in order to cope with funny renaming use cases when one tries to move a reference
	 * to a partially colliding namespace:
	 *  - a/b -> a/b/c
	 *  - a/b/c/d -> a/b/c
	 */
	if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0)
371
		return -1;
372

373 374
	if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path))) < 0) {
		error = -1;
375
		goto cleanup;
376
	}
377

378 379
	p_close(fd);

380 381
	if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) {
		giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
382
		error = -1;
383
		goto cleanup;
384
	}
385 386

	if (git_path_isdir(git_buf_cstr(&new_path)) && 
387 388
		(git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
		error = -1;
389
		goto cleanup;
390
	}
391

392 393
	if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
		error = -1;
394
		goto cleanup;
395
	}
396

397 398
	if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) {
		giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
399
		error = -1;
400
	}
401 402 403

cleanup:
	git_buf_free(&temp_path);
404 405
	git_buf_free(&old_path);
	git_buf_free(&new_path);
406
	git_buf_free(&normalized);
407

408
	return error;
409 410 411 412
}

int git_reflog_delete(git_reference *ref)
{
413
	int error;
414 415
	git_buf path = GIT_BUF_INIT;

416
	error = retrieve_reflog_path(&path, ref);
417

418
	if (!error && git_path_exists(path.ptr))
419
		error = p_unlink(path.ptr);
420

421
	git_buf_free(&path);
422

423
	return error;
424 425
}

426
size_t git_reflog_entrycount(git_reflog *reflog)
427 428
{
	assert(reflog);
429
	return reflog->entries.length;
430 431
}

432
GIT_INLINE(size_t) reflog_inverse_index(size_t idx, size_t total)
433
{
434 435
	return (total - 1) - idx;
}
436

437 438
const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, size_t idx)
{
439
	assert(reflog);
440

441
	if (idx >= reflog->entries.length)
442 443
		return NULL;

444 445
	return git_vector_get(
		&reflog->entries, reflog_inverse_index(idx, reflog->entries.length));
446 447
}

448
const git_oid * git_reflog_entry_id_old(const git_reflog_entry *entry)
449 450
{
	assert(entry);
451
	return &entry->oid_old;
452 453
}

454
const git_oid * git_reflog_entry_id_new(const git_reflog_entry *entry)
455 456
{
	assert(entry);
457
	return &entry->oid_cur;
458 459
}

460
const git_signature * git_reflog_entry_committer(const git_reflog_entry *entry)
461 462 463 464 465
{
	assert(entry);
	return entry->committer;
}

466
const char * git_reflog_entry_message(const git_reflog_entry *entry)
467 468 469 470
{
	assert(entry);
	return entry->msg;
}
471

472
int git_reflog_drop(
473
	git_reflog *reflog,
474
	size_t idx,
475 476
	int rewrite_previous_entry)
{
477
	size_t entrycount;
478 479 480 481 482 483
	git_reflog_entry *entry, *previous;

	assert(reflog);

	entrycount = git_reflog_entrycount(reflog);

484 485
	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);

486 487
	if (entry == NULL) {
		giterr_set(GITERR_REFERENCE, "No reflog entry at index "PRIuZ, idx);
488
		return GIT_ENOTFOUND;
489
	}
490 491 492

	reflog_entry_free(entry);

493 494
	if (git_vector_remove(
			&reflog->entries, reflog_inverse_index(idx, entrycount)) < 0)
495 496 497 498 499
		return -1;

	if (!rewrite_previous_entry)
		return 0;

500
	/* No need to rewrite anything when removing the most recent entry */
501
	if (idx == 0)
502 503
		return 0;

504
	/* Have the latest entry just been dropped? */
505 506 507
	if (entrycount == 1)
		return 0;

508
	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
509

510
	/* If the oldest entry has just been removed... */
511
	if (idx == entrycount - 1) {
nulltoken committed
512
		/* ...clear the oid_old member of the "new" oldest entry */
513 514
		if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0)
			return -1;
515

516 517 518
		return 0;
	}

519
	previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
520 521 522 523
	git_oid_cpy(&entry->oid_old, &previous->oid_cur);

	return 0;
}