Commit 854eccbb by Russell Belfer

Clean up GIT_UNUSED macros on all platforms

It turns out that commit 31e9cfc4cbcaf1b38cdd3dbe3282a8f57e5366a5
did not fix the GIT_USUSED behavior on all platforms.  This commit
walks through and really cleans things up more thoroughly, getting
rid of the unnecessary stuff.

To remove the use of some GIT_UNUSED, I ended up adding a couple
of new iterators for hashtables that allow you to iterator just
over keys or just over values.

In making this change, I found a bug in the clar tests (where we
were doing *count++ but meant to do (*count)++ to increment the
value).  I fixed that but then found the test failing because it
was not really using an empty repo.  So, I took some of the code
that I wrote for iterator testing and moved it to clar_helpers.c,
then made use of that to make it easier to open fixtures on a
per test basis even within a single test file.
parent 74fa4bfa
...@@ -408,10 +408,9 @@ void git_attr_cache_flush( ...@@ -408,10 +408,9 @@ void git_attr_cache_flush(
return; return;
if (repo->attrcache.files) { if (repo->attrcache.files) {
const void *GIT_UNUSED(name);
git_attr_file *file; git_attr_file *file;
GIT_HASHTABLE_FOREACH(repo->attrcache.files, name, file, GIT_HASHTABLE_FOREACH_VALUE(repo->attrcache.files, file,
git_attr_file__free(file)); git_attr_file__free(file));
git_hashtable_free(repo->attrcache.files); git_hashtable_free(repo->attrcache.files);
...@@ -419,10 +418,9 @@ void git_attr_cache_flush( ...@@ -419,10 +418,9 @@ void git_attr_cache_flush(
} }
if (repo->attrcache.macros) { if (repo->attrcache.macros) {
const void *GIT_UNUSED(name);
git_attr_rule *rule; git_attr_rule *rule;
GIT_HASHTABLE_FOREACH(repo->attrcache.macros, name, rule, GIT_HASHTABLE_FOREACH_VALUE(repo->attrcache.macros, rule,
git_attr_rule__free(rule)); git_attr_rule__free(rule));
git_hashtable_free(repo->attrcache.macros); git_hashtable_free(repo->attrcache.macros);
......
...@@ -33,8 +33,7 @@ ...@@ -33,8 +33,7 @@
# define GIT_TYPEOF(x) # define GIT_TYPEOF(x)
#endif #endif
#define GIT_UNUSED(x) x #define GIT_UNUSED(x) ((void)(x))
#define GIT_UNUSED_ARG(x) ((void)(x))
/* Define the printf format specifer to use for size_t output */ /* Define the printf format specifer to use for size_t output */
#if defined(_MSC_VER) || defined(__MINGW32__) #if defined(_MSC_VER) || defined(__MINGW32__)
......
...@@ -125,13 +125,12 @@ static int normalize_name(const char *in, char **out) ...@@ -125,13 +125,12 @@ static int normalize_name(const char *in, char **out)
static void free_vars(git_hashtable *values) static void free_vars(git_hashtable *values)
{ {
const char *GIT_UNUSED(_unused) = NULL;
cvar_t *var = NULL; cvar_t *var = NULL;
if (values == NULL) if (values == NULL)
return; return;
GIT_HASHTABLE_FOREACH(values, _unused, var, GIT_HASHTABLE_FOREACH_VALUE(values, var,
do { do {
cvar_t *next = CVAR_LIST_NEXT(var); cvar_t *next = CVAR_LIST_NEXT(var);
cvar_free(var); cvar_free(var);
......
...@@ -325,7 +325,7 @@ static int maybe_modified( ...@@ -325,7 +325,7 @@ static int maybe_modified(
int error = GIT_SUCCESS; int error = GIT_SUCCESS;
git_oid noid, *use_noid = NULL; git_oid noid, *use_noid = NULL;
GIT_UNUSED_ARG(old); GIT_UNUSED(old);
/* support "assume unchanged" & "skip worktree" bits */ /* support "assume unchanged" & "skip worktree" bits */
if ((oitem->flags_extended & GIT_IDXENTRY_INTENT_TO_ADD) != 0 || if ((oitem->flags_extended & GIT_IDXENTRY_INTENT_TO_ADD) != 0 ||
......
...@@ -161,7 +161,7 @@ static int file_is_binary_by_content( ...@@ -161,7 +161,7 @@ static int file_is_binary_by_content(
git_map *old_data, git_map *old_data,
git_map *new_data) git_map *new_data)
{ {
GIT_UNUSED_ARG(diff); GIT_UNUSED(diff);
if ((delta->old.flags & BINARY_DIFF_FLAGS) == 0) { if ((delta->old.flags & BINARY_DIFF_FLAGS) == 0) {
size_t search_len = min(old_data->len, 4000); size_t search_len = min(old_data->len, 4000);
...@@ -448,7 +448,7 @@ static int print_compact(void *data, git_diff_delta *delta, float progress) ...@@ -448,7 +448,7 @@ static int print_compact(void *data, git_diff_delta *delta, float progress)
diff_print_info *pi = data; diff_print_info *pi = data;
char code, old_suffix, new_suffix; char code, old_suffix, new_suffix;
GIT_UNUSED_ARG(progress); GIT_UNUSED(progress);
switch (delta->status) { switch (delta->status) {
case GIT_STATUS_ADDED: code = 'A'; break; case GIT_STATUS_ADDED: code = 'A'; break;
...@@ -546,7 +546,7 @@ static int print_patch_file(void *data, git_diff_delta *delta, float progress) ...@@ -546,7 +546,7 @@ static int print_patch_file(void *data, git_diff_delta *delta, float progress)
const char *newpfx = pi->diff->opts.dst_prefix; const char *newpfx = pi->diff->opts.dst_prefix;
const char *newpath = delta->new.path; const char *newpath = delta->new.path;
GIT_UNUSED_ARG(progress); GIT_UNUSED(progress);
git_buf_clear(pi->buf); git_buf_clear(pi->buf);
git_buf_printf(pi->buf, "diff --git %s%s %s%s\n", oldpfx, delta->old.path, newpfx, delta->new.path); git_buf_printf(pi->buf, "diff --git %s%s %s%s\n", oldpfx, delta->old.path, newpfx, delta->new.path);
...@@ -593,8 +593,8 @@ static int print_patch_hunk( ...@@ -593,8 +593,8 @@ static int print_patch_hunk(
{ {
diff_print_info *pi = data; diff_print_info *pi = data;
GIT_UNUSED_ARG(d); GIT_UNUSED(d);
GIT_UNUSED_ARG(r); GIT_UNUSED(r);
git_buf_clear(pi->buf); git_buf_clear(pi->buf);
...@@ -613,7 +613,7 @@ static int print_patch_line( ...@@ -613,7 +613,7 @@ static int print_patch_line(
{ {
diff_print_info *pi = data; diff_print_info *pi = data;
GIT_UNUSED_ARG(delta); GIT_UNUSED(delta);
git_buf_clear(pi->buf); git_buf_clear(pi->buf);
......
...@@ -65,20 +65,20 @@ GIT_INLINE(int) git_hashtable_insert(git_hashtable *h, const void *key, void *va ...@@ -65,20 +65,20 @@ GIT_INLINE(int) git_hashtable_insert(git_hashtable *h, const void *key, void *va
#define git_hashtable_node_at(nodes, pos) ((git_hashtable_node *)(&nodes[pos])) #define git_hashtable_node_at(nodes, pos) ((git_hashtable_node *)(&nodes[pos]))
#define GIT_HASHTABLE_FOREACH(self, pkey, pvalue, code) {\ #define GIT_HASHTABLE__FOREACH(self,block) { \
git_hashtable *_self = (self);\ unsigned int _c; \
git_hashtable_node *_nodes = _self->nodes;\ git_hashtable_node *_n = (self)->nodes; \
unsigned int _i, _size = _self->size;\ for (_c = (self)->size; _c > 0; _c--, _n++) { \
for (_i = 0; _i < _size; _i ++) {\ if (!_n->key) continue; block } }
git_hashtable_node *_node = git_hashtable_node_at(_nodes, _i);\
if (_node->key)\ #define GIT_HASHTABLE_FOREACH(self, pkey, pvalue, code)\
{\ GIT_HASHTABLE__FOREACH(self,{(pkey)=_n->key;(pvalue)=_n->value;code;})
pkey = _node->key;\
pvalue = _node->value;\ #define GIT_HASHTABLE_FOREACH_KEY(self, pkey, code)\
code;\ GIT_HASHTABLE__FOREACH(self,{(pkey)=_n->key;code;})
}\
}\ #define GIT_HASHTABLE_FOREACH_VALUE(self, pvalue, code)\
} GIT_HASHTABLE__FOREACH(self,{(pvalue)=_n->value;code;})
#define GIT_HASHTABLE_FOREACH_DELETE() {\ #define GIT_HASHTABLE_FOREACH_DELETE() {\
_node->key = NULL; _node->value = NULL; _self->key_count--;\ _node->key = NULL; _node->value = NULL; _self->key_count--;\
......
...@@ -159,9 +159,9 @@ static int pack_entry_find_prefix(struct git_pack_entry *e, ...@@ -159,9 +159,9 @@ static int pack_entry_find_prefix(struct git_pack_entry *e,
* *
***********************************************************/ ***********************************************************/
GIT_INLINE(void) pack_window_free_all(struct pack_backend *GIT_UNUSED(backend), struct git_pack_file *p) GIT_INLINE(void) pack_window_free_all(struct pack_backend *backend, struct git_pack_file *p)
{ {
GIT_UNUSED_ARG(backend); GIT_UNUSED(backend);
git_mwindow_free_all(&p->mwf); git_mwindow_free_all(&p->mwf);
} }
......
...@@ -41,11 +41,11 @@ static int flush_pkt(git_pkt **out) ...@@ -41,11 +41,11 @@ static int flush_pkt(git_pkt **out)
} }
/* the rest of the line will be useful for multi_ack */ /* the rest of the line will be useful for multi_ack */
static int ack_pkt(git_pkt **out, const char *GIT_UNUSED(line), size_t GIT_UNUSED(len)) static int ack_pkt(git_pkt **out, const char *line, size_t len)
{ {
git_pkt *pkt; git_pkt *pkt;
GIT_UNUSED_ARG(line); GIT_UNUSED(line);
GIT_UNUSED_ARG(len); GIT_UNUSED(len);
pkt = git__malloc(sizeof(git_pkt)); pkt = git__malloc(sizeof(git_pkt));
if (pkt == NULL) if (pkt == NULL)
......
...@@ -105,7 +105,7 @@ static int reference_alloc( ...@@ -105,7 +105,7 @@ static int reference_alloc(
reference->name = git__strdup(name); reference->name = git__strdup(name);
if (reference->name == NULL) { if (reference->name == NULL) {
free(reference); git__free(reference);
return GIT_ENOMEM; return GIT_ENOMEM;
} }
...@@ -222,7 +222,7 @@ static int loose_lookup(git_reference *ref) ...@@ -222,7 +222,7 @@ static int loose_lookup(git_reference *ref)
return GIT_SUCCESS; return GIT_SUCCESS;
if (ref->flags & GIT_REF_SYMBOLIC) { if (ref->flags & GIT_REF_SYMBOLIC) {
free(ref->target.symbolic); git__free(ref->target.symbolic);
ref->target.symbolic = NULL; ref->target.symbolic = NULL;
} }
...@@ -278,7 +278,8 @@ static int loose_lookup_to_packfile( ...@@ -278,7 +278,8 @@ static int loose_lookup_to_packfile(
cleanup: cleanup:
git_buf_free(&ref_file); git_buf_free(&ref_file);
free(ref); git__free(ref);
return git__rethrow(error, "Failed to lookup loose reference"); return git__rethrow(error, "Failed to lookup loose reference");
} }
...@@ -420,7 +421,7 @@ static int packed_parse_oid( ...@@ -420,7 +421,7 @@ static int packed_parse_oid(
return GIT_SUCCESS; return GIT_SUCCESS;
cleanup: cleanup:
free(ref); git__free(ref);
return git__rethrow(error, "Failed to parse OID of packed reference"); return git__rethrow(error, "Failed to parse OID of packed reference");
} }
...@@ -495,7 +496,7 @@ static int packed_load(git_repository *repo) ...@@ -495,7 +496,7 @@ static int packed_load(git_repository *repo)
error = git_hashtable_insert(ref_cache->packfile, ref->name, ref); error = git_hashtable_insert(ref_cache->packfile, ref->name, ref);
if (error < GIT_SUCCESS) { if (error < GIT_SUCCESS) {
free(ref); git__free(ref);
goto cleanup; goto cleanup;
} }
} }
...@@ -560,12 +561,12 @@ static int _dirent_loose_load(void *data, git_buf *full_path) ...@@ -560,12 +561,12 @@ static int _dirent_loose_load(void *data, git_buf *full_path)
if (git_hashtable_insert2( if (git_hashtable_insert2(
repository->references.packfile, repository->references.packfile,
ref->name, ref, &old_ref) < GIT_SUCCESS) { ref->name, ref, &old_ref) < GIT_SUCCESS) {
free(ref); git__free(ref);
return GIT_ENOMEM; return GIT_ENOMEM;
} }
if (old_ref != NULL) if (old_ref != NULL)
free(old_ref); git__free(old_ref);
} }
return error == GIT_SUCCESS ? return error == GIT_SUCCESS ?
...@@ -773,9 +774,8 @@ static int packed_write(git_repository *repo) ...@@ -773,9 +774,8 @@ static int packed_write(git_repository *repo)
/* Load all the packfile into a vector */ /* Load all the packfile into a vector */
{ {
struct packref *reference; struct packref *reference;
const void *GIT_UNUSED(_unused);
GIT_HASHTABLE_FOREACH(repo->references.packfile, _unused, reference, GIT_HASHTABLE_FOREACH_VALUE(repo->references.packfile, reference,
/* cannot fail: vector already has the right size */ /* cannot fail: vector already has the right size */
git_vector_insert(&packing_list, reference); git_vector_insert(&packing_list, reference);
); );
...@@ -929,7 +929,7 @@ static int packed_lookup(git_reference *ref) ...@@ -929,7 +929,7 @@ static int packed_lookup(git_reference *ref)
return GIT_SUCCESS; return GIT_SUCCESS;
if (ref->flags & GIT_REF_SYMBOLIC) { if (ref->flags & GIT_REF_SYMBOLIC) {
free(ref->target.symbolic); git__free(ref->target.symbolic);
ref->target.symbolic = NULL; ref->target.symbolic = NULL;
} }
...@@ -1513,12 +1513,11 @@ int git_reference_foreach( ...@@ -1513,12 +1513,11 @@ int git_reference_foreach(
/* list all the packed references first */ /* list all the packed references first */
if (list_flags & GIT_REF_PACKED) { if (list_flags & GIT_REF_PACKED) {
const char *ref_name; const char *ref_name;
void *GIT_UNUSED(_unused);
if ((error = packed_load(repo)) < GIT_SUCCESS) if ((error = packed_load(repo)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to list references"); return git__rethrow(error, "Failed to list references");
GIT_HASHTABLE_FOREACH(repo->references.packfile, ref_name, _unused, GIT_HASHTABLE_FOREACH_KEY(repo->references.packfile, ref_name,
if ((error = callback(ref_name, payload)) < GIT_SUCCESS) if ((error = callback(ref_name, payload)) < GIT_SUCCESS)
return git__throw(error, return git__throw(error,
"Failed to list references. User callback failed"); "Failed to list references. User callback failed");
...@@ -1595,12 +1594,10 @@ void git_repository__refcache_free(git_refcache *refs) ...@@ -1595,12 +1594,10 @@ void git_repository__refcache_free(git_refcache *refs)
assert(refs); assert(refs);
if (refs->packfile) { if (refs->packfile) {
const void *GIT_UNUSED(_unused);
struct packref *reference; struct packref *reference;
GIT_HASHTABLE_FOREACH(refs->packfile, _unused, reference, GIT_HASHTABLE_FOREACH_VALUE(
free(reference); refs->packfile, reference, git__free(reference));
);
git_hashtable_free(refs->packfile); git_hashtable_free(refs->packfile);
} }
......
...@@ -431,13 +431,13 @@ struct cb_data { ...@@ -431,13 +431,13 @@ struct cb_data {
regex_t *preg; regex_t *preg;
}; };
static int remote_list_cb(const char *name, const char *GIT_UNUSED(value), void *data_) static int remote_list_cb(const char *name, const char *value, void *data_)
{ {
struct cb_data *data = (struct cb_data *)data_; struct cb_data *data = (struct cb_data *)data_;
size_t nmatch = 2; size_t nmatch = 2;
regmatch_t pmatch[2]; regmatch_t pmatch[2];
int error; int error;
GIT_UNUSED_ARG(value); GIT_UNUSED(value);
if (!regexec(data->preg, name, nmatch, pmatch, 0)) { if (!regexec(data->preg, name, nmatch, pmatch, 0)) {
char *remote_name = git__strndup(&name[pmatch[1].rm_so], pmatch[1].rm_eo - pmatch[1].rm_so); char *remote_name = git__strndup(&name[pmatch[1].rm_so], pmatch[1].rm_eo - pmatch[1].rm_so);
......
...@@ -590,7 +590,6 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) ...@@ -590,7 +590,6 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
void git_revwalk_free(git_revwalk *walk) void git_revwalk_free(git_revwalk *walk)
{ {
unsigned int i; unsigned int i;
const void *GIT_UNUSED(_unused);
commit_object *commit; commit_object *commit;
if (walk == NULL) if (walk == NULL)
...@@ -602,7 +601,7 @@ void git_revwalk_free(git_revwalk *walk) ...@@ -602,7 +601,7 @@ void git_revwalk_free(git_revwalk *walk)
/* if the parent has more than PARENTS_PER_COMMIT parents, /* if the parent has more than PARENTS_PER_COMMIT parents,
* we had to allocate a separate array for those parents. * we had to allocate a separate array for those parents.
* make sure it's being free'd */ * make sure it's being free'd */
GIT_HASHTABLE_FOREACH(walk->commits, _unused, commit, { GIT_HASHTABLE_FOREACH_VALUE(walk->commits, commit, {
if (commit->out_degree > PARENTS_PER_COMMIT) if (commit->out_degree > PARENTS_PER_COMMIT)
git__free(commit->parents); git__free(commit->parents);
}); });
...@@ -669,12 +668,11 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk) ...@@ -669,12 +668,11 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk)
void git_revwalk_reset(git_revwalk *walk) void git_revwalk_reset(git_revwalk *walk)
{ {
const void *GIT_UNUSED(_unused);
commit_object *commit; commit_object *commit;
assert(walk); assert(walk);
GIT_HASHTABLE_FOREACH(walk->commits, _unused, commit, GIT_HASHTABLE_FOREACH_VALUE(walk->commits, commit,
commit->seen = 0; commit->seen = 0;
commit->in_degree = 0; commit->in_degree = 0;
commit->topo_delay = 0; commit->topo_delay = 0;
......
...@@ -43,9 +43,9 @@ static git_transport_cb transport_find_fn(const char *url) ...@@ -43,9 +43,9 @@ static git_transport_cb transport_find_fn(const char *url)
* Public API * * Public API *
**************/ **************/
int git_transport_dummy(git_transport **GIT_UNUSED(transport)) int git_transport_dummy(git_transport **transport)
{ {
GIT_UNUSED_ARG(transport); GIT_UNUSED(transport);
return git__throw(GIT_ENOTIMPLEMENTED, "This protocol isn't implemented. Sorry"); return git__throw(GIT_ENOTIMPLEMENTED, "This protocol isn't implemented. Sorry");
} }
......
...@@ -154,7 +154,7 @@ static int local_ls(git_transport *transport, git_headlist_cb list_cb, void *pay ...@@ -154,7 +154,7 @@ static int local_ls(git_transport *transport, git_headlist_cb list_cb, void *pay
* Try to open the url as a git directory. The direction doesn't * Try to open the url as a git directory. The direction doesn't
* matter in this case because we're calulating the heads ourselves. * matter in this case because we're calulating the heads ourselves.
*/ */
static int local_connect(git_transport *transport, int GIT_UNUSED(direction)) static int local_connect(git_transport *transport, int direction)
{ {
git_repository *repo; git_repository *repo;
int error; int error;
...@@ -162,7 +162,7 @@ static int local_connect(git_transport *transport, int GIT_UNUSED(direction)) ...@@ -162,7 +162,7 @@ static int local_connect(git_transport *transport, int GIT_UNUSED(direction))
const char *path; const char *path;
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
GIT_UNUSED_ARG(direction); GIT_UNUSED(direction);
/* The repo layer doesn't want the prefix */ /* The repo layer doesn't want the prefix */
if (!git__prefixcmp(transport->url, "file://")) { if (!git__prefixcmp(transport->url, "file://")) {
...@@ -194,7 +194,7 @@ static int local_connect(git_transport *transport, int GIT_UNUSED(direction)) ...@@ -194,7 +194,7 @@ static int local_connect(git_transport *transport, int GIT_UNUSED(direction))
return GIT_SUCCESS; return GIT_SUCCESS;
} }
static int local_close(git_transport *GIT_UNUSED(transport)) static int local_close(git_transport *transport)
{ {
transport_local *t = (transport_local *)transport; transport_local *t = (transport_local *)transport;
......
...@@ -11,20 +11,20 @@ ...@@ -11,20 +11,20 @@
#include "fnmatch.h" #include "fnmatch.h"
#include "utf-conv.h" #include "utf-conv.h"
GIT_INLINE(int) p_link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new)) GIT_INLINE(int) p_link(const char *old, const char *new)
{ {
GIT_UNUSED_ARG(old); GIT_UNUSED(old);
GIT_UNUSED_ARG(new); GIT_UNUSED(new);
errno = ENOSYS; errno = ENOSYS;
return -1; return -1;
} }
GIT_INLINE(int) p_mkdir(const char *path, mode_t GIT_UNUSED(mode)) GIT_INLINE(int) p_mkdir(const char *path, mode_t mode)
{ {
wchar_t* buf = gitwin_to_utf16(path); wchar_t* buf = gitwin_to_utf16(path);
int ret = _wmkdir(buf); int ret = _wmkdir(buf);
GIT_UNUSED_ARG(mode); GIT_UNUSED(mode);
git__free(buf); git__free(buf);
return ret; return ret;
......
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
#include "pthread.h" #include "pthread.h"
int pthread_create(pthread_t *GIT_RESTRICT thread, int pthread_create(pthread_t *GIT_RESTRICT thread,
const pthread_attr_t *GIT_RESTRICT GIT_UNUSED(attr), const pthread_attr_t *GIT_RESTRICT attr,
void *(*start_routine)(void*), void *GIT_RESTRICT arg) void *(*start_routine)(void*), void *GIT_RESTRICT arg)
{ {
GIT_UNUSED_ARG(attr); GIT_UNUSED(attr);
*thread = (pthread_t) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, NULL); *thread = (pthread_t) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, NULL);
return *thread ? GIT_SUCCESS : git__throw(GIT_EOSERR, "Failed to create pthread"); return *thread ? GIT_SUCCESS : git__throw(GIT_EOSERR, "Failed to create pthread");
} }
...@@ -26,9 +26,9 @@ int pthread_join(pthread_t thread, void **value_ptr) ...@@ -26,9 +26,9 @@ int pthread_join(pthread_t thread, void **value_ptr)
} }
int pthread_mutex_init(pthread_mutex_t *GIT_RESTRICT mutex, int pthread_mutex_init(pthread_mutex_t *GIT_RESTRICT mutex,
const pthread_mutexattr_t *GIT_RESTRICT GIT_UNUSED(mutexattr)) const pthread_mutexattr_t *GIT_RESTRICT mutexattr)
{ {
GIT_UNUSED_ARG(mutexattr); GIT_UNUSED(mutexattr);
InitializeCriticalSection(mutex); InitializeCriticalSection(mutex);
return 0; return 0;
} }
......
...@@ -104,12 +104,12 @@ void test_attr_repo__get_many(void) ...@@ -104,12 +104,12 @@ void test_attr_repo__get_many(void)
} }
static int count_attrs( static int count_attrs(
const char *GIT_UNUSED(name), const char *name,
const char *GIT_UNUSED(value), const char *value,
void *payload) void *payload)
{ {
GIT_UNUSED_ARG(name); GIT_UNUSED(name);
GIT_UNUSED_ARG(value); GIT_UNUSED(value);
*((int *)payload) += 1; *((int *)payload) += 1;
......
...@@ -39,3 +39,52 @@ void cl_git_append2file(const char *filename, const char *new_content) ...@@ -39,3 +39,52 @@ void cl_git_append2file(const char *filename, const char *new_content)
cl_must_pass(p_chmod(filename, 0644)); cl_must_pass(p_chmod(filename, 0644));
} }
static const char *_cl_sandbox = NULL;
static git_repository *_cl_repo = NULL;
git_repository *cl_git_sandbox_init(const char *sandbox)
{
/* Copy the whole sandbox folder from our fixtures to our test sandbox
* area. After this it can be accessed with `./sandbox`
*/
cl_fixture_sandbox(sandbox);
_cl_sandbox = sandbox;
p_chdir(sandbox);
/* Rename `sandbox/.gitted` to `sandbox/.git` which must be done since
* we cannot store a folder named `.git` inside the fixtures folder of
* our libgit2 repo.
*/
cl_git_pass(p_rename(".gitted", ".git"));
/* If we have `gitattributes`, rename to `.gitattributes`. This may
* be necessary if we don't want the attributes to be applied in the
* libgit2 repo, but just during testing.
*/
if (p_access("gitattributes", F_OK) == 0)
cl_git_pass(p_rename("gitattributes", ".gitattributes"));
/* As with `gitattributes`, we may need `gitignore` just for testing. */
if (p_access("gitignore", F_OK) == 0)
cl_git_pass(p_rename("gitignore", ".gitignore"));
p_chdir("..");
/* Now open the sandbox repository and make it available for tests */
cl_git_pass(git_repository_open(&_cl_repo, sandbox));
return _cl_repo;
}
void cl_git_sandbox_cleanup(void)
{
if (_cl_repo) {
git_repository_free(_cl_repo);
_cl_repo = NULL;
}
if (_cl_sandbox) {
cl_fixture_cleanup(_cl_sandbox);
_cl_sandbox = NULL;
}
}
...@@ -58,4 +58,9 @@ GIT_INLINE(void) cl_assert_strequal_internal( ...@@ -58,4 +58,9 @@ GIT_INLINE(void) cl_assert_strequal_internal(
void cl_git_mkfile(const char *filename, const char *content); void cl_git_mkfile(const char *filename, const char *content);
void cl_git_append2file(const char *filename, const char *new_content); void cl_git_append2file(const char *filename, const char *new_content);
/* Git sandbox setup helpers */
git_repository *cl_git_sandbox_init(const char *sandbox);
void cl_git_sandbox_cleanup(void);
#endif #endif
...@@ -12,10 +12,12 @@ void test_config_multivar__cleanup(void) ...@@ -12,10 +12,12 @@ void test_config_multivar__cleanup(void)
cl_fixture_cleanup("config"); cl_fixture_cleanup("config");
} }
static int mv_read_cb(const char *name, const char *GIT_UNUSED(value), void *data) static int mv_read_cb(const char *name, const char *value, void *data)
{ {
int *n = (int *) data; int *n = (int *) data;
GIT_UNUSED(value);
if (!strcmp(name, _name)) if (!strcmp(name, _name))
(*n)++; (*n)++;
...@@ -35,10 +37,12 @@ void test_config_multivar__foreach(void) ...@@ -35,10 +37,12 @@ void test_config_multivar__foreach(void)
git_config_free(cfg); git_config_free(cfg);
} }
static int cb(const char *GIT_UNUSED(val), void *data) static int cb(const char *val, void *data)
{ {
int *n = (int *) data; int *n = (int *) data;
GIT_UNUSED(val);
(*n)++; (*n)++;
return GIT_SUCCESS; return GIT_SUCCESS;
......
...@@ -88,10 +88,10 @@ static int one_entry(void *state, git_buf *path) ...@@ -88,10 +88,10 @@ static int one_entry(void *state, git_buf *path)
return GIT_ERROR; return GIT_ERROR;
} }
static int dont_call_me(void *GIT_UNUSED(state), git_buf *GIT_UNUSED(path)) static int dont_call_me(void *state, git_buf *path)
{ {
GIT_UNUSED_ARG(state); GIT_UNUSED(state);
GIT_UNUSED_ARG(path); GIT_UNUSED(path);
return GIT_ERROR; return GIT_ERROR;
} }
......
...@@ -5,17 +5,12 @@ static git_repository *g_repo = NULL; ...@@ -5,17 +5,12 @@ static git_repository *g_repo = NULL;
void test_diff_blob__initialize(void) void test_diff_blob__initialize(void)
{ {
cl_fixture_sandbox("attr"); g_repo = cl_git_sandbox_init("attr");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
} }
void test_diff_blob__cleanup(void) void test_diff_blob__cleanup(void)
{ {
git_repository_free(g_repo); cl_git_sandbox_cleanup();
g_repo = NULL;
cl_fixture_cleanup("attr");
} }
void test_diff_blob__0(void) void test_diff_blob__0(void)
......
...@@ -5,16 +5,12 @@ static git_repository *g_repo = NULL; ...@@ -5,16 +5,12 @@ static git_repository *g_repo = NULL;
void test_diff_index__initialize(void) void test_diff_index__initialize(void)
{ {
cl_fixture_sandbox("status"); g_repo = cl_git_sandbox_init("status");
cl_git_pass(p_rename("status/.gitted", "status/.git"));
cl_git_pass(git_repository_open(&g_repo, "status/.git"));
} }
void test_diff_index__cleanup(void) void test_diff_index__cleanup(void)
{ {
git_repository_free(g_repo); cl_git_sandbox_cleanup();
g_repo = NULL;
cl_fixture_cleanup("status");
} }
void test_diff_index__0(void) void test_diff_index__0(void)
......
...@@ -2,37 +2,6 @@ ...@@ -2,37 +2,6 @@
#include "diff_helpers.h" #include "diff_helpers.h"
#include "iterator.h" #include "iterator.h"
static git_repository *g_repo = NULL;
static const char *g_sandbox = NULL;
static void setup_sandbox(const char *sandbox)
{
cl_fixture_sandbox(sandbox);
g_sandbox = sandbox;
p_chdir(sandbox);
cl_git_pass(p_rename(".gitted", ".git"));
if (p_access("gitattributes", F_OK) == 0)
cl_git_pass(p_rename("gitattributes", ".gitattributes"));
if (p_access("gitignore", F_OK) == 0)
cl_git_pass(p_rename("gitignore", ".gitignore"));
p_chdir("..");
cl_git_pass(git_repository_open(&g_repo, sandbox));
}
static void cleanup_sandbox(void)
{
if (g_repo) {
git_repository_free(g_repo);
g_repo = NULL;
}
if (g_sandbox) {
cl_fixture_cleanup(g_sandbox);
g_sandbox = NULL;
}
}
void test_diff_iterator__initialize(void) void test_diff_iterator__initialize(void)
{ {
/* since we are doing tests with different sandboxes, defer setup /* since we are doing tests with different sandboxes, defer setup
...@@ -44,7 +13,7 @@ void test_diff_iterator__initialize(void) ...@@ -44,7 +13,7 @@ void test_diff_iterator__initialize(void)
void test_diff_iterator__cleanup(void) void test_diff_iterator__cleanup(void)
{ {
cleanup_sandbox(); cl_git_sandbox_cleanup();
} }
...@@ -60,11 +29,10 @@ static void tree_iterator_test( ...@@ -60,11 +29,10 @@ static void tree_iterator_test(
git_iterator *i; git_iterator *i;
const git_index_entry *entry; const git_index_entry *entry;
int count = 0; int count = 0;
git_repository *repo = cl_git_sandbox_init(sandbox);
setup_sandbox(sandbox); cl_assert(t = resolve_commit_oid_to_tree(repo, treeish));
cl_git_pass(git_iterator_for_tree(repo, t, &i));
cl_assert(t = resolve_commit_oid_to_tree(g_repo, treeish));
cl_git_pass(git_iterator_for_tree(g_repo, t, &i));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(i, &entry));
while (entry != NULL) { while (entry != NULL) {
...@@ -183,10 +151,9 @@ static void index_iterator_test( ...@@ -183,10 +151,9 @@ static void index_iterator_test(
git_iterator *i; git_iterator *i;
const git_index_entry *entry; const git_index_entry *entry;
int count = 0; int count = 0;
git_repository *repo = cl_git_sandbox_init(sandbox);
setup_sandbox(sandbox); cl_git_pass(git_iterator_for_index(repo, &i));
cl_git_pass(git_iterator_for_index(g_repo, &i));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(i, &entry));
while (entry != NULL) { while (entry != NULL) {
...@@ -303,10 +270,9 @@ static void workdir_iterator_test( ...@@ -303,10 +270,9 @@ static void workdir_iterator_test(
git_iterator *i; git_iterator *i;
const git_index_entry *entry; const git_index_entry *entry;
int count = 0, count_all = 0; int count = 0, count_all = 0;
git_repository *repo = cl_git_sandbox_init(sandbox);
setup_sandbox(sandbox); cl_git_pass(git_iterator_for_workdir(repo, &i));
cl_git_pass(git_iterator_for_workdir(g_repo, &i));
cl_git_pass(git_iterator_current(i, &entry)); cl_git_pass(git_iterator_current(i, &entry));
while (entry != NULL) { while (entry != NULL) {
......
...@@ -5,17 +5,12 @@ static git_repository *g_repo = NULL; ...@@ -5,17 +5,12 @@ static git_repository *g_repo = NULL;
void test_diff_tree__initialize(void) void test_diff_tree__initialize(void)
{ {
cl_fixture_sandbox("attr"); g_repo = cl_git_sandbox_init("attr");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
} }
void test_diff_tree__cleanup(void) void test_diff_tree__cleanup(void)
{ {
git_repository_free(g_repo); cl_git_sandbox_cleanup();
g_repo = NULL;
cl_fixture_cleanup("attr");
} }
void test_diff_tree__0(void) void test_diff_tree__0(void)
......
...@@ -5,16 +5,12 @@ static git_repository *g_repo = NULL; ...@@ -5,16 +5,12 @@ static git_repository *g_repo = NULL;
void test_diff_workdir__initialize(void) void test_diff_workdir__initialize(void)
{ {
cl_fixture_sandbox("status"); g_repo = cl_git_sandbox_init("status");
cl_git_pass(p_rename("status/.gitted", "status/.git"));
cl_git_pass(git_repository_open(&g_repo, "status/.git"));
} }
void test_diff_workdir__cleanup(void) void test_diff_workdir__cleanup(void)
{ {
git_repository_free(g_repo); cl_git_sandbox_cleanup();
g_repo = NULL;
cl_fixture_cleanup("status");
} }
void test_diff_workdir__to_index(void) void test_diff_workdir__to_index(void)
......
...@@ -7,21 +7,12 @@ static git_repository *g_repo = NULL; ...@@ -7,21 +7,12 @@ static git_repository *g_repo = NULL;
void test_status_ignore__initialize(void) void test_status_ignore__initialize(void)
{ {
/* Before each test, instantiate the attr repo from the fixtures and g_repo = cl_git_sandbox_init("attr");
* rename the .gitted to .git so it is a repo with a working dir. Also
* rename gitignore to .gitignore.
*/
cl_fixture_sandbox("attr");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
cl_git_pass(p_rename("attr/gitignore", "attr/.gitignore"));
cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
} }
void test_status_ignore__cleanup(void) void test_status_ignore__cleanup(void)
{ {
git_repository_free(g_repo); cl_git_sandbox_cleanup();
g_repo = NULL;
cl_fixture_cleanup("attr");
} }
void test_status_ignore__0(void) void test_status_ignore__0(void)
......
...@@ -5,12 +5,6 @@ ...@@ -5,12 +5,6 @@
/** /**
* Test fixtures
*/
static git_repository *_repository = NULL;
/**
* Auxiliary methods * Auxiliary methods
*/ */
static int static int
...@@ -37,48 +31,27 @@ exit: ...@@ -37,48 +31,27 @@ exit:
} }
static int static int
cb_status__count(const char *GIT_UNUSED(p), unsigned int GIT_UNUSED(s), void *payload) cb_status__count(const char *p, unsigned int s, void *payload)
{ {
volatile int *count = (int *)payload; volatile int *count = (int *)payload;
GIT_UNUSED_ARG(p); GIT_UNUSED(p);
GIT_UNUSED_ARG(s); GIT_UNUSED(s);
*count++; (*count)++;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
/** /**
* Initializer * Initializer
* *
* This method is called once before starting each * Not all of the tests in this file use the same fixtures, so we allow each
* test, and will load the required fixtures * test to load their fixture at the top of the test function.
*/ */
void test_status_worktree__initialize(void) void test_status_worktree__initialize(void)
{ {
/*
* Sandbox the `status/` repository from our Fixtures.
* This will copy the whole folder to our sandbox,
* so now it can be accessed with `./status`
*/
cl_fixture_sandbox("status");
/*
* Rename `status/.gitted` to `status/.git`
* We do this because we cannot store a folder named `.git`
* inside the fixtures folder in our libgit2 repo.
*/
cl_git_pass(
p_rename("status/.gitted", "status/.git")
);
/*
* Open the sandboxed "status" repository
*/
cl_git_pass(git_repository_open(&_repository, "status/.git"));
} }
/** /**
...@@ -89,10 +62,7 @@ void test_status_worktree__initialize(void) ...@@ -89,10 +62,7 @@ void test_status_worktree__initialize(void)
*/ */
void test_status_worktree__cleanup(void) void test_status_worktree__cleanup(void)
{ {
git_repository_free(_repository); cl_git_sandbox_cleanup();
_repository = NULL;
cl_fixture_cleanup("status");
} }
/** /**
...@@ -101,6 +71,7 @@ void test_status_worktree__cleanup(void) ...@@ -101,6 +71,7 @@ void test_status_worktree__cleanup(void)
void test_status_worktree__whole_repository(void) void test_status_worktree__whole_repository(void)
{ {
struct status_entry_counts counts; struct status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
memset(&counts, 0x0, sizeof(struct status_entry_counts)); memset(&counts, 0x0, sizeof(struct status_entry_counts));
counts.expected_entry_count = entry_count0; counts.expected_entry_count = entry_count0;
...@@ -108,7 +79,7 @@ void test_status_worktree__whole_repository(void) ...@@ -108,7 +79,7 @@ void test_status_worktree__whole_repository(void)
counts.expected_statuses = entry_statuses0; counts.expected_statuses = entry_statuses0;
cl_git_pass( cl_git_pass(
git_status_foreach(_repository, cb_status__normal, &counts) git_status_foreach(repo, cb_status__normal, &counts)
); );
cl_assert(counts.entry_count == counts.expected_entry_count); cl_assert(counts.entry_count == counts.expected_entry_count);
...@@ -119,8 +90,10 @@ void test_status_worktree__whole_repository(void) ...@@ -119,8 +90,10 @@ void test_status_worktree__whole_repository(void)
void test_status_worktree__empty_repository(void) void test_status_worktree__empty_repository(void)
{ {
int count = 0; int count = 0;
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_status_foreach(repo, cb_status__count, &count));
git_status_foreach(_repository, cb_status__count, &count);
cl_assert(count == 0); cl_assert(count == 0);
} }
...@@ -128,10 +101,11 @@ void test_status_worktree__single_file(void) ...@@ -128,10 +101,11 @@ void test_status_worktree__single_file(void)
{ {
int i; int i;
unsigned int status_flags; unsigned int status_flags;
git_repository *repo = cl_git_sandbox_init("status");
for (i = 0; i < (int)entry_count0; i++) { for (i = 0; i < (int)entry_count0; i++) {
cl_git_pass( cl_git_pass(
git_status_file(&status_flags, _repository, entry_paths0[i]) git_status_file(&status_flags, repo, entry_paths0[i])
); );
cl_assert(entry_statuses0[i] == status_flags); cl_assert(entry_statuses0[i] == status_flags);
} }
...@@ -140,15 +114,22 @@ void test_status_worktree__single_file(void) ...@@ -140,15 +114,22 @@ void test_status_worktree__single_file(void)
void test_status_worktree__ignores(void) void test_status_worktree__ignores(void)
{ {
int i, ignored; int i, ignored;
git_repository *repo = cl_git_sandbox_init("status");
for (i = 0; i < (int)entry_count0; i++) { for (i = 0; i < (int)entry_count0; i++) {
cl_git_pass(git_status_should_ignore(_repository, entry_paths0[i], &ignored)); cl_git_pass(
git_status_should_ignore(repo, entry_paths0[i], &ignored)
);
cl_assert(ignored == (entry_statuses0[i] == GIT_STATUS_WT_IGNORED)); cl_assert(ignored == (entry_statuses0[i] == GIT_STATUS_WT_IGNORED));
} }
cl_git_pass(git_status_should_ignore(_repository, "nonexistent_file", &ignored)); cl_git_pass(
git_status_should_ignore(repo, "nonexistent_file", &ignored)
);
cl_assert(!ignored); cl_assert(!ignored);
cl_git_pass(git_status_should_ignore(_repository, "ignored_nonexistent_file", &ignored)); cl_git_pass(
git_status_should_ignore(repo, "ignored_nonexistent_file", &ignored)
);
cl_assert(ignored); cl_assert(ignored);
} }
...@@ -424,10 +424,10 @@ static walk_data empty = { ...@@ -424,10 +424,10 @@ static walk_data empty = {
GIT_BUF_INIT GIT_BUF_INIT
}; };
static int dont_call_me(void *GIT_UNUSED(state), git_buf *GIT_UNUSED(path)) static int dont_call_me(void *state, git_buf *path)
{ {
GIT_UNUSED_ARG(state); GIT_UNUSED(state);
GIT_UNUSED_ARG(path); GIT_UNUSED(path);
return GIT_ERROR; return GIT_ERROR;
} }
......
...@@ -154,7 +154,6 @@ BEGIN_TEST(tableit0, "iterate through all the contents of the table") ...@@ -154,7 +154,6 @@ BEGIN_TEST(tableit0, "iterate through all the contents of the table")
const int objects_n = 32; const int objects_n = 32;
int i; int i;
table_item *objects, *ob; table_item *objects, *ob;
const void *GIT_UNUSED(_unused);
git_hashtable *table = NULL; git_hashtable *table = NULL;
...@@ -170,9 +169,7 @@ BEGIN_TEST(tableit0, "iterate through all the contents of the table") ...@@ -170,9 +169,7 @@ BEGIN_TEST(tableit0, "iterate through all the contents of the table")
must_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i]))); must_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
} }
GIT_HASHTABLE_FOREACH(table, _unused, ob, GIT_HASHTABLE_FOREACH_VALUE(table, ob, ob->visited = 1);
ob->visited = 1;
);
/* make sure all nodes have been visited */ /* make sure all nodes have been visited */
for (i = 0; i < objects_n; ++i) for (i = 0; i < objects_n; ++i)
......
...@@ -156,14 +156,14 @@ BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository") ...@@ -156,14 +156,14 @@ BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository")
git_futils_rmdir_r(TEMP_REPO_FOLDER, 1); git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
END_TEST END_TEST
static int status_cb1(const char *GIT_UNUSED(path), unsigned int GIT_UNUSED(status_flags), void *payload) static int status_cb1(const char *path, unsigned int status_flags, void *payload)
{ {
int *count = (int *)payload;; int *count = (int *)payload;;
GIT_UNUSED_ARG(path); GIT_UNUSED(path);
GIT_UNUSED_ARG(status_flags); GIT_UNUSED(status_flags);
(void) *count++; (*count)++;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
......
...@@ -70,12 +70,12 @@ int __cdecl ...@@ -70,12 +70,12 @@ int __cdecl
#else #else
int int
#endif #endif
main(int GIT_UNUSED(argc), char *GIT_UNUSED(argv[])) main(int argc, char *argv[])
{ {
unsigned int i, failures; unsigned int i, failures;
GIT_UNUSED_ARG(argc); GIT_UNUSED(argc);
GIT_UNUSED_ARG(argv); GIT_UNUSED(argv);
git_threads_init(); git_threads_init();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment