reflog.c 8.17 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
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 13 14 15 16 17 18
 */

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

static int reflog_init(git_reflog **reflog, git_reference *ref)
{
	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 32
	}

	*reflog = log;

33
	return 0;
34 35
}

36 37 38
static int reflog_write(const char *log_path, const char *oid_old,
			const char *oid_new, const git_signature *committer,
			const char *msg)
39 40
{
	int error;
Vicent Marti committed
41
	git_buf log = GIT_BUF_INIT;
42
	git_filebuf fbuf = GIT_FILEBUF_INIT;
43
	bool trailing_newline = false;
44

45
	assert(log_path && oid_old && oid_new && committer);
46

47 48 49 50 51 52 53 54 55 56 57 58
	if (msg) {
		const char *newline = strchr(msg, '\n');
		if (newline) {
			if (*(newline + 1) == '\0')
				trailing_newline = true;
			else {
				giterr_set(GITERR_INVALID, "Reflog message cannot contain newline");
				return -1;
			}
		}
	}

Vicent Marti committed
59
	git_buf_puts(&log, oid_old);
60 61
	git_buf_putc(&log, ' ');

Vicent Marti committed
62
	git_buf_puts(&log, oid_new);
63

64
	git_signature__writebuf(&log, " ", committer);
65
	git_buf_truncate(&log, log.size - 1); /* drop LF */
66 67

	if (msg) {
Vicent Marti committed
68 69
		git_buf_putc(&log, '\t');
		git_buf_puts(&log, msg);
70 71
	}

72 73
	if (!trailing_newline)
		git_buf_putc(&log, '\n');
74

75
	if (git_buf_oom(&log)) {
76
		git_buf_free(&log);
77
		return -1;
78 79
	}

80 81 82 83 84 85
	error = git_filebuf_open(&fbuf, log_path, GIT_FILEBUF_APPEND);
	if (!error) {
		if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
			git_filebuf_cleanup(&fbuf);
		else
			error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE);
Vicent Marti committed
86
	}
87

Vicent Marti committed
88
	git_buf_free(&log);
89

90
	return error;
91 92 93 94 95 96 97
}

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

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

	while (buf_size > GIT_REFLOG_SIZE_MIN) {
		entry = git__malloc(sizeof(git_reflog_entry));
109
		GITERR_CHECK_ALLOC(entry);
110

111 112 113 114 115
		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;
116
		seek_forward(GIT_OID_HEXSZ + 1);
117

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

		ptr = buf;

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

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

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

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

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

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

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

151 152
	return 0;

153 154
#undef seek_forward

155 156 157 158 159 160
fail:
	if (entry) {
		git__free(entry->committer);
		git__free(entry);
	}
	return -1;
161 162 163 164 165 166 167 168 169 170 171 172
}

void git_reflog_free(git_reflog *reflog)
{
	unsigned int i;
	git_reflog_entry *entry;

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

		git_signature_free(entry->committer);

173 174
		git__free(entry->msg);
		git__free(entry);
175 176 177
	}

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

int git_reflog_read(git_reflog **reflog, git_reference *ref)
{
	int error;
185
	git_buf log_path = GIT_BUF_INIT;
186
	git_buf log_file = GIT_BUF_INIT;
187 188 189 190
	git_reflog *log = NULL;

	*reflog = NULL;

191 192
	if (reflog_init(&log, ref) < 0)
		return -1;
193

194 195
	error = git_buf_join_n(&log_path, '/', 3,
		ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
196

197 198
	if (!error)
		error = git_futils_readbuffer(&log_file, log_path.ptr);
199

200 201
	if (!error)
		error = reflog_parse(log, log_file.ptr, log_file.size);
202

203 204 205
	if (!error)
		*reflog = log;
	else
schu committed
206
		git_reflog_free(log);
207

208
	git_buf_free(&log_file);
209
	git_buf_free(&log_path);
210

211
	return error;
212 213 214
}

int git_reflog_write(git_reference *ref, const git_oid *oid_old,
Vicent Marti committed
215
				const git_signature *committer, const char *msg)
216 217 218 219
{
	int error;
	char old[GIT_OID_HEXSZ+1];
	char new[GIT_OID_HEXSZ+1];
220
	git_buf log_path = GIT_BUF_INIT;
221 222 223
	git_reference *r;
	const git_oid *oid;

224 225
	if ((error = git_reference_resolve(&r, ref)) < 0)
		return error;
226 227

	oid = git_reference_oid(r);
228
	if (oid == NULL) {
229
		giterr_set(GITERR_REFERENCE,
230
			"Failed to write reflog. Cannot resolve reference `%s`", r->name);
231
		git_reference_free(r);
232
		return -1;
233 234
	}

235
	git_oid_tostr(new, GIT_OID_HEXSZ+1, oid);
236

237 238
	git_reference_free(r);

239
	error = git_buf_join_n(&log_path, '/', 3,
240
		ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
241
	if (error < 0)
242
		goto cleanup;
243

244
	if (git_path_exists(log_path.ptr) == false) {
245
		error = git_futils_mkpath2file(log_path.ptr, GIT_REFLOG_DIR_MODE);
246
	} else if (git_path_isfile(log_path.ptr) == false) {
247
		giterr_set(GITERR_REFERENCE,
248
			"Failed to write reflog. `%s` is directory", log_path.ptr);
249
		error = -1;
250
	} else if (oid_old == NULL) {
251
		giterr_set(GITERR_REFERENCE,
252
			"Failed to write reflog. Old OID cannot be NULL for existing reference");
253
		error = -1;
254
	}
255
	if (error < 0)
256 257
		goto cleanup;

258
	if (oid_old)
259
		git_oid_tostr(old, sizeof(old), oid_old);
260
	else
261 262 263
		p_snprintf(old, sizeof(old), "%0*d", GIT_OID_HEXSZ, 0);

	error = reflog_write(log_path.ptr, old, new, committer, msg);
264

265 266 267
cleanup:
	git_buf_free(&log_path);
	return error;
268 269
}

270 271
int git_reflog_rename(git_reference *ref, const char *new_name)
{
272
	int error = -1, fd;
273 274
	git_buf old_path = GIT_BUF_INIT;
	git_buf new_path = GIT_BUF_INIT;
275
	git_buf temp_path = GIT_BUF_INIT;
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	assert(ref && new_name);

	if (git_buf_joinpath(&temp_path, ref->owner->path_repository, GIT_REFLOG_DIR) < 0)
		return -1;

	if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), ref->name) < 0)
		goto cleanup;

	if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), new_name) < 0)
		goto cleanup;

	/*
	 * 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)
		goto cleanup;
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path))) < 0)
		goto cleanup;
	p_close(fd);

	if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0)
		goto cleanup;

	if (git_path_isdir(git_buf_cstr(&new_path)) && 
		(git_futils_rmdir_r(git_buf_cstr(&new_path), GIT_DIRREMOVAL_ONLY_EMPTY_DIRS) < 0))
		goto cleanup;

	if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0)
		goto cleanup;

	error = p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path));

cleanup:
	git_buf_free(&temp_path);
316 317
	git_buf_free(&old_path);
	git_buf_free(&new_path);
318

319
	return error;
320 321 322 323
}

int git_reflog_delete(git_reference *ref)
{
324
	int error;
325 326
	git_buf path = GIT_BUF_INIT;

327 328
	error = git_buf_join_n(
		&path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
329

330
	if (!error && git_path_exists(path.ptr))
331
		error = p_unlink(path.ptr);
332

333
	git_buf_free(&path);
334

335
	return error;
336 337
}

338 339 340 341 342 343 344 345 346 347 348 349
unsigned int git_reflog_entrycount(git_reflog *reflog)
{
	assert(reflog);
	return reflog->entries.length;
}

const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, unsigned int idx)
{
	assert(reflog);
	return git_vector_get(&reflog->entries, idx);
}

350
const git_oid * git_reflog_entry_oidold(const git_reflog_entry *entry)
351 352
{
	assert(entry);
353
	return &entry->oid_old;
354 355
}

356
const git_oid * git_reflog_entry_oidnew(const git_reflog_entry *entry)
357 358
{
	assert(entry);
359
	return &entry->oid_cur;
360 361 362 363 364 365 366 367 368 369 370 371 372
}

git_signature * git_reflog_entry_committer(const git_reflog_entry *entry)
{
	assert(entry);
	return entry->committer;
}

char * git_reflog_entry_msg(const git_reflog_entry *entry)
{
	assert(entry);
	return entry->msg;
}