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