Elijah Newren | 5e3f94d | 2023-04-22 20:17:23 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 2 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 3 | #include "gettext.h" |
Kousik Sanagavarapu | f46094a | 2023-07-23 21:49:58 +0530 | [diff] [blame] | 4 | #include "config.h" |
Elijah Newren | d4a4f92 | 2023-04-22 20:17:26 +0000 | [diff] [blame] | 5 | #include "gpg-interface.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 6 | #include "hex.h" |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 7 | #include "parse-options.h" |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 8 | #include "run-command.h" |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 9 | #include "refs.h" |
| 10 | #include "wildmatch.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 11 | #include "object-name.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 12 | #include "object-store-ll.h" |
Elijah Newren | 6f2d743 | 2023-04-11 03:00:42 +0000 | [diff] [blame] | 13 | #include "oid-array.h" |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 14 | #include "repository.h" |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 15 | #include "commit.h" |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 16 | #include "mailmap.h" |
| 17 | #include "ident.h" |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 18 | #include "remote.h" |
| 19 | #include "color.h" |
| 20 | #include "tag.h" |
| 21 | #include "quote.h" |
| 22 | #include "ref-filter.h" |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 23 | #include "revision.h" |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 24 | #include "utf8.h" |
Elijah Newren | 3467663 | 2023-04-22 20:17:17 +0000 | [diff] [blame] | 25 | #include "versioncmp.h" |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 26 | #include "trailer.h" |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 27 | #include "wt-status.h" |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 28 | #include "commit-slab.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 29 | #include "commit-reach.h" |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 30 | #include "worktree.h" |
| 31 | #include "hashmap.h" |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 32 | |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 33 | static struct ref_msg { |
| 34 | const char *gone; |
| 35 | const char *ahead; |
| 36 | const char *behind; |
| 37 | const char *ahead_behind; |
| 38 | } msgs = { |
| 39 | /* Untranslated plumbing messages: */ |
| 40 | "gone", |
| 41 | "ahead %d", |
| 42 | "behind %d", |
| 43 | "ahead %d, behind %d" |
| 44 | }; |
| 45 | |
| 46 | void setup_ref_filter_porcelain_msg(void) |
| 47 | { |
| 48 | msgs.gone = _("gone"); |
| 49 | msgs.ahead = _("ahead %d"); |
| 50 | msgs.behind = _("behind %d"); |
| 51 | msgs.ahead_behind = _("ahead %d, behind %d"); |
| 52 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 53 | |
| 54 | typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 55 | typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 56 | typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 57 | |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 58 | struct align { |
| 59 | align_type position; |
| 60 | unsigned int width; |
| 61 | }; |
| 62 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 63 | struct if_then_else { |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 64 | cmp_status cmp_status; |
| 65 | const char *str; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 66 | unsigned int then_atom_seen : 1, |
| 67 | else_atom_seen : 1, |
| 68 | condition_satisfied : 1; |
| 69 | }; |
| 70 | |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 71 | struct refname_atom { |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 72 | enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option; |
| 73 | int lstrip, rstrip; |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 74 | }; |
| 75 | |
Hariom Verma | ee82a48 | 2021-02-13 01:52:43 +0000 | [diff] [blame] | 76 | static struct ref_trailer_buf { |
| 77 | struct string_list filter_list; |
| 78 | struct strbuf sepbuf; |
| 79 | struct strbuf kvsepbuf; |
| 80 | } ref_trailer_buf = {STRING_LIST_INIT_NODUP, STRBUF_INIT, STRBUF_INIT}; |
| 81 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 82 | static struct expand_data { |
| 83 | struct object_id oid; |
| 84 | enum object_type type; |
| 85 | unsigned long size; |
| 86 | off_t disk_size; |
| 87 | struct object_id delta_base_oid; |
| 88 | void *content; |
| 89 | |
| 90 | struct object_info info; |
| 91 | } oi, oi_deref; |
| 92 | |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 93 | struct ref_to_worktree_entry { |
Eric Wong | e2b5038 | 2019-10-06 23:30:43 +0000 | [diff] [blame] | 94 | struct hashmap_entry ent; |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 95 | struct worktree *wt; /* key is wt->head_ref */ |
| 96 | }; |
| 97 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 98 | static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 99 | const struct hashmap_entry *eptr, |
| 100 | const struct hashmap_entry *kptr, |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 101 | const void *keydata_aka_refname) |
| 102 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 103 | const struct ref_to_worktree_entry *e, *k; |
| 104 | |
| 105 | e = container_of(eptr, const struct ref_to_worktree_entry, ent); |
| 106 | k = container_of(kptr, const struct ref_to_worktree_entry, ent); |
| 107 | |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 108 | return strcmp(e->wt->head_ref, |
| 109 | keydata_aka_refname ? keydata_aka_refname : k->wt->head_ref); |
| 110 | } |
| 111 | |
| 112 | static struct ref_to_worktree_map { |
| 113 | struct hashmap map; |
| 114 | struct worktree **worktrees; |
| 115 | } ref_to_worktree_map; |
| 116 | |
Karthik Nayak | 50cd83d | 2016-02-17 23:36:10 +0530 | [diff] [blame] | 117 | /* |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 118 | * The enum atom_type is used as the index of valid_atom array. |
| 119 | * In the atom parsing stage, it will be passed to used_atom.atom_type |
| 120 | * as the identifier of the atom type. We can check the type of used_atom |
| 121 | * entry by `if (used_atom[i].atom_type == ATOM_*)`. |
| 122 | */ |
| 123 | enum atom_type { |
| 124 | ATOM_REFNAME, |
| 125 | ATOM_OBJECTTYPE, |
| 126 | ATOM_OBJECTSIZE, |
| 127 | ATOM_OBJECTNAME, |
| 128 | ATOM_DELTABASE, |
| 129 | ATOM_TREE, |
| 130 | ATOM_PARENT, |
| 131 | ATOM_NUMPARENT, |
| 132 | ATOM_OBJECT, |
| 133 | ATOM_TYPE, |
| 134 | ATOM_TAG, |
| 135 | ATOM_AUTHOR, |
| 136 | ATOM_AUTHORNAME, |
| 137 | ATOM_AUTHOREMAIL, |
| 138 | ATOM_AUTHORDATE, |
| 139 | ATOM_COMMITTER, |
| 140 | ATOM_COMMITTERNAME, |
| 141 | ATOM_COMMITTEREMAIL, |
| 142 | ATOM_COMMITTERDATE, |
| 143 | ATOM_TAGGER, |
| 144 | ATOM_TAGGERNAME, |
| 145 | ATOM_TAGGEREMAIL, |
| 146 | ATOM_TAGGERDATE, |
| 147 | ATOM_CREATOR, |
| 148 | ATOM_CREATORDATE, |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 149 | ATOM_DESCRIBE, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 150 | ATOM_SUBJECT, |
| 151 | ATOM_BODY, |
| 152 | ATOM_TRAILERS, |
| 153 | ATOM_CONTENTS, |
Kousik Sanagavarapu | 26c9c03 | 2023-06-04 23:52:47 +0530 | [diff] [blame] | 154 | ATOM_SIGNATURE, |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 155 | ATOM_RAW, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 156 | ATOM_UPSTREAM, |
| 157 | ATOM_PUSH, |
| 158 | ATOM_SYMREF, |
| 159 | ATOM_FLAG, |
| 160 | ATOM_HEAD, |
| 161 | ATOM_COLOR, |
| 162 | ATOM_WORKTREEPATH, |
| 163 | ATOM_ALIGN, |
| 164 | ATOM_END, |
| 165 | ATOM_IF, |
| 166 | ATOM_THEN, |
| 167 | ATOM_ELSE, |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 168 | ATOM_REST, |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 169 | ATOM_AHEADBEHIND, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /* |
Karthik Nayak | 50cd83d | 2016-02-17 23:36:10 +0530 | [diff] [blame] | 173 | * An atom is a valid field atom listed below, possibly prefixed with |
| 174 | * a "*" to denote deref_tag(). |
| 175 | * |
| 176 | * We parse given format string and sort specifiers, and make a list |
| 177 | * of properties that we need to extract out of objects. ref_array_item |
| 178 | * structure will hold an array of values extracted that can be |
| 179 | * indexed with the "atom number", which is an index into this |
| 180 | * array. |
| 181 | */ |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 182 | static struct used_atom { |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 183 | enum atom_type atom_type; |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 184 | const char *name; |
| 185 | cmp_type type; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 186 | info_source source; |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 187 | union { |
| 188 | char color[COLOR_MAXLEN]; |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 189 | struct align align; |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 190 | struct { |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 191 | enum { |
J Wyman | 9700fae | 2017-11-07 17:31:08 +0100 | [diff] [blame] | 192 | RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 193 | } option; |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 194 | struct refname_atom refname; |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 195 | unsigned int nobracket : 1, push : 1, push_remote : 1; |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 196 | } remote_ref; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 197 | struct { |
Hariom Verma | 905f0a4 | 2020-08-21 21:41:50 +0000 | [diff] [blame] | 198 | enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES, |
| 199 | C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option; |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 200 | struct process_trailer_options trailer_opts; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 201 | unsigned int nlines; |
| 202 | } contents; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 203 | struct { |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 204 | enum { RAW_BARE, RAW_LENGTH } option; |
| 205 | } raw_data; |
| 206 | struct { |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 207 | cmp_status cmp_status; |
| 208 | const char *str; |
| 209 | } if_then_else; |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 210 | struct { |
| 211 | enum { O_FULL, O_LENGTH, O_SHORT } option; |
| 212 | unsigned int length; |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 213 | } oid; |
ZheNing Hu | 0caf20f | 2021-05-13 15:15:37 +0000 | [diff] [blame] | 214 | struct { |
| 215 | enum { O_SIZE, O_SIZE_DISK } option; |
| 216 | } objectsize; |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 217 | struct { |
| 218 | enum { N_RAW, N_MAILMAP } option; |
| 219 | } name_option; |
| 220 | struct { |
| 221 | enum { |
| 222 | EO_RAW = 0, |
| 223 | EO_TRIM = 1<<0, |
| 224 | EO_LOCALPART = 1<<1, |
| 225 | EO_MAILMAP = 1<<2, |
| 226 | } option; |
Hariom Verma | b82445d | 2020-08-21 21:41:43 +0000 | [diff] [blame] | 227 | } email_option; |
Kousik Sanagavarapu | 26c9c03 | 2023-06-04 23:52:47 +0530 | [diff] [blame] | 228 | struct { |
| 229 | enum { S_BARE, S_GRADE, S_SIGNER, S_KEY, |
| 230 | S_FINGERPRINT, S_PRI_KEY_FP, S_TRUST_LEVEL } option; |
| 231 | } signature; |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 232 | const char **describe_args; |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 233 | struct refname_atom refname; |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 234 | char *head; |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 235 | } u; |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 236 | } *used_atom; |
Karthik Nayak | 50cd83d | 2016-02-17 23:36:10 +0530 | [diff] [blame] | 237 | static int used_atom_cnt, need_tagged, need_symref; |
Karthik Nayak | 50cd83d | 2016-02-17 23:36:10 +0530 | [diff] [blame] | 238 | |
Olga Telezhnaya | e2e7a24 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 239 | /* |
| 240 | * Expand string, append it to strbuf *sb, then return error code ret. |
| 241 | * Allow to save few lines of code. |
| 242 | */ |
Ævar Arnfjörð Bjarmason | 48ca53c | 2021-07-13 10:05:18 +0200 | [diff] [blame] | 243 | __attribute__((format (printf, 3, 4))) |
Olga Telezhnaya | e2e7a24 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 244 | static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...) |
| 245 | { |
| 246 | va_list ap; |
| 247 | va_start(ap, fmt); |
| 248 | strbuf_vaddf(sb, fmt, ap); |
| 249 | va_end(ap); |
| 250 | return ret; |
| 251 | } |
| 252 | |
Jeff King | a33d0fa | 2022-12-14 11:19:43 -0500 | [diff] [blame] | 253 | static int err_no_arg(struct strbuf *sb, const char *name) |
| 254 | { |
Jeff King | 1955ef1 | 2022-12-14 11:23:53 -0500 | [diff] [blame] | 255 | size_t namelen = strchrnul(name, ':') - name; |
| 256 | strbuf_addf(sb, _("%%(%.*s) does not take arguments"), |
| 257 | (int)namelen, name); |
Jeff King | a33d0fa | 2022-12-14 11:19:43 -0500 | [diff] [blame] | 258 | return -1; |
| 259 | } |
| 260 | |
Jeff King | dda4fc1 | 2022-12-14 11:20:19 -0500 | [diff] [blame] | 261 | static int err_bad_arg(struct strbuf *sb, const char *name, const char *arg) |
| 262 | { |
Jeff King | 1955ef1 | 2022-12-14 11:23:53 -0500 | [diff] [blame] | 263 | size_t namelen = strchrnul(name, ':') - name; |
| 264 | strbuf_addf(sb, _("unrecognized %%(%.*s) argument: %s"), |
| 265 | (int)namelen, name, arg); |
Jeff King | dda4fc1 | 2022-12-14 11:20:19 -0500 | [diff] [blame] | 266 | return -1; |
| 267 | } |
| 268 | |
Kousik Sanagavarapu | f46094a | 2023-07-23 21:49:58 +0530 | [diff] [blame] | 269 | /* |
| 270 | * Parse option of name "candidate" in the option string "to_parse" of |
| 271 | * the form |
| 272 | * |
| 273 | * "candidate1[=val1],candidate2[=val2],candidate3[=val3],..." |
| 274 | * |
| 275 | * The remaining part of "to_parse" is stored in "end" (if we are |
| 276 | * parsing the last candidate, then this is NULL) and the value of |
| 277 | * the candidate is stored in "valuestart" and its length in "valuelen", |
| 278 | * that is the portion after "=". Since it is possible for a "candidate" |
| 279 | * to not have a value, in such cases, "valuestart" is set to point to |
| 280 | * NULL and "valuelen" to 0. |
| 281 | * |
| 282 | * The function returns 1 on success. It returns 0 if we don't find |
| 283 | * "candidate" in "to_parse" or we find "candidate" but it is followed |
| 284 | * by more chars (for example, "candidatefoo"), that is, we don't find |
| 285 | * an exact match. |
| 286 | * |
| 287 | * This function only does the above for one "candidate" at a time. So |
| 288 | * it has to be called each time trying to parse a "candidate" in the |
| 289 | * option string "to_parse". |
| 290 | */ |
| 291 | static int match_atom_arg_value(const char *to_parse, const char *candidate, |
| 292 | const char **end, const char **valuestart, |
| 293 | size_t *valuelen) |
| 294 | { |
| 295 | const char *atom; |
| 296 | |
| 297 | if (!skip_prefix(to_parse, candidate, &atom)) |
| 298 | return 0; /* definitely not "candidate" */ |
| 299 | |
| 300 | if (*atom == '=') { |
| 301 | /* we just saw "candidate=" */ |
| 302 | *valuestart = atom + 1; |
| 303 | atom = strchrnul(*valuestart, ','); |
| 304 | *valuelen = atom - *valuestart; |
| 305 | } else if (*atom != ',' && *atom != '\0') { |
| 306 | /* key begins with "candidate" but has more chars */ |
| 307 | return 0; |
| 308 | } else { |
| 309 | /* just "candidate" without "=val" */ |
| 310 | *valuestart = NULL; |
| 311 | *valuelen = 0; |
| 312 | } |
| 313 | |
| 314 | /* atom points at either the ',' or NUL after this key[=val] */ |
| 315 | if (*atom == ',') |
| 316 | atom++; |
| 317 | else if (*atom) |
| 318 | BUG("Why is *atom not NULL yet?"); |
| 319 | |
| 320 | *end = atom; |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Parse boolean option of name "candidate" in the option list "to_parse" |
| 326 | * of the form |
| 327 | * |
| 328 | * "candidate1[=bool1],candidate2[=bool2],candidate3[=bool3],..." |
| 329 | * |
| 330 | * The remaining part of "to_parse" is stored in "end" (if we are parsing |
| 331 | * the last candidate, then this is NULL) and the value (if given) is |
| 332 | * parsed and stored in "val", so "val" always points to either 0 or 1. |
| 333 | * If the value is not given, then "val" is set to point to 1. |
| 334 | * |
| 335 | * The boolean value is parsed using "git_parse_maybe_bool()", so the |
| 336 | * accepted values are |
| 337 | * |
| 338 | * to set true - "1", "yes", "true" |
| 339 | * to set false - "0", "no", "false" |
| 340 | * |
| 341 | * This function returns 1 on success. It returns 0 when we don't find |
| 342 | * an exact match for "candidate" or when the boolean value given is |
| 343 | * not valid. |
| 344 | */ |
| 345 | static int match_atom_bool_arg(const char *to_parse, const char *candidate, |
| 346 | const char **end, int *val) |
| 347 | { |
| 348 | const char *argval; |
| 349 | char *strval; |
| 350 | size_t arglen; |
| 351 | int v; |
| 352 | |
| 353 | if (!match_atom_arg_value(to_parse, candidate, end, &argval, &arglen)) |
| 354 | return 0; |
| 355 | |
| 356 | if (!argval) { |
| 357 | *val = 1; |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | strval = xstrndup(argval, arglen); |
| 362 | v = git_parse_maybe_bool(strval); |
| 363 | free(strval); |
| 364 | |
| 365 | if (v == -1) |
| 366 | return 0; |
| 367 | |
| 368 | *val = v; |
| 369 | |
| 370 | return 1; |
| 371 | } |
| 372 | |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 373 | static int color_atom_parser(struct ref_format *format, struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 374 | const char *color_value, struct strbuf *err) |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 375 | { |
| 376 | if (!color_value) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 377 | return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)")); |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 378 | if (color_parse(color_value, atom->u.color) < 0) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 379 | return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"), |
| 380 | color_value); |
Jeff King | 11b087a | 2017-07-13 11:09:32 -0400 | [diff] [blame] | 381 | /* |
| 382 | * We check this after we've parsed the color, which lets us complain |
| 383 | * about syntactically bogus color names even if they won't be used. |
| 384 | */ |
| 385 | if (!want_color(format->use_color)) |
| 386 | color_parse("", atom->u.color); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 387 | return 0; |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 388 | } |
| 389 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 390 | static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg, |
| 391 | const char *name, struct strbuf *err) |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 392 | { |
| 393 | if (!arg) |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 394 | atom->option = R_NORMAL; |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 395 | else if (!strcmp(arg, "short")) |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 396 | atom->option = R_SHORT; |
Junio C Hamano | 44a6b6c | 2017-02-07 11:50:34 -0800 | [diff] [blame] | 397 | else if (skip_prefix(arg, "lstrip=", &arg) || |
| 398 | skip_prefix(arg, "strip=", &arg)) { |
Karthik Nayak | 17938f1 | 2017-01-10 14:19:46 +0530 | [diff] [blame] | 399 | atom->option = R_LSTRIP; |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 400 | if (strtol_i(arg, 10, &atom->lstrip)) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 401 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg); |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 402 | } else if (skip_prefix(arg, "rstrip=", &arg)) { |
| 403 | atom->option = R_RSTRIP; |
| 404 | if (strtol_i(arg, 10, &atom->rstrip)) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 405 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg); |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 406 | } else |
Jeff King | 1955ef1 | 2022-12-14 11:23:53 -0500 | [diff] [blame] | 407 | return err_bad_arg(err, name, arg); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 408 | return 0; |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 409 | } |
| 410 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 411 | static int remote_ref_atom_parser(struct ref_format *format UNUSED, |
| 412 | struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 413 | const char *arg, struct strbuf *err) |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 414 | { |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 415 | struct string_list params = STRING_LIST_INIT_DUP; |
| 416 | int i; |
| 417 | |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 418 | if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:")) |
| 419 | atom->u.remote_ref.push = 1; |
| 420 | |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 421 | if (!arg) { |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 422 | atom->u.remote_ref.option = RR_REF; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 423 | return refname_atom_parser_internal(&atom->u.remote_ref.refname, |
| 424 | arg, atom->name, err); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | atom->u.remote_ref.nobracket = 0; |
| 428 | string_list_split(¶ms, arg, ',', -1); |
| 429 | |
| 430 | for (i = 0; i < params.nr; i++) { |
| 431 | const char *s = params.items[i].string; |
| 432 | |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 433 | if (!strcmp(s, "track")) |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 434 | atom->u.remote_ref.option = RR_TRACK; |
| 435 | else if (!strcmp(s, "trackshort")) |
| 436 | atom->u.remote_ref.option = RR_TRACKSHORT; |
| 437 | else if (!strcmp(s, "nobracket")) |
| 438 | atom->u.remote_ref.nobracket = 1; |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 439 | else if (!strcmp(s, "remotename")) { |
| 440 | atom->u.remote_ref.option = RR_REMOTE_NAME; |
| 441 | atom->u.remote_ref.push_remote = 1; |
J Wyman | 9700fae | 2017-11-07 17:31:08 +0100 | [diff] [blame] | 442 | } else if (!strcmp(s, "remoteref")) { |
| 443 | atom->u.remote_ref.option = RR_REMOTE_REF; |
| 444 | atom->u.remote_ref.push_remote = 1; |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 445 | } else { |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 446 | atom->u.remote_ref.option = RR_REF; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 447 | if (refname_atom_parser_internal(&atom->u.remote_ref.refname, |
| 448 | arg, atom->name, err)) { |
| 449 | string_list_clear(¶ms, 0); |
| 450 | return -1; |
| 451 | } |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 452 | } |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | string_list_clear(¶ms, 0); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 456 | return 0; |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 457 | } |
| 458 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 459 | static int objecttype_atom_parser(struct ref_format *format UNUSED, |
| 460 | struct used_atom *atom, |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 461 | const char *arg, struct strbuf *err) |
| 462 | { |
| 463 | if (arg) |
Jeff King | a33d0fa | 2022-12-14 11:19:43 -0500 | [diff] [blame] | 464 | return err_no_arg(err, "objecttype"); |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 465 | if (*atom->name == '*') |
| 466 | oi_deref.info.typep = &oi_deref.type; |
| 467 | else |
| 468 | oi.info.typep = &oi.type; |
| 469 | return 0; |
| 470 | } |
| 471 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 472 | static int objectsize_atom_parser(struct ref_format *format UNUSED, |
| 473 | struct used_atom *atom, |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 474 | const char *arg, struct strbuf *err) |
| 475 | { |
Olga Telezhnaya | 1867ce6 | 2018-12-24 13:24:30 +0000 | [diff] [blame] | 476 | if (!arg) { |
ZheNing Hu | 0caf20f | 2021-05-13 15:15:37 +0000 | [diff] [blame] | 477 | atom->u.objectsize.option = O_SIZE; |
Olga Telezhnaya | 1867ce6 | 2018-12-24 13:24:30 +0000 | [diff] [blame] | 478 | if (*atom->name == '*') |
| 479 | oi_deref.info.sizep = &oi_deref.size; |
| 480 | else |
| 481 | oi.info.sizep = &oi.size; |
| 482 | } else if (!strcmp(arg, "disk")) { |
ZheNing Hu | 0caf20f | 2021-05-13 15:15:37 +0000 | [diff] [blame] | 483 | atom->u.objectsize.option = O_SIZE_DISK; |
Olga Telezhnaya | 1867ce6 | 2018-12-24 13:24:30 +0000 | [diff] [blame] | 484 | if (*atom->name == '*') |
| 485 | oi_deref.info.disk_sizep = &oi_deref.disk_size; |
| 486 | else |
| 487 | oi.info.disk_sizep = &oi.disk_size; |
| 488 | } else |
Jeff King | dda4fc1 | 2022-12-14 11:20:19 -0500 | [diff] [blame] | 489 | return err_bad_arg(err, "objectsize", arg); |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 493 | static int deltabase_atom_parser(struct ref_format *format UNUSED, |
| 494 | struct used_atom *atom, |
Olga Telezhnaya | 33311fa | 2018-12-24 13:24:30 +0000 | [diff] [blame] | 495 | const char *arg, struct strbuf *err) |
| 496 | { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 497 | if (arg) |
Jeff King | a33d0fa | 2022-12-14 11:19:43 -0500 | [diff] [blame] | 498 | return err_no_arg(err, "deltabase"); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 499 | if (*atom->name == '*') |
Jeff King | b99b6bc | 2020-02-23 23:36:56 -0500 | [diff] [blame] | 500 | oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 501 | else |
Jeff King | b99b6bc | 2020-02-23 23:36:56 -0500 | [diff] [blame] | 502 | oi.info.delta_base_oid = &oi.delta_base_oid; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 506 | static int body_atom_parser(struct ref_format *format UNUSED, |
| 507 | struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 508 | const char *arg, struct strbuf *err) |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 509 | { |
| 510 | if (arg) |
Jeff King | a33d0fa | 2022-12-14 11:19:43 -0500 | [diff] [blame] | 511 | return err_no_arg(err, "body"); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 512 | atom->u.contents.option = C_BODY_DEP; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 513 | return 0; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 514 | } |
| 515 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 516 | static int subject_atom_parser(struct ref_format *format UNUSED, |
| 517 | struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 518 | const char *arg, struct strbuf *err) |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 519 | { |
Hariom Verma | 905f0a4 | 2020-08-21 21:41:50 +0000 | [diff] [blame] | 520 | if (!arg) |
| 521 | atom->u.contents.option = C_SUB; |
| 522 | else if (!strcmp(arg, "sanitize")) |
| 523 | atom->u.contents.option = C_SUB_SANITIZE; |
| 524 | else |
Jeff King | dda4fc1 | 2022-12-14 11:20:19 -0500 | [diff] [blame] | 525 | return err_bad_arg(err, "subject", arg); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 526 | return 0; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 527 | } |
| 528 | |
Kousik Sanagavarapu | 26c9c03 | 2023-06-04 23:52:47 +0530 | [diff] [blame] | 529 | static int parse_signature_option(const char *arg) |
| 530 | { |
| 531 | if (!arg) |
| 532 | return S_BARE; |
| 533 | else if (!strcmp(arg, "signer")) |
| 534 | return S_SIGNER; |
| 535 | else if (!strcmp(arg, "grade")) |
| 536 | return S_GRADE; |
| 537 | else if (!strcmp(arg, "key")) |
| 538 | return S_KEY; |
| 539 | else if (!strcmp(arg, "fingerprint")) |
| 540 | return S_FINGERPRINT; |
| 541 | else if (!strcmp(arg, "primarykeyfingerprint")) |
| 542 | return S_PRI_KEY_FP; |
| 543 | else if (!strcmp(arg, "trustlevel")) |
| 544 | return S_TRUST_LEVEL; |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | static int signature_atom_parser(struct ref_format *format UNUSED, |
| 549 | struct used_atom *atom, |
| 550 | const char *arg, struct strbuf *err) |
| 551 | { |
| 552 | int opt = parse_signature_option(arg); |
| 553 | if (opt < 0) |
| 554 | return err_bad_arg(err, "signature", arg); |
| 555 | atom->u.signature.option = opt; |
| 556 | return 0; |
| 557 | } |
| 558 | |
Jeff King | 29c9f2c | 2023-08-29 19:45:06 -0400 | [diff] [blame] | 559 | static int trailers_atom_parser(struct ref_format *format UNUSED, |
| 560 | struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 561 | const char *arg, struct strbuf *err) |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 562 | { |
Jeff King | e5fba5d | 2018-08-22 20:50:17 -0400 | [diff] [blame] | 563 | atom->u.contents.trailer_opts.no_divider = 1; |
| 564 | |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 565 | if (arg) { |
Hariom Verma | ee82a48 | 2021-02-13 01:52:43 +0000 | [diff] [blame] | 566 | const char *argbuf = xstrfmt("%s)", arg); |
| 567 | char *invalid_arg = NULL; |
| 568 | |
| 569 | if (format_set_trailers_options(&atom->u.contents.trailer_opts, |
| 570 | &ref_trailer_buf.filter_list, |
| 571 | &ref_trailer_buf.sepbuf, |
| 572 | &ref_trailer_buf.kvsepbuf, |
| 573 | &argbuf, &invalid_arg)) { |
| 574 | if (!invalid_arg) |
| 575 | strbuf_addf(err, _("expected %%(trailers:key=<value>)")); |
| 576 | else |
| 577 | strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg); |
| 578 | free((char *)invalid_arg); |
| 579 | return -1; |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 582 | atom->u.contents.option = C_TRAILERS; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 583 | return 0; |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 584 | } |
| 585 | |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 586 | static int contents_atom_parser(struct ref_format *format, struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 587 | const char *arg, struct strbuf *err) |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 588 | { |
| 589 | if (!arg) |
| 590 | atom->u.contents.option = C_BARE; |
| 591 | else if (!strcmp(arg, "body")) |
| 592 | atom->u.contents.option = C_BODY; |
Kousik Sanagavarapu | 6d79cd8 | 2023-09-02 14:30:39 +0530 | [diff] [blame] | 593 | else if (!strcmp(arg, "size")) { |
| 594 | atom->type = FIELD_ULONG; |
Christian Couder | b6839fd | 2020-07-16 14:19:40 +0200 | [diff] [blame] | 595 | atom->u.contents.option = C_LENGTH; |
Kousik Sanagavarapu | 6d79cd8 | 2023-09-02 14:30:39 +0530 | [diff] [blame] | 596 | } else if (!strcmp(arg, "signature")) |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 597 | atom->u.contents.option = C_SIG; |
| 598 | else if (!strcmp(arg, "subject")) |
| 599 | atom->u.contents.option = C_SUB; |
Hariom Verma | 2c22e10 | 2020-08-21 21:06:14 +0000 | [diff] [blame] | 600 | else if (!strcmp(arg, "trailers")) { |
| 601 | if (trailers_atom_parser(format, atom, NULL, err)) |
| 602 | return -1; |
| 603 | } else if (skip_prefix(arg, "trailers:", &arg)) { |
| 604 | if (trailers_atom_parser(format, atom, arg, err)) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 605 | return -1; |
Taylor Blau | 7a5edbd | 2017-10-01 22:25:24 -0700 | [diff] [blame] | 606 | } else if (skip_prefix(arg, "lines=", &arg)) { |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 607 | atom->u.contents.option = C_LINES; |
| 608 | if (strtoul_ui(arg, 10, &atom->u.contents.nlines)) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 609 | return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 610 | } else |
Jeff King | dda4fc1 | 2022-12-14 11:20:19 -0500 | [diff] [blame] | 611 | return err_bad_arg(err, "contents", arg); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 612 | return 0; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 613 | } |
| 614 | |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 615 | static int describe_atom_option_parser(struct strvec *args, const char **arg, |
| 616 | struct strbuf *err) |
| 617 | { |
| 618 | const char *argval; |
| 619 | size_t arglen = 0; |
| 620 | int optval = 0; |
| 621 | |
| 622 | if (match_atom_bool_arg(*arg, "tags", arg, &optval)) { |
| 623 | if (!optval) |
| 624 | strvec_push(args, "--no-tags"); |
| 625 | else |
| 626 | strvec_push(args, "--tags"); |
| 627 | return 1; |
| 628 | } |
| 629 | |
| 630 | if (match_atom_arg_value(*arg, "abbrev", arg, &argval, &arglen)) { |
| 631 | char *endptr; |
| 632 | |
| 633 | if (!arglen) |
| 634 | return strbuf_addf_ret(err, -1, |
| 635 | _("argument expected for %s"), |
| 636 | "describe:abbrev"); |
| 637 | if (strtol(argval, &endptr, 10) < 0) |
| 638 | return strbuf_addf_ret(err, -1, |
| 639 | _("positive value expected %s=%s"), |
| 640 | "describe:abbrev", argval); |
| 641 | if (endptr - argval != arglen) |
| 642 | return strbuf_addf_ret(err, -1, |
| 643 | _("cannot fully parse %s=%s"), |
| 644 | "describe:abbrev", argval); |
| 645 | |
| 646 | strvec_pushf(args, "--abbrev=%.*s", (int)arglen, argval); |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | if (match_atom_arg_value(*arg, "match", arg, &argval, &arglen)) { |
| 651 | if (!arglen) |
| 652 | return strbuf_addf_ret(err, -1, |
| 653 | _("value expected %s="), |
| 654 | "describe:match"); |
| 655 | |
| 656 | strvec_pushf(args, "--match=%.*s", (int)arglen, argval); |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | if (match_atom_arg_value(*arg, "exclude", arg, &argval, &arglen)) { |
| 661 | if (!arglen) |
| 662 | return strbuf_addf_ret(err, -1, |
| 663 | _("value expected %s="), |
| 664 | "describe:exclude"); |
| 665 | |
| 666 | strvec_pushf(args, "--exclude=%.*s", (int)arglen, argval); |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int describe_atom_parser(struct ref_format *format UNUSED, |
| 674 | struct used_atom *atom, |
| 675 | const char *arg, struct strbuf *err) |
| 676 | { |
| 677 | struct strvec args = STRVEC_INIT; |
| 678 | |
| 679 | for (;;) { |
| 680 | int found = 0; |
| 681 | const char *bad_arg = arg; |
| 682 | |
| 683 | if (!arg || !*arg) |
| 684 | break; |
| 685 | |
| 686 | found = describe_atom_option_parser(&args, &arg, err); |
| 687 | if (found < 0) |
| 688 | return found; |
| 689 | if (!found) |
| 690 | return err_bad_arg(err, "describe", bad_arg); |
| 691 | } |
| 692 | atom->u.describe_args = strvec_detach(&args); |
| 693 | return 0; |
| 694 | } |
| 695 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 696 | static int raw_atom_parser(struct ref_format *format UNUSED, |
| 697 | struct used_atom *atom, |
| 698 | const char *arg, struct strbuf *err) |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 699 | { |
| 700 | if (!arg) |
| 701 | atom->u.raw_data.option = RAW_BARE; |
Kousik Sanagavarapu | 6d79cd8 | 2023-09-02 14:30:39 +0530 | [diff] [blame] | 702 | else if (!strcmp(arg, "size")) { |
| 703 | atom->type = FIELD_ULONG; |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 704 | atom->u.raw_data.option = RAW_LENGTH; |
Kousik Sanagavarapu | 6d79cd8 | 2023-09-02 14:30:39 +0530 | [diff] [blame] | 705 | } else |
Jeff King | dda4fc1 | 2022-12-14 11:20:19 -0500 | [diff] [blame] | 706 | return err_bad_arg(err, "raw", arg); |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 710 | static int oid_atom_parser(struct ref_format *format UNUSED, |
| 711 | struct used_atom *atom, |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 712 | const char *arg, struct strbuf *err) |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 713 | { |
| 714 | if (!arg) |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 715 | atom->u.oid.option = O_FULL; |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 716 | else if (!strcmp(arg, "short")) |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 717 | atom->u.oid.option = O_SHORT; |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 718 | else if (skip_prefix(arg, "short=", &arg)) { |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 719 | atom->u.oid.option = O_LENGTH; |
| 720 | if (strtoul_ui(arg, 10, &atom->u.oid.length) || |
| 721 | atom->u.oid.length == 0) |
Hariom Verma | e7601eb | 2020-08-21 21:41:45 +0000 | [diff] [blame] | 722 | return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name); |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 723 | if (atom->u.oid.length < MINIMUM_ABBREV) |
| 724 | atom->u.oid.length = MINIMUM_ABBREV; |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 725 | } else |
Jeff King | 1955ef1 | 2022-12-14 11:23:53 -0500 | [diff] [blame] | 726 | return err_bad_arg(err, atom->name, arg); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 727 | return 0; |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 728 | } |
| 729 | |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 730 | static int person_name_atom_parser(struct ref_format *format UNUSED, |
| 731 | struct used_atom *atom, |
| 732 | const char *arg, struct strbuf *err) |
| 733 | { |
| 734 | if (!arg) |
| 735 | atom->u.name_option.option = N_RAW; |
| 736 | else if (!strcmp(arg, "mailmap")) |
| 737 | atom->u.name_option.option = N_MAILMAP; |
| 738 | else |
| 739 | return err_bad_arg(err, atom->name, arg); |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int email_atom_option_parser(struct used_atom *atom, |
| 744 | const char **arg, struct strbuf *err) |
| 745 | { |
| 746 | if (!*arg) |
| 747 | return EO_RAW; |
| 748 | if (skip_prefix(*arg, "trim", arg)) |
| 749 | return EO_TRIM; |
| 750 | if (skip_prefix(*arg, "localpart", arg)) |
| 751 | return EO_LOCALPART; |
| 752 | if (skip_prefix(*arg, "mailmap", arg)) |
| 753 | return EO_MAILMAP; |
| 754 | return -1; |
| 755 | } |
| 756 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 757 | static int person_email_atom_parser(struct ref_format *format UNUSED, |
| 758 | struct used_atom *atom, |
Hariom Verma | b82445d | 2020-08-21 21:41:43 +0000 | [diff] [blame] | 759 | const char *arg, struct strbuf *err) |
| 760 | { |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 761 | for (;;) { |
| 762 | int opt = email_atom_option_parser(atom, &arg, err); |
| 763 | const char *bad_arg = arg; |
| 764 | |
| 765 | if (opt < 0) |
| 766 | return err_bad_arg(err, atom->name, bad_arg); |
| 767 | atom->u.email_option.option |= opt; |
| 768 | |
| 769 | if (!arg || !*arg) |
| 770 | break; |
| 771 | if (*arg == ',') |
| 772 | arg++; |
| 773 | else |
| 774 | return err_bad_arg(err, atom->name, bad_arg); |
| 775 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 779 | static int refname_atom_parser(struct ref_format *format UNUSED, |
| 780 | struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 781 | const char *arg, struct strbuf *err) |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 782 | { |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 783 | return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 784 | } |
| 785 | |
Karthik Nayak | 25a8d79 | 2016-02-17 23:36:14 +0530 | [diff] [blame] | 786 | static align_type parse_align_position(const char *s) |
| 787 | { |
| 788 | if (!strcmp(s, "right")) |
| 789 | return ALIGN_RIGHT; |
| 790 | else if (!strcmp(s, "middle")) |
| 791 | return ALIGN_MIDDLE; |
| 792 | else if (!strcmp(s, "left")) |
| 793 | return ALIGN_LEFT; |
| 794 | return -1; |
| 795 | } |
| 796 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 797 | static int align_atom_parser(struct ref_format *format UNUSED, |
| 798 | struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 799 | const char *arg, struct strbuf *err) |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 800 | { |
| 801 | struct align *align = &atom->u.align; |
| 802 | struct string_list params = STRING_LIST_INIT_DUP; |
| 803 | int i; |
| 804 | unsigned int width = ~0U; |
| 805 | |
| 806 | if (!arg) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 807 | return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)")); |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 808 | |
| 809 | align->position = ALIGN_LEFT; |
| 810 | |
| 811 | string_list_split(¶ms, arg, ',', -1); |
| 812 | for (i = 0; i < params.nr; i++) { |
| 813 | const char *s = params.items[i].string; |
| 814 | int position; |
| 815 | |
Karthik Nayak | 395fb8f | 2016-02-17 23:36:16 +0530 | [diff] [blame] | 816 | if (skip_prefix(s, "position=", &s)) { |
| 817 | position = parse_align_position(s); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 818 | if (position < 0) { |
| 819 | strbuf_addf(err, _("unrecognized position:%s"), s); |
| 820 | string_list_clear(¶ms, 0); |
| 821 | return -1; |
| 822 | } |
Karthik Nayak | 395fb8f | 2016-02-17 23:36:16 +0530 | [diff] [blame] | 823 | align->position = position; |
| 824 | } else if (skip_prefix(s, "width=", &s)) { |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 825 | if (strtoul_ui(s, 10, &width)) { |
| 826 | strbuf_addf(err, _("unrecognized width:%s"), s); |
| 827 | string_list_clear(¶ms, 0); |
| 828 | return -1; |
| 829 | } |
Karthik Nayak | 395fb8f | 2016-02-17 23:36:16 +0530 | [diff] [blame] | 830 | } else if (!strtoul_ui(s, 10, &width)) |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 831 | ; |
| 832 | else if ((position = parse_align_position(s)) >= 0) |
| 833 | align->position = position; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 834 | else { |
Jean-Noël Avila | 68e2ea0 | 2022-01-05 20:02:21 +0000 | [diff] [blame] | 835 | strbuf_addf(err, _("unrecognized %%(%s) argument: %s"), "align", s); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 836 | string_list_clear(¶ms, 0); |
| 837 | return -1; |
| 838 | } |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 839 | } |
| 840 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 841 | if (width == ~0U) { |
| 842 | string_list_clear(¶ms, 0); |
| 843 | return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom")); |
| 844 | } |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 845 | align->width = width; |
| 846 | string_list_clear(¶ms, 0); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 847 | return 0; |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 848 | } |
| 849 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 850 | static int if_atom_parser(struct ref_format *format UNUSED, |
| 851 | struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 852 | const char *arg, struct strbuf *err) |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 853 | { |
| 854 | if (!arg) { |
| 855 | atom->u.if_then_else.cmp_status = COMPARE_NONE; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 856 | return 0; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 857 | } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) { |
| 858 | atom->u.if_then_else.cmp_status = COMPARE_EQUAL; |
| 859 | } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) { |
| 860 | atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 861 | } else |
Jeff King | dda4fc1 | 2022-12-14 11:20:19 -0500 | [diff] [blame] | 862 | return err_bad_arg(err, "if", arg); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 863 | return 0; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 864 | } |
| 865 | |
Jeff King | 29c9f2c | 2023-08-29 19:45:06 -0400 | [diff] [blame] | 866 | static int rest_atom_parser(struct ref_format *format UNUSED, |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 867 | struct used_atom *atom UNUSED, |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 868 | const char *arg, struct strbuf *err) |
| 869 | { |
| 870 | if (arg) |
Jeff King | a33d0fa | 2022-12-14 11:19:43 -0500 | [diff] [blame] | 871 | return err_no_arg(err, "rest"); |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
Jeff King | 29c9f2c | 2023-08-29 19:45:06 -0400 | [diff] [blame] | 875 | static int ahead_behind_atom_parser(struct ref_format *format, |
| 876 | struct used_atom *atom UNUSED, |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 877 | const char *arg, struct strbuf *err) |
| 878 | { |
| 879 | struct string_list_item *item; |
| 880 | |
| 881 | if (!arg) |
| 882 | return strbuf_addf_ret(err, -1, _("expected format: %%(ahead-behind:<committish>)")); |
| 883 | |
| 884 | item = string_list_append(&format->bases, arg); |
| 885 | item->util = lookup_commit_reference_by_name(arg); |
| 886 | if (!item->util) |
| 887 | die("failed to find '%s'", arg); |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 892 | static int head_atom_parser(struct ref_format *format UNUSED, |
| 893 | struct used_atom *atom, |
Jeff King | afc1a94 | 2022-12-14 11:18:49 -0500 | [diff] [blame] | 894 | const char *arg, struct strbuf *err) |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 895 | { |
Jeff King | afc1a94 | 2022-12-14 11:18:49 -0500 | [diff] [blame] | 896 | if (arg) |
Jeff King | a33d0fa | 2022-12-14 11:19:43 -0500 | [diff] [blame] | 897 | return err_no_arg(err, "HEAD"); |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 898 | atom->u.head = refs_resolve_refdup(get_main_ref_store(the_repository), |
| 899 | "HEAD", RESOLVE_REF_READING, NULL, |
| 900 | NULL); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 901 | return 0; |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 902 | } |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 903 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 904 | static struct { |
| 905 | const char *name; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 906 | info_source source; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 907 | cmp_type cmp_type; |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 908 | int (*parser)(struct ref_format *format, struct used_atom *atom, |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 909 | const char *arg, struct strbuf *err); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 910 | } valid_atom[] = { |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 911 | [ATOM_REFNAME] = { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser }, |
| 912 | [ATOM_OBJECTTYPE] = { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser }, |
| 913 | [ATOM_OBJECTSIZE] = { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser }, |
| 914 | [ATOM_OBJECTNAME] = { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser }, |
| 915 | [ATOM_DELTABASE] = { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser }, |
| 916 | [ATOM_TREE] = { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser }, |
| 917 | [ATOM_PARENT] = { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser }, |
| 918 | [ATOM_NUMPARENT] = { "numparent", SOURCE_OBJ, FIELD_ULONG }, |
| 919 | [ATOM_OBJECT] = { "object", SOURCE_OBJ }, |
| 920 | [ATOM_TYPE] = { "type", SOURCE_OBJ }, |
| 921 | [ATOM_TAG] = { "tag", SOURCE_OBJ }, |
| 922 | [ATOM_AUTHOR] = { "author", SOURCE_OBJ }, |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 923 | [ATOM_AUTHORNAME] = { "authorname", SOURCE_OBJ, FIELD_STR, person_name_atom_parser }, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 924 | [ATOM_AUTHOREMAIL] = { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser }, |
| 925 | [ATOM_AUTHORDATE] = { "authordate", SOURCE_OBJ, FIELD_TIME }, |
| 926 | [ATOM_COMMITTER] = { "committer", SOURCE_OBJ }, |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 927 | [ATOM_COMMITTERNAME] = { "committername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser }, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 928 | [ATOM_COMMITTEREMAIL] = { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser }, |
| 929 | [ATOM_COMMITTERDATE] = { "committerdate", SOURCE_OBJ, FIELD_TIME }, |
| 930 | [ATOM_TAGGER] = { "tagger", SOURCE_OBJ }, |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 931 | [ATOM_TAGGERNAME] = { "taggername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser }, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 932 | [ATOM_TAGGEREMAIL] = { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser }, |
| 933 | [ATOM_TAGGERDATE] = { "taggerdate", SOURCE_OBJ, FIELD_TIME }, |
| 934 | [ATOM_CREATOR] = { "creator", SOURCE_OBJ }, |
| 935 | [ATOM_CREATORDATE] = { "creatordate", SOURCE_OBJ, FIELD_TIME }, |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 936 | [ATOM_DESCRIBE] = { "describe", SOURCE_OBJ, FIELD_STR, describe_atom_parser }, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 937 | [ATOM_SUBJECT] = { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser }, |
| 938 | [ATOM_BODY] = { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser }, |
| 939 | [ATOM_TRAILERS] = { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser }, |
| 940 | [ATOM_CONTENTS] = { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser }, |
Kousik Sanagavarapu | 26c9c03 | 2023-06-04 23:52:47 +0530 | [diff] [blame] | 941 | [ATOM_SIGNATURE] = { "signature", SOURCE_OBJ, FIELD_STR, signature_atom_parser }, |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 942 | [ATOM_RAW] = { "raw", SOURCE_OBJ, FIELD_STR, raw_atom_parser }, |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 943 | [ATOM_UPSTREAM] = { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, |
| 944 | [ATOM_PUSH] = { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, |
| 945 | [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser }, |
| 946 | [ATOM_FLAG] = { "flag", SOURCE_NONE }, |
| 947 | [ATOM_HEAD] = { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser }, |
| 948 | [ATOM_COLOR] = { "color", SOURCE_NONE, FIELD_STR, color_atom_parser }, |
| 949 | [ATOM_WORKTREEPATH] = { "worktreepath", SOURCE_NONE }, |
| 950 | [ATOM_ALIGN] = { "align", SOURCE_NONE, FIELD_STR, align_atom_parser }, |
| 951 | [ATOM_END] = { "end", SOURCE_NONE }, |
| 952 | [ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser }, |
| 953 | [ATOM_THEN] = { "then", SOURCE_NONE }, |
| 954 | [ATOM_ELSE] = { "else", SOURCE_NONE }, |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 955 | [ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser }, |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 956 | [ATOM_AHEADBEHIND] = { "ahead-behind", SOURCE_OTHER, FIELD_STR, ahead_behind_atom_parser }, |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 957 | /* |
| 958 | * Please update $__git_ref_fieldlist in git-completion.bash |
| 959 | * when you add new atoms |
| 960 | */ |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 961 | }; |
| 962 | |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 963 | #define REF_FORMATTING_STATE_INIT { 0 } |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 964 | |
| 965 | struct ref_formatting_stack { |
| 966 | struct ref_formatting_stack *prev; |
| 967 | struct strbuf output; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 968 | void (*at_end)(struct ref_formatting_stack **stack); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 969 | void *at_end_data; |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 970 | }; |
| 971 | |
| 972 | struct ref_formatting_state { |
| 973 | int quote_style; |
| 974 | struct ref_formatting_stack *stack; |
| 975 | }; |
| 976 | |
Karthik Nayak | 3a25761 | 2015-08-22 09:09:37 +0530 | [diff] [blame] | 977 | struct atom_value { |
| 978 | const char *s; |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 979 | ssize_t s_size; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 980 | int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state, |
| 981 | struct strbuf *err); |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 982 | uintmax_t value; /* used for sorting when not FIELD_STR */ |
Karthik Nayak | c58fc85 | 2017-01-10 14:19:35 +0530 | [diff] [blame] | 983 | struct used_atom *atom; |
Karthik Nayak | 3a25761 | 2015-08-22 09:09:37 +0530 | [diff] [blame] | 984 | }; |
| 985 | |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 986 | #define ATOM_SIZE_UNSPECIFIED (-1) |
| 987 | |
| 988 | #define ATOM_VALUE_INIT { \ |
| 989 | .s_size = ATOM_SIZE_UNSPECIFIED \ |
| 990 | } |
| 991 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 992 | /* |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 993 | * Used to parse format string and sort specifiers |
| 994 | */ |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 995 | static int parse_ref_filter_atom(struct ref_format *format, |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 996 | const char *atom, const char *ep, |
| 997 | struct strbuf *err) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 998 | { |
| 999 | const char *sp; |
Karthik Nayak | 4de707e | 2016-02-17 23:36:12 +0530 | [diff] [blame] | 1000 | const char *arg; |
SZEDER Gábor | e94ce13 | 2016-10-02 18:35:11 +0200 | [diff] [blame] | 1001 | int i, at, atom_len; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1002 | |
| 1003 | sp = atom; |
| 1004 | if (*sp == '*' && sp < ep) |
| 1005 | sp++; /* deref */ |
| 1006 | if (ep <= sp) |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1007 | return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"), |
| 1008 | (int)(ep-atom), atom); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1009 | |
SZEDER Gábor | e94ce13 | 2016-10-02 18:35:11 +0200 | [diff] [blame] | 1010 | /* |
| 1011 | * If the atom name has a colon, strip it and everything after |
| 1012 | * it off - it specifies the format for this entry, and |
| 1013 | * shouldn't be used for checking against the valid_atom |
| 1014 | * table. |
| 1015 | */ |
| 1016 | arg = memchr(sp, ':', ep - sp); |
| 1017 | atom_len = (arg ? arg : ep) - sp; |
| 1018 | |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1019 | /* Do we have the atom already used elsewhere? */ |
| 1020 | for (i = 0; i < used_atom_cnt; i++) { |
| 1021 | int len = strlen(used_atom[i].name); |
| 1022 | if (len == ep - atom && !memcmp(used_atom[i].name, atom, len)) |
| 1023 | return i; |
| 1024 | } |
| 1025 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1026 | /* Is the atom a valid one? */ |
| 1027 | for (i = 0; i < ARRAY_SIZE(valid_atom); i++) { |
| 1028 | int len = strlen(valid_atom[i].name); |
SZEDER Gábor | e94ce13 | 2016-10-02 18:35:11 +0200 | [diff] [blame] | 1029 | if (len == atom_len && !memcmp(valid_atom[i].name, sp, len)) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | if (ARRAY_SIZE(valid_atom) <= i) |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1034 | return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"), |
| 1035 | (int)(ep-atom), atom); |
SZEDER Gábor | 47bd3d0 | 2018-11-14 13:27:25 +0100 | [diff] [blame] | 1036 | if (valid_atom[i].source != SOURCE_NONE && !have_git_dir()) |
| 1037 | return strbuf_addf_ret(err, -1, |
| 1038 | _("not a git repository, but the field '%.*s' requires access to object data"), |
| 1039 | (int)(ep-atom), atom); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1040 | |
| 1041 | /* Add it in, including the deref prefix */ |
| 1042 | at = used_atom_cnt; |
| 1043 | used_atom_cnt++; |
| 1044 | REALLOC_ARRAY(used_atom, used_atom_cnt); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1045 | used_atom[at].atom_type = i; |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1046 | used_atom[at].name = xmemdupz(atom, ep - atom); |
| 1047 | used_atom[at].type = valid_atom[i].cmp_type; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1048 | used_atom[at].source = valid_atom[i].source; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1049 | if (used_atom[at].source == SOURCE_OBJ) { |
| 1050 | if (*atom == '*') |
| 1051 | oi_deref.info.contentp = &oi_deref.content; |
| 1052 | else |
| 1053 | oi.info.contentp = &oi.content; |
| 1054 | } |
Taylor Blau | bea4dbe | 2017-10-02 09:10:34 -0700 | [diff] [blame] | 1055 | if (arg) { |
Karthik Nayak | 4de707e | 2016-02-17 23:36:12 +0530 | [diff] [blame] | 1056 | arg = used_atom[at].name + (arg - atom) + 1; |
Taylor Blau | bea4dbe | 2017-10-02 09:10:34 -0700 | [diff] [blame] | 1057 | if (!*arg) { |
| 1058 | /* |
| 1059 | * Treat empty sub-arguments list as NULL (i.e., |
| 1060 | * "%(atom:)" is equivalent to "%(atom)"). |
| 1061 | */ |
| 1062 | arg = NULL; |
| 1063 | } |
| 1064 | } |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 1065 | memset(&used_atom[at].u, 0, sizeof(used_atom[at].u)); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1066 | if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err)) |
| 1067 | return -1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1068 | if (*atom == '*') |
| 1069 | need_tagged = 1; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1070 | if (i == ATOM_SYMREF) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1071 | need_symref = 1; |
| 1072 | return at; |
| 1073 | } |
| 1074 | |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1075 | static void quote_formatting(struct strbuf *s, const char *str, ssize_t len, int quote_style) |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 1076 | { |
| 1077 | switch (quote_style) { |
| 1078 | case QUOTE_NONE: |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1079 | if (len < 0) |
| 1080 | strbuf_addstr(s, str); |
| 1081 | else |
| 1082 | strbuf_add(s, str, len); |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 1083 | break; |
| 1084 | case QUOTE_SHELL: |
| 1085 | sq_quote_buf(s, str); |
| 1086 | break; |
| 1087 | case QUOTE_PERL: |
ZheNing Hu | 7121c4d | 2021-07-26 03:26:48 +0000 | [diff] [blame] | 1088 | if (len < 0) |
| 1089 | perl_quote_buf(s, str); |
| 1090 | else |
| 1091 | perl_quote_buf_with_len(s, str, len); |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 1092 | break; |
| 1093 | case QUOTE_PYTHON: |
| 1094 | python_quote_buf(s, str); |
| 1095 | break; |
| 1096 | case QUOTE_TCL: |
| 1097 | tcl_quote_buf(s, str); |
| 1098 | break; |
| 1099 | } |
| 1100 | } |
| 1101 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1102 | static int append_atom(struct atom_value *v, struct ref_formatting_state *state, |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 1103 | struct strbuf *err UNUSED) |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 1104 | { |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1105 | /* |
| 1106 | * Quote formatting is only done when the stack has a single |
| 1107 | * element. Otherwise quote formatting is done on the |
| 1108 | * element's entire output strbuf when the %(end) atom is |
| 1109 | * encountered. |
| 1110 | */ |
| 1111 | if (!state->stack->prev) |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1112 | quote_formatting(&state->stack->output, v->s, v->s_size, state->quote_style); |
| 1113 | else if (v->s_size < 0) |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1114 | strbuf_addstr(&state->stack->output, v->s); |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1115 | else |
| 1116 | strbuf_add(&state->stack->output, v->s, v->s_size); |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1117 | return 0; |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 1118 | } |
| 1119 | |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 1120 | static void push_stack_element(struct ref_formatting_stack **stack) |
| 1121 | { |
| 1122 | struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack)); |
| 1123 | |
| 1124 | strbuf_init(&s->output, 0); |
| 1125 | s->prev = *stack; |
| 1126 | *stack = s; |
| 1127 | } |
| 1128 | |
| 1129 | static void pop_stack_element(struct ref_formatting_stack **stack) |
| 1130 | { |
| 1131 | struct ref_formatting_stack *current = *stack; |
| 1132 | struct ref_formatting_stack *prev = current->prev; |
| 1133 | |
| 1134 | if (prev) |
| 1135 | strbuf_addbuf(&prev->output, ¤t->output); |
| 1136 | strbuf_release(¤t->output); |
| 1137 | free(current); |
| 1138 | *stack = prev; |
| 1139 | } |
| 1140 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1141 | static void end_align_handler(struct ref_formatting_stack **stack) |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1142 | { |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1143 | struct ref_formatting_stack *cur = *stack; |
| 1144 | struct align *align = (struct align *)cur->at_end_data; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1145 | struct strbuf s = STRBUF_INIT; |
| 1146 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1147 | strbuf_utf8_align(&s, align->position, align->width, cur->output.buf); |
| 1148 | strbuf_swap(&cur->output, &s); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1149 | strbuf_release(&s); |
| 1150 | } |
| 1151 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1152 | static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 1153 | struct strbuf *err UNUSED) |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1154 | { |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 1155 | struct ref_formatting_stack *new_stack; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1156 | |
| 1157 | push_stack_element(&state->stack); |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 1158 | new_stack = state->stack; |
| 1159 | new_stack->at_end = end_align_handler; |
| 1160 | new_stack->at_end_data = &atomv->atom->u.align; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1161 | return 0; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1162 | } |
| 1163 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1164 | static void if_then_else_handler(struct ref_formatting_stack **stack) |
| 1165 | { |
| 1166 | struct ref_formatting_stack *cur = *stack; |
| 1167 | struct ref_formatting_stack *prev = cur->prev; |
| 1168 | struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data; |
| 1169 | |
| 1170 | if (!if_then_else->then_atom_seen) |
Jean-Noël Avila | d7d30ba | 2022-01-05 20:02:23 +0000 | [diff] [blame] | 1171 | die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then"); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1172 | |
| 1173 | if (if_then_else->else_atom_seen) { |
| 1174 | /* |
| 1175 | * There is an %(else) atom: we need to drop one state from the |
| 1176 | * stack, either the %(else) branch if the condition is satisfied, or |
| 1177 | * the %(then) branch if it isn't. |
| 1178 | */ |
| 1179 | if (if_then_else->condition_satisfied) { |
| 1180 | strbuf_reset(&cur->output); |
| 1181 | pop_stack_element(&cur); |
| 1182 | } else { |
| 1183 | strbuf_swap(&cur->output, &prev->output); |
| 1184 | strbuf_reset(&cur->output); |
| 1185 | pop_stack_element(&cur); |
| 1186 | } |
| 1187 | } else if (!if_then_else->condition_satisfied) { |
| 1188 | /* |
| 1189 | * No %(else) atom: just drop the %(then) branch if the |
| 1190 | * condition is not satisfied. |
| 1191 | */ |
| 1192 | strbuf_reset(&cur->output); |
| 1193 | } |
| 1194 | |
| 1195 | *stack = cur; |
| 1196 | free(if_then_else); |
| 1197 | } |
| 1198 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1199 | static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 1200 | struct strbuf *err UNUSED) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1201 | { |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 1202 | struct ref_formatting_stack *new_stack; |
René Scharfe | 241b5d3 | 2021-03-06 12:26:19 +0100 | [diff] [blame] | 1203 | struct if_then_else *if_then_else = xcalloc(1, |
| 1204 | sizeof(struct if_then_else)); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1205 | |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 1206 | if_then_else->str = atomv->atom->u.if_then_else.str; |
| 1207 | if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status; |
| 1208 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1209 | push_stack_element(&state->stack); |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 1210 | new_stack = state->stack; |
| 1211 | new_stack->at_end = if_then_else_handler; |
| 1212 | new_stack->at_end_data = if_then_else; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1213 | return 0; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1214 | } |
| 1215 | |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1216 | static int is_empty(struct strbuf *buf) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1217 | { |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1218 | const char *cur = buf->buf; |
| 1219 | const char *end = buf->buf + buf->len; |
| 1220 | |
| 1221 | while (cur != end && (isspace(*cur))) |
| 1222 | cur++; |
| 1223 | |
| 1224 | return cur == end; |
| 1225 | } |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1226 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 1227 | static int then_atom_handler(struct atom_value *atomv UNUSED, |
| 1228 | struct ref_formatting_state *state, |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1229 | struct strbuf *err) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1230 | { |
| 1231 | struct ref_formatting_stack *cur = state->stack; |
| 1232 | struct if_then_else *if_then_else = NULL; |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1233 | size_t str_len = 0; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1234 | |
| 1235 | if (cur->at_end == if_then_else_handler) |
| 1236 | if_then_else = (struct if_then_else *)cur->at_end_data; |
| 1237 | if (!if_then_else) |
Jean-Noël Avila | d7d30ba | 2022-01-05 20:02:23 +0000 | [diff] [blame] | 1238 | return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if"); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1239 | if (if_then_else->then_atom_seen) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1240 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1241 | if (if_then_else->else_atom_seen) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1242 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1243 | if_then_else->then_atom_seen = 1; |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1244 | if (if_then_else->str) |
| 1245 | str_len = strlen(if_then_else->str); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1246 | /* |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 1247 | * If the 'equals' or 'notequals' attribute is used then |
| 1248 | * perform the required comparison. If not, only non-empty |
| 1249 | * strings satisfy the 'if' condition. |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1250 | */ |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 1251 | if (if_then_else->cmp_status == COMPARE_EQUAL) { |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1252 | if (str_len == cur->output.len && |
| 1253 | !memcmp(if_then_else->str, cur->output.buf, cur->output.len)) |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 1254 | if_then_else->condition_satisfied = 1; |
| 1255 | } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) { |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1256 | if (str_len != cur->output.len || |
| 1257 | memcmp(if_then_else->str, cur->output.buf, cur->output.len)) |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 1258 | if_then_else->condition_satisfied = 1; |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1259 | } else if (cur->output.len && !is_empty(&cur->output)) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1260 | if_then_else->condition_satisfied = 1; |
| 1261 | strbuf_reset(&cur->output); |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1262 | return 0; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1263 | } |
| 1264 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 1265 | static int else_atom_handler(struct atom_value *atomv UNUSED, |
| 1266 | struct ref_formatting_state *state, |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1267 | struct strbuf *err) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1268 | { |
| 1269 | struct ref_formatting_stack *prev = state->stack; |
| 1270 | struct if_then_else *if_then_else = NULL; |
| 1271 | |
| 1272 | if (prev->at_end == if_then_else_handler) |
| 1273 | if_then_else = (struct if_then_else *)prev->at_end_data; |
| 1274 | if (!if_then_else) |
Jean-Noël Avila | d7d30ba | 2022-01-05 20:02:23 +0000 | [diff] [blame] | 1275 | return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if"); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1276 | if (!if_then_else->then_atom_seen) |
Jean-Noël Avila | d7d30ba | 2022-01-05 20:02:23 +0000 | [diff] [blame] | 1277 | return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then"); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1278 | if (if_then_else->else_atom_seen) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1279 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1280 | if_then_else->else_atom_seen = 1; |
| 1281 | push_stack_element(&state->stack); |
| 1282 | state->stack->at_end_data = prev->at_end_data; |
| 1283 | state->stack->at_end = prev->at_end; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1284 | return 0; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1285 | } |
| 1286 | |
Jeff King | 5fe9e1c | 2023-02-24 01:39:06 -0500 | [diff] [blame] | 1287 | static int end_atom_handler(struct atom_value *atomv UNUSED, |
| 1288 | struct ref_formatting_state *state, |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1289 | struct strbuf *err) |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1290 | { |
| 1291 | struct ref_formatting_stack *current = state->stack; |
| 1292 | struct strbuf s = STRBUF_INIT; |
| 1293 | |
| 1294 | if (!current->at_end) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1295 | return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1296 | current->at_end(&state->stack); |
| 1297 | |
| 1298 | /* Stack may have been popped within at_end(), hence reset the current pointer */ |
| 1299 | current = state->stack; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1300 | |
| 1301 | /* |
| 1302 | * Perform quote formatting when the stack element is that of |
| 1303 | * a supporting atom. If nested then perform quote formatting |
| 1304 | * only on the topmost supporting atom. |
| 1305 | */ |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1306 | if (!current->prev->prev) { |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1307 | quote_formatting(&s, current->output.buf, current->output.len, state->quote_style); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1308 | strbuf_swap(¤t->output, &s); |
| 1309 | } |
| 1310 | strbuf_release(&s); |
| 1311 | pop_stack_element(&state->stack); |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1312 | return 0; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1313 | } |
| 1314 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1315 | /* |
| 1316 | * In a format string, find the next occurrence of %(atom). |
| 1317 | */ |
| 1318 | static const char *find_next(const char *cp) |
| 1319 | { |
| 1320 | while (*cp) { |
| 1321 | if (*cp == '%') { |
| 1322 | /* |
| 1323 | * %( is the start of an atom; |
| 1324 | * %% is a quoted per-cent. |
| 1325 | */ |
| 1326 | if (cp[1] == '(') |
| 1327 | return cp; |
| 1328 | else if (cp[1] == '%') |
| 1329 | cp++; /* skip over two % */ |
| 1330 | /* otherwise this is a singleton, literal % */ |
| 1331 | } |
| 1332 | cp++; |
| 1333 | } |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 1337 | static int reject_atom(enum atom_type atom_type) |
| 1338 | { |
| 1339 | return atom_type == ATOM_REST; |
| 1340 | } |
| 1341 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1342 | /* |
| 1343 | * Make sure the format string is well formed, and parse out |
| 1344 | * the used atoms. |
| 1345 | */ |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 1346 | int verify_ref_format(struct ref_format *format) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1347 | { |
| 1348 | const char *cp, *sp; |
| 1349 | |
Jeff King | bf285ae | 2017-07-13 11:02:30 -0400 | [diff] [blame] | 1350 | format->need_color_reset_at_eol = 0; |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 1351 | for (cp = format->format; *cp && (sp = find_next(cp)); ) { |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1352 | struct strbuf err = STRBUF_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1353 | const char *color, *ep = strchr(sp, ')'); |
| 1354 | int at; |
| 1355 | |
| 1356 | if (!ep) |
Nguyễn Thái Ngọc Duy | 1823c61 | 2016-02-27 13:42:04 +0700 | [diff] [blame] | 1357 | return error(_("malformed format string %s"), sp); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1358 | /* sp points at "%(" and ep points at the closing ")" */ |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1359 | at = parse_ref_filter_atom(format, sp + 2, ep, &err); |
| 1360 | if (at < 0) |
| 1361 | die("%s", err.buf); |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 1362 | if (reject_atom(used_atom[at].atom_type)) |
| 1363 | die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2); |
ZheNing Hu | 7121c4d | 2021-07-26 03:26:48 +0000 | [diff] [blame] | 1364 | |
| 1365 | if ((format->quote_style == QUOTE_PYTHON || |
| 1366 | format->quote_style == QUOTE_SHELL || |
| 1367 | format->quote_style == QUOTE_TCL) && |
| 1368 | used_atom[at].atom_type == ATOM_RAW && |
| 1369 | used_atom[at].u.raw_data.option == RAW_BARE) |
Jiang Xin | f733719 | 2021-11-01 10:14:17 +0800 | [diff] [blame] | 1370 | die(_("--format=%.*s cannot be used with " |
ZheNing Hu | 7121c4d | 2021-07-26 03:26:48 +0000 | [diff] [blame] | 1371 | "--python, --shell, --tcl"), (int)(ep - sp - 2), sp + 2); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1372 | cp = ep + 1; |
| 1373 | |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1374 | if (skip_prefix(used_atom[at].name, "color:", &color)) |
Jeff King | bf285ae | 2017-07-13 11:02:30 -0400 | [diff] [blame] | 1375 | format->need_color_reset_at_eol = !!strcmp(color, "reset"); |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1376 | strbuf_release(&err); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1377 | } |
Jeff King | 11b087a | 2017-07-13 11:09:32 -0400 | [diff] [blame] | 1378 | if (format->need_color_reset_at_eol && !want_color(format->use_color)) |
| 1379 | format->need_color_reset_at_eol = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 1383 | static const char *do_grab_oid(const char *field, const struct object_id *oid, |
| 1384 | struct used_atom *atom) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1385 | { |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 1386 | switch (atom->u.oid.option) { |
Hariom Verma | 5101100 | 2020-08-21 21:41:44 +0000 | [diff] [blame] | 1387 | case O_FULL: |
| 1388 | return oid_to_hex(oid); |
| 1389 | case O_LENGTH: |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1390 | return repo_find_unique_abbrev(the_repository, oid, |
| 1391 | atom->u.oid.length); |
Hariom Verma | 5101100 | 2020-08-21 21:41:44 +0000 | [diff] [blame] | 1392 | case O_SHORT: |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1393 | return repo_find_unique_abbrev(the_repository, oid, |
| 1394 | DEFAULT_ABBREV); |
Hariom Verma | 5101100 | 2020-08-21 21:41:44 +0000 | [diff] [blame] | 1395 | default: |
| 1396 | BUG("unknown %%(%s) option", field); |
| 1397 | } |
| 1398 | } |
| 1399 | |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 1400 | static int grab_oid(const char *name, const char *field, const struct object_id *oid, |
| 1401 | struct atom_value *v, struct used_atom *atom) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1402 | { |
Hariom Verma | 5101100 | 2020-08-21 21:41:44 +0000 | [diff] [blame] | 1403 | if (starts_with(name, field)) { |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 1404 | v->s = xstrdup(do_grab_oid(field, oid, atom)); |
Hariom Verma | 5101100 | 2020-08-21 21:41:44 +0000 | [diff] [blame] | 1405 | return 1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1406 | } |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | /* See grab_values */ |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1411 | static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1412 | { |
| 1413 | int i; |
| 1414 | |
| 1415 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1416 | const char *name = used_atom[i].name; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1417 | enum atom_type atom_type = used_atom[i].atom_type; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1418 | struct atom_value *v = &val[i]; |
| 1419 | if (!!deref != (*name == '*')) |
| 1420 | continue; |
| 1421 | if (deref) |
| 1422 | name++; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1423 | if (atom_type == ATOM_OBJECTTYPE) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1424 | v->s = xstrdup(type_name(oi->type)); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1425 | else if (atom_type == ATOM_OBJECTSIZE) { |
ZheNing Hu | 0caf20f | 2021-05-13 15:15:37 +0000 | [diff] [blame] | 1426 | if (used_atom[i].u.objectsize.option == O_SIZE_DISK) { |
| 1427 | v->value = oi->disk_size; |
| 1428 | v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size); |
| 1429 | } else if (used_atom[i].u.objectsize.option == O_SIZE) { |
| 1430 | v->value = oi->size; |
| 1431 | v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size); |
| 1432 | } |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1433 | } else if (atom_type == ATOM_DELTABASE) |
Olga Telezhnaya | 33311fa | 2018-12-24 13:24:30 +0000 | [diff] [blame] | 1434 | v->s = xstrdup(oid_to_hex(&oi->delta_base_oid)); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1435 | else if (atom_type == ATOM_OBJECTNAME && deref) |
Hariom Verma | 87d3beb | 2020-08-21 21:41:46 +0000 | [diff] [blame] | 1436 | grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | /* See grab_values */ |
Jeff King | e032919 | 2019-02-14 00:50:54 -0500 | [diff] [blame] | 1441 | static void grab_tag_values(struct atom_value *val, int deref, struct object *obj) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1442 | { |
| 1443 | int i; |
| 1444 | struct tag *tag = (struct tag *) obj; |
| 1445 | |
| 1446 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1447 | const char *name = used_atom[i].name; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1448 | enum atom_type atom_type = used_atom[i].atom_type; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1449 | struct atom_value *v = &val[i]; |
| 1450 | if (!!deref != (*name == '*')) |
| 1451 | continue; |
| 1452 | if (deref) |
| 1453 | name++; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1454 | if (atom_type == ATOM_TAG) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1455 | v->s = xstrdup(tag->tag); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1456 | else if (atom_type == ATOM_TYPE && tag->tagged) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1457 | v->s = xstrdup(type_name(tag->tagged->type)); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1458 | else if (atom_type == ATOM_OBJECT && tag->tagged) |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1459 | v->s = xstrdup(oid_to_hex(&tag->tagged->oid)); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /* See grab_values */ |
Jeff King | e032919 | 2019-02-14 00:50:54 -0500 | [diff] [blame] | 1464 | static void grab_commit_values(struct atom_value *val, int deref, struct object *obj) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1465 | { |
| 1466 | int i; |
| 1467 | struct commit *commit = (struct commit *) obj; |
| 1468 | |
| 1469 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1470 | const char *name = used_atom[i].name; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1471 | enum atom_type atom_type = used_atom[i].atom_type; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1472 | struct atom_value *v = &val[i]; |
| 1473 | if (!!deref != (*name == '*')) |
| 1474 | continue; |
| 1475 | if (deref) |
| 1476 | name++; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1477 | if (atom_type == ATOM_TREE && |
| 1478 | grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i])) |
Hariom Verma | 837adb1 | 2020-08-21 21:41:47 +0000 | [diff] [blame] | 1479 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1480 | if (atom_type == ATOM_NUMPARENT) { |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 1481 | v->value = commit_list_count(commit->parents); |
| 1482 | v->s = xstrfmt("%lu", (unsigned long)v->value); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1483 | } |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1484 | else if (atom_type == ATOM_PARENT) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1485 | struct commit_list *parents; |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 1486 | struct strbuf s = STRBUF_INIT; |
| 1487 | for (parents = commit->parents; parents; parents = parents->next) { |
Hariom Verma | 26bc0aa | 2020-08-21 21:41:48 +0000 | [diff] [blame] | 1488 | struct object_id *oid = &parents->item->object.oid; |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 1489 | if (parents != commit->parents) |
| 1490 | strbuf_addch(&s, ' '); |
Hariom Verma | 26bc0aa | 2020-08-21 21:41:48 +0000 | [diff] [blame] | 1491 | strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i])); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1492 | } |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 1493 | v->s = strbuf_detach(&s, NULL); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | } |
| 1497 | |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 1498 | static const char *find_wholine(const char *who, int wholen, const char *buf) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1499 | { |
| 1500 | const char *eol; |
| 1501 | while (*buf) { |
| 1502 | if (!strncmp(buf, who, wholen) && |
| 1503 | buf[wholen] == ' ') |
| 1504 | return buf + wholen + 1; |
| 1505 | eol = strchr(buf, '\n'); |
| 1506 | if (!eol) |
| 1507 | return ""; |
| 1508 | eol++; |
| 1509 | if (*eol == '\n') |
| 1510 | return ""; /* end of header */ |
| 1511 | buf = eol; |
| 1512 | } |
| 1513 | return ""; |
| 1514 | } |
| 1515 | |
| 1516 | static const char *copy_line(const char *buf) |
| 1517 | { |
| 1518 | const char *eol = strchrnul(buf, '\n'); |
| 1519 | return xmemdupz(buf, eol - buf); |
| 1520 | } |
| 1521 | |
| 1522 | static const char *copy_name(const char *buf) |
| 1523 | { |
| 1524 | const char *cp; |
| 1525 | for (cp = buf; *cp && *cp != '\n'; cp++) { |
Jeff King | 20869d1 | 2023-01-07 08:26:18 -0500 | [diff] [blame] | 1526 | if (starts_with(cp, " <")) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1527 | return xmemdupz(buf, cp - buf); |
| 1528 | } |
Mischa POSLAWSKY | 8b3f33e | 2019-08-17 23:51:07 +0200 | [diff] [blame] | 1529 | return xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1530 | } |
| 1531 | |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1532 | static const char *find_end_of_email(const char *email, int opt) |
| 1533 | { |
| 1534 | const char *eoemail; |
| 1535 | |
| 1536 | if (opt & EO_LOCALPART) { |
| 1537 | eoemail = strchr(email, '@'); |
| 1538 | if (eoemail) |
| 1539 | return eoemail; |
| 1540 | return strchr(email, '>'); |
| 1541 | } |
| 1542 | |
| 1543 | if (opt & EO_TRIM) |
| 1544 | return strchr(email, '>'); |
| 1545 | |
| 1546 | /* |
| 1547 | * The option here is either the raw email option or the raw |
| 1548 | * mailmap option (that is EO_RAW or EO_MAILMAP). In such cases, |
| 1549 | * we directly grab the whole email including the closing |
| 1550 | * angle brackets. |
| 1551 | * |
| 1552 | * If EO_MAILMAP was set with any other option (that is either |
| 1553 | * EO_TRIM or EO_LOCALPART), we already grab the end of email |
| 1554 | * above. |
| 1555 | */ |
| 1556 | eoemail = strchr(email, '>'); |
| 1557 | if (eoemail) |
| 1558 | eoemail++; |
| 1559 | return eoemail; |
| 1560 | } |
| 1561 | |
Hariom Verma | b82445d | 2020-08-21 21:41:43 +0000 | [diff] [blame] | 1562 | static const char *copy_email(const char *buf, struct used_atom *atom) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1563 | { |
| 1564 | const char *email = strchr(buf, '<'); |
| 1565 | const char *eoemail; |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1566 | int opt = atom->u.email_option.option; |
| 1567 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1568 | if (!email) |
Mischa POSLAWSKY | 8b3f33e | 2019-08-17 23:51:07 +0200 | [diff] [blame] | 1569 | return xstrdup(""); |
Hariom Verma | b82445d | 2020-08-21 21:41:43 +0000 | [diff] [blame] | 1570 | |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1571 | if (opt & (EO_LOCALPART | EO_TRIM)) |
| 1572 | email++; |
| 1573 | |
| 1574 | eoemail = find_end_of_email(email, opt); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1575 | if (!eoemail) |
Mischa POSLAWSKY | 8b3f33e | 2019-08-17 23:51:07 +0200 | [diff] [blame] | 1576 | return xstrdup(""); |
Hariom Verma | b82445d | 2020-08-21 21:41:43 +0000 | [diff] [blame] | 1577 | return xmemdupz(email, eoemail - email); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | static char *copy_subject(const char *buf, unsigned long len) |
| 1581 | { |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1582 | struct strbuf sb = STRBUF_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1583 | int i; |
| 1584 | |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1585 | for (i = 0; i < len; i++) { |
| 1586 | if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n') |
| 1587 | continue; /* ignore CR in CRLF */ |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1588 | |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1589 | if (buf[i] == '\n') |
| 1590 | strbuf_addch(&sb, ' '); |
| 1591 | else |
| 1592 | strbuf_addch(&sb, buf[i]); |
| 1593 | } |
| 1594 | return strbuf_detach(&sb, NULL); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | static void grab_date(const char *buf, struct atom_value *v, const char *atomname) |
| 1598 | { |
| 1599 | const char *eoemail = strstr(buf, "> "); |
| 1600 | char *zone; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 1601 | timestamp_t timestamp; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1602 | long tz; |
Ævar Arnfjörð Bjarmason | f184289 | 2022-02-16 09:14:03 +0100 | [diff] [blame] | 1603 | struct date_mode date_mode = DATE_MODE_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1604 | const char *formatp; |
| 1605 | |
| 1606 | /* |
| 1607 | * We got here because atomname ends in "date" or "date<something>"; |
| 1608 | * it's not possible that <something> is not ":<format>" because |
| 1609 | * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no |
| 1610 | * ":" means no format is specified, and use the default. |
| 1611 | */ |
| 1612 | formatp = strchr(atomname, ':'); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 1613 | if (formatp) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1614 | formatp++; |
Junio C Hamano | d939af1 | 2015-08-03 11:01:27 -0700 | [diff] [blame] | 1615 | parse_date_format(formatp, &date_mode); |
Victoria Dye | 46176d7 | 2024-02-08 01:57:19 +0000 | [diff] [blame] | 1616 | |
| 1617 | /* |
| 1618 | * If this is a sort field and a format was specified, we'll |
| 1619 | * want to compare formatted date by string value. |
| 1620 | */ |
| 1621 | v->atom->type = FIELD_STR; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | if (!eoemail) |
| 1625 | goto bad; |
Johannes Schindelin | 1aeb7e7 | 2017-04-21 12:45:44 +0200 | [diff] [blame] | 1626 | timestamp = parse_timestamp(eoemail + 2, &zone, 10); |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 1627 | if (timestamp == TIME_MAX) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1628 | goto bad; |
| 1629 | tz = strtol(zone, NULL, 10); |
| 1630 | if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) |
| 1631 | goto bad; |
René Scharfe | 9720d23 | 2024-04-05 19:44:59 +0200 | [diff] [blame] | 1632 | v->s = xstrdup(show_date(timestamp, tz, date_mode)); |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 1633 | v->value = timestamp; |
Ævar Arnfjörð Bjarmason | 974c919 | 2022-02-16 09:14:05 +0100 | [diff] [blame] | 1634 | date_mode_release(&date_mode); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1635 | return; |
| 1636 | bad: |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1637 | v->s = xstrdup(""); |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 1638 | v->value = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1639 | } |
| 1640 | |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1641 | static struct string_list mailmap = STRING_LIST_INIT_NODUP; |
| 1642 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1643 | /* See grab_values */ |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 1644 | static void grab_person(const char *who, struct atom_value *val, int deref, void *buf) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1645 | { |
| 1646 | int i; |
| 1647 | int wholen = strlen(who); |
| 1648 | const char *wholine = NULL; |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1649 | const char *headers[] = { "author ", "committer ", |
| 1650 | "tagger ", NULL }; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1651 | |
| 1652 | for (i = 0; i < used_atom_cnt; i++) { |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1653 | struct used_atom *atom = &used_atom[i]; |
| 1654 | const char *name = atom->name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1655 | struct atom_value *v = &val[i]; |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1656 | struct strbuf mailmap_buf = STRBUF_INIT; |
| 1657 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1658 | if (!!deref != (*name == '*')) |
| 1659 | continue; |
| 1660 | if (deref) |
| 1661 | name++; |
| 1662 | if (strncmp(who, name, wholen)) |
| 1663 | continue; |
| 1664 | if (name[wholen] != 0 && |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1665 | !starts_with(name + wholen, "name") && |
Hariom Verma | b82445d | 2020-08-21 21:41:43 +0000 | [diff] [blame] | 1666 | !starts_with(name + wholen, "email") && |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1667 | !starts_with(name + wholen, "date")) |
| 1668 | continue; |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1669 | |
| 1670 | if ((starts_with(name + wholen, "name") && |
| 1671 | (atom->u.name_option.option == N_MAILMAP)) || |
| 1672 | (starts_with(name + wholen, "email") && |
| 1673 | (atom->u.email_option.option & EO_MAILMAP))) { |
| 1674 | if (!mailmap.items) |
| 1675 | read_mailmap(&mailmap); |
| 1676 | strbuf_addstr(&mailmap_buf, buf); |
| 1677 | apply_mailmap_to_header(&mailmap_buf, headers, &mailmap); |
| 1678 | wholine = find_wholine(who, wholen, mailmap_buf.buf); |
| 1679 | } else { |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 1680 | wholine = find_wholine(who, wholen, buf); |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1681 | } |
| 1682 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1683 | if (!wholine) |
| 1684 | return; /* no point looking for it */ |
| 1685 | if (name[wholen] == 0) |
| 1686 | v->s = copy_line(wholine); |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1687 | else if (starts_with(name + wholen, "name")) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1688 | v->s = copy_name(wholine); |
Hariom Verma | b82445d | 2020-08-21 21:41:43 +0000 | [diff] [blame] | 1689 | else if (starts_with(name + wholen, "email")) |
| 1690 | v->s = copy_email(wholine, &used_atom[i]); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1691 | else if (starts_with(name + wholen, "date")) |
| 1692 | grab_date(wholine, v, name); |
Kousik Sanagavarapu | a3d2e83 | 2023-09-25 23:13:10 +0530 | [diff] [blame] | 1693 | |
| 1694 | strbuf_release(&mailmap_buf); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * For a tag or a commit object, if "creator" or "creatordate" is |
| 1699 | * requested, do something special. |
| 1700 | */ |
| 1701 | if (strcmp(who, "tagger") && strcmp(who, "committer")) |
| 1702 | return; /* "author" for commit object is not wanted */ |
| 1703 | if (!wholine) |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 1704 | wholine = find_wholine(who, wholen, buf); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1705 | if (!wholine) |
| 1706 | return; |
| 1707 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1708 | const char *name = used_atom[i].name; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1709 | enum atom_type atom_type = used_atom[i].atom_type; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1710 | struct atom_value *v = &val[i]; |
| 1711 | if (!!deref != (*name == '*')) |
| 1712 | continue; |
| 1713 | if (deref) |
| 1714 | name++; |
| 1715 | |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1716 | if (atom_type == ATOM_CREATORDATE) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1717 | grab_date(wholine, v, name); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 1718 | else if (atom_type == ATOM_CREATOR) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1719 | v->s = copy_line(wholine); |
| 1720 | } |
| 1721 | } |
| 1722 | |
Kousik Sanagavarapu | 26c9c03 | 2023-06-04 23:52:47 +0530 | [diff] [blame] | 1723 | static void grab_signature(struct atom_value *val, int deref, struct object *obj) |
| 1724 | { |
| 1725 | int i; |
| 1726 | struct commit *commit = (struct commit *) obj; |
| 1727 | struct signature_check sigc = { 0 }; |
| 1728 | int signature_checked = 0; |
| 1729 | |
| 1730 | for (i = 0; i < used_atom_cnt; i++) { |
| 1731 | struct used_atom *atom = &used_atom[i]; |
| 1732 | const char *name = atom->name; |
| 1733 | struct atom_value *v = &val[i]; |
| 1734 | int opt; |
| 1735 | |
| 1736 | if (!!deref != (*name == '*')) |
| 1737 | continue; |
| 1738 | if (deref) |
| 1739 | name++; |
| 1740 | |
| 1741 | if (!skip_prefix(name, "signature", &name) || |
| 1742 | (*name && *name != ':')) |
| 1743 | continue; |
| 1744 | if (!*name) |
| 1745 | name = NULL; |
| 1746 | else |
| 1747 | name++; |
| 1748 | |
| 1749 | opt = parse_signature_option(name); |
| 1750 | if (opt < 0) |
| 1751 | continue; |
| 1752 | |
| 1753 | if (!signature_checked) { |
| 1754 | check_commit_signature(commit, &sigc); |
| 1755 | signature_checked = 1; |
| 1756 | } |
| 1757 | |
| 1758 | switch (opt) { |
| 1759 | case S_BARE: |
| 1760 | v->s = xstrdup(sigc.output ? sigc.output: ""); |
| 1761 | break; |
| 1762 | case S_SIGNER: |
| 1763 | v->s = xstrdup(sigc.signer ? sigc.signer : ""); |
| 1764 | break; |
| 1765 | case S_GRADE: |
| 1766 | switch (sigc.result) { |
| 1767 | case 'G': |
| 1768 | switch (sigc.trust_level) { |
| 1769 | case TRUST_UNDEFINED: |
| 1770 | case TRUST_NEVER: |
| 1771 | v->s = xstrfmt("%c", (char)'U'); |
| 1772 | break; |
| 1773 | default: |
| 1774 | v->s = xstrfmt("%c", (char)'G'); |
| 1775 | break; |
| 1776 | } |
| 1777 | break; |
| 1778 | case 'B': |
| 1779 | case 'E': |
| 1780 | case 'N': |
| 1781 | case 'X': |
| 1782 | case 'Y': |
| 1783 | case 'R': |
| 1784 | v->s = xstrfmt("%c", (char)sigc.result); |
| 1785 | break; |
| 1786 | } |
| 1787 | break; |
| 1788 | case S_KEY: |
| 1789 | v->s = xstrdup(sigc.key ? sigc.key : ""); |
| 1790 | break; |
| 1791 | case S_FINGERPRINT: |
| 1792 | v->s = xstrdup(sigc.fingerprint ? |
| 1793 | sigc.fingerprint : ""); |
| 1794 | break; |
| 1795 | case S_PRI_KEY_FP: |
| 1796 | v->s = xstrdup(sigc.primary_key_fingerprint ? |
| 1797 | sigc.primary_key_fingerprint : ""); |
| 1798 | break; |
| 1799 | case S_TRUST_LEVEL: |
| 1800 | v->s = xstrdup(gpg_trust_level_to_str(sigc.trust_level)); |
| 1801 | break; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | if (signature_checked) |
| 1806 | signature_check_clear(&sigc); |
| 1807 | } |
| 1808 | |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 1809 | static void find_subpos(const char *buf, |
brian m. carlson | 83dff3e | 2021-01-18 23:49:10 +0000 | [diff] [blame] | 1810 | const char **sub, size_t *sublen, |
| 1811 | const char **body, size_t *bodylen, |
| 1812 | size_t *nonsiglen, |
| 1813 | const char **sig, size_t *siglen) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1814 | { |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 1815 | struct strbuf payload = STRBUF_INIT; |
| 1816 | struct strbuf signature = STRBUF_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1817 | const char *eol; |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 1818 | const char *end = buf + strlen(buf); |
| 1819 | const char *sigstart; |
| 1820 | |
brian m. carlson | 88bce0e | 2021-02-11 02:08:05 +0000 | [diff] [blame] | 1821 | /* parse signature first; we might not even have a subject line */ |
| 1822 | parse_signature(buf, end - buf, &payload, &signature); |
Ævar Arnfjörð Bjarmason | b6046ab | 2022-11-08 19:17:42 +0100 | [diff] [blame] | 1823 | strbuf_release(&payload); |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 1824 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1825 | /* skip past header until we hit empty line */ |
| 1826 | while (*buf && *buf != '\n') { |
| 1827 | eol = strchrnul(buf, '\n'); |
| 1828 | if (*eol) |
| 1829 | eol++; |
| 1830 | buf = eol; |
| 1831 | } |
| 1832 | /* skip any empty lines */ |
| 1833 | while (*buf == '\n') |
| 1834 | buf++; |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 1835 | *sig = strbuf_detach(&signature, siglen); |
| 1836 | sigstart = buf + parse_signed_buffer(buf, strlen(buf)); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1837 | |
| 1838 | /* subject is first non-empty line */ |
| 1839 | *sub = buf; |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1840 | /* subject goes to first empty line before signature begins */ |
Jeff King | 8e1c5fc | 2022-11-02 03:44:00 -0400 | [diff] [blame] | 1841 | if ((eol = strstr(*sub, "\n\n")) || |
| 1842 | (eol = strstr(*sub, "\r\n\r\n"))) { |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 1843 | eol = eol < sigstart ? eol : sigstart; |
Jeff King | 8e1c5fc | 2022-11-02 03:44:00 -0400 | [diff] [blame] | 1844 | } else { |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1845 | /* treat whole message as subject */ |
Jeff King | b01e1c7 | 2022-11-02 03:42:07 -0400 | [diff] [blame] | 1846 | eol = sigstart; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1847 | } |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1848 | buf = eol; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1849 | *sublen = buf - *sub; |
| 1850 | /* drop trailing newline, if present */ |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1851 | while (*sublen && ((*sub)[*sublen - 1] == '\n' || |
| 1852 | (*sub)[*sublen - 1] == '\r')) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1853 | *sublen -= 1; |
| 1854 | |
| 1855 | /* skip any empty lines */ |
Philippe Blain | 9f75ce3 | 2020-10-29 12:48:28 +0000 | [diff] [blame] | 1856 | while (*buf == '\n' || *buf == '\r') |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1857 | buf++; |
| 1858 | *body = buf; |
| 1859 | *bodylen = strlen(buf); |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 1860 | *nonsiglen = sigstart - buf; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1861 | } |
| 1862 | |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1863 | /* |
| 1864 | * If 'lines' is greater than 0, append that many lines from the given |
| 1865 | * 'buf' of length 'size' to the given strbuf. |
| 1866 | */ |
| 1867 | static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines) |
| 1868 | { |
| 1869 | int i; |
| 1870 | const char *sp, *eol; |
| 1871 | size_t len; |
| 1872 | |
| 1873 | sp = buf; |
| 1874 | |
| 1875 | for (i = 0; i < lines && sp < buf + size; i++) { |
| 1876 | if (i) |
| 1877 | strbuf_addstr(out, "\n "); |
| 1878 | eol = memchr(sp, '\n', size - (sp - buf)); |
| 1879 | len = eol ? eol - sp : size - (sp - buf); |
| 1880 | strbuf_add(out, sp, len); |
| 1881 | if (!eol) |
| 1882 | break; |
| 1883 | sp = eol + 1; |
| 1884 | } |
| 1885 | } |
| 1886 | |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 1887 | static void grab_describe_values(struct atom_value *val, int deref, |
| 1888 | struct object *obj) |
| 1889 | { |
| 1890 | struct commit *commit = (struct commit *)obj; |
| 1891 | int i; |
| 1892 | |
| 1893 | for (i = 0; i < used_atom_cnt; i++) { |
| 1894 | struct used_atom *atom = &used_atom[i]; |
| 1895 | enum atom_type type = atom->atom_type; |
| 1896 | const char *name = atom->name; |
| 1897 | struct atom_value *v = &val[i]; |
| 1898 | |
| 1899 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 1900 | struct strbuf out = STRBUF_INIT; |
| 1901 | struct strbuf err = STRBUF_INIT; |
| 1902 | |
| 1903 | if (type != ATOM_DESCRIBE) |
| 1904 | continue; |
| 1905 | |
| 1906 | if (!!deref != (*name == '*')) |
| 1907 | continue; |
| 1908 | |
| 1909 | cmd.git_cmd = 1; |
| 1910 | strvec_push(&cmd.args, "describe"); |
| 1911 | strvec_pushv(&cmd.args, atom->u.describe_args); |
| 1912 | strvec_push(&cmd.args, oid_to_hex(&commit->object.oid)); |
| 1913 | if (pipe_command(&cmd, NULL, 0, &out, 0, &err, 0) < 0) { |
| 1914 | error(_("failed to run 'describe'")); |
| 1915 | v->s = xstrdup(""); |
| 1916 | continue; |
| 1917 | } |
| 1918 | strbuf_rtrim(&out); |
| 1919 | v->s = strbuf_detach(&out, NULL); |
| 1920 | |
| 1921 | strbuf_release(&err); |
| 1922 | } |
| 1923 | } |
| 1924 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1925 | /* See grab_values */ |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 1926 | static void grab_sub_body_contents(struct atom_value *val, int deref, struct expand_data *data) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1927 | { |
| 1928 | int i; |
| 1929 | const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; |
brian m. carlson | 83dff3e | 2021-01-18 23:49:10 +0000 | [diff] [blame] | 1930 | size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 1931 | void *buf = data->content; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1932 | |
| 1933 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1934 | struct used_atom *atom = &used_atom[i]; |
| 1935 | const char *name = atom->name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1936 | struct atom_value *v = &val[i]; |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1937 | enum atom_type atom_type = atom->atom_type; |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 1938 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1939 | if (!!deref != (*name == '*')) |
| 1940 | continue; |
| 1941 | if (deref) |
| 1942 | name++; |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 1943 | |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1944 | if (atom_type == ATOM_RAW) { |
| 1945 | unsigned long buf_size = data->size; |
| 1946 | |
| 1947 | if (atom->u.raw_data.option == RAW_BARE) { |
| 1948 | v->s = xmemdupz(buf, buf_size); |
| 1949 | v->s_size = buf_size; |
| 1950 | } else if (atom->u.raw_data.option == RAW_LENGTH) { |
Kousik Sanagavarapu | 6d79cd8 | 2023-09-02 14:30:39 +0530 | [diff] [blame] | 1951 | v->value = buf_size; |
| 1952 | v->s = xstrfmt("%"PRIuMAX, v->value); |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 1953 | } |
| 1954 | continue; |
| 1955 | } |
| 1956 | |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 1957 | if ((data->type != OBJ_TAG && |
| 1958 | data->type != OBJ_COMMIT) || |
| 1959 | (strcmp(name, "body") && |
| 1960 | !starts_with(name, "subject") && |
| 1961 | !starts_with(name, "trailers") && |
| 1962 | !starts_with(name, "contents"))) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1963 | continue; |
| 1964 | if (!subpos) |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 1965 | find_subpos(buf, |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1966 | &subpos, &sublen, |
| 1967 | &bodypos, &bodylen, &nonsiglen, |
| 1968 | &sigpos, &siglen); |
| 1969 | |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1970 | if (atom->u.contents.option == C_SUB) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1971 | v->s = copy_subject(subpos, sublen); |
Hariom Verma | 905f0a4 | 2020-08-21 21:41:50 +0000 | [diff] [blame] | 1972 | else if (atom->u.contents.option == C_SUB_SANITIZE) { |
| 1973 | struct strbuf sb = STRBUF_INIT; |
| 1974 | format_sanitized_subject(&sb, subpos, sublen); |
| 1975 | v->s = strbuf_detach(&sb, NULL); |
| 1976 | } else if (atom->u.contents.option == C_BODY_DEP) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1977 | v->s = xmemdupz(bodypos, bodylen); |
Kousik Sanagavarapu | 6d79cd8 | 2023-09-02 14:30:39 +0530 | [diff] [blame] | 1978 | else if (atom->u.contents.option == C_LENGTH) { |
| 1979 | v->value = strlen(subpos); |
| 1980 | v->s = xstrfmt("%"PRIuMAX, v->value); |
| 1981 | } else if (atom->u.contents.option == C_BODY) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1982 | v->s = xmemdupz(bodypos, nonsiglen); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1983 | else if (atom->u.contents.option == C_SIG) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1984 | v->s = xmemdupz(sigpos, siglen); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1985 | else if (atom->u.contents.option == C_LINES) { |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1986 | struct strbuf s = STRBUF_INIT; |
brian m. carlson | 88bce0e | 2021-02-11 02:08:05 +0000 | [diff] [blame] | 1987 | const char *contents_end = bodypos + nonsiglen; |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1988 | |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1989 | /* Size is the length of the message after removing the signature */ |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1990 | append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines); |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1991 | v->s = strbuf_detach(&s, NULL); |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 1992 | } else if (atom->u.contents.option == C_TRAILERS) { |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 1993 | struct strbuf s = STRBUF_INIT; |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 1994 | |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 1995 | /* Format the trailer info according to the trailer_opts given */ |
Linus Arver | 0383dc5 | 2024-03-01 00:14:42 +0000 | [diff] [blame] | 1996 | format_trailers_from_commit(&atom->u.contents.trailer_opts, subpos, &s); |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 1997 | |
| 1998 | v->s = strbuf_detach(&s, NULL); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1999 | } else if (atom->u.contents.option == C_BARE) |
| 2000 | v->s = xstrdup(subpos); |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 2001 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2002 | } |
brian m. carlson | 482c119 | 2021-02-11 02:08:03 +0000 | [diff] [blame] | 2003 | free((void *)sigpos); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * We want to have empty print-string for field requests |
| 2008 | * that do not apply (e.g. "authordate" for a tag object) |
| 2009 | */ |
| 2010 | static void fill_missing_values(struct atom_value *val) |
| 2011 | { |
| 2012 | int i; |
| 2013 | for (i = 0; i < used_atom_cnt; i++) { |
| 2014 | struct atom_value *v = &val[i]; |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 2015 | if (!v->s) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2016 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * val is a list of atom_value to hold returned values. Extract |
| 2022 | * the values for atoms in used_atom array out of (obj, buf, sz). |
| 2023 | * when deref is false, (obj, buf, sz) is the object that is |
| 2024 | * pointed at by the ref itself; otherwise it is the object the |
| 2025 | * ref (which is a tag) refers to. |
| 2026 | */ |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 2027 | static void grab_values(struct atom_value *val, int deref, struct object *obj, struct expand_data *data) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2028 | { |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 2029 | void *buf = data->content; |
| 2030 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2031 | switch (obj->type) { |
| 2032 | case OBJ_TAG: |
Jeff King | e032919 | 2019-02-14 00:50:54 -0500 | [diff] [blame] | 2033 | grab_tag_values(val, deref, obj); |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 2034 | grab_sub_body_contents(val, deref, data); |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 2035 | grab_person("tagger", val, deref, buf); |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 2036 | grab_describe_values(val, deref, obj); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2037 | break; |
| 2038 | case OBJ_COMMIT: |
Jeff King | e032919 | 2019-02-14 00:50:54 -0500 | [diff] [blame] | 2039 | grab_commit_values(val, deref, obj); |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 2040 | grab_sub_body_contents(val, deref, data); |
Jeff King | 5c326d1 | 2019-02-14 00:51:03 -0500 | [diff] [blame] | 2041 | grab_person("author", val, deref, buf); |
| 2042 | grab_person("committer", val, deref, buf); |
Kousik Sanagavarapu | 26c9c03 | 2023-06-04 23:52:47 +0530 | [diff] [blame] | 2043 | grab_signature(val, deref, obj); |
Kousik Sanagavarapu | f5d18f8 | 2023-07-23 21:49:59 +0530 | [diff] [blame] | 2044 | grab_describe_values(val, deref, obj); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2045 | break; |
| 2046 | case OBJ_TREE: |
| 2047 | /* grab_tree_values(val, deref, obj, buf, sz); */ |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 2048 | grab_sub_body_contents(val, deref, data); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2049 | break; |
| 2050 | case OBJ_BLOB: |
| 2051 | /* grab_blob_values(val, deref, obj, buf, sz); */ |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 2052 | grab_sub_body_contents(val, deref, data); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2053 | break; |
| 2054 | default: |
| 2055 | die("Eh? Object of type %d?", obj->type); |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | static inline char *copy_advance(char *dst, const char *src) |
| 2060 | { |
| 2061 | while (*src) |
| 2062 | *dst++ = *src++; |
| 2063 | return dst; |
| 2064 | } |
| 2065 | |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 2066 | static const char *lstrip_ref_components(const char *refname, int len) |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 2067 | { |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2068 | long remaining = len; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2069 | const char *start = xstrdup(refname); |
| 2070 | const char *to_free = start; |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 2071 | |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 2072 | if (len < 0) { |
| 2073 | int i; |
| 2074 | const char *p = refname; |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 2075 | |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 2076 | /* Find total no of '/' separated path-components */ |
| 2077 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) |
| 2078 | ; |
| 2079 | /* |
| 2080 | * The number of components we need to strip is now |
| 2081 | * the total minus the components to be left (Plus one |
| 2082 | * because we count the number of '/', but the number |
| 2083 | * of components is one more than the no of '/'). |
| 2084 | */ |
| 2085 | remaining = i + len + 1; |
| 2086 | } |
| 2087 | |
| 2088 | while (remaining > 0) { |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 2089 | switch (*start++) { |
| 2090 | case '\0': |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2091 | free((char *)to_free); |
| 2092 | return xstrdup(""); |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 2093 | case '/': |
| 2094 | remaining--; |
| 2095 | break; |
| 2096 | } |
| 2097 | } |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 2098 | |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2099 | start = xstrdup(start); |
| 2100 | free((char *)to_free); |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 2101 | return start; |
| 2102 | } |
| 2103 | |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 2104 | static const char *rstrip_ref_components(const char *refname, int len) |
| 2105 | { |
| 2106 | long remaining = len; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2107 | const char *start = xstrdup(refname); |
| 2108 | const char *to_free = start; |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 2109 | |
| 2110 | if (len < 0) { |
| 2111 | int i; |
| 2112 | const char *p = refname; |
| 2113 | |
| 2114 | /* Find total no of '/' separated path-components */ |
| 2115 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) |
| 2116 | ; |
| 2117 | /* |
| 2118 | * The number of components we need to strip is now |
| 2119 | * the total minus the components to be left (Plus one |
| 2120 | * because we count the number of '/', but the number |
| 2121 | * of components is one more than the no of '/'). |
| 2122 | */ |
| 2123 | remaining = i + len + 1; |
| 2124 | } |
| 2125 | |
| 2126 | while (remaining-- > 0) { |
| 2127 | char *p = strrchr(start, '/'); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 2128 | if (!p) { |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2129 | free((char *)to_free); |
| 2130 | return xstrdup(""); |
| 2131 | } else |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 2132 | p[0] = '\0'; |
| 2133 | } |
| 2134 | return start; |
| 2135 | } |
| 2136 | |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2137 | static const char *show_ref(struct refname_atom *atom, const char *refname) |
| 2138 | { |
| 2139 | if (atom->option == R_SHORT) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 2140 | return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), |
| 2141 | refname, |
| 2142 | warn_ambiguous_refs); |
Karthik Nayak | 17938f1 | 2017-01-10 14:19:46 +0530 | [diff] [blame] | 2143 | else if (atom->option == R_LSTRIP) |
| 2144 | return lstrip_ref_components(refname, atom->lstrip); |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 2145 | else if (atom->option == R_RSTRIP) |
| 2146 | return rstrip_ref_components(refname, atom->rstrip); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2147 | else |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2148 | return xstrdup(refname); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2149 | } |
| 2150 | |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2151 | static void fill_remote_ref_details(struct used_atom *atom, const char *refname, |
| 2152 | struct branch *branch, const char **s) |
| 2153 | { |
| 2154 | int num_ours, num_theirs; |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 2155 | if (atom->u.remote_ref.option == RR_REF) |
| 2156 | *s = show_ref(&atom->u.remote_ref.refname, refname); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 2157 | else if (atom->u.remote_ref.option == RR_TRACK) { |
Jeff Hostetler | d7d1b49 | 2018-01-09 18:50:15 +0000 | [diff] [blame] | 2158 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
Damien Robert | c646d09 | 2019-04-16 14:16:46 +0200 | [diff] [blame] | 2159 | NULL, atom->u.remote_ref.push, |
| 2160 | AHEAD_BEHIND_FULL) < 0) { |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 2161 | *s = xstrdup(msgs.gone); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 2162 | } else if (!num_ours && !num_theirs) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2163 | *s = xstrdup(""); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2164 | else if (!num_ours) |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 2165 | *s = xstrfmt(msgs.behind, num_theirs); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2166 | else if (!num_theirs) |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 2167 | *s = xstrfmt(msgs.ahead, num_ours); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2168 | else |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 2169 | *s = xstrfmt(msgs.ahead_behind, |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2170 | num_ours, num_theirs); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 2171 | if (!atom->u.remote_ref.nobracket && *s[0]) { |
| 2172 | const char *to_free = *s; |
| 2173 | *s = xstrfmt("[%s]", *s); |
| 2174 | free((void *)to_free); |
| 2175 | } |
| 2176 | } else if (atom->u.remote_ref.option == RR_TRACKSHORT) { |
Jeff Hostetler | d7d1b49 | 2018-01-09 18:50:15 +0000 | [diff] [blame] | 2177 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
Damien Robert | c646d09 | 2019-04-16 14:16:46 +0200 | [diff] [blame] | 2178 | NULL, atom->u.remote_ref.push, |
| 2179 | AHEAD_BEHIND_FULL) < 0) { |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2180 | *s = xstrdup(""); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2181 | return; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2182 | } |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2183 | if (!num_ours && !num_theirs) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2184 | *s = xstrdup("="); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2185 | else if (!num_ours) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2186 | *s = xstrdup("<"); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2187 | else if (!num_theirs) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2188 | *s = xstrdup(">"); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2189 | else |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2190 | *s = xstrdup("<>"); |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 2191 | } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) { |
| 2192 | int explicit; |
| 2193 | const char *remote = atom->u.remote_ref.push ? |
| 2194 | pushremote_for_branch(branch, &explicit) : |
| 2195 | remote_for_branch(branch, &explicit); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2196 | *s = xstrdup(explicit ? remote : ""); |
J Wyman | 9700fae | 2017-11-07 17:31:08 +0100 | [diff] [blame] | 2197 | } else if (atom->u.remote_ref.option == RR_REMOTE_REF) { |
J Wyman | 9700fae | 2017-11-07 17:31:08 +0100 | [diff] [blame] | 2198 | const char *merge; |
| 2199 | |
Jeff King | af8ccd8 | 2020-03-03 17:12:22 +0100 | [diff] [blame] | 2200 | merge = remote_ref_for_branch(branch, atom->u.remote_ref.push); |
| 2201 | *s = xstrdup(merge ? merge : ""); |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 2202 | } else |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 2203 | BUG("unhandled RR_* enum"); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2204 | } |
| 2205 | |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 2206 | char *get_head_description(void) |
| 2207 | { |
| 2208 | struct strbuf desc = STRBUF_INIT; |
| 2209 | struct wt_status_state state; |
| 2210 | memset(&state, 0, sizeof(state)); |
Nguyễn Thái Ngọc Duy | 7884545 | 2018-11-10 06:48:50 +0100 | [diff] [blame] | 2211 | wt_status_get_state(the_repository, &state, 1); |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 2212 | if (state.rebase_in_progress || |
Kaartic Sivaraam | a236f90 | 2018-04-03 10:01:00 +0530 | [diff] [blame] | 2213 | state.rebase_interactive_in_progress) { |
| 2214 | if (state.branch) |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 2215 | strbuf_addf(&desc, _("(no branch, rebasing %s)"), |
Kaartic Sivaraam | a236f90 | 2018-04-03 10:01:00 +0530 | [diff] [blame] | 2216 | state.branch); |
| 2217 | else |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 2218 | strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"), |
Kaartic Sivaraam | a236f90 | 2018-04-03 10:01:00 +0530 | [diff] [blame] | 2219 | state.detached_from); |
| 2220 | } else if (state.bisect_in_progress) |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 2221 | strbuf_addf(&desc, _("(no branch, bisect started on %s)"), |
Rubén Justo | 990adcc | 2023-09-09 22:12:47 +0200 | [diff] [blame] | 2222 | state.bisecting_from); |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 2223 | else if (state.detached_from) { |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 2224 | if (state.detached_at) |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 2225 | strbuf_addf(&desc, _("(HEAD detached at %s)"), |
| 2226 | state.detached_from); |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 2227 | else |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 2228 | strbuf_addf(&desc, _("(HEAD detached from %s)"), |
| 2229 | state.detached_from); |
| 2230 | } else |
| 2231 | strbuf_addstr(&desc, _("(no branch)")); |
Matthew DeVore | 28438e8 | 2019-06-18 15:29:15 -0700 | [diff] [blame] | 2232 | |
Rubén Justo | abcac2e | 2022-09-25 00:53:18 +0200 | [diff] [blame] | 2233 | wt_status_state_free_buffers(&state); |
| 2234 | |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 2235 | return strbuf_detach(&desc, NULL); |
| 2236 | } |
| 2237 | |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2238 | static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref) |
| 2239 | { |
| 2240 | if (!ref->symref) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2241 | return xstrdup(""); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2242 | else |
| 2243 | return show_ref(&atom->u.refname, ref->symref); |
| 2244 | } |
| 2245 | |
| 2246 | static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref) |
| 2247 | { |
| 2248 | if (ref->kind & FILTER_REFS_DETACHED_HEAD) |
| 2249 | return get_head_description(); |
| 2250 | return show_ref(&atom->u.refname, ref->refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2251 | } |
| 2252 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2253 | static int get_object(struct ref_array_item *ref, int deref, struct object **obj, |
| 2254 | struct expand_data *oi, struct strbuf *err) |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 2255 | { |
Olga Telezhnaya | 04f6ee1 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2256 | /* parse_object_buffer() will set eaten to 0 if free() will be needed */ |
| 2257 | int eaten = 1; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2258 | if (oi->info.contentp) { |
| 2259 | /* We need to know that to use parse_object_buffer properly */ |
| 2260 | oi->info.sizep = &oi->size; |
| 2261 | oi->info.typep = &oi->type; |
Olga Telezhnaya | e225517 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2262 | } |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2263 | if (oid_object_info_extended(the_repository, &oi->oid, &oi->info, |
| 2264 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 2265 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), |
| 2266 | oid_to_hex(&oi->oid), ref->refname); |
Olga Telezhnaya | 5305a55 | 2018-12-24 13:24:30 +0000 | [diff] [blame] | 2267 | if (oi->info.disk_sizep && oi->disk_size < 0) |
| 2268 | BUG("Object size is less than zero."); |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2269 | |
| 2270 | if (oi->info.contentp) { |
Junio C Hamano | c83149a | 2018-08-17 13:09:57 -0700 | [diff] [blame] | 2271 | *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten); |
Jeff King | c685450 | 2021-04-01 04:32:24 -0400 | [diff] [blame] | 2272 | if (!*obj) { |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2273 | if (!eaten) |
| 2274 | free(oi->content); |
| 2275 | return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"), |
| 2276 | oid_to_hex(&oi->oid), ref->refname); |
| 2277 | } |
ZheNing Hu | 311d0b8 | 2021-07-26 03:26:46 +0000 | [diff] [blame] | 2278 | grab_values(ref->value, deref, *obj, oi); |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | grab_common_values(ref->value, deref, oi); |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 2282 | if (!eaten) |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2283 | free(oi->content); |
| 2284 | return 0; |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2287 | static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees) |
| 2288 | { |
| 2289 | int i; |
| 2290 | |
| 2291 | for (i = 0; worktrees[i]; i++) { |
| 2292 | if (worktrees[i]->head_ref) { |
| 2293 | struct ref_to_worktree_entry *entry; |
| 2294 | entry = xmalloc(sizeof(*entry)); |
| 2295 | entry->wt = worktrees[i]; |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 2296 | hashmap_entry_init(&entry->ent, |
| 2297 | strhash(worktrees[i]->head_ref)); |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2298 | |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 2299 | hashmap_add(map, &entry->ent); |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | static void lazy_init_worktree_map(void) |
| 2305 | { |
| 2306 | if (ref_to_worktree_map.worktrees) |
| 2307 | return; |
| 2308 | |
Eric Sunshine | 03f2465 | 2020-06-19 19:35:44 -0400 | [diff] [blame] | 2309 | ref_to_worktree_map.worktrees = get_worktrees(); |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2310 | hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0); |
| 2311 | populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees); |
| 2312 | } |
| 2313 | |
Jeff King | fe6258c | 2023-02-24 01:34:44 -0500 | [diff] [blame] | 2314 | static char *get_worktree_path(const struct ref_array_item *ref) |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2315 | { |
Eric Wong | f23a465 | 2019-10-06 23:30:36 +0000 | [diff] [blame] | 2316 | struct hashmap_entry entry, *e; |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2317 | struct ref_to_worktree_entry *lookup_result; |
| 2318 | |
| 2319 | lazy_init_worktree_map(); |
| 2320 | |
| 2321 | hashmap_entry_init(&entry, strhash(ref->refname)); |
Eric Wong | f23a465 | 2019-10-06 23:30:36 +0000 | [diff] [blame] | 2322 | e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname); |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2323 | |
Eric Wong | f23a465 | 2019-10-06 23:30:36 +0000 | [diff] [blame] | 2324 | if (!e) |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2325 | return xstrdup(""); |
Eric Wong | f23a465 | 2019-10-06 23:30:36 +0000 | [diff] [blame] | 2326 | |
| 2327 | lookup_result = container_of(e, struct ref_to_worktree_entry, ent); |
| 2328 | |
| 2329 | return xstrdup(lookup_result->wt->path); |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2332 | /* |
| 2333 | * Parse the object referred by ref, and grab needed value. |
| 2334 | */ |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2335 | static int populate_value(struct ref_array_item *ref, struct strbuf *err) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2336 | { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2337 | struct object *obj; |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 2338 | int i; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2339 | struct object_info empty = OBJECT_INFO_INIT; |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 2340 | int ahead_behind_atoms = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2341 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2342 | CALLOC_ARRAY(ref->value, used_atom_cnt); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2343 | |
| 2344 | if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 2345 | ref->symref = refs_resolve_refdup(get_main_ref_store(the_repository), |
| 2346 | ref->refname, |
| 2347 | RESOLVE_REF_READING, |
| 2348 | NULL, NULL); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2349 | if (!ref->symref) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2350 | ref->symref = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | /* Fill in specials first */ |
| 2354 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 2355 | struct used_atom *atom = &used_atom[i]; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2356 | enum atom_type atom_type = atom->atom_type; |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 2357 | const char *name = used_atom[i].name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2358 | struct atom_value *v = &ref->value[i]; |
| 2359 | int deref = 0; |
| 2360 | const char *refname; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2361 | struct branch *branch = NULL; |
| 2362 | |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 2363 | v->s_size = ATOM_SIZE_UNSPECIFIED; |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 2364 | v->handler = append_atom; |
Kousik Sanagavarapu | 6d79cd8 | 2023-09-02 14:30:39 +0530 | [diff] [blame] | 2365 | v->value = 0; |
Karthik Nayak | c58fc85 | 2017-01-10 14:19:35 +0530 | [diff] [blame] | 2366 | v->atom = atom; |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 2367 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2368 | if (*name == '*') { |
| 2369 | deref = 1; |
| 2370 | name++; |
| 2371 | } |
| 2372 | |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2373 | if (atom_type == ATOM_REFNAME) |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2374 | refname = get_refname(atom, ref); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2375 | else if (atom_type == ATOM_WORKTREEPATH) { |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2376 | if (ref->kind == FILTER_REFS_BRANCHES) |
Jeff King | fe6258c | 2023-02-24 01:34:44 -0500 | [diff] [blame] | 2377 | v->s = get_worktree_path(ref); |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2378 | else |
| 2379 | v->s = xstrdup(""); |
| 2380 | continue; |
| 2381 | } |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2382 | else if (atom_type == ATOM_SYMREF) |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 2383 | refname = get_symref(atom, ref); |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2384 | else if (atom_type == ATOM_UPSTREAM) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2385 | const char *branch_name; |
| 2386 | /* only local branches may have an upstream */ |
| 2387 | if (!skip_prefix(ref->refname, "refs/heads/", |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2388 | &branch_name)) { |
| 2389 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2390 | continue; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2391 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2392 | branch = branch_get(branch_name); |
| 2393 | |
| 2394 | refname = branch_get_upstream(branch, NULL); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2395 | if (refname) |
| 2396 | fill_remote_ref_details(atom, refname, branch, &v->s); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2397 | else |
| 2398 | v->s = xstrdup(""); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2399 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2400 | } else if (atom_type == ATOM_PUSH && atom->u.remote_ref.push) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2401 | const char *branch_name; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2402 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2403 | if (!skip_prefix(ref->refname, "refs/heads/", |
| 2404 | &branch_name)) |
| 2405 | continue; |
| 2406 | branch = branch_get(branch_name); |
| 2407 | |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 2408 | if (atom->u.remote_ref.push_remote) |
| 2409 | refname = NULL; |
| 2410 | else { |
| 2411 | refname = branch_get_push(branch, NULL); |
| 2412 | if (!refname) |
| 2413 | continue; |
| 2414 | } |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2415 | /* We will definitely re-init v->s on the next line. */ |
| 2416 | free((char *)v->s); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 2417 | fill_remote_ref_details(atom, refname, branch, &v->s); |
| 2418 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2419 | } else if (atom_type == ATOM_COLOR) { |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2420 | v->s = xstrdup(atom->u.color); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2421 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2422 | } else if (atom_type == ATOM_FLAG) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2423 | char buf[256], *cp = buf; |
| 2424 | if (ref->flag & REF_ISSYMREF) |
| 2425 | cp = copy_advance(cp, ",symref"); |
| 2426 | if (ref->flag & REF_ISPACKED) |
| 2427 | cp = copy_advance(cp, ",packed"); |
| 2428 | if (cp == buf) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2429 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2430 | else { |
| 2431 | *cp = '\0'; |
| 2432 | v->s = xstrdup(buf + 1); |
| 2433 | } |
| 2434 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2435 | } else if (!deref && atom_type == ATOM_OBJECTNAME && |
| 2436 | grab_oid(name, "objectname", &ref->objectname, v, atom)) { |
| 2437 | continue; |
| 2438 | } else if (atom_type == ATOM_HEAD) { |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 2439 | if (atom->u.head && !strcmp(ref->refname, atom->u.head)) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2440 | v->s = xstrdup("*"); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2441 | else |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2442 | v->s = xstrdup(" "); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2443 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2444 | } else if (atom_type == ATOM_ALIGN) { |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 2445 | v->handler = align_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2446 | v->s = xstrdup(""); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 2447 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2448 | } else if (atom_type == ATOM_END) { |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 2449 | v->handler = end_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2450 | v->s = xstrdup(""); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 2451 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2452 | } else if (atom_type == ATOM_IF) { |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 2453 | const char *s; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 2454 | if (skip_prefix(name, "if:", &s)) |
| 2455 | v->s = xstrdup(s); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2456 | else |
| 2457 | v->s = xstrdup(""); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 2458 | v->handler = if_atom_handler; |
| 2459 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2460 | } else if (atom_type == ATOM_THEN) { |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 2461 | v->handler = then_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2462 | v->s = xstrdup(""); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 2463 | continue; |
ZheNing Hu | 1197f1a | 2021-05-13 15:15:38 +0000 | [diff] [blame] | 2464 | } else if (atom_type == ATOM_ELSE) { |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 2465 | v->handler = else_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2466 | v->s = xstrdup(""); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 2467 | continue; |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 2468 | } else if (atom_type == ATOM_REST) { |
| 2469 | if (ref->rest) |
| 2470 | v->s = xstrdup(ref->rest); |
| 2471 | else |
| 2472 | v->s = xstrdup(""); |
| 2473 | continue; |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 2474 | } else if (atom_type == ATOM_AHEADBEHIND) { |
| 2475 | if (ref->counts) { |
| 2476 | const struct ahead_behind_count *count; |
| 2477 | count = ref->counts[ahead_behind_atoms++]; |
| 2478 | v->s = xstrfmt("%d %d", count->ahead, count->behind); |
| 2479 | } else { |
| 2480 | /* Not a commit. */ |
| 2481 | v->s = xstrdup(""); |
| 2482 | } |
| 2483 | continue; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2484 | } else |
| 2485 | continue; |
| 2486 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2487 | if (!deref) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2488 | v->s = xstrdup(refname); |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 2489 | else |
| 2490 | v->s = xstrfmt("%s^{}", refname); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2491 | free((char *)refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | for (i = 0; i < used_atom_cnt; i++) { |
| 2495 | struct atom_value *v = &ref->value[i]; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2496 | if (v->s == NULL && used_atom[i].source == SOURCE_NONE) |
| 2497 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), |
| 2498 | oid_to_hex(&ref->objectname), ref->refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2499 | } |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2500 | |
| 2501 | if (need_tagged) |
| 2502 | oi.info.contentp = &oi.content; |
| 2503 | if (!memcmp(&oi.info, &empty, sizeof(empty)) && |
| 2504 | !memcmp(&oi_deref.info, &empty, sizeof(empty))) |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2505 | return 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2506 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2507 | |
| 2508 | oi.oid = ref->objectname; |
| 2509 | if (get_object(ref, 0, &obj, &oi, err)) |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2510 | return -1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2511 | |
| 2512 | /* |
| 2513 | * If there is no atom that wants to know about tagged |
| 2514 | * object, we are done. |
| 2515 | */ |
| 2516 | if (!need_tagged || (obj->type != OBJ_TAG)) |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2517 | return 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2518 | |
| 2519 | /* |
Victoria Dye | 188782e | 2023-11-14 19:53:57 +0000 | [diff] [blame] | 2520 | * If it is a tag object, see if we use the peeled value. If we do, |
| 2521 | * grab the peeled OID. |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2522 | */ |
Patrick Steinhardt | 30aaff4 | 2024-05-17 10:19:04 +0200 | [diff] [blame] | 2523 | if (need_tagged && peel_iterated_oid(the_repository, &obj->oid, &oi_deref.oid)) |
Victoria Dye | 188782e | 2023-11-14 19:53:57 +0000 | [diff] [blame] | 2524 | die("bad tag"); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2525 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 2526 | return get_object(ref, 1, &obj, &oi_deref, err); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | /* |
| 2530 | * Given a ref, return the value for the atom. This lazily gets value |
| 2531 | * out of the object by calling populate value. |
| 2532 | */ |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2533 | static int get_ref_atom_value(struct ref_array_item *ref, int atom, |
| 2534 | struct atom_value **v, struct strbuf *err) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2535 | { |
| 2536 | if (!ref->value) { |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2537 | if (populate_value(ref, err)) |
| 2538 | return -1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2539 | fill_missing_values(ref->value); |
| 2540 | } |
| 2541 | *v = &ref->value[atom]; |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2542 | return 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2543 | } |
| 2544 | |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 2545 | /* |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2546 | * Return 1 if the refname matches one of the patterns, otherwise 0. |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2547 | * A pattern can be a literal prefix (e.g. a refname "refs/heads/master" |
| 2548 | * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref |
| 2549 | * matches "refs/heads/mas*", too). |
| 2550 | */ |
Jeff King | 284c55d | 2023-07-10 17:12:16 -0400 | [diff] [blame] | 2551 | static int match_pattern(const char **patterns, const char *refname, |
| 2552 | int ignore_case) |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2553 | { |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2554 | unsigned flags = 0; |
| 2555 | |
Jeff King | 284c55d | 2023-07-10 17:12:16 -0400 | [diff] [blame] | 2556 | if (ignore_case) |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2557 | flags |= WM_CASEFOLD; |
| 2558 | |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2559 | /* |
| 2560 | * When no '--format' option is given we need to skip the prefix |
| 2561 | * for matching refs of tags and branches. |
| 2562 | */ |
| 2563 | (void)(skip_prefix(refname, "refs/tags/", &refname) || |
| 2564 | skip_prefix(refname, "refs/heads/", &refname) || |
| 2565 | skip_prefix(refname, "refs/remotes/", &refname) || |
| 2566 | skip_prefix(refname, "refs/", &refname)); |
| 2567 | |
| 2568 | for (; *patterns; patterns++) { |
Ævar Arnfjörð Bjarmason | 55d3426 | 2017-06-22 21:38:08 +0000 | [diff] [blame] | 2569 | if (!wildmatch(*patterns, refname, flags)) |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2570 | return 1; |
| 2571 | } |
| 2572 | return 0; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
| 2576 | * Return 1 if the refname matches one of the patterns, otherwise 0. |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2577 | * A pattern can be path prefix (e.g. a refname "refs/heads/master" |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2578 | * matches a pattern "refs/heads/" but not "refs/heads/m") or a |
| 2579 | * wildcard (e.g. the same ref matches "refs/heads/m*", too). |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2580 | */ |
Jeff King | 284c55d | 2023-07-10 17:12:16 -0400 | [diff] [blame] | 2581 | static int match_name_as_path(const char **pattern, const char *refname, |
| 2582 | int ignore_case) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2583 | { |
| 2584 | int namelen = strlen(refname); |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2585 | unsigned flags = WM_PATHNAME; |
| 2586 | |
Jeff King | 284c55d | 2023-07-10 17:12:16 -0400 | [diff] [blame] | 2587 | if (ignore_case) |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2588 | flags |= WM_CASEFOLD; |
| 2589 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2590 | for (; *pattern; pattern++) { |
| 2591 | const char *p = *pattern; |
| 2592 | int plen = strlen(p); |
| 2593 | |
| 2594 | if ((plen <= namelen) && |
| 2595 | !strncmp(refname, p, plen) && |
| 2596 | (refname[plen] == '\0' || |
| 2597 | refname[plen] == '/' || |
| 2598 | p[plen-1] == '/')) |
| 2599 | return 1; |
Aleksandr Makarov | 639ab5e | 2018-07-02 17:11:59 -0400 | [diff] [blame] | 2600 | if (!wildmatch(p, refname, flags)) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2601 | return 1; |
| 2602 | } |
| 2603 | return 0; |
| 2604 | } |
| 2605 | |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2606 | /* Return 1 if the refname matches one of the patterns, otherwise 0. */ |
| 2607 | static int filter_pattern_match(struct ref_filter *filter, const char *refname) |
| 2608 | { |
| 2609 | if (!*filter->name_patterns) |
| 2610 | return 1; /* No pattern always matches */ |
| 2611 | if (filter->match_as_path) |
Jeff King | 284c55d | 2023-07-10 17:12:16 -0400 | [diff] [blame] | 2612 | return match_name_as_path(filter->name_patterns, refname, |
| 2613 | filter->ignore_case); |
| 2614 | return match_pattern(filter->name_patterns, refname, |
| 2615 | filter->ignore_case); |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2616 | } |
| 2617 | |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 2618 | static int filter_exclude_match(struct ref_filter *filter, const char *refname) |
| 2619 | { |
| 2620 | if (!filter->exclude.nr) |
| 2621 | return 0; |
| 2622 | if (filter->match_as_path) |
| 2623 | return match_name_as_path(filter->exclude.v, refname, |
| 2624 | filter->ignore_case); |
| 2625 | return match_pattern(filter->exclude.v, refname, filter->ignore_case); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2626 | } |
| 2627 | |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2628 | /* |
| 2629 | * This is the same as for_each_fullref_in(), but it tries to iterate |
| 2630 | * only over the patterns we'll care about. Note that it _doesn't_ do a full |
| 2631 | * pattern match, so the callback still has to match each ref individually. |
| 2632 | */ |
| 2633 | static int for_each_fullref_in_pattern(struct ref_filter *filter, |
| 2634 | each_ref_fn cb, |
Jeff King | 67985e4 | 2021-09-24 14:48:48 -0400 | [diff] [blame] | 2635 | void *cb_data) |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2636 | { |
Patrick Steinhardt | f1701f2 | 2024-05-15 08:51:05 +0200 | [diff] [blame] | 2637 | if (filter->kind & FILTER_REFS_ROOT_REFS) { |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 2638 | /* In this case, we want to print all refs including root refs. */ |
| 2639 | return refs_for_each_include_root_refs(get_main_ref_store(the_repository), |
| 2640 | cb, cb_data); |
| 2641 | } |
| 2642 | |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2643 | if (!filter->match_as_path) { |
| 2644 | /* |
| 2645 | * in this case, the patterns are applied after |
| 2646 | * prefixes like "refs/heads/" etc. are stripped off, |
| 2647 | * so we have to look at everything: |
| 2648 | */ |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 2649 | return refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 2650 | "", NULL, cb, cb_data); |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
Jeff King | e674eb2 | 2018-07-02 17:12:42 -0400 | [diff] [blame] | 2653 | if (filter->ignore_case) { |
| 2654 | /* |
| 2655 | * we can't handle case-insensitive comparisons, |
| 2656 | * so just return everything and let the caller |
| 2657 | * sort it out. |
| 2658 | */ |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 2659 | return refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 2660 | "", NULL, cb, cb_data); |
Jeff King | e674eb2 | 2018-07-02 17:12:42 -0400 | [diff] [blame] | 2661 | } |
| 2662 | |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2663 | if (!filter->name_patterns[0]) { |
| 2664 | /* no patterns; we have to look at everything */ |
Taylor Blau | 59c35fa | 2023-07-10 17:12:28 -0400 | [diff] [blame] | 2665 | return refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 2666 | "", filter->exclude.v, cb, cb_data); |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2667 | } |
| 2668 | |
Jeff King | 91e2ab1 | 2022-12-13 06:11:10 -0500 | [diff] [blame] | 2669 | return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository), |
| 2670 | NULL, filter->name_patterns, |
Taylor Blau | 59c35fa | 2023-07-10 17:12:28 -0400 | [diff] [blame] | 2671 | filter->exclude.v, |
Jeff King | 91e2ab1 | 2022-12-13 06:11:10 -0500 | [diff] [blame] | 2672 | cb, cb_data); |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | /* |
Jeff King | c79eddf | 2020-03-30 10:04:11 -0400 | [diff] [blame] | 2676 | * Given a ref (oid, refname), check if the ref belongs to the array |
| 2677 | * of oids. If the given ref is a tag, check if the given tag points |
Jeff King | d9e0062 | 2023-07-02 18:38:29 -0400 | [diff] [blame] | 2678 | * at one of the oids in the given oid array. Returns non-zero if a |
| 2679 | * match is found. |
| 2680 | * |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 2681 | * NEEDSWORK: |
Jan Klötzke | 468887f | 2023-07-01 22:57:02 +0200 | [diff] [blame] | 2682 | * As the refs are cached we might know what refname peels to without |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 2683 | * the need to parse the object via parse_object(). peel_ref() might be a |
| 2684 | * more efficient alternative to obtain the pointee. |
| 2685 | */ |
Jeff King | d9e0062 | 2023-07-02 18:38:29 -0400 | [diff] [blame] | 2686 | static int match_points_at(struct oid_array *points_at, |
| 2687 | const struct object_id *oid, |
| 2688 | const char *refname) |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 2689 | { |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 2690 | struct object *obj; |
| 2691 | |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2692 | if (oid_array_lookup(points_at, oid) >= 0) |
Jeff King | d9e0062 | 2023-07-02 18:38:29 -0400 | [diff] [blame] | 2693 | return 1; |
Jeff King | 870eb53 | 2023-07-02 18:37:47 -0400 | [diff] [blame] | 2694 | obj = parse_object_with_flags(the_repository, oid, |
| 2695 | PARSE_OBJECT_SKIP_HASH_CHECK); |
Jan Klötzke | 468887f | 2023-07-01 22:57:02 +0200 | [diff] [blame] | 2696 | while (obj && obj->type == OBJ_TAG) { |
Jeff King | b9584c5 | 2023-07-02 18:35:40 -0400 | [diff] [blame] | 2697 | struct tag *tag = (struct tag *)obj; |
| 2698 | |
| 2699 | if (parse_tag(tag) < 0) { |
| 2700 | obj = NULL; |
| 2701 | break; |
| 2702 | } |
| 2703 | |
Jeff King | d9e0062 | 2023-07-02 18:38:29 -0400 | [diff] [blame] | 2704 | if (oid_array_lookup(points_at, get_tagged_oid(tag)) >= 0) |
| 2705 | return 1; |
Jeff King | b9584c5 | 2023-07-02 18:35:40 -0400 | [diff] [blame] | 2706 | |
| 2707 | obj = tag->tagged; |
Jan Klötzke | 468887f | 2023-07-01 22:57:02 +0200 | [diff] [blame] | 2708 | } |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 2709 | if (!obj) |
| 2710 | die(_("malformed object at '%s'"), refname); |
Jeff King | d9e0062 | 2023-07-02 18:38:29 -0400 | [diff] [blame] | 2711 | return 0; |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 2712 | } |
| 2713 | |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 2714 | /* |
| 2715 | * Allocate space for a new ref_array_item and copy the name and oid to it. |
| 2716 | * |
| 2717 | * Callers can then fill in other struct members at their leisure. |
| 2718 | */ |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2719 | static struct ref_array_item *new_ref_array_item(const char *refname, |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 2720 | const struct object_id *oid) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2721 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 2722 | struct ref_array_item *ref; |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 2723 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 2724 | FLEX_ALLOC_STR(ref, refname, refname); |
Jeff King | 53df97a | 2018-04-06 14:58:32 -0400 | [diff] [blame] | 2725 | oidcpy(&ref->objectname, oid); |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 2726 | ref->rest = NULL; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2727 | |
| 2728 | return ref; |
| 2729 | } |
| 2730 | |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2731 | static void ref_array_append(struct ref_array *array, struct ref_array_item *ref) |
| 2732 | { |
| 2733 | ALLOC_GROW(array->items, array->nr + 1, array->alloc); |
| 2734 | array->items[array->nr++] = ref; |
| 2735 | } |
| 2736 | |
Jeff King | 427cbc9 | 2018-04-06 14:59:45 -0400 | [diff] [blame] | 2737 | struct ref_array_item *ref_array_push(struct ref_array *array, |
| 2738 | const char *refname, |
| 2739 | const struct object_id *oid) |
| 2740 | { |
| 2741 | struct ref_array_item *ref = new_ref_array_item(refname, oid); |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2742 | ref_array_append(array, ref); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2743 | return ref; |
| 2744 | } |
| 2745 | |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 2746 | static int ref_kind_from_refname(const char *refname) |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2747 | { |
| 2748 | unsigned int i; |
| 2749 | |
| 2750 | static struct { |
| 2751 | const char *prefix; |
| 2752 | unsigned int kind; |
| 2753 | } ref_kind[] = { |
| 2754 | { "refs/heads/" , FILTER_REFS_BRANCHES }, |
| 2755 | { "refs/remotes/" , FILTER_REFS_REMOTES }, |
| 2756 | { "refs/tags/", FILTER_REFS_TAGS} |
| 2757 | }; |
| 2758 | |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 2759 | if (!strcmp(refname, "HEAD")) |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2760 | return FILTER_REFS_DETACHED_HEAD; |
| 2761 | |
| 2762 | for (i = 0; i < ARRAY_SIZE(ref_kind); i++) { |
| 2763 | if (starts_with(refname, ref_kind[i].prefix)) |
| 2764 | return ref_kind[i].kind; |
| 2765 | } |
| 2766 | |
Patrick Steinhardt | f1701f2 | 2024-05-15 08:51:05 +0200 | [diff] [blame] | 2767 | if (is_pseudo_ref(refname)) |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 2768 | return FILTER_REFS_PSEUDOREFS; |
Patrick Steinhardt | f1701f2 | 2024-05-15 08:51:05 +0200 | [diff] [blame] | 2769 | if (is_root_ref(refname)) |
| 2770 | return FILTER_REFS_ROOT_REFS; |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 2771 | |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2772 | return FILTER_REFS_OTHERS; |
| 2773 | } |
| 2774 | |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 2775 | static int filter_ref_kind(struct ref_filter *filter, const char *refname) |
| 2776 | { |
| 2777 | if (filter->kind == FILTER_REFS_BRANCHES || |
| 2778 | filter->kind == FILTER_REFS_REMOTES || |
| 2779 | filter->kind == FILTER_REFS_TAGS) |
| 2780 | return filter->kind; |
| 2781 | return ref_kind_from_refname(refname); |
| 2782 | } |
| 2783 | |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2784 | static struct ref_array_item *apply_ref_filter(const char *refname, const struct object_id *oid, |
| 2785 | int flag, struct ref_filter *filter) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2786 | { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2787 | struct ref_array_item *ref; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2788 | struct commit *commit = NULL; |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2789 | unsigned int kind; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2790 | |
| 2791 | if (flag & REF_BAD_NAME) { |
Nguyễn Thái Ngọc Duy | 1823c61 | 2016-02-27 13:42:04 +0700 | [diff] [blame] | 2792 | warning(_("ignoring ref with broken name %s"), refname); |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2793 | return NULL; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2794 | } |
| 2795 | |
Junio C Hamano | 7ebc8cb | 2015-08-03 11:01:10 -0700 | [diff] [blame] | 2796 | if (flag & REF_ISBROKEN) { |
Nguyễn Thái Ngọc Duy | 1823c61 | 2016-02-27 13:42:04 +0700 | [diff] [blame] | 2797 | warning(_("ignoring broken ref %s"), refname); |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2798 | return NULL; |
Junio C Hamano | 7ebc8cb | 2015-08-03 11:01:10 -0700 | [diff] [blame] | 2799 | } |
| 2800 | |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2801 | /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */ |
| 2802 | kind = filter_ref_kind(filter, refname); |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 2803 | |
| 2804 | /* |
| 2805 | * Generally HEAD refs are printed with special description denoting a rebase, |
| 2806 | * detached state and so forth. This is useful when only printing the HEAD ref |
Patrick Steinhardt | f1701f2 | 2024-05-15 08:51:05 +0200 | [diff] [blame] | 2807 | * But when it is being printed along with other root refs, it makes sense to |
| 2808 | * keep the formatting consistent. So we mask the type to act like a root ref. |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 2809 | */ |
Patrick Steinhardt | f1701f2 | 2024-05-15 08:51:05 +0200 | [diff] [blame] | 2810 | if (filter->kind & FILTER_REFS_ROOT_REFS && kind == FILTER_REFS_DETACHED_HEAD) |
| 2811 | kind = FILTER_REFS_ROOT_REFS; |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 2812 | else if (!(kind & filter->kind)) |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2813 | return NULL; |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2814 | |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 2815 | if (!filter_pattern_match(filter, refname)) |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2816 | return NULL; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2817 | |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 2818 | if (filter_exclude_match(filter, refname)) |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2819 | return NULL; |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 2820 | |
brian m. carlson | 4ce3621 | 2017-03-31 01:39:57 +0000 | [diff] [blame] | 2821 | if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname)) |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2822 | return NULL; |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 2823 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2824 | /* |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2825 | * A merge filter is applied on refs pointing to commits. Hence |
| 2826 | * obtain the commit using the 'oid' available and discard all |
| 2827 | * non-commits early. The actual filtering is done later. |
| 2828 | */ |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 2829 | if (filter->reachable_from || filter->unreachable_from || |
| 2830 | filter->with_commit || filter->no_commit || filter->verbose) { |
| 2831 | commit = lookup_commit_reference_gently(the_repository, oid, 1); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2832 | if (!commit) |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2833 | return NULL; |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 2834 | /* We perform the filtering for the '--contains' option... */ |
Karthik Nayak | ee2bd06 | 2015-07-07 21:36:16 +0530 | [diff] [blame] | 2835 | if (filter->with_commit && |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2836 | !commit_contains(filter, commit, filter->with_commit, &filter->internal.contains_cache)) |
| 2837 | return NULL; |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 2838 | /* ...or for the `--no-contains' option */ |
| 2839 | if (filter->no_commit && |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2840 | commit_contains(filter, commit, filter->no_commit, &filter->internal.no_contains_cache)) |
| 2841 | return NULL; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2842 | } |
| 2843 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2844 | /* |
| 2845 | * We do not open the object yet; sort may only need refname |
| 2846 | * to do its job and the resulting list may yet to be pruned |
| 2847 | * by maxcount logic. |
| 2848 | */ |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2849 | ref = new_ref_array_item(refname, oid); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2850 | ref->commit = commit; |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 2851 | ref->flag = flag; |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2852 | ref->kind = kind; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2853 | |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2854 | return ref; |
| 2855 | } |
| 2856 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2857 | struct ref_filter_cbdata { |
| 2858 | struct ref_array *array; |
| 2859 | struct ref_filter *filter; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2860 | }; |
| 2861 | |
| 2862 | /* |
| 2863 | * A call-back given to for_each_ref(). Filter refs and keep them for |
| 2864 | * later object processing. |
| 2865 | */ |
Victoria Dye | 9c575b0 | 2023-11-14 19:53:53 +0000 | [diff] [blame] | 2866 | static int filter_one(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2867 | { |
| 2868 | struct ref_filter_cbdata *ref_cbdata = cb_data; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2869 | struct ref_array_item *ref; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2870 | |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 2871 | ref = apply_ref_filter(refname, oid, flag, ref_cbdata->filter); |
| 2872 | if (ref) |
| 2873 | ref_array_append(ref_cbdata->array, ref); |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 2874 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2875 | return 0; |
| 2876 | } |
| 2877 | |
| 2878 | /* Free memory allocated for a ref_array_item */ |
| 2879 | static void free_array_item(struct ref_array_item *item) |
| 2880 | { |
| 2881 | free((char *)item->symref); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2882 | if (item->value) { |
Martin Ågren | 14d30cd | 2019-07-10 20:36:39 +0200 | [diff] [blame] | 2883 | int i; |
| 2884 | for (i = 0; i < used_atom_cnt; i++) |
| 2885 | free((char *)item->value[i].s); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2886 | free(item->value); |
| 2887 | } |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 2888 | free(item->counts); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2889 | free(item); |
| 2890 | } |
| 2891 | |
Victoria Dye | bd98f97 | 2023-11-14 19:53:55 +0000 | [diff] [blame] | 2892 | struct ref_filter_and_format_cbdata { |
| 2893 | struct ref_filter *filter; |
| 2894 | struct ref_format *format; |
| 2895 | |
| 2896 | struct ref_filter_and_format_internal { |
| 2897 | int count; |
| 2898 | } internal; |
| 2899 | }; |
| 2900 | |
| 2901 | static int filter_and_format_one(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
| 2902 | { |
| 2903 | struct ref_filter_and_format_cbdata *ref_cbdata = cb_data; |
| 2904 | struct ref_array_item *ref; |
| 2905 | struct strbuf output = STRBUF_INIT, err = STRBUF_INIT; |
| 2906 | |
| 2907 | ref = apply_ref_filter(refname, oid, flag, ref_cbdata->filter); |
| 2908 | if (!ref) |
| 2909 | return 0; |
| 2910 | |
| 2911 | if (format_ref_array_item(ref, ref_cbdata->format, &output, &err)) |
| 2912 | die("%s", err.buf); |
| 2913 | |
| 2914 | if (output.len || !ref_cbdata->format->array_opts.omit_empty) { |
| 2915 | fwrite(output.buf, 1, output.len, stdout); |
| 2916 | putchar('\n'); |
| 2917 | } |
| 2918 | |
| 2919 | strbuf_release(&output); |
| 2920 | strbuf_release(&err); |
| 2921 | free_array_item(ref); |
| 2922 | |
| 2923 | /* |
| 2924 | * Increment the running count of refs that match the filter. If |
| 2925 | * max_count is set and we've reached the max, stop the ref |
| 2926 | * iteration by returning a nonzero value. |
| 2927 | */ |
| 2928 | if (ref_cbdata->format->array_opts.max_count && |
| 2929 | ++ref_cbdata->internal.count >= ref_cbdata->format->array_opts.max_count) |
| 2930 | return 1; |
| 2931 | |
| 2932 | return 0; |
| 2933 | } |
| 2934 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2935 | /* Free all memory allocated for ref_array */ |
| 2936 | void ref_array_clear(struct ref_array *array) |
| 2937 | { |
| 2938 | int i; |
| 2939 | |
| 2940 | for (i = 0; i < array->nr; i++) |
| 2941 | free_array_item(array->items[i]); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 2942 | FREE_AND_NULL(array->items); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2943 | array->nr = array->alloc = 0; |
Martin Ågren | 14d30cd | 2019-07-10 20:36:39 +0200 | [diff] [blame] | 2944 | |
Andrzej Hunt | d7cf418 | 2021-07-25 15:08:24 +0200 | [diff] [blame] | 2945 | for (i = 0; i < used_atom_cnt; i++) { |
| 2946 | struct used_atom *atom = &used_atom[i]; |
| 2947 | if (atom->atom_type == ATOM_HEAD) |
| 2948 | free(atom->u.head); |
| 2949 | free((char *)atom->name); |
| 2950 | } |
Martin Ågren | 14d30cd | 2019-07-10 20:36:39 +0200 | [diff] [blame] | 2951 | FREE_AND_NULL(used_atom); |
| 2952 | used_atom_cnt = 0; |
| 2953 | |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2954 | if (ref_to_worktree_map.worktrees) { |
Elijah Newren | 6da1a25 | 2020-11-02 18:55:05 +0000 | [diff] [blame] | 2955 | hashmap_clear_and_free(&(ref_to_worktree_map.map), |
Eric Wong | c8e424c | 2019-10-06 23:30:40 +0000 | [diff] [blame] | 2956 | struct ref_to_worktree_entry, ent); |
Nickolai Belakovski | 2582083 | 2019-04-28 22:19:42 -0700 | [diff] [blame] | 2957 | free_worktrees(ref_to_worktree_map.worktrees); |
| 2958 | ref_to_worktree_map.worktrees = NULL; |
| 2959 | } |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 2960 | |
| 2961 | FREE_AND_NULL(array->counts); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2962 | } |
| 2963 | |
Aaron Lipman | a1b19aa | 2020-09-18 17:58:41 -0400 | [diff] [blame] | 2964 | #define EXCLUDE_REACHED 0 |
| 2965 | #define INCLUDE_REACHED 1 |
| 2966 | static void reach_filter(struct ref_array *array, |
Jeff King | 311bfe1 | 2023-07-10 17:12:10 -0400 | [diff] [blame] | 2967 | struct commit_list **check_reachable, |
Aaron Lipman | a1b19aa | 2020-09-18 17:58:41 -0400 | [diff] [blame] | 2968 | int include_reached) |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2969 | { |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2970 | int i, old_nr; |
René Scharfe | 5336d50 | 2020-09-26 10:37:29 +0200 | [diff] [blame] | 2971 | struct commit **to_clear; |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 2972 | |
Jeff King | 311bfe1 | 2023-07-10 17:12:10 -0400 | [diff] [blame] | 2973 | if (!*check_reachable) |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 2974 | return; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2975 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2976 | CALLOC_ARRAY(to_clear, array->nr); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2977 | for (i = 0; i < array->nr; i++) { |
| 2978 | struct ref_array_item *item = array->items[i]; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2979 | to_clear[i] = item->commit; |
| 2980 | } |
| 2981 | |
Derrick Stolee | cbfe360 | 2023-03-20 11:26:55 +0000 | [diff] [blame] | 2982 | tips_reachable_from_bases(the_repository, |
Jeff King | 311bfe1 | 2023-07-10 17:12:10 -0400 | [diff] [blame] | 2983 | *check_reachable, |
Derrick Stolee | cbfe360 | 2023-03-20 11:26:55 +0000 | [diff] [blame] | 2984 | to_clear, array->nr, |
| 2985 | UNINTERESTING); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2986 | |
| 2987 | old_nr = array->nr; |
| 2988 | array->nr = 0; |
| 2989 | |
| 2990 | for (i = 0; i < old_nr; i++) { |
| 2991 | struct ref_array_item *item = array->items[i]; |
| 2992 | struct commit *commit = item->commit; |
| 2993 | |
| 2994 | int is_merged = !!(commit->object.flags & UNINTERESTING); |
| 2995 | |
Aaron Lipman | a1b19aa | 2020-09-18 17:58:41 -0400 | [diff] [blame] | 2996 | if (is_merged == include_reached) |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2997 | array->items[array->nr++] = array->items[i]; |
| 2998 | else |
| 2999 | free_array_item(item); |
| 3000 | } |
| 3001 | |
René Scharfe | 5dee6d6 | 2017-12-25 18:44:12 +0100 | [diff] [blame] | 3002 | clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS); |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 3003 | |
Jeff King | 311bfe1 | 2023-07-10 17:12:10 -0400 | [diff] [blame] | 3004 | while (*check_reachable) { |
| 3005 | struct commit *merge_commit = pop_commit(check_reachable); |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 3006 | clear_commit_marks(merge_commit, ALL_REV_FLAGS); |
| 3007 | } |
| 3008 | |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 3009 | free(to_clear); |
| 3010 | } |
| 3011 | |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 3012 | void filter_ahead_behind(struct repository *r, |
| 3013 | struct ref_format *format, |
| 3014 | struct ref_array *array) |
| 3015 | { |
| 3016 | struct commit **commits; |
| 3017 | size_t commits_nr = format->bases.nr + array->nr; |
| 3018 | |
| 3019 | if (!format->bases.nr || !array->nr) |
| 3020 | return; |
| 3021 | |
| 3022 | ALLOC_ARRAY(commits, commits_nr); |
| 3023 | for (size_t i = 0; i < format->bases.nr; i++) |
| 3024 | commits[i] = format->bases.items[i].util; |
| 3025 | |
| 3026 | ALLOC_ARRAY(array->counts, st_mult(format->bases.nr, array->nr)); |
| 3027 | |
| 3028 | commits_nr = format->bases.nr; |
| 3029 | array->counts_nr = 0; |
| 3030 | for (size_t i = 0; i < array->nr; i++) { |
| 3031 | const char *name = array->items[i]->refname; |
| 3032 | commits[commits_nr] = lookup_commit_reference_by_name(name); |
| 3033 | |
| 3034 | if (!commits[commits_nr]) |
| 3035 | continue; |
| 3036 | |
| 3037 | CALLOC_ARRAY(array->items[i]->counts, format->bases.nr); |
| 3038 | for (size_t j = 0; j < format->bases.nr; j++) { |
| 3039 | struct ahead_behind_count *count; |
| 3040 | count = &array->counts[array->counts_nr++]; |
| 3041 | count->tip_index = commits_nr; |
| 3042 | count->base_index = j; |
| 3043 | |
| 3044 | array->items[i]->counts[j] = count; |
| 3045 | } |
| 3046 | commits_nr++; |
| 3047 | } |
| 3048 | |
| 3049 | ahead_behind(r, commits, commits_nr, array->counts, array->counts_nr); |
| 3050 | free(commits); |
| 3051 | } |
| 3052 | |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 3053 | static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref_fn fn, void *cb_data) |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 3054 | { |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 3055 | int ret = 0; |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 3056 | |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 3057 | filter->kind = type & FILTER_REFS_KIND_MASK; |
| 3058 | |
Victoria Dye | 6d6e5c5 | 2023-11-14 19:53:51 +0000 | [diff] [blame] | 3059 | init_contains_cache(&filter->internal.contains_cache); |
| 3060 | init_contains_cache(&filter->internal.no_contains_cache); |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 3061 | |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 3062 | /* Simple per-ref filtering */ |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 3063 | if (!filter->kind) |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 3064 | die("filter_refs: invalid type"); |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 3065 | else { |
| 3066 | /* |
| 3067 | * For common cases where we need only branches or remotes or tags, |
| 3068 | * we only iterate through those refs. If a mix of refs is needed, |
| 3069 | * we iterate over all refs and filter out required refs with the help |
| 3070 | * of filter_ref_kind(). |
| 3071 | */ |
| 3072 | if (filter->kind == FILTER_REFS_BRANCHES) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 3073 | ret = refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 3074 | "refs/heads/", NULL, |
| 3075 | fn, cb_data); |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 3076 | else if (filter->kind == FILTER_REFS_REMOTES) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 3077 | ret = refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 3078 | "refs/remotes/", NULL, |
| 3079 | fn, cb_data); |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 3080 | else if (filter->kind == FILTER_REFS_TAGS) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 3081 | ret = refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 3082 | "refs/tags/", NULL, fn, |
| 3083 | cb_data); |
Karthik Nayak | 810f7a1 | 2024-02-23 11:01:11 +0100 | [diff] [blame] | 3084 | else if (filter->kind & FILTER_REFS_REGULAR) |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 3085 | ret = for_each_fullref_in_pattern(filter, fn, cb_data); |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 3086 | |
| 3087 | /* |
| 3088 | * When printing all ref types, HEAD is already included, |
| 3089 | * so we don't want to print HEAD again. |
| 3090 | */ |
Patrick Steinhardt | f1701f2 | 2024-05-15 08:51:05 +0200 | [diff] [blame] | 3091 | if (!ret && !(filter->kind & FILTER_REFS_ROOT_REFS) && |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 3092 | (filter->kind & FILTER_REFS_DETACHED_HEAD)) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 3093 | refs_head_ref(get_main_ref_store(the_repository), fn, |
| 3094 | cb_data); |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 3095 | } |
| 3096 | |
Victoria Dye | 6d6e5c5 | 2023-11-14 19:53:51 +0000 | [diff] [blame] | 3097 | clear_contains_cache(&filter->internal.contains_cache); |
| 3098 | clear_contains_cache(&filter->internal.no_contains_cache); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 3099 | |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 3100 | return ret; |
| 3101 | } |
| 3102 | |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 3103 | /* |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 3104 | * API for filtering a set of refs. Based on the type of refs the user |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3105 | * has requested, we iterate through those refs and apply filters |
| 3106 | * as per the given ref_filter structure and finally store the |
| 3107 | * filtered refs in the ref_array structure. |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 3108 | */ |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 3109 | int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type) |
| 3110 | { |
| 3111 | struct ref_filter_cbdata ref_cbdata; |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 3112 | int save_commit_buffer_orig; |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 3113 | int ret = 0; |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 3114 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3115 | ref_cbdata.array = array; |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 3116 | ref_cbdata.filter = filter; |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 3117 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3118 | save_commit_buffer_orig = save_commit_buffer; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3119 | save_commit_buffer = 0; |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 3120 | |
Victoria Dye | 613d991 | 2023-11-14 19:53:54 +0000 | [diff] [blame] | 3121 | ret = do_filter_refs(filter, type, filter_one, &ref_cbdata); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3122 | |
| 3123 | /* Filters that need revision walking */ |
Jeff King | 311bfe1 | 2023-07-10 17:12:10 -0400 | [diff] [blame] | 3124 | reach_filter(array, &filter->reachable_from, INCLUDE_REACHED); |
| 3125 | reach_filter(array, &filter->unreachable_from, EXCLUDE_REACHED); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3126 | |
Jeff King | 359b01c | 2022-07-11 10:48:06 -0400 | [diff] [blame] | 3127 | save_commit_buffer = save_commit_buffer_orig; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3128 | return ret; |
| 3129 | } |
| 3130 | |
Victoria Dye | bd98f97 | 2023-11-14 19:53:55 +0000 | [diff] [blame] | 3131 | static inline int can_do_iterative_format(struct ref_filter *filter, |
| 3132 | struct ref_sorting *sorting, |
| 3133 | struct ref_format *format) |
| 3134 | { |
| 3135 | /* |
| 3136 | * Filtering & formatting results within a single ref iteration |
| 3137 | * callback is not compatible with options that require |
| 3138 | * post-processing a filtered ref_array. These include: |
| 3139 | * - filtering on reachability |
| 3140 | * - sorting the filtered results |
| 3141 | * - including ahead-behind information in the formatted output |
| 3142 | */ |
| 3143 | return !(filter->reachable_from || |
| 3144 | filter->unreachable_from || |
| 3145 | sorting || |
| 3146 | format->bases.nr); |
| 3147 | } |
| 3148 | |
Victoria Dye | e7574b0 | 2023-11-14 19:53:52 +0000 | [diff] [blame] | 3149 | void filter_and_format_refs(struct ref_filter *filter, unsigned int type, |
| 3150 | struct ref_sorting *sorting, |
| 3151 | struct ref_format *format) |
| 3152 | { |
Victoria Dye | bd98f97 | 2023-11-14 19:53:55 +0000 | [diff] [blame] | 3153 | if (can_do_iterative_format(filter, sorting, format)) { |
| 3154 | int save_commit_buffer_orig; |
| 3155 | struct ref_filter_and_format_cbdata ref_cbdata = { |
| 3156 | .filter = filter, |
| 3157 | .format = format, |
| 3158 | }; |
| 3159 | |
| 3160 | save_commit_buffer_orig = save_commit_buffer; |
| 3161 | save_commit_buffer = 0; |
| 3162 | |
| 3163 | do_filter_refs(filter, type, filter_and_format_one, &ref_cbdata); |
| 3164 | |
| 3165 | save_commit_buffer = save_commit_buffer_orig; |
| 3166 | } else { |
| 3167 | struct ref_array array = { 0 }; |
| 3168 | filter_refs(&array, filter, type); |
| 3169 | filter_ahead_behind(the_repository, format, &array); |
| 3170 | ref_array_sort(sorting, &array); |
| 3171 | print_formatted_ref_array(&array, format); |
| 3172 | ref_array_clear(&array); |
| 3173 | } |
Victoria Dye | e7574b0 | 2023-11-14 19:53:52 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 3176 | static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b) |
| 3177 | { |
| 3178 | if (!(a->kind ^ b->kind)) |
| 3179 | BUG("ref_kind_from_refname() should only mark one ref as HEAD"); |
| 3180 | if (a->kind & FILTER_REFS_DETACHED_HEAD) |
| 3181 | return -1; |
| 3182 | else if (b->kind & FILTER_REFS_DETACHED_HEAD) |
| 3183 | return 1; |
| 3184 | BUG("should have died in the xor check above"); |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 3188 | static int memcasecmp(const void *vs1, const void *vs2, size_t n) |
| 3189 | { |
| 3190 | const char *s1 = vs1, *s2 = vs2; |
| 3191 | const char *end = s1 + n; |
| 3192 | |
| 3193 | for (; s1 < end; s1++, s2++) { |
| 3194 | int diff = tolower(*s1) - tolower(*s2); |
| 3195 | if (diff) |
| 3196 | return diff; |
| 3197 | } |
| 3198 | return 0; |
| 3199 | } |
| 3200 | |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 3201 | struct ref_sorting { |
| 3202 | struct ref_sorting *next; |
| 3203 | int atom; /* index into used_atom array (internal) */ |
| 3204 | enum ref_sorting_order sort_flags; |
| 3205 | }; |
| 3206 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3207 | static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b) |
| 3208 | { |
| 3209 | struct atom_value *va, *vb; |
| 3210 | int cmp; |
Ævar Arnfjörð Bjarmason | 4045f65 | 2021-01-07 10:51:53 +0100 | [diff] [blame] | 3211 | int cmp_detached_head = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3212 | cmp_type cmp_type = used_atom[s->atom].type; |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3213 | struct strbuf err = STRBUF_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3214 | |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3215 | if (get_ref_atom_value(a, s->atom, &va, &err)) |
| 3216 | die("%s", err.buf); |
| 3217 | if (get_ref_atom_value(b, s->atom, &vb, &err)) |
| 3218 | die("%s", err.buf); |
| 3219 | strbuf_release(&err); |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 3220 | if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST && |
| 3221 | ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) { |
| 3222 | cmp = compare_detached_head(a, b); |
Ævar Arnfjörð Bjarmason | 4045f65 | 2021-01-07 10:51:53 +0100 | [diff] [blame] | 3223 | cmp_detached_head = 1; |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 3224 | } else if (s->sort_flags & REF_SORTING_VERSION) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3225 | cmp = versioncmp(va->s, vb->s); |
Ævar Arnfjörð Bjarmason | 75c50e5 | 2021-01-07 10:51:49 +0100 | [diff] [blame] | 3226 | } else if (cmp_type == FIELD_STR) { |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 3227 | if (va->s_size < 0 && vb->s_size < 0) { |
| 3228 | int (*cmp_fn)(const char *, const char *); |
| 3229 | cmp_fn = s->sort_flags & REF_SORTING_ICASE |
| 3230 | ? strcasecmp : strcmp; |
| 3231 | cmp = cmp_fn(va->s, vb->s); |
| 3232 | } else { |
| 3233 | size_t a_size = va->s_size < 0 ? |
| 3234 | strlen(va->s) : va->s_size; |
| 3235 | size_t b_size = vb->s_size < 0 ? |
| 3236 | strlen(vb->s) : vb->s_size; |
| 3237 | int (*cmp_fn)(const void *, const void *, size_t); |
| 3238 | cmp_fn = s->sort_flags & REF_SORTING_ICASE |
| 3239 | ? memcasecmp : memcmp; |
| 3240 | |
| 3241 | cmp = cmp_fn(va->s, vb->s, b_size > a_size ? |
| 3242 | a_size : b_size); |
| 3243 | if (!cmp) { |
| 3244 | if (a_size > b_size) |
| 3245 | cmp = 1; |
| 3246 | else if (a_size < b_size) |
| 3247 | cmp = -1; |
| 3248 | } |
| 3249 | } |
Ævar Arnfjörð Bjarmason | 75c50e5 | 2021-01-07 10:51:49 +0100 | [diff] [blame] | 3250 | } else { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3251 | if (va->value < vb->value) |
| 3252 | cmp = -1; |
| 3253 | else if (va->value == vb->value) |
Jeff King | 7c5045f | 2020-05-03 05:13:09 -0400 | [diff] [blame] | 3254 | cmp = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3255 | else |
| 3256 | cmp = 1; |
| 3257 | } |
| 3258 | |
Ævar Arnfjörð Bjarmason | 4045f65 | 2021-01-07 10:51:53 +0100 | [diff] [blame] | 3259 | return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head) |
| 3260 | ? -cmp : cmp; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3261 | } |
| 3262 | |
René Scharfe | 83fc4d6 | 2017-01-22 18:58:07 +0100 | [diff] [blame] | 3263 | static int compare_refs(const void *a_, const void *b_, void *ref_sorting) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3264 | { |
| 3265 | struct ref_array_item *a = *((struct ref_array_item **)a_); |
| 3266 | struct ref_array_item *b = *((struct ref_array_item **)b_); |
| 3267 | struct ref_sorting *s; |
| 3268 | |
| 3269 | for (s = ref_sorting; s; s = s->next) { |
| 3270 | int cmp = cmp_ref_sorting(s, a, b); |
| 3271 | if (cmp) |
| 3272 | return cmp; |
| 3273 | } |
Jeff King | 7c5045f | 2020-05-03 05:13:09 -0400 | [diff] [blame] | 3274 | s = ref_sorting; |
Ævar Arnfjörð Bjarmason | 7c269a7 | 2021-01-07 10:51:51 +0100 | [diff] [blame] | 3275 | return s && s->sort_flags & REF_SORTING_ICASE ? |
Jeff King | 7c5045f | 2020-05-03 05:13:09 -0400 | [diff] [blame] | 3276 | strcasecmp(a->refname, b->refname) : |
| 3277 | strcmp(a->refname, b->refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3278 | } |
| 3279 | |
Ævar Arnfjörð Bjarmason | 7c269a7 | 2021-01-07 10:51:51 +0100 | [diff] [blame] | 3280 | void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, |
| 3281 | unsigned int mask, int on) |
Jeff King | 76f9e56 | 2020-05-03 05:11:57 -0400 | [diff] [blame] | 3282 | { |
Ævar Arnfjörð Bjarmason | 7c269a7 | 2021-01-07 10:51:51 +0100 | [diff] [blame] | 3283 | for (; sorting; sorting = sorting->next) { |
| 3284 | if (on) |
| 3285 | sorting->sort_flags |= mask; |
| 3286 | else |
| 3287 | sorting->sort_flags &= ~mask; |
| 3288 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) |
| 3292 | { |
Victoria Dye | 56d26ad | 2023-11-14 19:53:49 +0000 | [diff] [blame] | 3293 | if (sorting) |
| 3294 | QSORT_S(array->items, array->nr, compare_refs, sorting); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3295 | } |
| 3296 | |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3297 | static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3298 | { |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3299 | struct strbuf *s = &state->stack->output; |
| 3300 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3301 | while (*cp && (!ep || cp < ep)) { |
| 3302 | if (*cp == '%') { |
| 3303 | if (cp[1] == '%') |
| 3304 | cp++; |
| 3305 | else { |
René Scharfe | d233097 | 2016-09-03 17:59:20 +0200 | [diff] [blame] | 3306 | int ch = hex2chr(cp + 1); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3307 | if (0 <= ch) { |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3308 | strbuf_addch(s, ch); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3309 | cp += 3; |
| 3310 | continue; |
| 3311 | } |
| 3312 | } |
| 3313 | } |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3314 | strbuf_addch(s, *cp); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3315 | cp++; |
| 3316 | } |
| 3317 | } |
| 3318 | |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3319 | int format_ref_array_item(struct ref_array_item *info, |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 3320 | struct ref_format *format, |
| 3321 | struct strbuf *final_buf, |
| 3322 | struct strbuf *error_buf) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3323 | { |
| 3324 | const char *cp, *sp, *ep; |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3325 | struct ref_formatting_state state = REF_FORMATTING_STATE_INIT; |
| 3326 | |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 3327 | state.quote_style = format->quote_style; |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3328 | push_stack_element(&state.stack); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3329 | |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 3330 | for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3331 | struct atom_value *atomv; |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3332 | int pos; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3333 | |
| 3334 | ep = strchr(sp, ')'); |
| 3335 | if (cp < sp) |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3336 | append_literal(cp, sp, &state); |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3337 | pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf); |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3338 | if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) || |
| 3339 | atomv->handler(atomv, &state, error_buf)) { |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3340 | pop_stack_element(&state.stack); |
| 3341 | return -1; |
| 3342 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3343 | } |
| 3344 | if (*cp) { |
| 3345 | sp = cp + strlen(cp); |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3346 | append_literal(cp, sp, &state); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3347 | } |
Jeff King | bf285ae | 2017-07-13 11:02:30 -0400 | [diff] [blame] | 3348 | if (format->need_color_reset_at_eol) { |
ZheNing Hu | bd0708c | 2021-07-26 03:26:47 +0000 | [diff] [blame] | 3349 | struct atom_value resetv = ATOM_VALUE_INIT; |
Jeff King | 51331aa | 2017-07-13 10:58:56 -0400 | [diff] [blame] | 3350 | resetv.s = GIT_COLOR_RESET; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3351 | if (append_atom(&resetv, &state, error_buf)) { |
| 3352 | pop_stack_element(&state.stack); |
| 3353 | return -1; |
| 3354 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3355 | } |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3356 | if (state.stack->prev) { |
| 3357 | pop_stack_element(&state.stack); |
| 3358 | return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing")); |
| 3359 | } |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 3360 | strbuf_addbuf(final_buf, &state.stack->output); |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 3361 | pop_stack_element(&state.stack); |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3362 | return 0; |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 3363 | } |
| 3364 | |
Victoria Dye | e7574b0 | 2023-11-14 19:53:52 +0000 | [diff] [blame] | 3365 | void print_formatted_ref_array(struct ref_array *array, struct ref_format *format) |
| 3366 | { |
| 3367 | int total; |
| 3368 | struct strbuf output = STRBUF_INIT, err = STRBUF_INIT; |
| 3369 | |
| 3370 | total = format->array_opts.max_count; |
| 3371 | if (!total || array->nr < total) |
| 3372 | total = array->nr; |
| 3373 | for (int i = 0; i < total; i++) { |
| 3374 | strbuf_reset(&err); |
| 3375 | strbuf_reset(&output); |
| 3376 | if (format_ref_array_item(array->items[i], format, &output, &err)) |
| 3377 | die("%s", err.buf); |
| 3378 | if (output.len || !format->array_opts.omit_empty) { |
| 3379 | fwrite(output.buf, 1, output.len, stdout); |
| 3380 | putchar('\n'); |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | strbuf_release(&err); |
| 3385 | strbuf_release(&output); |
| 3386 | } |
| 3387 | |
Jeff King | 53df97a | 2018-04-06 14:58:32 -0400 | [diff] [blame] | 3388 | void pretty_print_ref(const char *name, const struct object_id *oid, |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 3389 | struct ref_format *format) |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 3390 | { |
| 3391 | struct ref_array_item *ref_item; |
ZheNing Hu | 22f69a8 | 2021-04-19 11:28:44 +0000 | [diff] [blame] | 3392 | struct strbuf output = STRBUF_INIT; |
| 3393 | struct strbuf err = STRBUF_INIT; |
| 3394 | |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 3395 | ref_item = new_ref_array_item(name, oid); |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 3396 | ref_item->kind = ref_kind_from_refname(name); |
ZheNing Hu | 22f69a8 | 2021-04-19 11:28:44 +0000 | [diff] [blame] | 3397 | if (format_ref_array_item(ref_item, format, &output, &err)) |
| 3398 | die("%s", err.buf); |
| 3399 | fwrite(output.buf, 1, output.len, stdout); |
| 3400 | putchar('\n'); |
| 3401 | |
| 3402 | strbuf_release(&err); |
| 3403 | strbuf_release(&output); |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 3404 | free_array_item(ref_item); |
| 3405 | } |
| 3406 | |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 3407 | static int parse_sorting_atom(const char *atom) |
| 3408 | { |
Jeff King | ab7ded3 | 2017-07-13 11:06:40 -0400 | [diff] [blame] | 3409 | /* |
| 3410 | * This parses an atom using a dummy ref_format, since we don't |
| 3411 | * actually care about the formatting details. |
| 3412 | */ |
| 3413 | struct ref_format dummy = REF_FORMAT_INIT; |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 3414 | const char *end = atom + strlen(atom); |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 3415 | struct strbuf err = STRBUF_INIT; |
| 3416 | int res = parse_ref_filter_atom(&dummy, atom, end, &err); |
| 3417 | if (res < 0) |
| 3418 | die("%s", err.buf); |
| 3419 | strbuf_release(&err); |
| 3420 | return res; |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 3421 | } |
| 3422 | |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 3423 | static void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3424 | { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3425 | struct ref_sorting *s; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3426 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 3427 | CALLOC_ARRAY(s, 1); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3428 | s->next = *sorting_tail; |
| 3429 | *sorting_tail = s; |
| 3430 | |
| 3431 | if (*arg == '-') { |
Ævar Arnfjörð Bjarmason | 7c269a7 | 2021-01-07 10:51:51 +0100 | [diff] [blame] | 3432 | s->sort_flags |= REF_SORTING_REVERSE; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3433 | arg++; |
| 3434 | } |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 3435 | if (skip_prefix(arg, "version:", &arg) || |
| 3436 | skip_prefix(arg, "v:", &arg)) |
Ævar Arnfjörð Bjarmason | 7c269a7 | 2021-01-07 10:51:51 +0100 | [diff] [blame] | 3437 | s->sort_flags |= REF_SORTING_VERSION; |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 3438 | s->atom = parse_sorting_atom(arg); |
Jeff King | 18a2565 | 2017-07-13 11:02:44 -0400 | [diff] [blame] | 3439 | } |
| 3440 | |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 3441 | struct ref_sorting *ref_sorting_options(struct string_list *options) |
Jeff King | 18a2565 | 2017-07-13 11:02:44 -0400 | [diff] [blame] | 3442 | { |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 3443 | struct string_list_item *item; |
| 3444 | struct ref_sorting *sorting = NULL, **tail = &sorting; |
Jeff King | 95be717 | 2019-03-20 16:22:15 -0400 | [diff] [blame] | 3445 | |
Victoria Dye | 56d26ad | 2023-11-14 19:53:49 +0000 | [diff] [blame] | 3446 | if (options->nr) { |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 3447 | for_each_string_list_item(item, options) |
| 3448 | parse_ref_sorting(tail, item->string); |
| 3449 | } |
| 3450 | |
| 3451 | /* |
| 3452 | * From here on, the ref_sorting list should be used to talk |
| 3453 | * about the sort order used for the output. The caller |
| 3454 | * should not touch the string form anymore. |
| 3455 | */ |
| 3456 | string_list_clear(options, 0); |
| 3457 | return sorting; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 3458 | } |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 3459 | |
Ævar Arnfjörð Bjarmason | e5fb028 | 2021-10-20 20:27:20 +0200 | [diff] [blame] | 3460 | void ref_sorting_release(struct ref_sorting *sorting) |
| 3461 | { |
| 3462 | while (sorting) { |
| 3463 | struct ref_sorting *next = sorting->next; |
| 3464 | free(sorting); |
| 3465 | sorting = next; |
| 3466 | } |
| 3467 | } |
| 3468 | |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 3469 | int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset) |
| 3470 | { |
| 3471 | struct ref_filter *rf = opt->value; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 3472 | struct object_id oid; |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 3473 | struct commit *merge_commit; |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 3474 | |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 3475 | BUG_ON_OPT_NEG(unset); |
| 3476 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 3477 | if (repo_get_oid(the_repository, arg, &oid)) |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 3478 | die(_("malformed object name %s"), arg); |
| 3479 | |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 3480 | merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0); |
| 3481 | |
| 3482 | if (!merge_commit) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 3483 | return error(_("option `%s' must point to a commit"), opt->long_name); |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 3484 | |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 3485 | if (starts_with(opt->long_name, "no")) |
| 3486 | commit_list_insert(merge_commit, &rf->unreachable_from); |
| 3487 | else |
| 3488 | commit_list_insert(merge_commit, &rf->reachable_from); |
| 3489 | |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 3490 | return 0; |
| 3491 | } |
Jeff King | b571fb9 | 2023-07-10 17:12:13 -0400 | [diff] [blame] | 3492 | |
| 3493 | void ref_filter_init(struct ref_filter *filter) |
| 3494 | { |
| 3495 | struct ref_filter blank = REF_FILTER_INIT; |
| 3496 | memcpy(filter, &blank, sizeof(blank)); |
| 3497 | } |
| 3498 | |
| 3499 | void ref_filter_clear(struct ref_filter *filter) |
| 3500 | { |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 3501 | strvec_clear(&filter->exclude); |
Jeff King | b571fb9 | 2023-07-10 17:12:13 -0400 | [diff] [blame] | 3502 | oid_array_clear(&filter->points_at); |
| 3503 | free_commit_list(filter->with_commit); |
| 3504 | free_commit_list(filter->no_commit); |
| 3505 | free_commit_list(filter->reachable_from); |
| 3506 | free_commit_list(filter->unreachable_from); |
| 3507 | ref_filter_init(filter); |
| 3508 | } |