summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/branch.c57
-rw-r--r--src/clone.c3
-rw-r--r--src/commit.c2
-rw-r--r--src/config_file.c2
-rw-r--r--src/diff.c2
-rw-r--r--src/diff_output.c2
-rw-r--r--src/object.c2
-rw-r--r--src/push.c2
-rw-r--r--src/refdb.c80
-rw-r--r--src/refdb.h16
-rw-r--r--src/refdb_fs.c179
-rw-r--r--src/refs.c145
-rw-r--r--src/remote.c35
-rw-r--r--src/repository.c5
-rw-r--r--src/revparse.c69
-rw-r--r--src/revwalk.c2
-rw-r--r--src/status.c20
-rw-r--r--src/tag.c2
-rw-r--r--src/transports/local.c2
-rw-r--r--src/transports/smart_protocol.c12
-rw-r--r--src/transports/winhttp.c2
21 files changed, 369 insertions, 272 deletions
diff --git a/src/branch.c b/src/branch.c
index 830294941..dd6dc68f4 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -124,40 +124,43 @@ on_error:
return error;
}
-typedef struct {
- git_branch_foreach_cb branch_cb;
- void *callback_payload;
- unsigned int branch_type;
-} branch_foreach_filter;
-
-static int branch_foreach_cb(const char *branch_name, void *payload)
-{
- branch_foreach_filter *filter = (branch_foreach_filter *)payload;
-
- if (filter->branch_type & GIT_BRANCH_LOCAL &&
- git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0)
- return filter->branch_cb(branch_name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, filter->callback_payload);
-
- if (filter->branch_type & GIT_BRANCH_REMOTE &&
- git__prefixcmp(branch_name, GIT_REFS_REMOTES_DIR) == 0)
- return filter->branch_cb(branch_name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, filter->callback_payload);
-
- return 0;
-}
-
int git_branch_foreach(
git_repository *repo,
unsigned int list_flags,
- git_branch_foreach_cb branch_cb,
+ git_branch_foreach_cb callback,
void *payload)
{
- branch_foreach_filter filter;
+ git_reference_iterator *iter;
+ const char *name;
+ int error;
+
+ if (git_reference_iterator_new(&iter, repo) < 0)
+ return -1;
+
+ while ((error = git_reference_next(&name, iter)) == 0) {
+ if (list_flags & GIT_BRANCH_LOCAL &&
+ git__prefixcmp(name, GIT_REFS_HEADS_DIR) == 0) {
+ if (callback(name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, payload)) {
+ error = GIT_EUSER;
+ break;
+ }
+ }
+
+ if (list_flags & GIT_BRANCH_REMOTE &&
+ git__prefixcmp(name, GIT_REFS_REMOTES_DIR) == 0) {
+ if (callback(name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, payload)) {
+ error = GIT_EUSER;
+ break;
+ }
+ }
+ }
- filter.branch_cb = branch_cb;
- filter.branch_type = list_flags;
- filter.callback_payload = payload;
+ if (error == GIT_ITEROVER)
+ error = 0;
+
+ git_reference_iterator_free(iter);
+ return error;
- return git_reference_foreach(repo, GIT_REF_LISTALL, &branch_foreach_cb, (void *)&filter);
}
int git_branch_move(
diff --git a/src/clone.c b/src/clone.c
index 38c0d409e..499195dcc 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -243,7 +243,6 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
/* Not master. Check all the other refs. */
if (git_reference_foreach(
repo,
- GIT_REF_LISTALL,
reference_matches_remote_head,
&head_info) < 0)
goto cleanup;
@@ -355,7 +354,7 @@ static int setup_remotes_and_fetch(
const git_clone_options *options)
{
int retcode = GIT_ERROR;
- git_remote *origin;
+ git_remote *origin = NULL;
/* Construct an origin remote */
if ((retcode = create_and_configure_origin(&origin, repo, url, options)) < 0)
diff --git a/src/commit.c b/src/commit.c
index 3dc647c9b..be6e32c76 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -292,7 +292,7 @@ int git_commit_nth_gen_ancestor(
const git_commit *commit,
unsigned int n)
{
- git_commit *current, *parent;
+ git_commit *current, *parent = NULL;
int error;
assert(ancestor && commit);
diff --git a/src/config_file.c b/src/config_file.c
index a9a40c366..e57cd1e53 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -296,7 +296,7 @@ cleanup:
static int config_set(git_config_backend *cfg, const char *name, const char *value)
{
- cvar_t *var = NULL, *old_var;
+ cvar_t *var = NULL, *old_var = NULL;
diskfile_backend *b = (diskfile_backend *)cfg;
char *key, *esc_value = NULL;
khiter_t pos;
diff --git a/src/diff.c b/src/diff.c
index e0dff9c95..f466546bb 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -535,7 +535,7 @@ cleanup:
static bool diff_time_eq(
const git_index_time *a, const git_index_time *b, bool use_nanos)
{
- return a->seconds == a->seconds &&
+ return a->seconds == b->seconds &&
(!use_nanos || a->nanoseconds == b->nanoseconds);
}
diff --git a/src/diff_output.c b/src/diff_output.c
index 2214ae1b5..07fcf47a7 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -593,6 +593,8 @@ static int diff_patch_load(
delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
break;
case GIT_DELTA_MODIFIED:
+ case GIT_DELTA_COPIED:
+ case GIT_DELTA_RENAMED:
break;
case GIT_DELTA_UNTRACKED:
delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
diff --git a/src/object.c b/src/object.c
index a6807f26b..9b8ccdd3e 100644
--- a/src/object.c
+++ b/src/object.c
@@ -117,7 +117,7 @@ int git_object_lookup_prefix(
{
git_object *object = NULL;
git_odb *odb = NULL;
- git_odb_object *odb_obj;
+ git_odb_object *odb_obj = NULL;
int error = 0;
assert(repo && object_out && id);
diff --git a/src/push.c b/src/push.c
index 0499d8648..452d71789 100644
--- a/src/push.c
+++ b/src/push.c
@@ -180,7 +180,7 @@ int git_push_update_tips(git_push *push)
git_buf remote_ref_name = GIT_BUF_INIT;
size_t i, j;
git_refspec *fetch_spec;
- push_spec *push_spec;
+ push_spec *push_spec = NULL;
git_reference *remote_ref;
push_status *status;
int error = 0;
diff --git a/src/refdb.c b/src/refdb.c
index 33a1934d1..9f9037ce7 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -124,60 +124,70 @@ int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name)
return error;
}
-int git_refdb_foreach(
- git_refdb *db,
- unsigned int list_flags,
- git_reference_foreach_cb callback,
- void *payload)
+int git_refdb_iterator(git_reference_iterator **out, git_refdb *db)
{
- assert(db && db->backend);
+ if (!db->backend || !db->backend->iterator) {
+ giterr_set(GITERR_REFERENCE, "This backend doesn't support iterators");
+ return -1;
+ }
- return db->backend->foreach(db->backend, list_flags, callback, payload);
-}
+ if (db->backend->iterator(out, db->backend) < 0)
+ return -1;
-struct glob_cb_data {
- const char *glob;
- git_reference_foreach_cb callback;
- void *payload;
-};
+ return 0;
+}
-static int fromglob_cb(const char *reference_name, void *payload)
+int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const char *glob)
{
- struct glob_cb_data *data = (struct glob_cb_data *)payload;
+ if (!db->backend) {
+ giterr_set(GITERR_REFERENCE, "There are no backends loaded");
+ return -1;
+ }
- if (!p_fnmatch(data->glob, reference_name, 0))
- return data->callback(reference_name, data->payload);
+ if (db->backend->iterator_glob)
+ return db->backend->iterator_glob(out, db->backend, glob);
+
+ /* If the backend doesn't support glob-filtering themselves, we have to do it */
+ if (db->backend->iterator(out, db->backend) < 0)
+ return -1;
+
+ (*out)->glob = git__strdup(glob);
+ if (!(*out)->glob) {
+ db->backend->iterator_free(*out);
+ return -1;
+ }
return 0;
}
-int git_refdb_foreach_glob(
- git_refdb *db,
- const char *glob,
- unsigned int list_flags,
- git_reference_foreach_cb callback,
- void *payload)
+int git_refdb_next(const char **out, git_reference_iterator *iter)
{
int error;
- struct glob_cb_data data;
- assert(db && db->backend && glob && callback);
+ if (!iter->glob)
+ return iter->backend->next(out, iter);
- if(db->backend->foreach_glob != NULL)
- error = db->backend->foreach_glob(db->backend,
- glob, list_flags, callback, payload);
- else {
- data.glob = glob;
- data.callback = callback;
- data.payload = payload;
-
- error = db->backend->foreach(db->backend,
- list_flags, fromglob_cb, &data);
+ /* If the iterator has a glob, we need to filter */
+ while ((error = iter->backend->next(out, iter)) == 0) {
+ if (!p_fnmatch(iter->glob, *out, 0))
+ break;
}
return error;
}
+void git_refdb_iterator_free(git_reference_iterator *iter)
+{
+ git__free(iter->glob);
+ iter->backend->iterator_free(iter);
+}
+
+struct glob_cb_data {
+ const char *glob;
+ git_reference_foreach_cb callback;
+ void *payload;
+};
+
int git_refdb_write(git_refdb *db, const git_reference *ref)
{
assert(db && db->backend);
diff --git a/src/refdb.h b/src/refdb.h
index 047113ac8..2edd05d18 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -26,18 +26,10 @@ int git_refdb_lookup(
git_refdb *refdb,
const char *ref_name);
-int git_refdb_foreach(
- git_refdb *refdb,
- unsigned int list_flags,
- git_reference_foreach_cb callback,
- void *payload);
-
-int git_refdb_foreach_glob(
- git_refdb *refdb,
- const char *glob,
- unsigned int list_flags,
- git_reference_foreach_cb callback,
- void *payload);
+int git_refdb_iterator(git_reference_iterator **out, git_refdb *db);
+int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const char *glob);
+int git_refdb_next(const char **out, git_reference_iterator *iter);
+void git_refdb_iterator_free(git_reference_iterator *iter);
int git_refdb_write(git_refdb *refdb, const git_reference *ref);
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index c0a32bae7..c462a4251 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -13,6 +13,7 @@
#include "reflog.h"
#include "refdb.h"
#include "refdb_fs.h"
+#include "iterator.h"
#include <git2/tag.h>
#include <git2/object.h>
@@ -106,7 +107,7 @@ static int packed_parse_oid(
refname_len = refname_end - refname_begin;
- ref = git__malloc(sizeof(struct packref) + refname_len + 1);
+ ref = git__calloc(1, sizeof(struct packref) + refname_len + 1);
GITERR_CHECK_ALLOC(ref);
memcpy(ref->name, refname_begin, refname_len);
@@ -180,6 +181,9 @@ static int packed_load(refdb_fs_backend *backend)
GITERR_CHECK_ALLOC(ref_cache->packfile);
}
+ if (backend->path == NULL)
+ return 0;
+
result = reference_read(&packfile, &ref_cache->packfile_time,
backend->path, GIT_PACKEDREFS_FILE, &updated);
@@ -316,7 +320,7 @@ static int loose_lookup_to_packfile(
git_buf_rtrim(&ref_file);
name_len = strlen(name);
- ref = git__malloc(sizeof(struct packref) + name_len + 1);
+ ref = git__calloc(1, sizeof(struct packref) + name_len + 1);
GITERR_CHECK_ALLOC(ref);
memcpy(ref->name, name, name_len);
@@ -558,98 +562,129 @@ struct dirent_list_data {
int callback_error;
};
-static git_ref_t loose_guess_rtype(const git_buf *full_path)
+typedef struct {
+ git_reference_iterator parent;
+ unsigned int loose;
+ /* packed */
+ git_strmap *h;
+ khiter_t k;
+ /* loose */
+ git_iterator *fsiter;
+ git_buf buf;
+} refdb_fs_iter;
+
+static int refdb_fs_backend__iterator(git_reference_iterator **out, git_refdb_backend *_backend)
{
- git_buf ref_file = GIT_BUF_INIT;
- git_ref_t type;
+ refdb_fs_iter *iter;
+ refdb_fs_backend *backend;
- type = GIT_REF_INVALID;
+ assert(_backend);
+ backend = (refdb_fs_backend *)_backend;
- if (git_futils_readbuffer(&ref_file, full_path->ptr) == 0) {
- if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0)
- type = GIT_REF_SYMBOLIC;
- else
- type = GIT_REF_OID;
- }
+ if (packed_load(backend) < 0)
+ return -1;
- git_buf_free(&ref_file);
- return type;
+ iter = git__calloc(1, sizeof(refdb_fs_iter));
+ GITERR_CHECK_ALLOC(iter);
+
+ iter->parent.backend = _backend;
+ iter->h = backend->refcache.packfile;
+ iter->k = kh_begin(backend->refcache.packfile);
+
+ *out = (git_reference_iterator *)iter;
+
+ return 0;
}
-static int _dirent_loose_listall(void *_data, git_buf *full_path)
+static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
{
- struct dirent_list_data *data = (struct dirent_list_data *)_data;
- const char *file_path = full_path->ptr + data->repo_path_len;
-
- if (git_path_isdir(full_path->ptr) == true)
- return git_path_direach(full_path, _dirent_loose_listall, _data);
+ refdb_fs_iter *iter = (refdb_fs_iter *) _iter;
- /* do not add twice a reference that exists already in the packfile */
- if (git_strmap_exists(data->backend->refcache.packfile, file_path))
- return 0;
+ git_buf_free(&iter->buf);
+ git_iterator_free(iter->fsiter);
+ git__free(iter);
+}
- if (data->list_type != GIT_REF_LISTALL) {
- if ((data->list_type & loose_guess_rtype(full_path)) == 0)
- return 0; /* we are filtering out this reference */
+static int iter_packed(const char **out, refdb_fs_iter *iter)
+{
+ /* Move forward to the next entry */
+ while (!kh_exist(iter->h, iter->k)) {
+ iter->k++;
+ if (iter->k == kh_end(iter->h))
+ return GIT_ITEROVER;
}
- /* Locked references aren't returned */
- if (!git__suffixcmp(file_path, GIT_FILELOCK_EXTENSION))
- return 0;
+ *out = kh_key(iter->h, iter->k);
+ iter->k++;
- if (data->callback(file_path, data->callback_payload))
- data->callback_error = GIT_EUSER;
-
- return data->callback_error;
+ return 0;
}
-static int refdb_fs_backend__foreach(
- git_refdb_backend *_backend,
- unsigned int list_type,
- git_reference_foreach_cb callback,
- void *payload)
+static int iter_loose(const char **out, refdb_fs_iter *iter)
{
- refdb_fs_backend *backend;
- int result;
- struct dirent_list_data data;
- git_buf refs_path = GIT_BUF_INIT;
- const char *ref_name;
- void *ref = NULL;
+ const git_index_entry *entry;
+ int retry;
+ git_strmap *packfile_refs;
+ refdb_fs_backend *backend = (refdb_fs_backend *) iter->parent.backend;
- GIT_UNUSED(ref);
+ packfile_refs = backend->refcache.packfile;
- assert(_backend);
- backend = (refdb_fs_backend *)_backend;
+ do {
+ khiter_t pos;
+ if (git_iterator_current(&entry, iter->fsiter) < 0)
+ return -1;
- if (packed_load(backend) < 0)
- return -1;
+ git_buf_clear(&iter->buf);
+ if (!entry)
+ return GIT_ITEROVER;
- /* list all the packed references first */
- if (list_type & GIT_REF_OID) {
- git_strmap_foreach(backend->refcache.packfile, ref_name, ref, {
- if (callback(ref_name, payload))
- return GIT_EUSER;
- });
- }
+ if (git_buf_printf(&iter->buf, "refs/%s", entry->path) < 0)
+ return -1;
- /* now list the loose references, trying not to
- * duplicate the ref names already in the packed-refs file */
+ git_iterator_advance(NULL, iter->fsiter);
- data.repo_path_len = strlen(backend->path);
- data.list_type = list_type;
- data.backend = backend;
- data.callback = callback;
- data.callback_payload = payload;
- data.callback_error = 0;
+ /* Skip this one if we already listed it in packed */
+ pos = git_strmap_lookup_index(packfile_refs, git_buf_cstr(&iter->buf));
+ retry = 0;
+ if (git_strmap_valid_index(packfile_refs, pos) ||
+ !git_reference_is_valid_name(git_buf_cstr(&iter->buf)))
+ retry = 1;
- if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0)
+ *out = git_buf_cstr(&iter->buf);
+ } while (retry);
+
+ return 0;
+}
+
+static int iter_loose_setup(refdb_fs_iter *iter)
+{
+ refdb_fs_backend *backend = (refdb_fs_backend *) iter->parent.backend;
+
+ git_buf_clear(&iter->buf);
+ if (git_buf_printf(&iter->buf, "%s/refs", backend->path) < 0)
return -1;
- result = git_path_direach(&refs_path, _dirent_loose_listall, &data);
+ return git_iterator_for_filesystem(&iter->fsiter, git_buf_cstr(&iter->buf), 0, NULL, NULL);
+}
- git_buf_free(&refs_path);
+static int refdb_fs_backend__next(const char **out, git_reference_iterator *_iter)
+{
+ refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
+
+ if (iter->loose)
+ return iter_loose(out, iter);
- return data.callback_error ? GIT_EUSER : result;
+ if (iter->k != kh_end(iter->h)) {
+ int error = iter_packed(out, iter);
+ if (error != GIT_ITEROVER)
+ return error;
+ }
+
+ if (iter_loose_setup(iter) < 0)
+ return -1;
+ iter->loose = 1;
+
+ return iter_loose(out, iter);
}
static int loose_write(refdb_fs_backend *backend, const git_reference *ref)
@@ -1027,6 +1062,10 @@ static int setup_namespace(git_buf *path, git_repository *repo)
{
char *parts, *start, *end;
+ /* Not all repositories have a path */
+ if (repo->path_repository == NULL)
+ return 0;
+
/* Load the path to the repo first */
git_buf_puts(path, repo->path_repository);
@@ -1081,7 +1120,9 @@ int git_refdb_backend_fs(
backend->parent.exists = &refdb_fs_backend__exists;
backend->parent.lookup = &refdb_fs_backend__lookup;
- backend->parent.foreach = &refdb_fs_backend__foreach;
+ backend->parent.iterator = &refdb_fs_backend__iterator;
+ backend->parent.next = &refdb_fs_backend__next;
+ backend->parent.iterator_free = &refdb_fs_backend__iterator_free;
backend->parent.write = &refdb_fs_backend__write;
backend->parent.delete = &refdb_fs_backend__delete;
backend->parent.compress = &refdb_fs_backend__compress;
diff --git a/src/refs.c b/src/refs.c
index 8bba3941e..9c6c5c623 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -139,7 +139,7 @@ static int reference_path_available(
data.available = 1;
error = git_reference_foreach(
- repo, GIT_REF_LISTALL, _reference_available_cb, (void *)&data);
+ repo, _reference_available_cb, (void *)&data);
if (error < 0)
return error;
@@ -290,6 +290,67 @@ int git_reference_lookup_resolved(
return 0;
}
+int git_reference_dwim(git_reference **out, git_repository *repo, const char *refname)
+{
+ int error = 0, i;
+ bool fallbackmode = true, foundvalid = false;
+ git_reference *ref;
+ git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT;
+
+ static const char* formatters[] = {
+ "%s",
+ GIT_REFS_DIR "%s",
+ GIT_REFS_TAGS_DIR "%s",
+ GIT_REFS_HEADS_DIR "%s",
+ GIT_REFS_REMOTES_DIR "%s",
+ GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE,
+ NULL
+ };
+
+ if (*refname)
+ git_buf_puts(&name, refname);
+ else {
+ git_buf_puts(&name, GIT_HEAD_FILE);
+ fallbackmode = false;
+ }
+
+ for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) {
+
+ git_buf_clear(&refnamebuf);
+
+ if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0)
+ goto cleanup;
+
+ if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) {
+ error = GIT_EINVALIDSPEC;
+ continue;
+ }
+ foundvalid = true;
+
+ error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1);
+
+ if (!error) {
+ *out = ref;
+ error = 0;
+ goto cleanup;
+ }
+
+ if (error != GIT_ENOTFOUND)
+ goto cleanup;
+ }
+
+cleanup:
+ if (error && !foundvalid) {
+ /* never found a valid reference name */
+ giterr_set(GITERR_REFERENCE,
+ "Could not use '%s' as valid reference name", git_buf_cstr(&name));
+ }
+
+ git_buf_free(&name);
+ git_buf_free(&refnamebuf);
+ return error;
+}
+
/**
* Getters
*/
@@ -558,14 +619,59 @@ int git_reference_resolve(git_reference **ref_out, const git_reference *ref)
int git_reference_foreach(
git_repository *repo,
- unsigned int list_flags,
git_reference_foreach_cb callback,
void *payload)
{
+ git_reference_iterator *iter;
+ const char *name;
+ int error;
+
+ if (git_reference_iterator_new(&iter, repo) < 0)
+ return -1;
+
+ while ((error = git_reference_next(&name, iter)) == 0) {
+ if (callback(name, payload)) {
+ error = GIT_EUSER;
+ goto out;
+ }
+ }
+
+ if (error == GIT_ITEROVER)
+ error = 0;
+
+out:
+ git_reference_iterator_free(iter);
+ return error;
+}
+
+int git_reference_iterator_new(git_reference_iterator **out, git_repository *repo)
+{
+ git_refdb *refdb;
+
+ if (git_repository_refdb__weakptr(&refdb, repo) < 0)
+ return -1;
+
+ return git_refdb_iterator(out, refdb);
+}
+
+int git_reference_iterator_glob_new(git_reference_iterator **out, git_repository *repo, const char *glob)
+{
git_refdb *refdb;
- git_repository_refdb__weakptr(&refdb, repo);
- return git_refdb_foreach(refdb, list_flags, callback, payload);
+ if (git_repository_refdb__weakptr(&refdb, repo) < 0)
+ return -1;
+
+ return git_refdb_iterator_glob(out, refdb, glob);
+}
+
+int git_reference_next(const char **out, git_reference_iterator *iter)
+{
+ return git_refdb_next(out, iter);
+}
+
+void git_reference_iterator_free(git_reference_iterator *iter)
+{
+ git_refdb_iterator_free(iter);
}
static int cb__reflist_add(const char *ref, void *data)
@@ -575,8 +681,7 @@ static int cb__reflist_add(const char *ref, void *data)
int git_reference_list(
git_strarray *array,
- git_repository *repo,
- unsigned int list_flags)
+ git_repository *repo)
{
git_vector ref_list;
@@ -589,7 +694,7 @@ int git_reference_list(
return -1;
if (git_reference_foreach(
- repo, list_flags, &cb__reflist_add, (void *)&ref_list) < 0) {
+ repo, &cb__reflist_add, (void *)&ref_list) < 0) {
git_vector_free(&ref_list);
return -1;
}
@@ -889,19 +994,29 @@ int git_reference__update_terminal(
int git_reference_foreach_glob(
git_repository *repo,
const char *glob,
- unsigned int list_flags,
- int (*callback)(
- const char *reference_name,
- void *payload),
+ git_reference_foreach_cb callback,
void *payload)
{
- git_refdb *refdb;
+ git_reference_iterator *iter;
+ const char *name;
+ int error;
- assert(repo && glob && callback);
+ if (git_reference_iterator_glob_new(&iter, repo, glob) < 0)
+ return -1;
- git_repository_refdb__weakptr(&refdb, repo);
+ while ((error = git_reference_next(&name, iter)) == 0) {
+ if (callback(name, payload)) {
+ error = GIT_EUSER;
+ goto out;
+ }
+ }
+
+ if (error == GIT_ITEROVER)
+ error = 0;
- return git_refdb_foreach_glob(refdb, glob, list_flags, callback, payload);
+out:
+ git_reference_iterator_free(iter);
+ return error;
}
int git_reference_has_log(
diff --git a/src/remote.c b/src/remote.c
index 692537636..e5a7df75e 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -292,7 +292,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
git_buf_clear(&buf);
git_buf_printf(&buf, "remote.%s.pushurl", name);
- if ((error = get_optional_config(config, &buf, NULL, &val)) < 0)
+ if ((error = get_optional_config(config, &buf, NULL, (void *)&val)) < 0)
goto cleanup;
if (val) {
@@ -1251,14 +1251,6 @@ static int update_branch_remote_config_entry(
update_config_entries_cb, &data);
}
-static int rename_cb(const char *ref, void *data)
-{
- if (git__prefixcmp(ref, GIT_REFS_REMOTES_DIR))
- return 0;
-
- return git_vector_insert((git_vector *)data, git__strdup(ref));
-}
-
static int rename_one_remote_reference(
git_repository *repo,
const char *reference_name,
@@ -1298,16 +1290,29 @@ static int rename_remote_references(
int error = -1;
unsigned int i;
char *name;
+ const char *refname;
+ git_reference_iterator *iter;
if (git_vector_init(&refnames, 8, NULL) < 0)
+ return -1;
+
+ if (git_reference_iterator_new(&iter, repo) < 0)
goto cleanup;
- if (git_reference_foreach(
- repo,
- GIT_REF_LISTALL,
- rename_cb,
- &refnames) < 0)
- goto cleanup;
+ while ((error = git_reference_next(&refname, iter)) == 0) {
+ if (git__prefixcmp(refname, GIT_REFS_REMOTES_DIR))
+ continue;
+
+ if ((error = git_vector_insert(&refnames, git__strdup(refname))) < 0)
+ break;
+
+ }
+
+ git_reference_iterator_free(iter);
+ if (error == GIT_ITEROVER)
+ error = 0;
+ else
+ goto cleanup;
git_vector_foreach(&refnames, i, name) {
if ((error = rename_one_remote_reference(repo, name, old_name, new_name)) < 0)
diff --git a/src/repository.c b/src/repository.c
index e2cedc0f7..4ab3921bb 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1473,7 +1473,7 @@ static int at_least_one_cb(const char *refname, void *payload)
static int repo_contains_no_reference(git_repository *repo)
{
- int error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL);
+ int error = git_reference_foreach(repo, at_least_one_cb, NULL);
if (error == GIT_EUSER)
return 0;
@@ -1596,6 +1596,9 @@ int git_repository_message(char *buffer, size_t len, git_repository *repo)
struct stat st;
int error;
+ if (buffer != NULL)
+ *buffer = '\0';
+
if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0)
return -1;
diff --git a/src/revparse.c b/src/revparse.c
index e8cc32aff..05231e3fc 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -14,67 +14,6 @@
#include "git2.h"
-static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname)
-{
- int error = 0, i;
- bool fallbackmode = true, foundvalid = false;
- git_reference *ref;
- git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT;
-
- static const char* formatters[] = {
- "%s",
- GIT_REFS_DIR "%s",
- GIT_REFS_TAGS_DIR "%s",
- GIT_REFS_HEADS_DIR "%s",
- GIT_REFS_REMOTES_DIR "%s",
- GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE,
- NULL
- };
-
- if (*refname)
- git_buf_puts(&name, refname);
- else {
- git_buf_puts(&name, GIT_HEAD_FILE);
- fallbackmode = false;
- }
-
- for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) {
-
- git_buf_clear(&refnamebuf);
-
- if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0)
- goto cleanup;
-
- if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) {
- error = GIT_EINVALIDSPEC;
- continue;
- }
- foundvalid = true;
-
- error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1);
-
- if (!error) {
- *out = ref;
- error = 0;
- goto cleanup;
- }
-
- if (error != GIT_ENOTFOUND)
- goto cleanup;
- }
-
-cleanup:
- if (error && !foundvalid) {
- /* never found a valid reference name */
- giterr_set(GITERR_REFERENCE,
- "Could not use '%s' as valid reference name", git_buf_cstr(&name));
- }
-
- git_buf_free(&name);
- git_buf_free(&refnamebuf);
- return error;
-}
-
static int maybe_sha_or_abbrev(git_object** out, git_repository *repo, const char *spec, size_t speclen)
{
git_oid oid;
@@ -157,7 +96,7 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const
if (error < 0 && error != GIT_ENOTFOUND)
return error;
- error = disambiguate_refname(&ref, repo, spec);
+ error = git_reference_dwim(&ref, repo, spec);
if (!error) {
error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY);
git_reference_free(ref);
@@ -242,7 +181,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out,
git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so);
- if ((error = disambiguate_refname(base_ref, repo, git_buf_cstr(&buf))) == 0)
+ if ((error = git_reference_dwim(base_ref, repo, git_buf_cstr(&buf))) == 0)
goto cleanup;
if (error < 0 && error != GIT_ENOTFOUND)
@@ -323,7 +262,7 @@ static int retrieve_revobject_from_reflog(git_object **out, git_reference **base
int error = -1;
if (*base_ref == NULL) {
- if ((error = disambiguate_refname(&ref, repo, identifier)) < 0)
+ if ((error = git_reference_dwim(&ref, repo, identifier)) < 0)
return error;
} else {
ref = *base_ref;
@@ -351,7 +290,7 @@ static int retrieve_remote_tracking_reference(git_reference **base_ref, const ch
int error = -1;
if (*base_ref == NULL) {
- if ((error = disambiguate_refname(&ref, repo, identifier)) < 0)
+ if ((error = git_reference_dwim(&ref, repo, identifier)) < 0)
return error;
} else {
ref = *base_ref;
diff --git a/src/revwalk.c b/src/revwalk.c
index 16f06624d..528d02b20 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -186,7 +186,7 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide)
data.hide = hide;
if (git_reference_foreach_glob(
- walk->repo, git_buf_cstr(&buf), GIT_REF_LISTALL, push_glob_cb, &data) < 0)
+ walk->repo, git_buf_cstr(&buf), push_glob_cb, &data) < 0)
goto on_error;
regfree(&preg);
diff --git a/src/status.c b/src/status.c
index 73472ab14..89f3eedb5 100644
--- a/src/status.c
+++ b/src/status.c
@@ -266,6 +266,7 @@ int git_status_file(
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
+ GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
GIT_STATUS_OPT_INCLUDE_UNMODIFIED;
@@ -281,22 +282,9 @@ int git_status_file(
}
if (!error && !sfi.count) {
- git_buf full = GIT_BUF_INIT;
-
- /* if the file actually exists and we still did not get a callback
- * for it, then it must be contained inside an ignored directory, so
- * mark it as such instead of generating an error.
- */
- if (!git_buf_joinpath(&full, git_repository_workdir(repo), path) &&
- git_path_exists(full.ptr))
- sfi.status = GIT_STATUS_IGNORED;
- else {
- giterr_set(GITERR_INVALID,
- "Attempt to get status of nonexistent file '%s'", path);
- error = GIT_ENOTFOUND;
- }
-
- git_buf_free(&full);
+ giterr_set(GITERR_INVALID,
+ "Attempt to get status of nonexistent file '%s'", path);
+ error = GIT_ENOTFOUND;
}
*status_flags = sfi.status;
diff --git a/src/tag.c b/src/tag.c
index a4f2e2581..f81956de7 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -427,7 +427,7 @@ int git_tag_foreach(git_repository *repo, git_tag_foreach_cb cb, void *cb_data)
data.cb_data = cb_data;
data.repo = repo;
- return git_reference_foreach(repo, GIT_REF_OID, &tags_cb, &data);
+ return git_reference_foreach(repo, &tags_cb, &data);
}
typedef struct {
diff --git a/src/transports/local.c b/src/transports/local.c
index 8b4d50c14..bd3bf93bf 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -124,7 +124,7 @@ static int store_refs(transport_local *t)
assert(t);
- if (git_reference_list(&ref_names, t->repo, GIT_REF_LISTALL) < 0 ||
+ if (git_reference_list(&ref_names, t->repo) < 0 ||
git_vector_init(&t->refs, ref_names.count, NULL) < 0)
goto on_error;
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index 765b914b7..67d309523 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -21,8 +21,8 @@ int git_smart__store_refs(transport_smart *t, int flushes)
gitno_buffer *buf = &t->buffer;
git_vector *refs = &t->refs;
int error, flush = 0, recvd;
- const char *line_end;
- git_pkt *pkt;
+ const char *line_end = NULL;
+ git_pkt *pkt = NULL;
git_pkt_ref *ref;
size_t i;
@@ -135,7 +135,7 @@ int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps)
static int recv_pkt(git_pkt **out, gitno_buffer *buf)
{
const char *ptr = buf->data, *line_end = ptr;
- git_pkt *pkt;
+ git_pkt *pkt = NULL;
int pkt_type, error = 0, ret;
do {
@@ -193,7 +193,7 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
unsigned int i;
git_reference *ref;
- if (git_reference_list(&refs, repo, GIT_REF_LISTALL) < 0)
+ if (git_reference_list(&refs, repo) < 0)
return -1;
if (git_revwalk_new(&walk, repo) < 0)
@@ -640,8 +640,8 @@ static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt)
static int parse_report(gitno_buffer *buf, git_push *push)
{
- git_pkt *pkt;
- const char *line_end;
+ git_pkt *pkt = NULL;
+ const char *line_end = NULL;
int error, recvd;
for (;;) {
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index e502001cb..d803c812c 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -893,7 +893,7 @@ static int winhttp_connect(
wchar_t *ua = L"git/1.0 (libgit2 " WIDEN(LIBGIT2_VERSION) L")";
wchar_t host[GIT_WIN_PATH];
int32_t port;
- const char *default_port;
+ const char *default_port = "80";
int ret;
if (!git__prefixcmp(url, prefix_http)) {