Add git_tag_create_o_f() and git_tag_create_f() which overwrite existing tag reference, if any
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
diff --git a/include/git2/tag.h b/include/git2/tag.h
index b83d447..6468cfd 100644
--- a/include/git2/tag.h
+++ b/include/git2/tag.h
@@ -189,6 +189,64 @@ GIT_EXTERN(int) git_tag_create_o(
const git_signature *tagger,
const char *message);
+/**
+* Create a new tag in the repository from an OID
+* and overwrite an already existing tag reference, if any.
+*
+* @param oid Pointer where to store the OID of the
+* newly created tag
+*
+* @param repo Repository where to store the tag
+*
+* @param tag_name Name for the tag; this name is validated
+* for consistency.
+*
+* @param target OID to which this tag points; note that no
+* validation is done on this OID. Use the _o_f version of this
+* method to assure a proper object is being tagged
+*
+* @param target_type Type of the tagged OID; note that no
+* validation is performed here either
+*
+* @param tagger Signature of the tagger for this tag, and
+* of the tagging time
+*
+* @param message Full message for this tag
+*
+* @return 0 on success; error code otherwise.
+* A tag object is written to the ODB, and a proper reference
+* is written in the /refs/tags folder, pointing to it
+*/
+GIT_EXTERN(int) git_tag_create_f(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_oid *target,
+ git_otype target_type,
+ const git_signature *tagger,
+ const char *message);
+
+/**
+ * Create a new tag in the repository from an existing
+ * `git_object` instance and overwrite an already existing
+ * tag reference, if any.
+ *
+ * This method replaces the `target` and `target_type`
+ * paremeters of `git_tag_create_f` by a single instance
+ * of a `const git_object *`, which is assured to be
+ * a proper object in the ODB and hence will create
+ * a valid tag
+ *
+ * @see git_tag_create_f
+ */
+GIT_EXTERN(int) git_tag_create_o_f(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_object *target,
+ const git_signature *tagger,
+ const char *message);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/tag.c b/src/tag.c
index 9a00694..e75c469 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -153,29 +153,15 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
return GIT_SUCCESS;
}
-int git_tag_create_o(
- git_oid *oid,
- git_repository *repo,
- const char *tag_name,
- const git_object *target,
- const git_signature *tagger,
- const char *message)
-{
- return git_tag_create(
- oid, repo, tag_name,
- git_object_id(target),
- git_object_type(target),
- tagger, message);
-}
-
-int git_tag_create(
+static int tag_create(
git_oid *oid,
git_repository *repo,
const char *tag_name,
const git_oid *target,
git_otype target_type,
const git_signature *tagger,
- const char *message)
+ const char *message,
+ int allow_ref_overwrite)
{
size_t final_size = 0;
git_odb_stream *stream;
@@ -187,12 +173,27 @@ int git_tag_create(
char ref_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
int type_str_len, tag_name_len, tagger_str_len, message_len;
- int error;
+ int error, should_update_ref = 0;
- /** Ensure the tag name doesn't conflict with an already existing reference **/
+ /** Ensure the tag name doesn't conflict with an already existing
+ reference unless overwriting has explictly been requested **/
git__joinpath(ref_name, GIT_REFS_TAGS_DIR, tag_name);
- if (!git_reference_lookup(&new_ref, repo, ref_name))
- return GIT_EEXISTS;
+ error = git_reference_lookup(&new_ref, repo, ref_name);
+
+ switch (error) {
+ case GIT_SUCCESS:
+ if (!allow_ref_overwrite)
+ return GIT_EEXISTS;
+ should_update_ref = 1;
+
+ /* Fall trough */
+
+ case GIT_ENOTFOUND:
+ break;
+
+ default:
+ return error;
+ }
type_str = git_object_type2string(target_type);
@@ -234,9 +235,75 @@ int git_tag_create(
if (error < GIT_SUCCESS)
return error;
- return git_reference_create_oid(&new_ref, repo, ref_name, oid);
+ if (!should_update_ref)
+ error = git_reference_create_oid(&new_ref, repo, ref_name, oid);
+ else
+ error = git_reference_set_oid(new_ref, oid);
+
+ return error;
+}
+
+int git_tag_create_o(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_object *target,
+ const git_signature *tagger,
+ const char *message)
+{
+ return tag_create(
+ oid, repo, tag_name,
+ git_object_id(target),
+ git_object_type(target),
+ tagger, message, 0);
+}
+
+int git_tag_create(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_oid *target,
+ git_otype target_type,
+ const git_signature *tagger,
+ const char *message)
+{
+ return tag_create(
+ oid, repo, tag_name,
+ target,
+ target_type,
+ tagger, message, 0);
+}
+
+int git_tag_create_o_f(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_object *target,
+ const git_signature *tagger,
+ const char *message)
+{
+ return tag_create(
+ oid, repo, tag_name,
+ git_object_id(target),
+ git_object_type(target),
+ tagger, message, 1);
}
+int git_tag_create_f(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_oid *target,
+ git_otype target_type,
+ const git_signature *tagger,
+ const char *message)
+{
+ return tag_create(
+ oid, repo, tag_name,
+ target,
+ target_type,
+ tagger, message, 1);
+}
int git_tag__parse(git_tag *tag, git_odb_object *obj)
{
diff --git a/tests/t08-tag.c b/tests/t08-tag.c
index a5bdee3..2bea4bc 100644
--- a/tests/t08-tag.c
+++ b/tests/t08-tag.c
@@ -189,10 +189,46 @@ BEGIN_TEST(write2, "Attempt to write a tag bearing the same name than an already
END_TEST
+BEGIN_TEST(write3, "Replace an already existing tag")
+ git_repository *repo;
+ git_oid target_id, tag_id, old_tag_id;
+ const git_signature *tagger;
+ git_reference *ref_tag;
+
+ must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
+
+ git_oid_mkstr(&target_id, tagged_commit);
+
+ must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/very-simple"));
+ git_oid_cpy(&old_tag_id, git_reference_oid(ref_tag));
+
+ /* create signature */
+ tagger = git_signature_new(TAGGER_NAME, TAGGER_EMAIL, 123456789, 60);
+ must_be_true(tagger != NULL);
+
+ must_pass(git_tag_create_f(
+ &tag_id, /* out id */
+ repo,
+ "very-simple",
+ &target_id,
+ GIT_OBJ_COMMIT,
+ tagger,
+ TAGGER_MESSAGE));
+
+ git_signature_free((git_signature *)tagger);
+
+ must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/very-simple"));
+ must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0);
+ must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &old_tag_id) != 0);
+
+ close_temp_repo(repo);
+
+END_TEST
BEGIN_SUITE(tag)
ADD_TEST(read0);
ADD_TEST(write0);
ADD_TEST(write1);
ADD_TEST(write2);
+ ADD_TEST(write3);
END_SUITE