reflog.c 4.94 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
 */

#include "reflog.h"
#include "repository.h"
#include "filebuf.h"
#include "signature.h"
12
#include "refdb.h"
13

14
#include <git2/sys/refdb_backend.h>
15

16
git_reflog_entry *git_reflog_entry__alloc(void)
17
{
18
	return git__calloc(1, sizeof(git_reflog_entry));
19
}
20

21
void git_reflog_entry__free(git_reflog_entry *entry)
22 23
{
	git_signature_free(entry->committer);
24

25 26
	git__free(entry->msg);
	git__free(entry);
27 28 29 30
}

void git_reflog_free(git_reflog *reflog)
{
31
	size_t i;
32 33
	git_reflog_entry *entry;

34 35 36
	if (reflog == NULL)
		return;

37 38 39
	if (reflog->db)
		GIT_REFCOUNT_DEC(reflog->db, git_refdb__free);

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

43
		git_reflog_entry__free(entry);
44 45 46
	}

	git_vector_free(&reflog->entries);
47 48
	git__free(reflog->ref_name);
	git__free(reflog);
49 50
}

51
int git_reflog_read(git_reflog **reflog, git_repository *repo,  const char *name)
52
{
53 54
	git_refdb *refdb;
	int error;
55

56
	assert(reflog && repo && name);
57

58
	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
59
		return error;
60

61
	return git_refdb_reflog_read(reflog, refdb, name);
62 63
}

64 65
int git_reflog_write(git_reflog *reflog)
{
66
	git_refdb *db;
67

68
	assert(reflog && reflog->db);
69

70 71
	db = reflog->db;
	return db->backend->reflog_write(db->backend, reflog);
72 73
}

74
int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_signature *committer, const char *msg)
75
{
76 77 78
	git_reflog_entry *entry;
	const git_reflog_entry *previous;
	const char *newline;
79

80
	assert(reflog && new_oid && committer);
81

82 83 84
	entry = git__calloc(1, sizeof(git_reflog_entry));
	GITERR_CHECK_ALLOC(entry);

85
	if ((git_signature_dup(&entry->committer, committer)) < 0)
86
		goto cleanup;
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
	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';
		}
	}

	previous = git_reflog_entry_byindex(reflog, 0);

	if (previous == NULL)
		git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO);
	else
		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)
		goto cleanup;

	return 0;

cleanup:
	git_reflog_entry__free(entry);
	return -1;
121 122
}

123
int git_reflog_rename(git_repository *repo, const char *old_name, const char *new_name)
124
{
125 126
	git_refdb *refdb;
	int error;
127

128
	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
129
		return -1;
130

131
	return refdb->backend->reflog_rename(refdb->backend, old_name, new_name);
132 133
}

134
int git_reflog_delete(git_repository *repo, const char *name)
135
{
136
	git_refdb *refdb;
137
	int error;
138

139 140
	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
		return -1;
141

142
	return refdb->backend->reflog_delete(refdb->backend, name);
143 144
}

145
size_t git_reflog_entrycount(git_reflog *reflog)
146 147
{
	assert(reflog);
148
	return reflog->entries.length;
149 150
}

151 152
const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, size_t idx)
{
153
	assert(reflog);
154

155
	if (idx >= reflog->entries.length)
156 157
		return NULL;

158 159
	return git_vector_get(
		&reflog->entries, reflog_inverse_index(idx, reflog->entries.length));
160 161
}

162
const git_oid * git_reflog_entry_id_old(const git_reflog_entry *entry)
163 164
{
	assert(entry);
165
	return &entry->oid_old;
166 167
}

168
const git_oid * git_reflog_entry_id_new(const git_reflog_entry *entry)
169 170
{
	assert(entry);
171
	return &entry->oid_cur;
172 173
}

174
const git_signature * git_reflog_entry_committer(const git_reflog_entry *entry)
175 176 177 178 179
{
	assert(entry);
	return entry->committer;
}

180
const char * git_reflog_entry_message(const git_reflog_entry *entry)
181 182 183 184
{
	assert(entry);
	return entry->msg;
}
185

186
int git_reflog_drop(git_reflog *reflog, size_t idx, int rewrite_previous_entry)
187
{
188 189
	size_t entrycount;
	git_reflog_entry *entry, *previous;
190

191
	entrycount = git_reflog_entrycount(reflog);
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
	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);

	if (entry == NULL) {
		giterr_set(GITERR_REFERENCE, "No reflog entry at index "PRIuZ, idx);
		return GIT_ENOTFOUND;
	}

	git_reflog_entry__free(entry);

	if (git_vector_remove(
			&reflog->entries, reflog_inverse_index(idx, entrycount)) < 0)
		return -1;

	if (!rewrite_previous_entry)
		return 0;

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

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

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

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

		return 0;
	}

	previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
	git_oid_cpy(&entry->oid_old, &previous->oid_cur);

	return 0;
232
}