Merge pull request #3758 from libgit2/ethomson/annotated_commit_refs Annotated commits: differentiate between the ref names and the description
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
diff --git a/src/annotated_commit.c b/src/annotated_commit.c
index e53b95d..c2c770c 100644
--- a/src/annotated_commit.c
+++ b/src/annotated_commit.c
@@ -20,38 +20,101 @@
static int annotated_commit_init(
git_annotated_commit **out,
+ git_commit *commit,
+ const char *description)
+{
+ git_annotated_commit *annotated_commit;
+ int error = 0;
+
+ assert(out && commit);
+
+ *out = NULL;
+
+ annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
+ GITERR_CHECK_ALLOC(annotated_commit);
+
+ annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL;
+
+ if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0)
+ goto done;
+
+ git_oid_fmt(annotated_commit->id_str, git_commit_id(commit));
+ annotated_commit->id_str[GIT_OID_HEXSZ] = '\0';
+
+ if (!description)
+ description = annotated_commit->id_str;
+
+ annotated_commit->description = git__strdup(description);
+ GITERR_CHECK_ALLOC(annotated_commit->description);
+
+done:
+ if (!error)
+ *out = annotated_commit;
+
+ return error;
+}
+
+static int annotated_commit_init_from_id(
+ git_annotated_commit **out,
git_repository *repo,
const git_oid *id,
- const char *ref_name,
- const char *remote_url)
+ const char *description)
{
- git_annotated_commit *annotated_commit;
git_commit *commit = NULL;
int error = 0;
- assert(out && id);
+ assert(out && repo && id);
*out = NULL;
- if ((error = git_commit_lookup(&commit, repo, id)) < 0 ||
- (error = git_annotated_commit_from_commit(&annotated_commit,
- commit)) < 0)
+ if ((error = git_commit_lookup(&commit, repo, id)) < 0)
goto done;
- if (ref_name) {
- annotated_commit->ref_name = git__strdup(ref_name);
- GITERR_CHECK_ALLOC(annotated_commit->ref_name);
- }
+ error = annotated_commit_init(out, commit, description);
+
+done:
+ git_commit_free(commit);
+ return error;
+}
+
+int git_annotated_commit_lookup(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const git_oid *id)
+{
+ return annotated_commit_init_from_id(out, repo, id, NULL);
+}
+
+int git_annotated_commit_from_commit(
+ git_annotated_commit **out,
+ git_commit *commit)
+{
+ return annotated_commit_init(out, commit, NULL);
+}
- if (remote_url) {
- annotated_commit->remote_url = git__strdup(remote_url);
- GITERR_CHECK_ALLOC(annotated_commit->remote_url);
+int git_annotated_commit_from_revspec(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const char *revspec)
+{
+ git_object *obj, *commit;
+ int error;
+
+ assert(out && repo && revspec);
+
+ if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
+ return error;
+
+ if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) {
+ git_object_free(obj);
+ return error;
}
- *out = annotated_commit;
+ error = annotated_commit_init(out, (git_commit *)commit, revspec);
+
+ git_object_free(obj);
+ git_object_free(commit);
-done:
- git_commit_free(commit);
return error;
}
@@ -70,8 +133,15 @@ int git_annotated_commit_from_ref(
if ((error = git_reference_resolve(&resolved, ref)) < 0)
return error;
- error = annotated_commit_init(out, repo, git_reference_target(resolved),
- git_reference_name(ref), NULL);
+ error = annotated_commit_init_from_id(out,
+ repo,
+ git_reference_target(resolved),
+ git_reference_name(ref));
+
+ if (!error) {
+ (*out)->ref_name = git__strdup(git_reference_name(ref));
+ GITERR_CHECK_ALLOC((*out)->ref_name);
+ }
git_reference_free(resolved);
return error;
@@ -97,41 +167,6 @@ int git_annotated_commit_from_head(
return error;
}
-int git_annotated_commit_from_commit(
- git_annotated_commit **out,
- git_commit *commit)
-{
- git_annotated_commit *annotated_commit;
-
- assert(out && commit);
-
- *out = NULL;
-
- annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
- GITERR_CHECK_ALLOC(annotated_commit);
-
- annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL;
-
- git_cached_obj_incref(commit);
- annotated_commit->commit = commit;
-
- git_oid_fmt(annotated_commit->id_str, git_commit_id(commit));
- annotated_commit->id_str[GIT_OID_HEXSZ] = '\0';
-
- *out = annotated_commit;
- return 0;
-}
-
-int git_annotated_commit_lookup(
- git_annotated_commit **out,
- git_repository *repo,
- const git_oid *id)
-{
- assert(out && repo && id);
-
- return annotated_commit_init(out, repo, id, NULL, NULL);
-}
-
int git_annotated_commit_from_fetchhead(
git_annotated_commit **out,
git_repository *repo,
@@ -141,33 +176,16 @@ int git_annotated_commit_from_fetchhead(
{
assert(repo && id && branch_name && remote_url);
- return annotated_commit_init(out, repo, id, branch_name, remote_url);
-}
-
-int git_annotated_commit_from_revspec(
- git_annotated_commit **out,
- git_repository *repo,
- const char *revspec)
-{
- git_object *obj, *commit;
- int error;
-
- assert(out && repo && revspec);
-
- if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
- return error;
-
- if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) {
- git_object_free(obj);
- return error;
- }
+ if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0)
+ return -1;
- error = annotated_commit_init(out, repo, git_object_id(commit), revspec, NULL);
+ (*out)->ref_name = git__strdup(branch_name);
+ GITERR_CHECK_ALLOC((*out)->ref_name);
- git_object_free(obj);
- git_object_free(commit);
+ (*out)->remote_url = git__strdup(remote_url);
+ GITERR_CHECK_ALLOC((*out)->remote_url);
- return error;
+ return 0;
}
@@ -187,8 +205,9 @@ void git_annotated_commit_free(git_annotated_commit *annotated_commit)
case GIT_ANNOTATED_COMMIT_REAL:
git_commit_free(annotated_commit->commit);
git_tree_free(annotated_commit->tree);
- git__free(annotated_commit->ref_name);
- git__free(annotated_commit->remote_url);
+ git__free((char *)annotated_commit->description);
+ git__free((char *)annotated_commit->ref_name);
+ git__free((char *)annotated_commit->remote_url);
break;
case GIT_ANNOTATED_COMMIT_VIRTUAL:
git_index_free(annotated_commit->index);
diff --git a/src/annotated_commit.h b/src/annotated_commit.h
index cbb88fd..3ac8b5f 100644
--- a/src/annotated_commit.h
+++ b/src/annotated_commit.h
@@ -33,8 +33,11 @@ struct git_annotated_commit {
git_index *index;
git_array_oid_t parents;
- char *ref_name;
- char *remote_url;
+ /* how this commit was looked up */
+ const char *description;
+
+ const char *ref_name;
+ const char *remote_url;
char id_str[GIT_OID_HEXSZ+1];
};
diff --git a/src/branch.c b/src/branch.c
index 0dcc14c..51c35d7 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -121,7 +121,8 @@ int git_branch_create_from_annotated(
const git_annotated_commit *commit,
int force)
{
- return create_branch(ref_out, repository, branch_name, commit->commit, commit->ref_name, force);
+ return create_branch(ref_out,
+ repository, branch_name, commit->commit, commit->description, force);
}
int git_branch_delete(git_reference *branch)
diff --git a/src/repository.c b/src/repository.c
index 8a6fef0..d39a901 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -2162,7 +2162,7 @@ int git_repository_set_head_detached_from_annotated(
{
assert(repo && commitish);
- return detach(repo, git_annotated_commit_id(commitish), commitish->ref_name);
+ return detach(repo, git_annotated_commit_id(commitish), commitish->description);
}
int git_repository_detach_head(git_repository* repo)
diff --git a/src/reset.c b/src/reset.c
index f8a1a1d..db0bfb3 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -195,5 +195,5 @@ int git_reset_from_annotated(
git_reset_t reset_type,
const git_checkout_options *checkout_opts)
{
- return reset(repo, (git_object *) commit->commit, commit->ref_name, reset_type, checkout_opts);
+ return reset(repo, (git_object *) commit->commit, commit->description, reset_type, checkout_opts);
}
diff --git a/tests/rebase/abort.c b/tests/rebase/abort.c
index 7052952..d8891fb 100644
--- a/tests/rebase/abort.c
+++ b/tests/rebase/abort.c
@@ -145,6 +145,25 @@ void test_rebase_abort__merge_by_id(void)
git_rebase_free(rebase);
}
+void test_rebase_abort__merge_by_revspec(void)
+{
+ git_rebase *rebase;
+ git_annotated_commit *branch_head, *onto_head;
+
+ cl_git_pass(git_annotated_commit_from_revspec(&branch_head, repo, "b146bd7"));
+ cl_git_pass(git_annotated_commit_from_revspec(&onto_head, repo, "efad0b1"));
+
+ cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
+ cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
+
+ test_abort(branch_head, onto_head);
+
+ git_annotated_commit_free(branch_head);
+ git_annotated_commit_free(onto_head);
+
+ git_rebase_free(rebase);
+}
+
void test_rebase_abort__merge_by_id_immediately_after_init(void)
{
git_rebase *rebase;