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