|
||||
File indexing completed on 2025-01-18 09:59:37
0001 /* 0002 * Copyright (C) the libgit2 contributors. All rights reserved. 0003 * 0004 * This file is part of libgit2, distributed under the GNU GPL v2 with 0005 * a Linking Exception. For full terms see the included COPYING file. 0006 */ 0007 #ifndef INCLUDE_git_commit_h__ 0008 #define INCLUDE_git_commit_h__ 0009 0010 #include "common.h" 0011 #include "types.h" 0012 #include "oid.h" 0013 #include "object.h" 0014 0015 /** 0016 * @file git2/commit.h 0017 * @brief Git commit parsing, formatting routines 0018 * @defgroup git_commit Git commit parsing, formatting routines 0019 * @ingroup Git 0020 * @{ 0021 */ 0022 GIT_BEGIN_DECL 0023 0024 /** 0025 * Lookup a commit object from a repository. 0026 * 0027 * The returned object should be released with `git_commit_free` when no 0028 * longer needed. 0029 * 0030 * @param commit pointer to the looked up commit 0031 * @param repo the repo to use when locating the commit. 0032 * @param id identity of the commit to locate. If the object is 0033 * an annotated tag it will be peeled back to the commit. 0034 * @return 0 or an error code 0035 */ 0036 GIT_EXTERN(int) git_commit_lookup( 0037 git_commit **commit, git_repository *repo, const git_oid *id); 0038 0039 /** 0040 * Lookup a commit object from a repository, given a prefix of its 0041 * identifier (short id). 0042 * 0043 * The returned object should be released with `git_commit_free` when no 0044 * longer needed. 0045 * 0046 * @see git_object_lookup_prefix 0047 * 0048 * @param commit pointer to the looked up commit 0049 * @param repo the repo to use when locating the commit. 0050 * @param id identity of the commit to locate. If the object is 0051 * an annotated tag it will be peeled back to the commit. 0052 * @param len the length of the short identifier 0053 * @return 0 or an error code 0054 */ 0055 GIT_EXTERN(int) git_commit_lookup_prefix( 0056 git_commit **commit, git_repository *repo, const git_oid *id, size_t len); 0057 0058 /** 0059 * Close an open commit 0060 * 0061 * This is a wrapper around git_object_free() 0062 * 0063 * IMPORTANT: 0064 * It *is* necessary to call this method when you stop 0065 * using a commit. Failure to do so will cause a memory leak. 0066 * 0067 * @param commit the commit to close 0068 */ 0069 0070 GIT_EXTERN(void) git_commit_free(git_commit *commit); 0071 0072 /** 0073 * Get the id of a commit. 0074 * 0075 * @param commit a previously loaded commit. 0076 * @return object identity for the commit. 0077 */ 0078 GIT_EXTERN(const git_oid *) git_commit_id(const git_commit *commit); 0079 0080 /** 0081 * Get the repository that contains the commit. 0082 * 0083 * @param commit A previously loaded commit. 0084 * @return Repository that contains this commit. 0085 */ 0086 GIT_EXTERN(git_repository *) git_commit_owner(const git_commit *commit); 0087 0088 /** 0089 * Get the encoding for the message of a commit, 0090 * as a string representing a standard encoding name. 0091 * 0092 * The encoding may be NULL if the `encoding` header 0093 * in the commit is missing; in that case UTF-8 is assumed. 0094 * 0095 * @param commit a previously loaded commit. 0096 * @return NULL, or the encoding 0097 */ 0098 GIT_EXTERN(const char *) git_commit_message_encoding(const git_commit *commit); 0099 0100 /** 0101 * Get the full message of a commit. 0102 * 0103 * The returned message will be slightly prettified by removing any 0104 * potential leading newlines. 0105 * 0106 * @param commit a previously loaded commit. 0107 * @return the message of a commit 0108 */ 0109 GIT_EXTERN(const char *) git_commit_message(const git_commit *commit); 0110 0111 /** 0112 * Get the full raw message of a commit. 0113 * 0114 * @param commit a previously loaded commit. 0115 * @return the raw message of a commit 0116 */ 0117 GIT_EXTERN(const char *) git_commit_message_raw(const git_commit *commit); 0118 0119 /** 0120 * Get the short "summary" of the git commit message. 0121 * 0122 * The returned message is the summary of the commit, comprising the 0123 * first paragraph of the message with whitespace trimmed and squashed. 0124 * 0125 * @param commit a previously loaded commit. 0126 * @return the summary of a commit or NULL on error 0127 */ 0128 GIT_EXTERN(const char *) git_commit_summary(git_commit *commit); 0129 0130 /** 0131 * Get the long "body" of the git commit message. 0132 * 0133 * The returned message is the body of the commit, comprising 0134 * everything but the first paragraph of the message. Leading and 0135 * trailing whitespaces are trimmed. 0136 * 0137 * @param commit a previously loaded commit. 0138 * @return the body of a commit or NULL when no the message only 0139 * consists of a summary 0140 */ 0141 GIT_EXTERN(const char *) git_commit_body(git_commit *commit); 0142 0143 /** 0144 * Get the commit time (i.e. committer time) of a commit. 0145 * 0146 * @param commit a previously loaded commit. 0147 * @return the time of a commit 0148 */ 0149 GIT_EXTERN(git_time_t) git_commit_time(const git_commit *commit); 0150 0151 /** 0152 * Get the commit timezone offset (i.e. committer's preferred timezone) of a commit. 0153 * 0154 * @param commit a previously loaded commit. 0155 * @return positive or negative timezone offset, in minutes from UTC 0156 */ 0157 GIT_EXTERN(int) git_commit_time_offset(const git_commit *commit); 0158 0159 /** 0160 * Get the committer of a commit. 0161 * 0162 * @param commit a previously loaded commit. 0163 * @return the committer of a commit 0164 */ 0165 GIT_EXTERN(const git_signature *) git_commit_committer(const git_commit *commit); 0166 0167 /** 0168 * Get the author of a commit. 0169 * 0170 * @param commit a previously loaded commit. 0171 * @return the author of a commit 0172 */ 0173 GIT_EXTERN(const git_signature *) git_commit_author(const git_commit *commit); 0174 0175 /** 0176 * Get the committer of a commit, using the mailmap to map names and email 0177 * addresses to canonical real names and email addresses. 0178 * 0179 * Call `git_signature_free` to free the signature. 0180 * 0181 * @param out a pointer to store the resolved signature. 0182 * @param commit a previously loaded commit. 0183 * @param mailmap the mailmap to resolve with. (may be NULL) 0184 * @return 0 or an error code 0185 */ 0186 GIT_EXTERN(int) git_commit_committer_with_mailmap( 0187 git_signature **out, const git_commit *commit, const git_mailmap *mailmap); 0188 0189 /** 0190 * Get the author of a commit, using the mailmap to map names and email 0191 * addresses to canonical real names and email addresses. 0192 * 0193 * Call `git_signature_free` to free the signature. 0194 * 0195 * @param out a pointer to store the resolved signature. 0196 * @param commit a previously loaded commit. 0197 * @param mailmap the mailmap to resolve with. (may be NULL) 0198 * @return 0 or an error code 0199 */ 0200 GIT_EXTERN(int) git_commit_author_with_mailmap( 0201 git_signature **out, const git_commit *commit, const git_mailmap *mailmap); 0202 0203 /** 0204 * Get the full raw text of the commit header. 0205 * 0206 * @param commit a previously loaded commit 0207 * @return the header text of the commit 0208 */ 0209 GIT_EXTERN(const char *) git_commit_raw_header(const git_commit *commit); 0210 0211 /** 0212 * Get the tree pointed to by a commit. 0213 * 0214 * @param tree_out pointer where to store the tree object 0215 * @param commit a previously loaded commit. 0216 * @return 0 or an error code 0217 */ 0218 GIT_EXTERN(int) git_commit_tree(git_tree **tree_out, const git_commit *commit); 0219 0220 /** 0221 * Get the id of the tree pointed to by a commit. This differs from 0222 * `git_commit_tree` in that no attempts are made to fetch an object 0223 * from the ODB. 0224 * 0225 * @param commit a previously loaded commit. 0226 * @return the id of tree pointed to by commit. 0227 */ 0228 GIT_EXTERN(const git_oid *) git_commit_tree_id(const git_commit *commit); 0229 0230 /** 0231 * Get the number of parents of this commit 0232 * 0233 * @param commit a previously loaded commit. 0234 * @return integer of count of parents 0235 */ 0236 GIT_EXTERN(unsigned int) git_commit_parentcount(const git_commit *commit); 0237 0238 /** 0239 * Get the specified parent of the commit. 0240 * 0241 * @param out Pointer where to store the parent commit 0242 * @param commit a previously loaded commit. 0243 * @param n the position of the parent (from 0 to `parentcount`) 0244 * @return 0 or an error code 0245 */ 0246 GIT_EXTERN(int) git_commit_parent( 0247 git_commit **out, 0248 const git_commit *commit, 0249 unsigned int n); 0250 0251 /** 0252 * Get the oid of a specified parent for a commit. This is different from 0253 * `git_commit_parent`, which will attempt to load the parent commit from 0254 * the ODB. 0255 * 0256 * @param commit a previously loaded commit. 0257 * @param n the position of the parent (from 0 to `parentcount`) 0258 * @return the id of the parent, NULL on error. 0259 */ 0260 GIT_EXTERN(const git_oid *) git_commit_parent_id( 0261 const git_commit *commit, 0262 unsigned int n); 0263 0264 /** 0265 * Get the commit object that is the <n>th generation ancestor 0266 * of the named commit object, following only the first parents. 0267 * The returned commit has to be freed by the caller. 0268 * 0269 * Passing `0` as the generation number returns another instance of the 0270 * base commit itself. 0271 * 0272 * @param ancestor Pointer where to store the ancestor commit 0273 * @param commit a previously loaded commit. 0274 * @param n the requested generation 0275 * @return 0 on success; GIT_ENOTFOUND if no matching ancestor exists 0276 * or an error code 0277 */ 0278 GIT_EXTERN(int) git_commit_nth_gen_ancestor( 0279 git_commit **ancestor, 0280 const git_commit *commit, 0281 unsigned int n); 0282 0283 /** 0284 * Get an arbitrary header field 0285 * 0286 * @param out the buffer to fill; existing content will be 0287 * overwritten 0288 * @param commit the commit to look in 0289 * @param field the header field to return 0290 * @return 0 on succeess, GIT_ENOTFOUND if the field does not exist, 0291 * or an error code 0292 */ 0293 GIT_EXTERN(int) git_commit_header_field(git_buf *out, const git_commit *commit, const char *field); 0294 0295 /** 0296 * Extract the signature from a commit 0297 * 0298 * If the id is not for a commit, the error class will be 0299 * `GIT_ERROR_INVALID`. If the commit does not have a signature, the 0300 * error class will be `GIT_ERROR_OBJECT`. 0301 * 0302 * @param signature the signature block; existing content will be 0303 * overwritten 0304 * @param signed_data signed data; this is the commit contents minus the signature block; 0305 * existing content will be overwritten 0306 * @param repo the repository in which the commit exists 0307 * @param commit_id the commit from which to extract the data 0308 * @param field the name of the header field containing the signature 0309 * block; pass `NULL` to extract the default 'gpgsig' 0310 * @return 0 on success, GIT_ENOTFOUND if the id is not for a commit 0311 * or the commit does not have a signature. 0312 */ 0313 GIT_EXTERN(int) git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_repository *repo, git_oid *commit_id, const char *field); 0314 0315 /** 0316 * Create new commit in the repository from a list of `git_object` pointers 0317 * 0318 * The message will **not** be cleaned up automatically. You can do that 0319 * with the `git_message_prettify()` function. 0320 * 0321 * @param id Pointer in which to store the OID of the newly created commit 0322 * 0323 * @param repo Repository where to store the commit 0324 * 0325 * @param update_ref If not NULL, name of the reference that 0326 * will be updated to point to this commit. If the reference 0327 * is not direct, it will be resolved to a direct reference. 0328 * Use "HEAD" to update the HEAD of the current branch and 0329 * make it point to this commit. If the reference doesn't 0330 * exist yet, it will be created. If it does exist, the first 0331 * parent must be the tip of this branch. 0332 * 0333 * @param author Signature with author and author time of commit 0334 * 0335 * @param committer Signature with committer and * commit time of commit 0336 * 0337 * @param message_encoding The encoding for the message in the 0338 * commit, represented with a standard encoding name. 0339 * E.g. "UTF-8". If NULL, no encoding header is written and 0340 * UTF-8 is assumed. 0341 * 0342 * @param message Full message for this commit 0343 * 0344 * @param tree An instance of a `git_tree` object that will 0345 * be used as the tree for the commit. This tree object must 0346 * also be owned by the given `repo`. 0347 * 0348 * @param parent_count Number of parents for this commit 0349 * 0350 * @param parents Array of `parent_count` pointers to `git_commit` 0351 * objects that will be used as the parents for this commit. This 0352 * array may be NULL if `parent_count` is 0 (root commit). All the 0353 * given commits must be owned by the `repo`. 0354 * 0355 * @return 0 or an error code 0356 * The created commit will be written to the Object Database and 0357 * the given reference will be updated to point to it 0358 */ 0359 GIT_EXTERN(int) git_commit_create( 0360 git_oid *id, 0361 git_repository *repo, 0362 const char *update_ref, 0363 const git_signature *author, 0364 const git_signature *committer, 0365 const char *message_encoding, 0366 const char *message, 0367 const git_tree *tree, 0368 size_t parent_count, 0369 git_commit * const parents[]); 0370 0371 /** 0372 * Create new commit in the repository using a variable argument list. 0373 * 0374 * The message will **not** be cleaned up automatically. You can do that 0375 * with the `git_message_prettify()` function. 0376 * 0377 * The parents for the commit are specified as a variable list of pointers 0378 * to `const git_commit *`. Note that this is a convenience method which may 0379 * not be safe to export for certain languages or compilers 0380 * 0381 * All other parameters remain the same as `git_commit_create()`. 0382 * 0383 * @see git_commit_create 0384 */ 0385 GIT_EXTERN(int) git_commit_create_v( 0386 git_oid *id, 0387 git_repository *repo, 0388 const char *update_ref, 0389 const git_signature *author, 0390 const git_signature *committer, 0391 const char *message_encoding, 0392 const char *message, 0393 const git_tree *tree, 0394 size_t parent_count, 0395 ...); 0396 0397 typedef struct { 0398 unsigned int version; 0399 0400 /** 0401 * Flags for creating the commit. 0402 * 0403 * If `allow_empty_commit` is specified, a commit with no changes 0404 * from the prior commit (and "empty" commit) is allowed. Otherwise, 0405 * commit creation will be stopped. 0406 */ 0407 unsigned int allow_empty_commit : 1; 0408 0409 /** The commit author, or NULL for the default. */ 0410 const git_signature *author; 0411 0412 /** The committer, or NULL for the default. */ 0413 const git_signature *committer; 0414 0415 /** Encoding for the commit message; leave NULL for default. */ 0416 const char *message_encoding; 0417 } git_commit_create_options; 0418 0419 #define GIT_COMMIT_CREATE_OPTIONS_VERSION 1 0420 #define GIT_COMMIT_CREATE_OPTIONS_INIT { GIT_COMMIT_CREATE_OPTIONS_VERSION } 0421 0422 /** 0423 * Commits the staged changes in the repository; this is a near analog to 0424 * `git commit -m message`. 0425 * 0426 * By default, empty commits are not allowed. 0427 * 0428 * @param id pointer to store the new commit's object id 0429 * @param repo repository to commit changes in 0430 * @param message the commit message 0431 * @param opts options for creating the commit 0432 * @return 0 on success, GIT_EUNCHANGED if there were no changes to commit, or an error code 0433 */ 0434 GIT_EXTERN(int) git_commit_create_from_stage( 0435 git_oid *id, 0436 git_repository *repo, 0437 const char *message, 0438 const git_commit_create_options *opts); 0439 0440 /** 0441 * Amend an existing commit by replacing only non-NULL values. 0442 * 0443 * This creates a new commit that is exactly the same as the old commit, 0444 * except that any non-NULL values will be updated. The new commit has 0445 * the same parents as the old commit. 0446 * 0447 * The `update_ref` value works as in the regular `git_commit_create()`, 0448 * updating the ref to point to the newly rewritten commit. If you want 0449 * to amend a commit that is not currently the tip of the branch and then 0450 * rewrite the following commits to reach a ref, pass this as NULL and 0451 * update the rest of the commit chain and ref separately. 0452 * 0453 * Unlike `git_commit_create()`, the `author`, `committer`, `message`, 0454 * `message_encoding`, and `tree` parameters can be NULL in which case this 0455 * will use the values from the original `commit_to_amend`. 0456 * 0457 * All parameters have the same meanings as in `git_commit_create()`. 0458 * 0459 * @see git_commit_create 0460 */ 0461 GIT_EXTERN(int) git_commit_amend( 0462 git_oid *id, 0463 const git_commit *commit_to_amend, 0464 const char *update_ref, 0465 const git_signature *author, 0466 const git_signature *committer, 0467 const char *message_encoding, 0468 const char *message, 0469 const git_tree *tree); 0470 0471 /** 0472 * Create a commit and write it into a buffer 0473 * 0474 * Create a commit as with `git_commit_create()` but instead of 0475 * writing it to the objectdb, write the contents of the object into a 0476 * buffer. 0477 * 0478 * @param out the buffer into which to write the commit object content 0479 * 0480 * @param repo Repository where the referenced tree and parents live 0481 * 0482 * @param author Signature with author and author time of commit 0483 * 0484 * @param committer Signature with committer and * commit time of commit 0485 * 0486 * @param message_encoding The encoding for the message in the 0487 * commit, represented with a standard encoding name. 0488 * E.g. "UTF-8". If NULL, no encoding header is written and 0489 * UTF-8 is assumed. 0490 * 0491 * @param message Full message for this commit 0492 * 0493 * @param tree An instance of a `git_tree` object that will 0494 * be used as the tree for the commit. This tree object must 0495 * also be owned by the given `repo`. 0496 * 0497 * @param parent_count Number of parents for this commit 0498 * 0499 * @param parents Array of `parent_count` pointers to `git_commit` 0500 * objects that will be used as the parents for this commit. This 0501 * array may be NULL if `parent_count` is 0 (root commit). All the 0502 * given commits must be owned by the `repo`. 0503 * 0504 * @return 0 or an error code 0505 */ 0506 GIT_EXTERN(int) git_commit_create_buffer( 0507 git_buf *out, 0508 git_repository *repo, 0509 const git_signature *author, 0510 const git_signature *committer, 0511 const char *message_encoding, 0512 const char *message, 0513 const git_tree *tree, 0514 size_t parent_count, 0515 git_commit * const parents[]); 0516 0517 /** 0518 * Create a commit object from the given buffer and signature 0519 * 0520 * Given the unsigned commit object's contents, its signature and the 0521 * header field in which to store the signature, attach the signature 0522 * to the commit and write it into the given repository. 0523 * 0524 * @param out the resulting commit id 0525 * @param repo the repository to create the commit in. 0526 * @param commit_content the content of the unsigned commit object 0527 * @param signature the signature to add to the commit. Leave `NULL` 0528 * to create a commit without adding a signature field. 0529 * @param signature_field which header field should contain this 0530 * signature. Leave `NULL` for the default of "gpgsig" 0531 * @return 0 or an error code 0532 */ 0533 GIT_EXTERN(int) git_commit_create_with_signature( 0534 git_oid *out, 0535 git_repository *repo, 0536 const char *commit_content, 0537 const char *signature, 0538 const char *signature_field); 0539 0540 /** 0541 * Create an in-memory copy of a commit. The copy must be explicitly 0542 * free'd or it will leak. 0543 * 0544 * @param out Pointer to store the copy of the commit 0545 * @param source Original commit to copy 0546 * @return 0 0547 */ 0548 GIT_EXTERN(int) git_commit_dup(git_commit **out, git_commit *source); 0549 0550 /** 0551 * Commit creation callback: used when a function is going to create 0552 * commits (for example, in `git_rebase_commit`) to allow callers to 0553 * override the commit creation behavior. For example, users may 0554 * wish to sign commits by providing this information to 0555 * `git_commit_create_buffer`, signing that buffer, then calling 0556 * `git_commit_create_with_signature`. The resultant commit id 0557 * should be set in the `out` object id parameter. 0558 * 0559 * @param out pointer that this callback will populate with the object 0560 * id of the commit that is created 0561 * @param author the author name and time of the commit 0562 * @param committer the committer name and time of the commit 0563 * @param message_encoding the encoding of the given message, or NULL 0564 * to assume UTF8 0565 * @param message the commit message 0566 * @param tree the tree to be committed 0567 * @param parent_count the number of parents for this commit 0568 * @param parents the commit parents 0569 * @param payload the payload pointer in the rebase options 0570 * @return 0 if this callback has created the commit and populated the out 0571 * parameter, GIT_PASSTHROUGH if the callback has not created a 0572 * commit and wants the calling function to create the commit as 0573 * if no callback had been specified, any other value to stop 0574 * and return a failure 0575 */ 0576 typedef int (*git_commit_create_cb)( 0577 git_oid *out, 0578 const git_signature *author, 0579 const git_signature *committer, 0580 const char *message_encoding, 0581 const char *message, 0582 const git_tree *tree, 0583 size_t parent_count, 0584 git_commit * const parents[], 0585 void *payload); 0586 0587 /** An array of commits returned from the library */ 0588 typedef struct git_commitarray { 0589 git_commit *const *commits; 0590 size_t count; 0591 } git_commitarray; 0592 0593 /** 0594 * Free the commits contained in a commit array. This method should 0595 * be called on `git_commitarray` objects that were provided by the 0596 * library. Not doing so will result in a memory leak. 0597 * 0598 * This does not free the `git_commitarray` itself, since the library 0599 * will never allocate that object directly itself. 0600 * 0601 * @param array The git_commitarray that contains commits to free 0602 */ 0603 GIT_EXTERN(void) git_commitarray_dispose(git_commitarray *array); 0604 0605 /** @} */ 0606 GIT_END_DECL 0607 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |