index: rework index entry initialization routine index_init_entry() renamed to index_entry_init(). Now it allocates entry on its own. git_index_add() and git_index_append() reworked accordingly. This commit fixes warning: /home/kas/git/public/libgit2/src/index.c: In function ‘index_init_entry’: /home/kas/git/public/libgit2/src/index.c:452:14: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default] Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
diff --git a/src/index.c b/src/index.c
index f609cda..af71ebd 100644
--- a/src/index.c
+++ b/src/index.c
@@ -331,6 +331,57 @@ git_index_entry *git_index_get(git_index *index, unsigned int n)
return git_vector_get(&index->entries, n);
}
+static int index_entry_init(git_index_entry **entry_out, git_index *index, const char *rel_path, int stage)
+{
+ git_index_entry *entry;
+ char full_path[GIT_PATH_MAX];
+ struct stat st;
+ git_oid oid;
+ int error;
+
+ if (index->repository == NULL)
+ return git__throw(GIT_EBAREINDEX, "Failed to initialize entry. Repository is bare");
+
+ git_path_join(full_path, index->repository->path_workdir, rel_path);
+
+ if (p_lstat(full_path, &st) < 0)
+ return git__throw(GIT_ENOTFOUND, "Failed to initialize entry. '%s' cannot be opened", full_path);
+
+ if (stage < 0 || stage > 3)
+ return git__throw(GIT_ERROR, "Failed to initialize entry. Invalid stage %i", stage);
+
+ /* write the blob to disk and get the oid */
+ if ((error = git_blob_create_fromfile(&oid, index->repository, rel_path)) < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to initialize index entry");
+
+ entry = git__malloc(sizeof(git_index_entry));
+ if (!entry)
+ return GIT_ENOMEM;
+ memset(entry, 0x0, sizeof(git_index_entry));
+
+ entry->ctime.seconds = (git_time_t)st.st_ctime;
+ entry->mtime.seconds = (git_time_t)st.st_mtime;
+ /* entry.mtime.nanoseconds = st.st_mtimensec; */
+ /* entry.ctime.nanoseconds = st.st_ctimensec; */
+ entry->dev= st.st_rdev;
+ entry->ino = st.st_ino;
+ entry->mode = index_create_mode(st.st_mode);
+ entry->uid = st.st_uid;
+ entry->gid = st.st_gid;
+ entry->file_size = st.st_size;
+ entry->oid = oid;
+
+ entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
+ entry->path = git__strdup(rel_path);
+ if (entry->path == NULL) {
+ free(entry);
+ return GIT_ENOMEM;
+ }
+
+ *entry_out = entry;
+ return GIT_SUCCESS;
+}
+
static git_index_entry *index_entry_dup(const git_index_entry *source_entry)
{
git_index_entry *entry;
@@ -405,79 +456,40 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
/* exists, replace it */
entry_array = (git_index_entry **) index->entries.contents;
- index_entry_free(entry_array[position]);
+ free(entry_array[position]->path);
+ free(entry_array[position]);
entry_array[position] = entry;
return GIT_SUCCESS;
}
-static int index_init_entry(git_index_entry *entry, git_index *index, const char *rel_path, int stage)
+static int index_add(git_index *index, const char *path, int stage, int replace)
{
- char full_path[GIT_PATH_MAX];
- struct stat st;
- int error;
-
- if (index->repository == NULL)
- return git__throw(GIT_EBAREINDEX, "Failed to initialize entry. Repository is bare");
-
- git_path_join(full_path, index->repository->path_workdir, rel_path);
-
- if (p_lstat(full_path, &st) < 0)
- return git__throw(GIT_ENOTFOUND, "Failed to initialize entry. '%s' cannot be opened", full_path);
-
- if (stage < 0 || stage > 3)
- return git__throw(GIT_ERROR, "Failed to initialize entry. Invalid stage %i", stage);
-
- memset(entry, 0x0, sizeof(git_index_entry));
+ git_index_entry *entry = NULL;
+ int ret;
- entry->ctime.seconds = (git_time_t)st.st_ctime;
- entry->mtime.seconds = (git_time_t)st.st_mtime;
- /* entry.mtime.nanoseconds = st.st_mtimensec; */
- /* entry.ctime.nanoseconds = st.st_ctimensec; */
- entry->dev= st.st_rdev;
- entry->ino = st.st_ino;
- entry->mode = index_create_mode(st.st_mode);
- entry->uid = st.st_uid;
- entry->gid = st.st_gid;
- entry->file_size = st.st_size;
+ ret = index_entry_init(&entry, index, path, stage);
+ if (ret)
+ goto err;
- /* write the blob to disk and get the oid */
- if ((error = git_blob_create_fromfile(&entry->oid, index->repository, rel_path)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to initialize index entry");
+ ret = index_insert(index, entry, replace);
+ if (ret)
+ goto err;
- entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
- entry->path = rel_path; /* do not duplicate; index_insert already does this */
- return GIT_SUCCESS;
+ return ret;
+err:
+ index_entry_free(entry);
+ return git__rethrow(ret, "Failed to append to index");
}
int git_index_add(git_index *index, const char *path, int stage)
{
- int error;
- git_index_entry entry, *dup_entry;
-
- if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to add to index");
-
- dup_entry = index_entry_dup(&entry);
- if (dup_entry == NULL)
- return GIT_ENOMEM;
-
- return index_insert(index, dup_entry, 1);
+ return index_add(index, path, stage, 1);
}
int git_index_append(git_index *index, const char *path, int stage)
{
- int error;
- git_index_entry entry, *dup_entry;
-
- if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to append to index");
-
- dup_entry = index_entry_dup(&entry);
- if (dup_entry == NULL)
- return GIT_ENOMEM;
-
- return index_insert(index, dup_entry, 0);
+ return index_add(index, path, stage, 0);
}
static int index_add2(git_index *index, const git_index_entry *source_entry,