blob: 154c0e34ff7d2dbaddcfb66b74d26697ffba6381 [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"
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -07008
9struct commit_list {
10 struct commit *item;
11 struct commit_list *next;
12};
13
14struct commit {
15 struct object object;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -070016 void *util;
Linus Torvalds23c17d42007-11-02 13:32:58 -070017 unsigned int indegree;
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070018 unsigned long date;
19 struct commit_list *parents;
20 struct tree *tree;
Linus Torvaldsbd1e17e2005-05-25 19:26:28 -070021 char *buffer;
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070022};
23
Linus Torvalds60ab26d2005-09-15 14:43:17 -070024extern int save_commit_buffer;
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070025extern const char *commit_type;
26
Linus Torvaldsca135e72007-04-16 16:05:10 -070027/* While we can decorate any object with a name, it's only used for commits.. */
28extern struct decoration name_decoration;
29struct name_decoration {
30 struct name_decoration *next;
Nazri Ramliyeb3005e2010-06-19 09:37:33 +080031 int type;
Linus Torvaldsca135e72007-04-16 16:05:10 -070032 char name[1];
33};
34
Jason McMullan5d6ccf52005-06-03 11:05:39 -040035struct commit *lookup_commit(const unsigned char *sha1);
36struct commit *lookup_commit_reference(const unsigned char *sha1);
Junio C Hamanof76412e2005-08-21 02:51:10 -070037struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
38 int quiet);
Pat Notza6fa5992010-11-02 13:59:07 -060039struct commit *lookup_commit_reference_by_name(const char *name);
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070040
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100041/*
42 * Look up object named by "sha1", dereference tag as necessary,
43 * get a commit and return it. If "sha1" does not dereference to
44 * a commit, use ref_name to report an error and die.
45 */
46struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name);
47
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +070048int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size);
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070049int parse_commit(struct commit *item);
50
Christian Couder11af2aa2010-07-22 15:18:30 +020051/* Find beginning and length of commit subject. */
52int find_commit_subject(const char *commit_buffer, const char **subject);
53
Thiago Farina47e44ed2010-11-26 23:58:14 -020054struct commit_list *commit_list_insert(struct commit *item,
55 struct commit_list **list);
Miklos Vajna65319472008-06-27 18:21:55 +020056unsigned commit_list_count(const struct commit_list *l);
Thiago Farina47e44ed2010-11-26 23:58:14 -020057struct commit_list *commit_list_insert_by_date(struct commit *item,
58 struct commit_list **list);
59void commit_list_sort_by_date(struct commit_list **list);
Daniel Barkalowdd97f852005-04-23 18:47:23 -070060
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -070061void free_commit_list(struct commit_list *list);
62
Linus Torvalds000182e2005-06-05 09:02:03 -070063/* Commit formats */
64enum cmit_fmt {
65 CMIT_FMT_RAW,
66 CMIT_FMT_MEDIUM,
67 CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM,
Linus Torvalds9b66ec02005-06-26 17:50:46 -070068 CMIT_FMT_SHORT,
69 CMIT_FMT_FULL,
Junio C Hamanoff56fe12005-11-09 22:15:27 -080070 CMIT_FMT_FULLER,
Junio C Hamanod87449c2005-08-08 22:15:40 -070071 CMIT_FMT_ONELINE,
Junio C Hamano3eefc182006-04-18 16:45:27 -070072 CMIT_FMT_EMAIL,
Johannes Schindeline52a5de2007-02-23 01:35:03 +010073 CMIT_FMT_USERFORMAT,
Junio C Hamano6b9c58f2006-04-15 23:46:36 -070074
Gary V. Vaughan4b055482010-05-14 09:31:35 +000075 CMIT_FMT_UNSPECIFIED
Linus Torvalds000182e2005-06-05 09:02:03 -070076};
77
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050078struct pretty_print_context {
Jeff King6bf13942011-05-26 18:27:49 -040079 enum cmit_fmt fmt;
Thomas Rastdd2e7942009-10-19 17:48:08 +020080 int abbrev;
81 const char *subject;
82 const char *after_subject;
Jeff King9553d2b2011-05-26 18:28:17 -040083 int preserve_subject;
Thomas Rastdd2e7942009-10-19 17:48:08 +020084 enum date_mode date_mode;
85 int need_8bit_cte;
Junio C Hamano66b2ed02010-01-20 13:59:36 -080086 int show_notes;
Thomas Rast8f8f5472009-10-19 17:48:10 +020087 struct reflog_walk_info *reflog_info;
Pat Notz177b29d2010-11-02 13:59:08 -060088 const char *output_encoding;
Thomas Rastdd2e7942009-10-19 17:48:08 +020089};
90
Johannes Gilger5b163602010-04-13 22:31:12 +020091struct userformat_want {
92 unsigned notes:1;
93};
94
Johannes Schindelin28e9cf62009-08-10 18:22:18 +020095extern int has_non_ascii(const char *text);
Junio C Hamano4da45be2008-04-07 17:11:34 -070096struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
Pat Notz177b29d2010-11-02 13:59:08 -060097extern char *logmsg_reencode(const struct commit *commit,
98 const char *output_encoding);
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +040099extern char *reencode_commit_message(const struct commit *commit,
100 const char **encoding_p);
Junio C Hamano4da45be2008-04-07 17:11:34 -0700101extern void get_commit_format(const char *arg, struct rev_info *);
Ramsay Jonesc8f14442011-04-07 19:26:23 +0100102extern const char *format_subject(struct strbuf *sb, const char *msg,
103 const char *line_separator);
Johannes Gilger5b163602010-04-13 22:31:12 +0200104extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
Pierre Habouzit674d1722007-09-10 12:35:06 +0200105extern void format_commit_message(const struct commit *commit,
Junio C Hamano7f98ebc2009-10-15 22:59:41 -0700106 const char *format, struct strbuf *sb,
Thomas Rastdd2e7942009-10-19 17:48:08 +0200107 const struct pretty_print_context *context);
Jeff King6bf13942011-05-26 18:27:49 -0400108extern void pretty_print_commit(const struct pretty_print_context *pp,
109 const struct commit *commit,
110 struct strbuf *sb);
Jeff King8b8a5372011-05-26 18:27:24 -0400111extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
112 struct strbuf *sb);
Jeff King6bf13942011-05-26 18:27:49 -0400113void pp_user_info(const struct pretty_print_context *pp,
114 const char *what, struct strbuf *sb,
115 const char *line, const char *encoding);
116void pp_title_line(const struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500117 const char **msg_p,
118 struct strbuf *sb,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500119 const char *encoding,
Junio C Hamano267123b2008-03-15 00:09:20 -0700120 int need_8bit_cte);
Jeff King6bf13942011-05-26 18:27:49 -0400121void pp_remainder(const struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500122 const char **msg_p,
123 struct strbuf *sb,
124 int indent);
125
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700126
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700127/** Removes the first commit from a list sorted by date, and adds all
128 * of its parents.
129 **/
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700130struct commit *pop_most_recent_commit(struct commit_list **list,
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700131 unsigned int mark);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700132
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000133struct commit *pop_commit(struct commit_list **stack);
134
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800135void clear_commit_marks(struct commit *commit, unsigned int mark);
René Scharfe86a0a402011-10-01 18:16:08 +0200136void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark);
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800137
Jon Seymourab580ac2005-07-07 02:39:34 +1000138/*
139 * Performs an in-place topological sort of list supplied.
140 *
Jon Seymourab580ac2005-07-07 02:39:34 +1000141 * invariant of resulting list is:
142 * a reachable from b => ord(b) < ord(a)
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800143 * in addition, when lifo == 0, commits on parallel tracks are
144 * sorted in the dates order.
Jon Seymourab580ac2005-07-07 02:39:34 +1000145 */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800146void sort_in_topological_order(struct commit_list ** list, int lifo);
Junio C Hamano5040f172006-04-06 23:58:51 -0700147
148struct commit_graft {
149 unsigned char sha1[20];
Johannes Schindelined09aef2006-10-30 20:09:06 +0100150 int nr_parent; /* < 0 if shallow commit */
Junio C Hamano5040f172006-04-06 23:58:51 -0700151 unsigned char parent[FLEX_ARRAY][20]; /* more */
152};
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700153typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
Junio C Hamano5040f172006-04-06 23:58:51 -0700154
155struct commit_graft *read_graft_line(char *buf, int len);
156int register_commit_graft(struct commit_graft *, int);
Martin Koegler45163382008-02-25 22:46:07 +0100157struct commit_graft *lookup_commit_graft(const unsigned char *sha1);
Junio C Hamano5040f172006-04-06 23:58:51 -0700158
Rene Scharfec0fa8252006-07-02 11:49:38 +0200159extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2, int cleanup);
Christian Couder53eda892008-07-30 07:04:14 +0200160extern struct commit_list *get_merge_bases_many(struct commit *one, int n, struct commit **twos, int cleanup);
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200161extern struct commit_list *get_octopus_merge_bases(struct commit_list *in);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200162
Johannes Schindelined09aef2006-10-30 20:09:06 +0100163extern int register_shallow(const unsigned char *sha1);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100164extern int unregister_shallow(const unsigned char *sha1);
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700165extern int for_each_commit_graft(each_commit_graft_fn, void *);
Junio C Hamanof43117a2007-01-21 22:22:23 -0800166extern int is_repository_shallow(void);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100167extern struct commit_list *get_shallow_commits(struct object_array *heads,
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100168 int depth, int shallow_flag, int not_shallow_flag);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100169
Jake Goulding7fcdb362009-01-26 09:13:24 -0500170int is_descendant_of(struct commit *, struct commit_list *);
Junio C Hamano03840fc2007-01-08 23:22:31 -0800171int in_merge_bases(struct commit *, struct commit **, int);
Kristian Høgsberg58680162007-09-17 20:06:44 -0400172
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700173extern int interactive_add(int argc, const char **argv, const char *prefix, int patch);
Thomas Rast46b51392009-08-13 14:29:41 +0200174extern int run_add_interactive(const char *revision, const char *patch_mode,
175 const char **pathspec);
Kristian Høgsberg58680162007-09-17 20:06:44 -0400176
Linus Torvalds53b2c822007-11-05 13:22:34 -0800177static inline int single_parent(struct commit *commit)
178{
179 return commit->parents && !commit->parents->next;
180}
181
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200182struct commit_list *reduce_heads(struct commit_list *heads);
183
Junio C Hamano5231c632011-11-07 16:21:32 -0800184struct commit_extra_header {
185 struct commit_extra_header *next;
186 char *key;
187 char *value;
188 size_t len;
189};
190
191extern void append_merge_tag_headers(struct commit_list *parents,
192 struct commit_extra_header ***tail);
193
Nguyễn Thái Ngọc Duy13f8b722011-12-15 20:47:22 +0700194extern int commit_tree(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -0800195 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700196 const char *author, const char *sign_commit);
Junio C Hamano5231c632011-11-07 16:21:32 -0800197
Junio C Hamanof35ccd92011-12-22 11:27:26 -0800198extern int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -0800199 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700200 const char *author, const char *sign_commit,
Junio C Hamano5231c632011-11-07 16:21:32 -0800201 struct commit_extra_header *);
202
Junio C Hamanoc871a1d2012-01-05 10:54:14 -0800203extern struct commit_extra_header *read_commit_extra_headers(struct commit *, const char **);
204extern struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -0800205
Junio C Hamano5231c632011-11-07 16:21:32 -0800206extern void free_commit_extra_headers(struct commit_extra_header *extra);
Jeff King40d52ff2010-04-01 20:05:23 -0400207
Junio C Hamanoae8e4c92011-11-07 13:26:22 -0800208struct merge_remote_desc {
209 struct object *obj; /* the named object, could be a tag */
210 const char *name;
211};
212#define merge_remote_util(commit) ((struct merge_remote_desc *)((commit)->util))
213
214/*
215 * Given "name" from the command line to merge, find the commit object
216 * and return it, while storing merge_remote_desc in its ->util field,
217 * to allow callers to tell if we are told to merge a tag.
218 */
219struct commit *get_merge_parent(const char *name);
220
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700221extern int parse_signed_commit(const unsigned char *sha1,
222 struct strbuf *message, struct strbuf *signature);
Daniel Barkalow6eb8ae02005-04-18 11:39:48 -0700223#endif /* COMMIT_H */