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

#include "reflog.h"
9

10 11 12
#include "repository.h"
#include "filebuf.h"
#include "signature.h"
13
#include "refdb.h"
14

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

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

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

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

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

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

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

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

44
		git_reflog_entry__free(entry);
45 46 47
	}

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

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

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

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

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

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

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

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

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

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

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

86
	if ((git_signature_dup(&entry->committer, committer)) < 0)
87
		goto cleanup;
88

89 90 91 92 93 94 95 96
	if (msg != NULL) {
		if ((entry->msg = git__strdup(msg)) == NULL)
			goto cleanup;

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

		if (newline) {
			if (newline[1] != '\0') {
97
				giterr_set(GITERR_INVALID, "reflog message cannot contain newline");
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
				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;
122 123
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

192
	entrycount = git_reflog_entrycount(reflog);
193

194 195 196
	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);

	if (entry == NULL) {
197
		giterr_set(GITERR_REFERENCE, "no reflog entry at index %"PRIuZ, idx);
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
		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;
233
}