blob: 16d9c4351395ac55d6856d8d92d24ce7064df974 [file] [log] [blame]
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -07001#ifndef COMMIT_H
2#define COMMIT_H
3
4#include "object.h"
5#include "tree.h"
Pierre Habouzit674d1722007-09-10 12:35:06 +02006#include "strbuf.h"
Linus Torvaldsca135e72007-04-16 16:05:10 -07007#include "decorate.h"
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02008#include "gpg-interface.h"
Jeff Kinga9080472013-07-03 03:08:22 -04009#include "string-list.h"
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070010
11struct commit_list {
12 struct commit *item;
13 struct commit_list *next;
14};
15
16struct commit {
17 struct object object;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -070018 void *util;
Jeff King96c4f4a2013-04-09 02:52:56 -040019 unsigned int index;
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070020 unsigned long date;
21 struct commit_list *parents;
22 struct tree *tree;
Linus Torvaldsbd1e17e2005-05-25 19:26:28 -070023 char *buffer;
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070024};
25
Linus Torvalds60ab26d2005-09-15 14:43:17 -070026extern int save_commit_buffer;
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070027extern const char *commit_type;
28
Linus Torvaldsca135e72007-04-16 16:05:10 -070029/* While we can decorate any object with a name, it's only used for commits.. */
30extern struct decoration name_decoration;
31struct name_decoration {
32 struct name_decoration *next;
Nazri Ramliyeb3005e2010-06-19 09:37:33 +080033 int type;
Linus Torvaldsca135e72007-04-16 16:05:10 -070034 char name[1];
35};
36
Jason McMullan5d6ccf52005-06-03 11:05:39 -040037struct commit *lookup_commit(const unsigned char *sha1);
38struct commit *lookup_commit_reference(const unsigned char *sha1);
Junio C Hamanof76412e2005-08-21 02:51:10 -070039struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
40 int quiet);
Pat Notza6fa5992010-11-02 13:59:07 -060041struct commit *lookup_commit_reference_by_name(const char *name);
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070042
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100043/*
44 * Look up object named by "sha1", dereference tag as necessary,
45 * get a commit and return it. If "sha1" does not dereference to
46 * a commit, use ref_name to report an error and die.
47 */
48struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name);
49
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +070050int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size);
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070051int parse_commit(struct commit *item);
Jeff King7059dcc2013-10-24 04:52:36 -040052void parse_commit_or_die(struct commit *item);
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070053
Christian Couder11af2aa2010-07-22 15:18:30 +020054/* Find beginning and length of commit subject. */
55int find_commit_subject(const char *commit_buffer, const char **subject);
56
Thiago Farina47e44ed2010-11-26 23:58:14 -020057struct commit_list *commit_list_insert(struct commit *item,
58 struct commit_list **list);
René Scharfe89b5f1d2012-04-25 22:35:27 +020059struct commit_list **commit_list_append(struct commit *commit,
60 struct commit_list **next);
Miklos Vajna65319472008-06-27 18:21:55 +020061unsigned commit_list_count(const struct commit_list *l);
Thiago Farina47e44ed2010-11-26 23:58:14 -020062struct commit_list *commit_list_insert_by_date(struct commit *item,
63 struct commit_list **list);
64void commit_list_sort_by_date(struct commit_list **list);
Daniel Barkalowdd97f852005-04-23 18:47:23 -070065
Thomas Rast53d00b32013-07-31 22:13:20 +020066/* Shallow copy of the input list */
67struct commit_list *copy_commit_list(struct commit_list *list);
68
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070069void free_commit_list(struct commit_list *list);
70
Linus Torvalds000182e2005-06-05 09:02:03 -070071/* Commit formats */
72enum cmit_fmt {
73 CMIT_FMT_RAW,
74 CMIT_FMT_MEDIUM,
75 CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM,
Linus Torvalds9b66ec02005-06-26 17:50:46 -070076 CMIT_FMT_SHORT,
77 CMIT_FMT_FULL,
Junio C Hamanoff56fe12005-11-09 22:15:27 -080078 CMIT_FMT_FULLER,
Junio C Hamanod87449c2005-08-08 22:15:40 -070079 CMIT_FMT_ONELINE,
Junio C Hamano3eefc182006-04-18 16:45:27 -070080 CMIT_FMT_EMAIL,
Johannes Schindeline52a5de2007-02-23 01:35:03 +010081 CMIT_FMT_USERFORMAT,
Junio C Hamano6b9c58f2006-04-15 23:46:36 -070082
Gary V. Vaughan4b055482010-05-14 09:31:35 +000083 CMIT_FMT_UNSPECIFIED
Linus Torvalds000182e2005-06-05 09:02:03 -070084};
85
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050086struct pretty_print_context {
Jeff King10f2fbf2013-07-03 03:07:48 -040087 /*
88 * Callers should tweak these to change the behavior of pp_* functions.
89 */
Jeff King6bf13942011-05-26 18:27:49 -040090 enum cmit_fmt fmt;
Thomas Rastdd2e7942009-10-19 17:48:08 +020091 int abbrev;
92 const char *subject;
93 const char *after_subject;
Jeff King9553d2b2011-05-26 18:28:17 -040094 int preserve_subject;
Thomas Rastdd2e7942009-10-19 17:48:08 +020095 enum date_mode date_mode;
Jeff Kingf026c752012-05-04 01:25:18 -040096 unsigned date_mode_explicit:1;
Thomas Rastdd2e7942009-10-19 17:48:08 +020097 int need_8bit_cte;
Junio C Hamanoddf333f2012-10-17 18:51:47 -070098 char *notes_message;
Thomas Rast8f8f5472009-10-19 17:48:10 +020099 struct reflog_walk_info *reflog_info;
Pat Notz177b29d2010-11-02 13:59:08 -0600100 const char *output_encoding;
Antoine Pelisse0e2913b2013-01-05 22:26:41 +0100101 struct string_list *mailmap;
Junio C Hamano30825172012-12-17 17:56:49 -0500102 int color;
Jeff Kinga9080472013-07-03 03:08:22 -0400103 struct ident_split *from_ident;
Jeff King10f2fbf2013-07-03 03:07:48 -0400104
105 /*
106 * Fields below here are manipulated internally by pp_* functions and
107 * should not be counted on by callers.
108 */
Jeff Kinga9080472013-07-03 03:08:22 -0400109 struct string_list in_body_headers;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200110};
111
Johannes Gilger5b163602010-04-13 22:31:12 +0200112struct userformat_want {
113 unsigned notes:1;
114};
115
Johannes Schindelin28e9cf62009-08-10 18:22:18 +0200116extern int has_non_ascii(const char *text);
Junio C Hamano4da45be2008-04-07 17:11:34 -0700117struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
Pat Notz177b29d2010-11-02 13:59:08 -0600118extern char *logmsg_reencode(const struct commit *commit,
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000119 char **commit_encoding,
Pat Notz177b29d2010-11-02 13:59:08 -0600120 const char *output_encoding);
Jeff Kingdd0d3882013-01-26 04:44:06 -0500121extern void logmsg_free(char *msg, const struct commit *commit);
Junio C Hamano4da45be2008-04-07 17:11:34 -0700122extern void get_commit_format(const char *arg, struct rev_info *);
Ramsay Jonesc8f14442011-04-07 19:26:23 +0100123extern const char *format_subject(struct strbuf *sb, const char *msg,
124 const char *line_separator);
Johannes Gilger5b163602010-04-13 22:31:12 +0200125extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
Pierre Habouzit674d1722007-09-10 12:35:06 +0200126extern void format_commit_message(const struct commit *commit,
Junio C Hamano7f98ebc2009-10-15 22:59:41 -0700127 const char *format, struct strbuf *sb,
Thomas Rastdd2e7942009-10-19 17:48:08 +0200128 const struct pretty_print_context *context);
Jeff King10f2fbf2013-07-03 03:07:48 -0400129extern void pretty_print_commit(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -0400130 const struct commit *commit,
131 struct strbuf *sb);
Jeff King8b8a5372011-05-26 18:27:24 -0400132extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
133 struct strbuf *sb);
Jeff King10f2fbf2013-07-03 03:07:48 -0400134void pp_user_info(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -0400135 const char *what, struct strbuf *sb,
136 const char *line, const char *encoding);
Jeff King10f2fbf2013-07-03 03:07:48 -0400137void pp_title_line(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500138 const char **msg_p,
139 struct strbuf *sb,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500140 const char *encoding,
Junio C Hamano267123b2008-03-15 00:09:20 -0700141 int need_8bit_cte);
Jeff King10f2fbf2013-07-03 03:07:48 -0400142void pp_remainder(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500143 const char **msg_p,
144 struct strbuf *sb,
145 int indent);
146
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700147
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700148/** Removes the first commit from a list sorted by date, and adds all
149 * of its parents.
150 **/
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700151struct commit *pop_most_recent_commit(struct commit_list **list,
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700152 unsigned int mark);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700153
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000154struct commit *pop_commit(struct commit_list **stack);
155
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800156void clear_commit_marks(struct commit *commit, unsigned int mark);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800157void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark);
René Scharfe86a0a402011-10-01 18:16:08 +0200158void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark);
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800159
Junio C Hamano08f704f2013-06-06 16:07:14 -0700160
161enum rev_sort_order {
162 REV_SORT_IN_GRAPH_ORDER = 0,
Junio C Hamano81c6b382013-06-07 10:35:54 -0700163 REV_SORT_BY_COMMIT_DATE,
164 REV_SORT_BY_AUTHOR_DATE
Junio C Hamano08f704f2013-06-06 16:07:14 -0700165};
166
Jon Seymourab580ac2005-07-07 02:39:34 +1000167/*
168 * Performs an in-place topological sort of list supplied.
169 *
Jon Seymourab580ac2005-07-07 02:39:34 +1000170 * invariant of resulting list is:
171 * a reachable from b => ord(b) < ord(a)
Junio C Hamano08f704f2013-06-06 16:07:14 -0700172 * sort_order further specifies:
173 * REV_SORT_IN_GRAPH_ORDER: try to show a commit on a single-parent
174 * chain together.
175 * REV_SORT_BY_COMMIT_DATE: show eligible commits in committer-date order.
Jon Seymourab580ac2005-07-07 02:39:34 +1000176 */
Junio C Hamano08f704f2013-06-06 16:07:14 -0700177void sort_in_topological_order(struct commit_list **, enum rev_sort_order);
Junio C Hamano5040f172006-04-06 23:58:51 -0700178
179struct commit_graft {
180 unsigned char sha1[20];
Johannes Schindelined09aef2006-10-30 20:09:06 +0100181 int nr_parent; /* < 0 if shallow commit */
Junio C Hamano5040f172006-04-06 23:58:51 -0700182 unsigned char parent[FLEX_ARRAY][20]; /* more */
183};
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700184typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
Junio C Hamano5040f172006-04-06 23:58:51 -0700185
186struct commit_graft *read_graft_line(char *buf, int len);
187int register_commit_graft(struct commit_graft *, int);
Martin Koegler45163382008-02-25 22:46:07 +0100188struct commit_graft *lookup_commit_graft(const unsigned char *sha1);
Junio C Hamano5040f172006-04-06 23:58:51 -0700189
Rene Scharfec0fa8252006-07-02 11:49:38 +0200190extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2, int cleanup);
Christian Couder53eda892008-07-30 07:04:14 +0200191extern struct commit_list *get_merge_bases_many(struct commit *one, int n, struct commit **twos, int cleanup);
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200192extern struct commit_list *get_octopus_merge_bases(struct commit_list *in);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200193
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +0200194/* largest positive number a signed 32-bit integer can contain */
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +0700195#define INFINITE_DEPTH 0x7fffffff
196
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700197struct sha1_array;
198struct ref;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100199extern int register_shallow(const unsigned char *sha1);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100200extern int unregister_shallow(const unsigned char *sha1);
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700201extern int for_each_commit_graft(each_commit_graft_fn, void *);
Junio C Hamanof43117a2007-01-21 22:22:23 -0800202extern int is_repository_shallow(void);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100203extern struct commit_list *get_shallow_commits(struct object_array *heads,
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100204 int depth, int shallow_flag, int not_shallow_flag);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700205extern void check_shallow_file_for_update(void);
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700206extern void set_alternate_shallow_file(const char *path, int override);
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700207extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
208 const struct sha1_array *extra);
Nguyễn Thái Ngọc Duy3125fe52013-08-16 16:52:02 +0700209extern void setup_alternate_shallow(struct lock_file *shallow_lock,
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700210 const char **alternate_shallow_file,
211 const struct sha1_array *extra);
212extern char *setup_temporary_shallow(const struct sha1_array *extra);
Nguyễn Thái Ngọc Duyad491362013-12-05 20:02:32 +0700213extern void advertise_shallow_grafts(int);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100214
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700215struct shallow_info {
216 struct sha1_array *shallow;
217 int *ours, nr_ours;
218 int *theirs, nr_theirs;
219 struct sha1_array *ref;
Nguyễn Thái Ngọc Duy0a1bc122013-12-05 20:02:47 +0700220
221 /* for receive-pack */
222 uint32_t **used_shallow;
223 int *need_reachability_test;
224 int *reachable;
225 int *shallow_ref;
226 struct commit **commits;
227 int nr_commits;
Nguyễn Thái Ngọc Duy58babff2013-12-05 20:02:35 +0700228};
229
230extern void prepare_shallow_info(struct shallow_info *, struct sha1_array *);
231extern void clear_shallow_info(struct shallow_info *);
232extern void remove_nonexistent_theirs_shallow(struct shallow_info *);
Nguyễn Thái Ngọc Duy8e277382013-12-05 20:02:36 +0700233extern void assign_shallow_commits_to_refs(struct shallow_info *info,
234 uint32_t **used,
235 int *ref_status);
Nguyễn Thái Ngọc Duy0a1bc122013-12-05 20:02:47 +0700236extern int delayed_reachability_test(struct shallow_info *si, int c);
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700237extern void prune_shallow(int show_only);
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -0700238
Jake Goulding7fcdb362009-01-26 09:13:24 -0500239int is_descendant_of(struct commit *, struct commit_list *);
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700240int in_merge_bases(struct commit *, struct commit *);
Junio C Hamano4c4b27e2013-03-04 10:16:42 -0800241int in_merge_bases_many(struct commit *, int, struct commit **);
Kristian Høgsberg58680162007-09-17 20:06:44 -0400242
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700243extern int interactive_add(int argc, const char **argv, const char *prefix, int patch);
Thomas Rast46b51392009-08-13 14:29:41 +0200244extern int run_add_interactive(const char *revision, const char *patch_mode,
Nguyễn Thái Ngọc Duy480ca642013-07-14 15:35:50 +0700245 const struct pathspec *pathspec);
Kristian Høgsberg58680162007-09-17 20:06:44 -0400246
Linus Torvalds53b2c822007-11-05 13:22:34 -0800247static inline int single_parent(struct commit *commit)
248{
249 return commit->parents && !commit->parents->next;
250}
251
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200252struct commit_list *reduce_heads(struct commit_list *heads);
253
Junio C Hamano5231c632011-11-07 16:21:32 -0800254struct commit_extra_header {
255 struct commit_extra_header *next;
256 char *key;
257 char *value;
258 size_t len;
259};
260
261extern void append_merge_tag_headers(struct commit_list *parents,
262 struct commit_extra_header ***tail);
263
Nguyễn Thái Ngọc Duy8785b762013-12-22 09:56:41 +0700264extern int commit_tree(const struct strbuf *msg, const unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -0800265 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700266 const char *author, const char *sign_commit);
Junio C Hamano5231c632011-11-07 16:21:32 -0800267
Nguyễn Thái Ngọc Duy8785b762013-12-22 09:56:41 +0700268extern int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -0800269 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700270 const char *author, const char *sign_commit,
Junio C Hamano5231c632011-11-07 16:21:32 -0800271 struct commit_extra_header *);
272
Junio C Hamanoc871a1d2012-01-05 10:54:14 -0800273extern struct commit_extra_header *read_commit_extra_headers(struct commit *, const char **);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -0800274
Junio C Hamano5231c632011-11-07 16:21:32 -0800275extern void free_commit_extra_headers(struct commit_extra_header *extra);
Jeff King40d52ff2010-04-01 20:05:23 -0400276
Junio C Hamanoae8e4c92011-11-07 13:26:22 -0800277struct merge_remote_desc {
278 struct object *obj; /* the named object, could be a tag */
279 const char *name;
280};
281#define merge_remote_util(commit) ((struct merge_remote_desc *)((commit)->util))
282
283/*
284 * Given "name" from the command line to merge, find the commit object
285 * and return it, while storing merge_remote_desc in its ->util field,
286 * to allow callers to tell if we are told to merge a tag.
287 */
288struct commit *get_merge_parent(const char *name);
289
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700290extern int parse_signed_commit(const unsigned char *sha1,
291 struct strbuf *message, struct strbuf *signature);
Nguyễn Thái Ngọc Duyefc7df42012-10-26 22:53:51 +0700292extern void print_commit_list(struct commit_list *list,
293 const char *format_cur,
294 const char *format_last);
295
Sebastian Götteffb6d7d2013-03-31 18:00:14 +0200296/*
Sebastian Götteeb307ae2013-03-31 18:02:46 +0200297 * Check the signature of the given commit. The result of the check is stored
298 * in sig->check_result, 'G' for a good signature, 'U' for a good signature
299 * from an untrusted signer, 'B' for a bad signature and 'N' for no signature
300 * at all. This may allocate memory for sig->gpg_output, sig->gpg_status,
301 * sig->signer and sig->key.
Sebastian Götteffb6d7d2013-03-31 18:00:14 +0200302 */
303extern void check_commit_signature(const struct commit* commit, struct signature_check *sigc);
304
Jeff King727377f2013-07-02 02:21:48 -0400305int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
306
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -0700307#endif /* COMMIT_H */