Merge branch 'branch-delete-ref' into development Conflicts: include/git2/refs.h
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
diff --git a/include/git2/branch.h b/include/git2/branch.h
index 8bf7eb9..bbbdf1c 100644
--- a/include/git2/branch.h
+++ b/include/git2/branch.h
@@ -55,21 +55,13 @@ GIT_EXTERN(int) git_branch_create(
/**
* Delete an existing branch reference.
*
- * @param repo Repository where lives the branch.
+ * If the branch is successfully deleted, the passed reference
+ * object will be freed and invalidated.
*
- * @param branch_name Name of the branch to be deleted;
- * this name is validated for consistency.
- *
- * @param branch_type Type of the considered branch. This should
- * be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE.
- *
- * @return 0 on success, GIT_ENOTFOUND if the branch
- * doesn't exist or an error code.
+ * @param branch A valid reference representing a branch
+ * @return 0 on success, or an error code.
*/
-GIT_EXTERN(int) git_branch_delete(
- git_repository *repo,
- const char *branch_name,
- git_branch_t branch_type);
+GIT_EXTERN(int) git_branch_delete(git_reference *branch);
/**
* Loop over all the branches and issue a callback for each one.
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 6a8513b..660b48b 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -376,6 +376,16 @@ GIT_EXTERN(int) git_reference_has_log(git_reference *ref);
*/
GIT_EXTERN(int) git_reference_is_branch(git_reference *ref);
+/**
+ * Check if a reference is a remote tracking branch
+ *
+ * @param ref A git reference
+ *
+ * @return 1 when the reference lives in the refs/remotes
+ * namespace; 0 otherwise.
+ */
+GIT_EXTERN(int) git_reference_is_remote(git_reference *ref);
+
enum {
GIT_REF_FORMAT_NORMAL = 0,
diff --git a/src/branch.c b/src/branch.c
index f6f3140..cd5c10e 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -50,6 +50,12 @@ static int create_error_invalid(const char *msg)
return -1;
}
+static int not_a_local_branch(git_reference *ref)
+{
+ giterr_set(GITERR_INVALID, "Reference '%s' is not a local branch.", git_reference_name(ref));
+ return -1;
+}
+
int git_branch_create(
git_reference **ref_out,
git_repository *repository,
@@ -84,19 +90,19 @@ cleanup:
return error;
}
-int git_branch_delete(git_repository *repo, const char *branch_name, git_branch_t branch_type)
+int git_branch_delete(git_reference *branch)
{
- git_reference *branch = NULL;
git_reference *head = NULL;
- int error;
- assert(repo && branch_name);
- assert((branch_type == GIT_BRANCH_LOCAL) || (branch_type == GIT_BRANCH_REMOTE));
+ assert(branch);
- if ((error = retrieve_branch_reference(&branch, repo, branch_name, branch_type == GIT_BRANCH_REMOTE)) < 0)
- return error;
+ if (!git_reference_is_branch(branch) &&
+ !git_reference_is_remote(branch)) {
+ giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.", git_reference_name(branch));
+ return -1;
+ }
- if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) {
+ if (git_reference_lookup(&head, git_reference_owner(branch), GIT_HEAD_FILE) < 0) {
giterr_set(GITERR_REFERENCE, "Cannot locate HEAD.");
goto on_error;
}
@@ -104,7 +110,7 @@ int git_branch_delete(git_repository *repo, const char *branch_name, git_branch_
if ((git_reference_type(head) == GIT_REF_SYMBOLIC)
&& (strcmp(git_reference_target(head), git_reference_name(branch)) == 0)) {
giterr_set(GITERR_REFERENCE,
- "Cannot delete branch '%s' as it is the current HEAD of the repository.", branch_name);
+ "Cannot delete branch '%s' as it is the current HEAD of the repository.", git_reference_name(branch));
goto on_error;
}
@@ -116,7 +122,6 @@ int git_branch_delete(git_repository *repo, const char *branch_name, git_branch_
on_error:
git_reference_free(head);
- git_reference_free(branch);
return -1;
}
@@ -163,12 +168,6 @@ int git_branch_foreach(
return git_reference_foreach(repo, GIT_REF_LISTALL, &branch_foreach_cb, (void *)&filter);
}
-static int not_a_local_branch(git_reference *ref)
-{
- giterr_set(GITERR_INVALID, "Reference '%s' is not a local branch.", git_reference_name(ref));
- return -1;
-}
-
int git_branch_move(
git_reference *branch,
const char *new_branch_name,
diff --git a/src/refs.c b/src/refs.c
index 9fc194c..eb8af58 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1836,6 +1836,11 @@ int git_reference_has_log(
int git_reference_is_branch(git_reference *ref)
{
assert(ref);
-
return git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR) == 0;
}
+
+int git_reference_is_remote(git_reference *ref)
+{
+ assert(ref);
+ return git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR) == 0;
+}
diff --git a/src/unix/map.c b/src/unix/map.c
index 9dcae58..ee7888c 100644
--- a/src/unix/map.c
+++ b/src/unix/map.c
@@ -31,6 +31,8 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs
mflag = MAP_SHARED;
else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
mflag = MAP_PRIVATE;
+ else
+ mflag = MAP_SHARED;
out->data = mmap(NULL, len, mprot, mflag, fd, offset);
diff --git a/tests-clar/refs/branches/delete.c b/tests-clar/refs/branches/delete.c
index 699655f..b261240 100644
--- a/tests-clar/refs/branches/delete.c
+++ b/tests-clar/refs/branches/delete.c
@@ -23,37 +23,37 @@ void test_refs_branches_delete__cleanup(void)
cl_fixture_cleanup("testrepo.git");
}
-void test_refs_branches_delete__can_not_delete_a_non_existing_branch(void)
-{
- cl_git_fail(git_branch_delete(repo, "i-am-not-a-local-branch", GIT_BRANCH_LOCAL));
- cl_git_fail(git_branch_delete(repo, "neither/a-remote-one", GIT_BRANCH_REMOTE));
-}
-
void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
{
git_reference *head;
+ git_reference *branch;
/* Ensure HEAD targets the local master branch */
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
cl_assert(strcmp("refs/heads/master", git_reference_target(head)) == 0);
git_reference_free(head);
- cl_git_fail(git_branch_delete(repo, "master", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
+ cl_git_fail(git_branch_delete(branch));
+ git_reference_free(branch);
}
void test_refs_branches_delete__can_not_delete_a_branch_if_HEAD_is_missing(void)
{
git_reference *head;
+ git_reference *branch = NULL;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
git_reference_delete(head);
- cl_git_fail(git_branch_delete(repo, "br2", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
+ cl_git_fail(git_branch_delete(branch));
+ git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
- git_reference *master, *head;
+ git_reference *master, *head, *branch;
/* Detach HEAD and make it target the commit that "master" points to */
cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
@@ -61,30 +61,21 @@ void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(
git_reference_free(head);
git_reference_free(master);
- cl_git_pass(git_branch_delete(repo, "master", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_delete(branch));
}
void test_refs_branches_delete__can_delete_a_local_branch(void)
{
- cl_git_pass(git_branch_delete(repo, "br2", GIT_BRANCH_LOCAL));
+ git_reference *branch;
+ cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_delete(branch));
}
void test_refs_branches_delete__can_delete_a_remote_branch(void)
{
- cl_git_pass(git_branch_delete(repo, "nulltoken/master", GIT_BRANCH_REMOTE));
+ git_reference *branch;
+ cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
+ cl_git_pass(git_branch_delete(branch));
}
-static void assert_non_exisitng_branch_removal(const char *branch_name, git_branch_t branch_type)
-{
- int error;
- error = git_branch_delete(repo, branch_name, branch_type);
-
- cl_git_fail(error);
- cl_assert_equal_i(GIT_ENOTFOUND, error);
-}
-
-void test_refs_branches_delete__deleting_a_non_existing_branch_returns_ENOTFOUND(void)
-{
- assert_non_exisitng_branch_removal("i-do-not-locally-exist", GIT_BRANCH_LOCAL);
- assert_non_exisitng_branch_removal("neither/remotely", GIT_BRANCH_REMOTE);
-}