1 /* 2 * Copyright (C) the libgit2 contributors. All rights reserved. 3 * 4 * This file is part of libgit2, distributed under the GNU GPL v2 with 5 * a Linking Exception. For full terms see the included COPYING file. 6 */ 7 module libgit2_d.commit; 8 9 10 private static import libgit2_d.buffer; 11 private static import libgit2_d.oid; 12 private static import libgit2_d.types; 13 14 /** 15 * @file git2/commit.h 16 * @brief Git commit parsing, formatting routines 17 * @defgroup git_commit Git commit parsing, formatting routines 18 * @ingroup Git 19 * @{ 20 */ 21 extern (C): 22 nothrow @nogc: 23 public: 24 25 /** 26 * Lookup a commit object from a repository. 27 * 28 * The returned object should be released with `git_commit_free` when no 29 * longer needed. 30 * 31 * Params: 32 * commit = pointer to the looked up commit 33 * repo = the repo to use when locating the commit. 34 * id = identity of the commit to locate. If the object is an annotated tag it will be peeled back to the commit. 35 * 36 * Returns: 0 or an error code 37 */ 38 //GIT_EXTERN 39 int git_commit_lookup(libgit2_d.types.git_commit** commit, libgit2_d.types.git_repository* repo, const (libgit2_d.oid.git_oid)* id); 40 41 /** 42 * Lookup a commit object from a repository, given a prefix of its 43 * identifier (short id). 44 * 45 * The returned object should be released with `git_commit_free` when no 46 * longer needed. 47 * 48 * @see git_object_lookup_prefix 49 * 50 * Params: 51 * commit = pointer to the looked up commit 52 * repo = the repo to use when locating the commit. 53 * id = identity of the commit to locate. If the object is an annotated tag it will be peeled back to the commit. 54 * len = the length of the short identifier 55 * 56 * Returns: 0 or an error code 57 */ 58 //GIT_EXTERN 59 int git_commit_lookup_prefix(libgit2_d.types.git_commit** commit, libgit2_d.types.git_repository* repo, const (libgit2_d.oid.git_oid)* id, size_t len); 60 61 /** 62 * Close an open commit 63 * 64 * This is a wrapper around git_object_free() 65 * 66 * IMPORTANT: 67 * It *is* necessary to call this method when you stop 68 * using a commit. Failure to do so will cause a memory leak. 69 * 70 * Params: 71 * commit = the commit to close 72 */ 73 74 //GIT_EXTERN 75 void git_commit_free(libgit2_d.types.git_commit* commit); 76 77 /** 78 * Get the id of a commit. 79 * 80 * Params: 81 * commit = a previously loaded commit. 82 * 83 * Returns: object identity for the commit. 84 */ 85 //GIT_EXTERN 86 const (libgit2_d.oid.git_oid)* git_commit_id(const (libgit2_d.types.git_commit)* commit); 87 88 /** 89 * Get the repository that contains the commit. 90 * 91 * Params: 92 * commit = A previously loaded commit. 93 * 94 * Returns: Repository that contains this commit. 95 */ 96 //GIT_EXTERN 97 libgit2_d.types.git_repository* git_commit_owner(const (libgit2_d.types.git_commit)* commit); 98 99 /** 100 * Get the encoding for the message of a commit, 101 * as a string representing a standard encoding name. 102 * 103 * The encoding may be null if the `encoding` header 104 * in the commit is missing; in that case UTF-8 is assumed. 105 * 106 * Params: 107 * commit = a previously loaded commit. 108 * 109 * Returns: null, or the encoding 110 */ 111 //GIT_EXTERN 112 const (char)* git_commit_message_encoding(const (libgit2_d.types.git_commit)* commit); 113 114 /** 115 * Get the full message of a commit. 116 * 117 * The returned message will be slightly prettified by removing any 118 * potential leading newlines. 119 * 120 * Params: 121 * commit = a previously loaded commit. 122 * 123 * Returns: the message of a commit 124 */ 125 //GIT_EXTERN 126 const (char)* git_commit_message(const (libgit2_d.types.git_commit)* commit); 127 128 /** 129 * Get the full raw message of a commit. 130 * 131 * Params: 132 * commit = a previously loaded commit. 133 * 134 * Returns: the raw message of a commit 135 */ 136 //GIT_EXTERN 137 const (char)* git_commit_message_raw(const (libgit2_d.types.git_commit)* commit); 138 139 /** 140 * Get the short "summary" of the git commit message. 141 * 142 * The returned message is the summary of the commit, comprising the 143 * first paragraph of the message with whitespace trimmed and squashed. 144 * 145 * Params: 146 * commit = a previously loaded commit. 147 * 148 * Returns: the summary of a commit or null on error 149 */ 150 //GIT_EXTERN 151 const (char)* git_commit_summary(libgit2_d.types.git_commit* commit); 152 153 /** 154 * Get the long "body" of the git commit message. 155 * 156 * The returned message is the body of the commit, comprising 157 * everything but the first paragraph of the message. Leading and 158 * trailing whitespaces are trimmed. 159 * 160 * Params: 161 * commit = a previously loaded commit. 162 * 163 * Returns: the body of a commit or null when no the message only consists of a summary 164 */ 165 //GIT_EXTERN 166 const (char)* git_commit_body(libgit2_d.types.git_commit* commit); 167 168 /** 169 * Get the commit time (i.e. committer time) of a commit. 170 * 171 * Params: 172 * commit = a previously loaded commit. 173 * 174 * Returns: the time of a commit 175 */ 176 //GIT_EXTERN 177 libgit2_d.types.git_time_t git_commit_time(const (libgit2_d.types.git_commit)* commit); 178 179 /** 180 * Get the commit timezone offset (i.e. committer's preferred timezone) of a 181 * commit. 182 * 183 * Params: 184 * commit = a previously loaded commit. 185 * 186 * Returns: positive or negative timezone offset, in minutes from UTC 187 */ 188 //GIT_EXTERN 189 int git_commit_time_offset(const (libgit2_d.types.git_commit)* commit); 190 191 /** 192 * Get the committer of a commit. 193 * 194 * Params: 195 * commit = a previously loaded commit. 196 * 197 * Returns: the committer of a commit 198 */ 199 //GIT_EXTERN 200 const (libgit2_d.types.git_signature)* git_commit_committer(const (libgit2_d.types.git_commit)* commit); 201 202 /** 203 * Get the author of a commit. 204 * 205 * Params: 206 * commit = a previously loaded commit. 207 * 208 * Returns: the author of a commit 209 */ 210 //GIT_EXTERN 211 const (libgit2_d.types.git_signature)* git_commit_author(const (libgit2_d.types.git_commit)* commit); 212 213 /** 214 * Get the committer of a commit, using the mailmap to map names and email 215 * addresses to canonical real names and email addresses. 216 * 217 * Call `git_signature_free` to free the signature. 218 * 219 * Params: 220 * out_ = a pointer to store the resolved signature. 221 * commit = a previously loaded commit. 222 * mailmap = the mailmap to resolve with. (may be NULL) 223 * 224 * Returns: 0 or an error code 225 */ 226 //GIT_EXTERN 227 int git_commit_committer_with_mailmap(libgit2_d.types.git_signature** out_, const (libgit2_d.types.git_commit)* commit, const (libgit2_d.types.git_mailmap)* mailmap); 228 229 /** 230 * Get the author of a commit, using the mailmap to map names and email 231 * addresses to canonical real names and email addresses. 232 * 233 * Call `git_signature_free` to free the signature. 234 * 235 * Params: 236 * out_ = a pointer to store the resolved signature. 237 * commit = a previously loaded commit. 238 * mailmap = the mailmap to resolve with. (may be NULL) 239 * 240 * Returns: 0 or an error code 241 */ 242 //GIT_EXTERN 243 int git_commit_author_with_mailmap(libgit2_d.types.git_signature** out_, const (libgit2_d.types.git_commit)* commit, const (libgit2_d.types.git_mailmap)* mailmap); 244 245 /** 246 * Get the full raw text of the commit header. 247 * 248 * Params: 249 * commit = a previously loaded commit 250 * 251 * Returns: the header text of the commit 252 */ 253 //GIT_EXTERN 254 const (char)* git_commit_raw_header(const (libgit2_d.types.git_commit)* commit); 255 256 /** 257 * Get the tree pointed to by a commit. 258 * 259 * Params: 260 * tree_out = pointer where to store the tree object 261 * commit = a previously loaded commit. 262 * 263 * Returns: 0 or an error code 264 */ 265 //GIT_EXTERN 266 int git_commit_tree(libgit2_d.types.git_tree** tree_out, const (libgit2_d.types.git_commit)* commit); 267 268 /** 269 * Get the id of the tree pointed to by a commit. This differs from 270 * `git_commit_tree` in that no attempts are made to fetch an object 271 * from the ODB. 272 * 273 * Params: 274 * commit = a previously loaded commit. 275 * 276 * Returns: the id of tree pointed to by commit. 277 */ 278 //GIT_EXTERN 279 const (libgit2_d.oid.git_oid)* git_commit_tree_id(const (libgit2_d.types.git_commit)* commit); 280 281 /** 282 * Get the number of parents of this commit 283 * 284 * Params: 285 * commit = a previously loaded commit. 286 * 287 * Returns: integer of count of parents 288 */ 289 //GIT_EXTERN 290 uint git_commit_parentcount(const (libgit2_d.types.git_commit)* commit); 291 292 /** 293 * Get the specified parent of the commit. 294 * 295 * Params: 296 * out_ = Pointer where to store the parent commit 297 * commit = a previously loaded commit. 298 * n = the position of the parent (from 0 to `parentcount`) 299 * 300 * Returns: 0 or an error code 301 */ 302 //GIT_EXTERN 303 int git_commit_parent(libgit2_d.types.git_commit** out_, const (libgit2_d.types.git_commit)* commit, uint n); 304 305 /** 306 * Get the oid of a specified parent for a commit. This is different from 307 * `git_commit_parent`, which will attempt to load the parent commit from 308 * the ODB. 309 * 310 * Params: 311 * commit = a previously loaded commit. 312 * n = the position of the parent (from 0 to `parentcount`) 313 * 314 * Returns: the id of the parent, null on error. 315 */ 316 //GIT_EXTERN 317 const (libgit2_d.oid.git_oid)* git_commit_parent_id(const (libgit2_d.types.git_commit)* commit, uint n); 318 319 /** 320 * Get the commit object that is the <n>th generation ancestor 321 * of the named commit object, following only the first parents. 322 * The returned commit has to be freed by the caller. 323 * 324 * Passing `0` as the generation number returns another instance of the 325 * base commit itself. 326 * 327 * Params: 328 * ancestor = Pointer where to store the ancestor commit 329 * commit = a previously loaded commit. 330 * n = the requested generation 331 * 332 * Returns: 0 on success; git_error_code.GIT_ENOTFOUND if no matching ancestor exists or an error code 333 */ 334 //GIT_EXTERN 335 int git_commit_nth_gen_ancestor(libgit2_d.types.git_commit** ancestor, const (libgit2_d.types.git_commit)* commit, uint n); 336 337 /** 338 * Get an arbitrary header field 339 * 340 * Params: 341 * out_ = the buffer to fill; existing content will be overwritten 342 * commit = the commit to look in 343 * field = the header field to return 344 * 345 * Returns: 0 on succeess, git_error_code.GIT_ENOTFOUND if the field does not exist, or an error code 346 */ 347 //GIT_EXTERN 348 int git_commit_header_field(libgit2_d.buffer.git_buf* out_, const (libgit2_d.types.git_commit)* commit, const (char)* field); 349 350 /** 351 * Extract the signature from a commit 352 * 353 * If the id is not for a commit, the error class will be 354 * `git_error_t.GIT_ERROR_INVALID`. If the commit does not have a signature, the 355 * error class will be `git_error_t.GIT_ERROR_OBJECT`. 356 * 357 * Params: 358 * signature = the signature block; existing content will be overwritten 359 * signed_data = signed data; this is the commit contents minus the signature block; existing content will be overwritten 360 * repo = the repository in which the commit exists 361 * commit_id = the commit from which to extract the data 362 * field = the name of the header field containing the signature block; pass `null` to extract the default 'gpgsig' 363 * 364 * Returns: 0 on success, git_error_code.GIT_ENOTFOUND if the id is not for a commit or the commit does not have a signature. 365 */ 366 //GIT_EXTERN 367 int git_commit_extract_signature(libgit2_d.buffer.git_buf* signature, libgit2_d.buffer.git_buf* signed_data, libgit2_d.types.git_repository* repo, libgit2_d.oid.git_oid* commit_id, const (char)* field); 368 369 /** 370 * Create new commit in the repository from a list of `git_object` pointers 371 * 372 * The message will **not** be cleaned up automatically. You can do that 373 * with the `git_message_prettify()` function. 374 * 375 * Params: 376 * id = Pointer in which to store the OID of the newly created commit 377 * repo = Repository where to store the commit 378 * update_ref = If not null, name of the reference that will be updated to point to this commit. If the reference is not direct, it will be resolved to a direct reference. Use "HEAD" to update the HEAD of the current branch and make it point to this commit. If the reference doesn't exist yet, it will be created. If it does exist, the first parent must be the tip of this branch. 379 * author = Signature with author and author time of commit 380 * committer = Signature with committer and * commit time of commit 381 * message_encoding = The encoding for the message in the commit, represented with a standard encoding name. E.g. "UTF-8". If null, no encoding header is written and UTF-8 is assumed. 382 * message = Full message for this commit 383 * tree = An instance of a `git_tree` object that will be used as the tree for the commit. This tree object must also be owned by the given `repo`. 384 * parent_count = Number of parents for this commit 385 * parents = Array of `parent_count` pointers to `git_commit` objects that will be used as the parents for this commit. This array may be null if `parent_count` is 0 (root commit). All the given commits must be owned by the `repo`. 386 * 387 * Returns: 0 or an error code. The created commit will be written to the Object Database and the given reference will be updated to point to it 388 */ 389 //GIT_EXTERN 390 int git_commit_create(libgit2_d.oid.git_oid* id, libgit2_d.types.git_repository* repo, const (char)* update_ref, const (libgit2_d.types.git_signature)* author, const (libgit2_d.types.git_signature)* committer, const (char)* message_encoding, const (char)* message, const (libgit2_d.types.git_tree)* tree, size_t parent_count, const (libgit2_d.types.git_commit)** parents); 391 392 /** 393 * Create new commit in the repository using a variable argument list. 394 * 395 * The message will **not** be cleaned up automatically. You can do that 396 * with the `git_message_prettify()` function. 397 * 398 * The parents for the commit are specified as a variable list of pointers 399 * to `const git_commit *`. Note that this is a convenience method which may 400 * not be safe to export for certain languages or compilers 401 * 402 * All other parameters remain the same as `git_commit_create()`. 403 * 404 * @see git_commit_create 405 */ 406 //GIT_EXTERN 407 int git_commit_create_v(libgit2_d.oid.git_oid* id, libgit2_d.types.git_repository* repo, const (char)* update_ref, const (libgit2_d.types.git_signature)* author, const (libgit2_d.types.git_signature)* committer, const (char)* message_encoding, const (char)* message, const (libgit2_d.types.git_tree)* tree, size_t parent_count, ...); 408 409 /** 410 * Amend an existing commit by replacing only non-null values. 411 * 412 * This creates a new commit that is exactly the same as the old commit, 413 * except that any non-null values will be updated. The new commit has 414 * the same parents as the old commit. 415 * 416 * The `update_ref` value works as in the regular `git_commit_create()`, 417 * updating the ref to point to the newly rewritten commit. If you want 418 * to amend a commit that is not currently the tip of the branch and then 419 * rewrite the following commits to reach a ref, pass this as null and 420 * update the rest of the commit chain and ref separately. 421 * 422 * Unlike `git_commit_create()`, the `author`, `committer`, `message`, 423 * `message_encoding`, and `tree` parameters can be null in which case this 424 * will use the values from the original `commit_to_amend`. 425 * 426 * All parameters have the same meanings as in `git_commit_create()`. 427 * 428 * @see git_commit_create 429 */ 430 //GIT_EXTERN 431 int git_commit_amend(libgit2_d.oid.git_oid* id, const (libgit2_d.types.git_commit)* commit_to_amend, const (char)* update_ref, const (libgit2_d.types.git_signature)* author, const (libgit2_d.types.git_signature)* committer, const (char)* message_encoding, const (char)* message, const (libgit2_d.types.git_tree)* tree); 432 433 /** 434 * Create a commit and write it into a buffer 435 * 436 * Create a commit as with `git_commit_create()` but instead of 437 * writing it to the objectdb, write the contents of the object into a 438 * buffer. 439 * 440 * Params: 441 * out_ = the buffer into which to write the commit object content 442 * repo = Repository where the referenced tree and parents live 443 * author = Signature with author and author time of commit 444 * committer = Signature with committer and * commit time of commit 445 * message_encoding = The encoding for the message in the commit, represented with a standard encoding name. E.g. "UTF-8". If null, no encoding header is written and UTF-8 is assumed. 446 * message = Full message for this commit 447 * tree = An instance of a `git_tree` object that will be used as the tree for the commit. This tree object must also be owned by the given `repo`. 448 * parent_count = Number of parents for this commit 449 * parents = Array of `parent_count` pointers to `git_commit` objects that will be used as the parents for this commit. This array may be null if `parent_count` is 0 (root commit). All the given commits must be owned by the `repo`. 450 * 451 * Returns: 0 or an error code 452 */ 453 //GIT_EXTERN 454 int git_commit_create_buffer(libgit2_d.buffer.git_buf* out_, libgit2_d.types.git_repository* repo, const (libgit2_d.types.git_signature)* author, const (libgit2_d.types.git_signature)* committer, const (char)* message_encoding, const (char)* message, const (libgit2_d.types.git_tree)* tree, size_t parent_count, const (libgit2_d.types.git_commit)** parents); 455 456 /** 457 * Create a commit object from the given buffer and signature 458 * 459 * Given the unsigned commit object's contents, its signature and the 460 * header field in which to store the signature, attach the signature 461 * to the commit and write it into the given repository. 462 * 463 * Params: 464 * out_ = the resulting commit id 465 * commit_content = the content of the unsigned commit object 466 * signature = the signature to add to the commit. Leave `NULL` to create a commit without adding a signature field. 467 * signature_field = which header field should contain this signature. Leave `NULL` for the default of "gpgsig" 468 * 469 * Returns: 0 or an error code 470 */ 471 //GIT_EXTERN 472 int git_commit_create_with_signature(libgit2_d.oid.git_oid* out_, libgit2_d.types.git_repository* repo, const (char)* commit_content, const (char)* signature, const (char)* signature_field); 473 474 /** 475 * Create an in-memory copy of a commit. The copy must be explicitly 476 * free'd or it will leak. 477 * 478 * Params: 479 * out_ = Pointer to store the copy of the commit 480 * source = Original commit to copy 481 */ 482 //GIT_EXTERN 483 int git_commit_dup(libgit2_d.types.git_commit** out_, libgit2_d.types.git_commit* source); 484 485 /** 486 * Commit signing callback. 487 * 488 * The callback will be called with the commit content, giving a user an 489 * opportunity to sign the commit content. The signature_field 490 * buf may be left empty to specify the default field "gpgsig". 491 * 492 * Signatures can take the form of any string, and can be created on an arbitrary 493 * header field. Signatures are most commonly used for verifying authorship of a 494 * commit using GPG or a similar cryptographically secure signing algorithm. 495 * See https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work for more 496 * details. 497 * 498 * When the callback: 499 * - returns git_error_code.GIT_PASSTHROUGH, no signature will be added to the commit. 500 * - returns < 0, commit creation will be aborted. 501 * - returns git_error_code.GIT_OK, the signature parameter is expected to be filled. 502 */ 503 alias git_commit_signing_cb = int function(libgit2_d.buffer.git_buf* signature, libgit2_d.buffer.git_buf* signature_field, const (char)* commit_content, void* payload); 504 505 /** @} */