reflog.c 5.09 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 16
#include "git2/sys/refdb_backend.h"
#include "git2/sys/reflog.h"
17

18
void git_reflog_entry__free(git_reflog_entry *entry)
19 20
{
	git_signature_free(entry->committer);
21

22 23
	git__free(entry->msg);
	git__free(entry);
24 25 26 27
}

void git_reflog_free(git_reflog *reflog)
{
28
	size_t i;
29 30
	git_reflog_entry *entry;

31 32 33
	if (reflog == NULL)
		return;

34 35 36
	if (reflog->db)
		GIT_REFCOUNT_DEC(reflog->db, git_refdb__free);

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

40
		git_reflog_entry__free(entry);
41 42 43
	}

	git_vector_free(&reflog->entries);
44 45
	git__free(reflog->ref_name);
	git__free(reflog);
46 47
}

48
int git_reflog_read(git_reflog **reflog, git_repository *repo,  const char *name)
49
{
50 51
	git_refdb *refdb;
	int error;
52

53 54 55
	GIT_ASSERT_ARG(reflog);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(name);
56

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

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

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

67 68
	GIT_ASSERT_ARG(reflog);
	GIT_ASSERT_ARG(reflog->db);
69

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

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

83 84 85
	GIT_ASSERT_ARG(reflog);
	GIT_ASSERT_ARG(new_oid);
	GIT_ASSERT_ARG(committer);
86

87
	entry = git__calloc(1, sizeof(git_reflog_entry));
88
	GIT_ERROR_CHECK_ALLOC(entry);
89

90
	if ((git_signature_dup(&entry->committer, committer)) < 0)
91
		goto cleanup;
92

93
	if (msg != NULL) {
94
		size_t i, msglen = strlen(msg);
95

96 97
		if ((entry->msg = git__strndup(msg, msglen)) == NULL)
			goto cleanup;
98

99 100 101 102 103 104 105
		/*
		 * Replace all newlines with spaces, except for
		 * the final trailing newline.
		 */
		for (i = 0; i < msglen; i++)
			if (entry->msg[i] == '\n')
				entry->msg[i] = ' ';
106 107 108 109 110
	}

	previous = git_reflog_entry_byindex(reflog, 0);

	if (previous == NULL)
111
		git_oid_clear(&entry->oid_old, reflog->oid_type);
112 113 114 115 116 117 118 119 120 121 122 123 124
	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;
125 126
}

127
int git_reflog_rename(git_repository *repo, const char *old_name, const char *new_name)
128
{
129 130
	git_refdb *refdb;
	int error;
131

132
	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
133
		return -1;
134

135
	return refdb->backend->reflog_rename(refdb->backend, old_name, new_name);
136 137
}

138
int git_reflog_delete(git_repository *repo, const char *name)
139
{
140
	git_refdb *refdb;
141
	int error;
142

143 144
	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
		return -1;
145

146
	return refdb->backend->reflog_delete(refdb->backend, name);
147 148
}

149
size_t git_reflog_entrycount(git_reflog *reflog)
150
{
151
	GIT_ASSERT_ARG_WITH_RETVAL(reflog, 0);
152
	return reflog->entries.length;
153 154
}

155
const git_reflog_entry *git_reflog_entry_byindex(const git_reflog *reflog, size_t idx)
156
{
157
	GIT_ASSERT_ARG_WITH_RETVAL(reflog, NULL);
158

159
	if (idx >= reflog->entries.length)
160 161
		return NULL;

162 163
	return git_vector_get(
		&reflog->entries, reflog_inverse_index(idx, reflog->entries.length));
164 165
}

166
const git_oid *git_reflog_entry_id_old(const git_reflog_entry *entry)
167
{
168
	GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL);
169
	return &entry->oid_old;
170 171
}

172
const git_oid *git_reflog_entry_id_new(const git_reflog_entry *entry)
173
{
174
	GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL);
175
	return &entry->oid_cur;
176 177
}

178
const git_signature *git_reflog_entry_committer(const git_reflog_entry *entry)
179
{
180
	GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL);
181 182 183
	return entry->committer;
}

184
const char *git_reflog_entry_message(const git_reflog_entry *entry)
185
{
186
	GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL);
187 188
	return entry->msg;
}
189

190
int git_reflog_drop(git_reflog *reflog, size_t idx, int rewrite_previous_entry)
191
{
192 193
	size_t entrycount;
	git_reflog_entry *entry, *previous;
194

195
	entrycount = git_reflog_entrycount(reflog);
196

197 198 199
	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);

	if (entry == NULL) {
200
		git_error_set(GIT_ERROR_REFERENCE, "no reflog entry at index %"PRIuZ, idx);
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
		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 */
226
		git_oid_clear(&entry->oid_old, reflog->oid_type);
227 228 229 230 231 232 233
		return 0;
	}

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

	return 0;
234
}