transaction.c 8.49 KB
Newer Older
1 2 3 4 5 6 7
/*
 * 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.
 */

8 9
#include "transaction.h"

10 11 12 13 14 15
#include "repository.h"
#include "strmap.h"
#include "refdb.h"
#include "pool.h"
#include "reflog.h"
#include "signature.h"
16
#include "config.h"
17

18
#include "git2/transaction.h"
19 20 21 22
#include "git2/signature.h"
#include "git2/sys/refs.h"
#include "git2/sys/refdb_backend.h"

23 24 25 26 27 28
typedef enum {
	TRANSACTION_NONE,
	TRANSACTION_REFS,
	TRANSACTION_CONFIG,
} transaction_t;

29 30 31 32
typedef struct {
	const char *name;
	void *payload;

33
	git_reference_t ref_type;
34 35 36 37 38 39 40 41 42 43 44 45 46 47
	union {
		git_oid id;
		char *symbolic;
	} target;
	git_reflog *reflog;

	const char *message;
	git_signature *sig;

	unsigned int committed :1,
		remove :1;
} transaction_node;

struct git_transaction {
48
	transaction_t type;
49 50
	git_repository *repo;
	git_refdb *db;
51
	git_config *cfg;
52 53 54 55 56

	git_strmap *locks;
	git_pool pool;
};

57 58 59
int git_transaction_config_new(git_transaction **out, git_config *cfg)
{
	git_transaction *tx;
60 61 62

	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(cfg);
63 64

	tx = git__calloc(1, sizeof(git_transaction));
65
	GIT_ERROR_CHECK_ALLOC(tx);
66 67 68 69 70 71 72

	tx->type = TRANSACTION_CONFIG;
	tx->cfg = cfg;
	*out = tx;
	return 0;
}

73 74 75 76 77 78
int git_transaction_new(git_transaction **out, git_repository *repo)
{
	int error;
	git_pool pool;
	git_transaction *tx = NULL;

79 80
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(repo);
81

82 83
	if ((error = git_pool_init(&pool, 1)) < 0)
		goto on_error;
84 85 86 87 88 89 90

	tx = git_pool_mallocz(&pool, sizeof(git_transaction));
	if (!tx) {
		error = -1;
		goto on_error;
	}

91
	if ((error = git_strmap_new(&tx->locks)) < 0) {
92 93 94 95 96 97 98
		error = -1;
		goto on_error;
	}

	if ((error = git_repository_refdb(&tx->db, repo)) < 0)
		goto on_error;

99
	tx->type = TRANSACTION_REFS;
100 101 102 103 104 105 106 107 108 109
	memcpy(&tx->pool, &pool, sizeof(git_pool));
	tx->repo = repo;
	*out = tx;
	return 0;

on_error:
	git_pool_clear(&pool);
	return error;
}

110
int git_transaction_lock_ref(git_transaction *tx, const char *refname)
111 112 113 114
{
	int error;
	transaction_node *node;

115 116
	GIT_ASSERT_ARG(tx);
	GIT_ASSERT_ARG(refname);
117 118

	node = git_pool_mallocz(&tx->pool, sizeof(transaction_node));
119
	GIT_ERROR_CHECK_ALLOC(node);
120 121

	node->name = git_pool_strdup(&tx->pool, refname);
122
	GIT_ERROR_CHECK_ALLOC(node->name);
123 124 125 126

	if ((error = git_refdb_lock(&node->payload, tx->db, refname)) < 0)
		return error;

127
	if ((error = git_strmap_set(tx->locks, node->name, node)) < 0)
128 129 130 131 132 133 134 135 136 137 138 139 140 141
		goto cleanup;

	return 0;

cleanup:
	git_refdb_unlock(tx->db, node->payload, false, false, NULL, NULL, NULL);

	return error;
}

static int find_locked(transaction_node **out, git_transaction *tx, const char *refname)
{
	transaction_node *node;

142
	if ((node = git_strmap_get(tx->locks, refname)) == NULL) {
143
		git_error_set(GIT_ERROR_REFERENCE, "the specified reference is not locked");
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
		return GIT_ENOTFOUND;
	}

	*out = node;
	return 0;
}

static int copy_common(transaction_node *node, git_transaction *tx, const git_signature *sig, const char *msg)
{
	if (sig && git_signature__pdup(&node->sig, sig, &tx->pool) < 0)
		return -1;

	if (!node->sig) {
		git_signature *tmp;
		int error;

		if (git_reference__log_signature(&tmp, tx->repo) < 0)
			return -1;

		/* make sure the sig we use is in our pool */
		error = git_signature__pdup(&node->sig, tmp, &tx->pool);
		git_signature_free(tmp);
		if (error < 0)
			return error;
	}

	if (msg) {
		node->message = git_pool_strdup(&tx->pool, msg);
172
		GIT_ERROR_CHECK_ALLOC(node->message);
173 174 175 176 177 178 179 180 181 182
	}

	return 0;
}

int git_transaction_set_target(git_transaction *tx, const char *refname, const git_oid *target, const git_signature *sig, const char *msg)
{
	int error;
	transaction_node *node;

183 184 185
	GIT_ASSERT_ARG(tx);
	GIT_ASSERT_ARG(refname);
	GIT_ASSERT_ARG(target);
186 187 188 189 190 191 192 193

	if ((error = find_locked(&node, tx, refname)) < 0)
		return error;

	if ((error = copy_common(node, tx, sig, msg)) < 0)
		return error;

	git_oid_cpy(&node->target.id, target);
194
	node->ref_type = GIT_REFERENCE_DIRECT;
195 196 197 198 199 200 201 202 203

	return 0;
}

int git_transaction_set_symbolic_target(git_transaction *tx, const char *refname, const char *target, const git_signature *sig, const char *msg)
{
	int error;
	transaction_node *node;

204 205 206
	GIT_ASSERT_ARG(tx);
	GIT_ASSERT_ARG(refname);
	GIT_ASSERT_ARG(target);
207 208 209 210 211 212 213 214

	if ((error = find_locked(&node, tx, refname)) < 0)
		return error;

	if ((error = copy_common(node, tx, sig, msg)) < 0)
		return error;

	node->target.symbolic = git_pool_strdup(&tx->pool, target);
215
	GIT_ERROR_CHECK_ALLOC(node->target.symbolic);
216
	node->ref_type = GIT_REFERENCE_SYMBOLIC;
217 218 219 220 221 222 223 224 225 226 227 228 229

	return 0;
}

int git_transaction_remove(git_transaction *tx, const char *refname)
{
	int error;
	transaction_node *node;

	if ((error = find_locked(&node, tx, refname)) < 0)
		return error;

	node->remove = true;
230
	node->ref_type = GIT_REFERENCE_DIRECT; /* the id will be ignored */
231 232 233 234 235 236 237 238 239 240 241

	return 0;
}

static int dup_reflog(git_reflog **out, const git_reflog *in, git_pool *pool)
{
	git_reflog *reflog;
	git_reflog_entry *entries;
	size_t len, i;

	reflog = git_pool_mallocz(pool, sizeof(git_reflog));
242
	GIT_ERROR_CHECK_ALLOC(reflog);
243 244

	reflog->ref_name = git_pool_strdup(pool, in->ref_name);
245
	GIT_ERROR_CHECK_ALLOC(reflog->ref_name);
246 247 248 249

	len = in->entries.length;
	reflog->entries.length = len;
	reflog->entries.contents = git_pool_mallocz(pool, len * sizeof(void *));
250
	GIT_ERROR_CHECK_ALLOC(reflog->entries.contents);
251 252

	entries = git_pool_mallocz(pool, len * sizeof(git_reflog_entry));
253
	GIT_ERROR_CHECK_ALLOC(entries);
254 255 256 257 258 259 260 261 262 263 264 265 266

	for (i = 0; i < len; i++) {
		const git_reflog_entry *src;
		git_reflog_entry *tgt;

		tgt = &entries[i];
		reflog->entries.contents[i] = tgt;

		src = git_vector_get(&in->entries, i);
		git_oid_cpy(&tgt->oid_old, &src->oid_old);
		git_oid_cpy(&tgt->oid_cur, &src->oid_cur);

		tgt->msg = git_pool_strdup(pool, src->msg);
267
		GIT_ERROR_CHECK_ALLOC(tgt->msg);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

		if (git_signature__pdup(&tgt->committer, src->committer, pool) < 0)
			return -1;
	}


	*out = reflog;
	return 0;
}

int git_transaction_set_reflog(git_transaction *tx, const char *refname, const git_reflog *reflog)
{
	int error;
	transaction_node *node;

283 284 285
	GIT_ASSERT_ARG(tx);
	GIT_ASSERT_ARG(refname);
	GIT_ASSERT_ARG(reflog);
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

	if ((error = find_locked(&node, tx, refname)) < 0)
		return error;

	if ((error = dup_reflog(&node->reflog, reflog, &tx->pool)) < 0)
		return error;

	return 0;
}

static int update_target(git_refdb *db, transaction_node *node)
{
	git_reference *ref;
	int error, update_reflog;

301
	if (node->ref_type == GIT_REFERENCE_DIRECT) {
302
		ref = git_reference__alloc(node->name, &node->target.id, NULL);
303
	} else if (node->ref_type == GIT_REFERENCE_SYMBOLIC) {
304 305
		ref = git_reference__alloc_symbolic(node->name, node->target.symbolic);
	} else {
306
		abort();
307 308
	}

309
	GIT_ERROR_CHECK_ALLOC(ref);
310 311 312 313
	update_reflog = node->reflog == NULL;

	if (node->remove) {
		error =  git_refdb_unlock(db, node->payload, 2, false, ref, NULL, NULL);
314
	} else if (node->ref_type == GIT_REFERENCE_DIRECT) {
315
		error = git_refdb_unlock(db, node->payload, true, update_reflog, ref, node->sig, node->message);
316
	} else if (node->ref_type == GIT_REFERENCE_SYMBOLIC) {
317 318
		error = git_refdb_unlock(db, node->payload, true, update_reflog, ref, node->sig, node->message);
	} else {
319
		abort();
320 321 322 323 324 325 326 327 328 329 330
	}

	git_reference_free(ref);
	node->committed = true;

	return error;
}

int git_transaction_commit(git_transaction *tx)
{
	transaction_node *node;
331
	int error = 0;
332

333
	GIT_ASSERT_ARG(tx);
334

335 336 337 338 339 340 341
	if (tx->type == TRANSACTION_CONFIG) {
		error = git_config_unlock(tx->cfg, true);
		tx->cfg = NULL;

		return error;
	}

342
	git_strmap_foreach_value(tx->locks, node, {
343 344 345 346 347
		if (node->reflog) {
			if ((error = tx->db->backend->reflog_write(tx->db->backend, node->reflog)) < 0)
				return error;
		}

348 349 350 351 352 353 354
		if (node->ref_type == GIT_REFERENCE_INVALID) {
			/* ref was locked but not modified */
			if ((error = git_refdb_unlock(tx->db, node->payload, false, false, NULL, NULL, NULL)) < 0) {
				return error;
			}
			node->committed = true;
		} else {
355 356 357
			if ((error = update_target(tx->db, node)) < 0)
				return error;
		}
358
	});
359 360 361 362 363 364 365 366 367

	return 0;
}

void git_transaction_free(git_transaction *tx)
{
	transaction_node *node;
	git_pool pool;

368 369
	if (!tx)
		return;
370

371 372 373 374 375 376 377 378 379 380
	if (tx->type == TRANSACTION_CONFIG) {
		if (tx->cfg) {
			git_config_unlock(tx->cfg, false);
			git_config_free(tx->cfg);
		}

		git__free(tx);
		return;
	}

381
	/* start by unlocking the ones we've left hanging, if any */
382
	git_strmap_foreach_value(tx->locks, node, {
383 384 385 386
		if (node->committed)
			continue;

		git_refdb_unlock(tx->db, node->payload, false, false, NULL, NULL, NULL);
387
	});
388 389 390 391 392 393 394 395

	git_refdb_free(tx->db);
	git_strmap_free(tx->locks);

	/* tx is inside the pool, so we need to extract the data */
	memcpy(&pool, &tx->pool, sizeof(git_pool));
	git_pool_clear(&pool);
}