]>
git.proxmox.com Git - libgit2.git/blob - include/git2/commit.h
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_git_commit_h__
8 #define INCLUDE_git_commit_h__
17 * @brief Git commit parsing, formatting routines
18 * @defgroup git_commit Git commit parsing, formatting routines
25 * Lookup a commit object from a repository.
27 * The returned object should be released with `git_commit_free` when no
30 * @param commit pointer to the looked up commit
31 * @param repo the repo to use when locating the commit.
32 * @param id identity of the commit to locate. If the object is
33 * an annotated tag it will be peeled back to the commit.
34 * @return 0 or an error code
36 GIT_EXTERN(int) git_commit_lookup(
37 git_commit
**commit
, git_repository
*repo
, const git_oid
*id
);
40 * Lookup a commit object from a repository, given a prefix of its
41 * identifier (short id).
43 * The returned object should be released with `git_commit_free` when no
46 * @see git_object_lookup_prefix
48 * @param commit pointer to the looked up commit
49 * @param repo the repo to use when locating the commit.
50 * @param id identity of the commit to locate. If the object is
51 * an annotated tag it will be peeled back to the commit.
52 * @param len the length of the short identifier
53 * @return 0 or an error code
55 GIT_EXTERN(int) git_commit_lookup_prefix(
56 git_commit
**commit
, git_repository
*repo
, const git_oid
*id
, size_t len
);
59 * Close an open commit
61 * This is a wrapper around git_object_free()
64 * It *is* necessary to call this method when you stop
65 * using a commit. Failure to do so will cause a memory leak.
67 * @param commit the commit to close
70 GIT_EXTERN(void) git_commit_free(git_commit
*commit
);
73 * Get the id of a commit.
75 * @param commit a previously loaded commit.
76 * @return object identity for the commit.
78 GIT_EXTERN(const git_oid
*) git_commit_id(const git_commit
*commit
);
81 * Get the repository that contains the commit.
83 * @param commit A previously loaded commit.
84 * @return Repository that contains this commit.
86 GIT_EXTERN(git_repository
*) git_commit_owner(const git_commit
*commit
);
89 * Get the encoding for the message of a commit,
90 * as a string representing a standard encoding name.
92 * The encoding may be NULL if the `encoding` header
93 * in the commit is missing; in that case UTF-8 is assumed.
95 * @param commit a previously loaded commit.
96 * @return NULL, or the encoding
98 GIT_EXTERN(const char *) git_commit_message_encoding(const git_commit
*commit
);
101 * Get the full message of a commit.
103 * The returned message will be slightly prettified by removing any
104 * potential leading newlines.
106 * @param commit a previously loaded commit.
107 * @return the message of a commit
109 GIT_EXTERN(const char *) git_commit_message(const git_commit
*commit
);
112 * Get the full raw message of a commit.
114 * @param commit a previously loaded commit.
115 * @return the raw message of a commit
117 GIT_EXTERN(const char *) git_commit_message_raw(const git_commit
*commit
);
120 * Get the short "summary" of the git commit message.
122 * The returned message is the summary of the commit, comprising the
123 * first paragraph of the message with whitespace trimmed and squashed.
125 * @param commit a previously loaded commit.
126 * @return the summary of a commit or NULL on error
128 GIT_EXTERN(const char *) git_commit_summary(git_commit
*commit
);
131 * Get the long "body" of the git commit message.
133 * The returned message is the body of the commit, comprising
134 * everything but the first paragraph of the message. Leading and
135 * trailing whitespaces are trimmed.
137 * @param commit a previously loaded commit.
138 * @return the body of a commit or NULL when no the message only
139 * consists of a summary
141 GIT_EXTERN(const char *) git_commit_body(git_commit
*commit
);
144 * Get the commit time (i.e. committer time) of a commit.
146 * @param commit a previously loaded commit.
147 * @return the time of a commit
149 GIT_EXTERN(git_time_t
) git_commit_time(const git_commit
*commit
);
152 * Get the commit timezone offset (i.e. committer's preferred timezone) of a commit.
154 * @param commit a previously loaded commit.
155 * @return positive or negative timezone offset, in minutes from UTC
157 GIT_EXTERN(int) git_commit_time_offset(const git_commit
*commit
);
160 * Get the committer of a commit.
162 * @param commit a previously loaded commit.
163 * @return the committer of a commit
165 GIT_EXTERN(const git_signature
*) git_commit_committer(const git_commit
*commit
);
168 * Get the author of a commit.
170 * @param commit a previously loaded commit.
171 * @return the author of a commit
173 GIT_EXTERN(const git_signature
*) git_commit_author(const git_commit
*commit
);
176 * Get the full raw text of the commit header.
178 * @param commit a previously loaded commit
179 * @return the header text of the commit
181 GIT_EXTERN(const char *) git_commit_raw_header(const git_commit
*commit
);
184 * Get the tree pointed to by a commit.
186 * @param tree_out pointer where to store the tree object
187 * @param commit a previously loaded commit.
188 * @return 0 or an error code
190 GIT_EXTERN(int) git_commit_tree(git_tree
**tree_out
, const git_commit
*commit
);
193 * Get the id of the tree pointed to by a commit. This differs from
194 * `git_commit_tree` in that no attempts are made to fetch an object
197 * @param commit a previously loaded commit.
198 * @return the id of tree pointed to by commit.
200 GIT_EXTERN(const git_oid
*) git_commit_tree_id(const git_commit
*commit
);
203 * Get the number of parents of this commit
205 * @param commit a previously loaded commit.
206 * @return integer of count of parents
208 GIT_EXTERN(unsigned int) git_commit_parentcount(const git_commit
*commit
);
211 * Get the specified parent of the commit.
213 * @param out Pointer where to store the parent commit
214 * @param commit a previously loaded commit.
215 * @param n the position of the parent (from 0 to `parentcount`)
216 * @return 0 or an error code
218 GIT_EXTERN(int) git_commit_parent(
220 const git_commit
*commit
,
224 * Get the oid of a specified parent for a commit. This is different from
225 * `git_commit_parent`, which will attempt to load the parent commit from
228 * @param commit a previously loaded commit.
229 * @param n the position of the parent (from 0 to `parentcount`)
230 * @return the id of the parent, NULL on error.
232 GIT_EXTERN(const git_oid
*) git_commit_parent_id(
233 const git_commit
*commit
,
237 * Get the commit object that is the <n>th generation ancestor
238 * of the named commit object, following only the first parents.
239 * The returned commit has to be freed by the caller.
241 * Passing `0` as the generation number returns another instance of the
242 * base commit itself.
244 * @param ancestor Pointer where to store the ancestor commit
245 * @param commit a previously loaded commit.
246 * @param n the requested generation
247 * @return 0 on success; GIT_ENOTFOUND if no matching ancestor exists
250 GIT_EXTERN(int) git_commit_nth_gen_ancestor(
251 git_commit
**ancestor
,
252 const git_commit
*commit
,
256 * Get an arbitrary header field
258 * @param out the buffer to fill
259 * @param commit the commit to look in
260 * @param field the header field to return
261 * @return 0 on succeess, GIT_ENOTFOUND if the field does not exist,
264 GIT_EXTERN(int) git_commit_header_field(git_buf
*out
, const git_commit
*commit
, const char *field
);
267 * Extract the signature from a commit
269 * If the id is not for a commit, the error class will be
270 * `GITERR_INVALID`. If the commit does not have a signature, the
271 * error class will be `GITERR_OBJECT`.
273 * @param signature the signature block
274 * @param signed_data signed data; this is the commit contents minus the signature block
275 * @param repo the repository in which the commit exists
276 * @param commit_id the commit from which to extract the data
277 * @param field the name of the header field containing the signature
278 * block; pass `NULL` to extract the default 'gpgsig'
279 * @return 0 on success, GIT_ENOTFOUND if the id is not for a commit
280 * or the commit does not have a signature.
282 GIT_EXTERN(int) git_commit_extract_signature(git_buf
*signature
, git_buf
*signed_data
, git_repository
*repo
, git_oid
*commit_id
, const char *field
);
285 * Create new commit in the repository from a list of `git_object` pointers
287 * The message will **not** be cleaned up automatically. You can do that
288 * with the `git_message_prettify()` function.
290 * @param id Pointer in which to store the OID of the newly created commit
292 * @param repo Repository where to store the commit
294 * @param update_ref If not NULL, name of the reference that
295 * will be updated to point to this commit. If the reference
296 * is not direct, it will be resolved to a direct reference.
297 * Use "HEAD" to update the HEAD of the current branch and
298 * make it point to this commit. If the reference doesn't
299 * exist yet, it will be created. If it does exist, the first
300 * parent must be the tip of this branch.
302 * @param author Signature with author and author time of commit
304 * @param committer Signature with committer and * commit time of commit
306 * @param message_encoding The encoding for the message in the
307 * commit, represented with a standard encoding name.
308 * E.g. "UTF-8". If NULL, no encoding header is written and
311 * @param message Full message for this commit
313 * @param tree An instance of a `git_tree` object that will
314 * be used as the tree for the commit. This tree object must
315 * also be owned by the given `repo`.
317 * @param parent_count Number of parents for this commit
319 * @param parents Array of `parent_count` pointers to `git_commit`
320 * objects that will be used as the parents for this commit. This
321 * array may be NULL if `parent_count` is 0 (root commit). All the
322 * given commits must be owned by the `repo`.
324 * @return 0 or an error code
325 * The created commit will be written to the Object Database and
326 * the given reference will be updated to point to it
328 GIT_EXTERN(int) git_commit_create(
330 git_repository
*repo
,
331 const char *update_ref
,
332 const git_signature
*author
,
333 const git_signature
*committer
,
334 const char *message_encoding
,
336 const git_tree
*tree
,
338 const git_commit
*parents
[]);
341 * Create new commit in the repository using a variable argument list.
343 * The message will **not** be cleaned up automatically. You can do that
344 * with the `git_message_prettify()` function.
346 * The parents for the commit are specified as a variable list of pointers
347 * to `const git_commit *`. Note that this is a convenience method which may
348 * not be safe to export for certain languages or compilers
350 * All other parameters remain the same as `git_commit_create()`.
352 * @see git_commit_create
354 GIT_EXTERN(int) git_commit_create_v(
356 git_repository
*repo
,
357 const char *update_ref
,
358 const git_signature
*author
,
359 const git_signature
*committer
,
360 const char *message_encoding
,
362 const git_tree
*tree
,
367 * Amend an existing commit by replacing only non-NULL values.
369 * This creates a new commit that is exactly the same as the old commit,
370 * except that any non-NULL values will be updated. The new commit has
371 * the same parents as the old commit.
373 * The `update_ref` value works as in the regular `git_commit_create()`,
374 * updating the ref to point to the newly rewritten commit. If you want
375 * to amend a commit that is not currently the tip of the branch and then
376 * rewrite the following commits to reach a ref, pass this as NULL and
377 * update the rest of the commit chain and ref separately.
379 * Unlike `git_commit_create()`, the `author`, `committer`, `message`,
380 * `message_encoding`, and `tree` parameters can be NULL in which case this
381 * will use the values from the original `commit_to_amend`.
383 * All parameters have the same meanings as in `git_commit_create()`.
385 * @see git_commit_create
387 GIT_EXTERN(int) git_commit_amend(
389 const git_commit
*commit_to_amend
,
390 const char *update_ref
,
391 const git_signature
*author
,
392 const git_signature
*committer
,
393 const char *message_encoding
,
395 const git_tree
*tree
);
398 * Create a commit and write it into a buffer
400 * Create a commit as with `git_commit_create()` but instead of
401 * writing it to the objectdb, write the contents of the object into a
404 * @param out the buffer into which to write the commit object content
406 * @param repo Repository where the referenced tree and parents live
408 * @param author Signature with author and author time of commit
410 * @param committer Signature with committer and * commit time of commit
412 * @param message_encoding The encoding for the message in the
413 * commit, represented with a standard encoding name.
414 * E.g. "UTF-8". If NULL, no encoding header is written and
417 * @param message Full message for this commit
419 * @param tree An instance of a `git_tree` object that will
420 * be used as the tree for the commit. This tree object must
421 * also be owned by the given `repo`.
423 * @param parent_count Number of parents for this commit
425 * @param parents Array of `parent_count` pointers to `git_commit`
426 * objects that will be used as the parents for this commit. This
427 * array may be NULL if `parent_count` is 0 (root commit). All the
428 * given commits must be owned by the `repo`.
430 * @return 0 or an error code
432 GIT_EXTERN(int) git_commit_create_buffer(
434 git_repository
*repo
,
435 const git_signature
*author
,
436 const git_signature
*committer
,
437 const char *message_encoding
,
439 const git_tree
*tree
,
441 const git_commit
*parents
[]);
444 * Create a commit object from the given buffer and signature
446 * Given the unsigned commit object's contents, its signature and the
447 * header field in which to store the signature, attach the signature
448 * to the commit and write it into the given repository.
450 * @param out the resulting commit id
451 * @param commit_content the content of the unsigned commit object
452 * @param signature the signature to add to the commit
453 * @param signature_field which header field should contain this
454 * signature. Leave `NULL` for the default of "gpgsig"
455 * @return 0 or an error code
457 GIT_EXTERN(int) git_commit_create_with_signature(
459 git_repository
*repo
,
460 const char *commit_content
,
461 const char *signature
,
462 const char *signature_field
);
465 * Create an in-memory copy of a commit. The copy must be explicitly
466 * free'd or it will leak.
468 * @param out Pointer to store the copy of the commit
469 * @param source Original commit to copy
471 GIT_EXTERN(int) git_commit_dup(git_commit
**out
, git_commit
*source
);