Skip to content

Commit fd438a4

Browse files
committed
hiredis: Cleanup
1 parent 9d6b0b8 commit fd438a4

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

redis/hiredis.c

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,10 @@
2323
* Boston, MA 02110-1301, USA.
2424
*/
2525

26-
#include "common.h"
27-
#include "git2/object.h"
28-
#include "hash.h"
29-
#include "odb.h"
30-
31-
#include "git2/odb_backend.h"
32-
33-
#ifdef GIT2_HIREDIS_BACKEND
34-
26+
#include <assert.h>
27+
#include <string.h>
28+
#include <git2.h>
29+
#include <git2/odb_backend.h>
3530
#include <hiredis/hiredis.h>
3631

3732
typedef struct {
@@ -40,7 +35,8 @@ typedef struct {
4035
redisContext *db;
4136
} hiredis_backend;
4237

43-
int hiredis_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid) {
38+
int hiredis_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
39+
{
4440
hiredis_backend *backend;
4541
int error;
4642
redisReply *reply;
@@ -67,10 +63,11 @@ int hiredis_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backe
6763
}
6864

6965
freeReplyObject(reply);
70-
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to read header");
66+
return error;
7167
}
7268

73-
int hiredis_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid) {
69+
int hiredis_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
70+
{
7471
hiredis_backend *backend;
7572
int error;
7673
redisReply *reply;
@@ -89,7 +86,7 @@ int hiredis_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_o
8986
reply->element[2]->type != REDIS_REPLY_NIL) {
9087
*type_p = (git_otype) atoi(reply->element[0]->str);
9188
*len_p = (size_t) atoi(reply->element[1]->str);
92-
*data_p = git__malloc(*len_p);
89+
*data_p = malloc(*len_p);
9390
if (*data_p == NULL) {
9491
error = GIT_ENOMEM;
9592
} else {
@@ -104,11 +101,13 @@ int hiredis_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_o
104101
}
105102

106103
freeReplyObject(reply);
107-
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to read backend");
104+
return error == GIT_SUCCESS;
108105
}
109106

110-
int hiredis_backend__read_prefix(git_oid *out_oid, void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
111-
const git_oid *short_oid, unsigned int len) {
107+
int hiredis_backend__read_prefix(git_oid *out_oid,
108+
void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
109+
const git_oid *short_oid, unsigned int len)
110+
{
112111
if (len >= GIT_OID_HEXSZ) {
113112
/* Just match the full identifier */
114113
int error = hiredis_backend__read(data_p, len_p, type_p, backend, short_oid);
@@ -118,11 +117,12 @@ int hiredis_backend__read_prefix(git_oid *out_oid, void **data_p, size_t *len_p,
118117
return error;
119118
} else if (len < GIT_OID_HEXSZ) {
120119
/* TODO */
121-
return git__throw(GIT_ENOTIMPLEMENTED, "Hiredis backend cannot search objects from short oid");
120+
return GIT_ENOTIMPLEMENTED;
122121
}
123122
}
124123

125-
int hiredis_backend__exists(git_odb_backend *_backend, const git_oid *oid) {
124+
int hiredis_backend__exists(git_odb_backend *_backend, const git_oid *oid)
125+
{
126126
hiredis_backend *backend;
127127
int found;
128128
redisReply *reply;
@@ -136,12 +136,12 @@ int hiredis_backend__exists(git_odb_backend *_backend, const git_oid *oid) {
136136
if (reply && reply->type != REDIS_REPLY_NIL && reply->type != REDIS_REPLY_ERROR)
137137
found = 1;
138138

139-
140139
freeReplyObject(reply);
141140
return found;
142141
}
143142

144-
int hiredis_backend__write(git_oid *id, git_odb_backend *_backend, const void *data, size_t len, git_otype type) {
143+
int hiredis_backend__write(git_oid *id, git_odb_backend *_backend, const void *data, size_t len, git_otype type)
144+
{
145145
hiredis_backend *backend;
146146
int error;
147147
redisReply *reply;
@@ -152,7 +152,7 @@ int hiredis_backend__write(git_oid *id, git_odb_backend *_backend, const void *d
152152
error = GIT_ERROR;
153153

154154
if ((error = git_odb_hash(id, data, len, type)) < 0)
155-
return git__rethrow(error, "Failed to write backend");
155+
return error;
156156

157157
reply = redisCommand(backend->db, "HMSET %b "
158158
"type %d "
@@ -163,10 +163,11 @@ int hiredis_backend__write(git_oid *id, git_odb_backend *_backend, const void *d
163163
error = (reply == NULL || reply->type == REDIS_REPLY_ERROR) ? GIT_ERROR : GIT_SUCCESS;
164164

165165
freeReplyObject(reply);
166-
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write backend");
166+
return error;
167167
}
168168

169-
void hiredis_backend__free(git_odb_backend *_backend) {
169+
void hiredis_backend__free(git_odb_backend *_backend)
170+
{
170171
hiredis_backend *backend;
171172
assert(_backend);
172173
backend = (hiredis_backend *) _backend;
@@ -176,17 +177,19 @@ void hiredis_backend__free(git_odb_backend *_backend) {
176177
free(backend);
177178
}
178179

179-
int git_odb_backend_hiredis(git_odb_backend **backend_out, const char *host, int port) {
180+
int git_odb_backend_hiredis(git_odb_backend **backend_out, const char *host, int port)
181+
{
180182
hiredis_backend *backend;
181183

182-
backend = git__calloc(1, sizeof (hiredis_backend));
184+
backend = calloc(1, sizeof (hiredis_backend));
183185
if (backend == NULL)
184186
return GIT_ENOMEM;
185187

186-
187188
backend->db = redisConnect(host, port);
188-
if (backend->db->err)
189-
goto cleanup;
189+
if (backend->db->err) {
190+
free(backend);
191+
return GIT_ERROR;
192+
}
190193

191194
backend->parent.read = &hiredis_backend__read;
192195
backend->parent.read_prefix = &hiredis_backend__read_prefix;
@@ -198,20 +201,5 @@ int git_odb_backend_hiredis(git_odb_backend **backend_out, const char *host, int
198201
*backend_out = (git_odb_backend *) backend;
199202

200203
return GIT_SUCCESS;
201-
cleanup:
202-
free(backend);
203-
return git__throw(GIT_ERROR, "Failed to get ODB backend");
204204
}
205205

206-
#else
207-
208-
int git_odb_backend_hiredis(git_odb_backend ** GIT_UNUSED(backend_out),
209-
const char *GIT_UNUSED(host), int GIT_UNUSED(port)) {
210-
GIT_UNUSED_ARG(backend_out);
211-
GIT_UNUSED_ARG(host);
212-
GIT_UNUSED_ARG(port);
213-
return git__throw(GIT_ENOTIMPLEMENTED, "Failed to get ODB backend. Feature not yet implemented");
214-
}
215-
216-
217-
#endif /* HAVE_HIREDIS */

0 commit comments

Comments
 (0)