|
| 1 | +/* |
| 2 | + * Build a small history out of a tiny declarative input. Used by tests |
| 3 | + * that need specific merge topologies without long sequences of |
| 4 | + * plumbing commands or fragile shell helpers. |
| 5 | + * |
| 6 | + * The historian reads stdin line by line and emits an equivalent |
| 7 | + * stream to a `git fast-import` child process. It also allocates marks |
| 8 | + * for named objects so tests can refer to commits and blobs by name. |
| 9 | + * |
| 10 | + * Input directives (one per line, shell-style quoting): |
| 11 | + * |
| 12 | + * blob NAME LINE1 LINE2 ... |
| 13 | + * Each LINE becomes a content line in the blob; lines are |
| 14 | + * joined with '\n' and the blob ends with a final '\n'. With |
| 15 | + * no LINEs, the blob is empty. |
| 16 | + * |
| 17 | + * commit NAME BRANCH SUBJECT [from=PARENT] [merge=PARENT]... [PATH=BLOB]... |
| 18 | + * Creates a commit on refs/heads/BRANCH using the listed |
| 19 | + * file=blob mappings as the entire tree (no inheritance from |
| 20 | + * parents). Up to one `from=` and any number of `merge=` |
| 21 | + * parents may be given. `from=` defaults to the current branch |
| 22 | + * tip; if BRANCH has no tip yet, the commit becomes a root. |
| 23 | + * |
| 24 | + * Each `commit NAME` directive also creates a lightweight tag |
| 25 | + * `refs/tags/NAME` so tests can `git rev-parse NAME`. |
| 26 | + * |
| 27 | + * This helper trusts its caller; malformed input results in fast-import |
| 28 | + * errors. That is fine because test scripts feed it tightly controlled |
| 29 | + * input. |
| 30 | + */ |
| 31 | + |
| 32 | +#define USE_THE_REPOSITORY_VARIABLE |
| 33 | + |
| 34 | +#include "test-tool.h" |
| 35 | +#include "git-compat-util.h" |
| 36 | +#include "alias.h" |
| 37 | +#include "run-command.h" |
| 38 | +#include "setup.h" |
| 39 | +#include "strbuf.h" |
| 40 | +#include "strmap.h" |
| 41 | +#include "strvec.h" |
| 42 | + |
| 43 | +static int next_mark = 1; |
| 44 | + |
| 45 | +static int resolve_mark(struct strintmap *names, const char *name) |
| 46 | +{ |
| 47 | + int n = strintmap_get(names, name); |
| 48 | + if (!n) { |
| 49 | + n = next_mark++; |
| 50 | + strintmap_set(names, name, n); |
| 51 | + } |
| 52 | + return n; |
| 53 | +} |
| 54 | + |
| 55 | +static void emit_data(FILE *out, const char *data, size_t len) |
| 56 | +{ |
| 57 | + fprintf(out, "data %"PRIuMAX"\n", (uintmax_t)len); |
| 58 | + fwrite(data, 1, len, out); |
| 59 | + fputc('\n', out); |
| 60 | +} |
| 61 | + |
| 62 | +static void emit_blob(FILE *out, struct strintmap *names, |
| 63 | + int argc, const char **argv) |
| 64 | +{ |
| 65 | + struct strbuf content = STRBUF_INIT; |
| 66 | + int n = resolve_mark(names, argv[1]); |
| 67 | + int i; |
| 68 | + |
| 69 | + for (i = 2; i < argc; i++) { |
| 70 | + strbuf_addstr(&content, argv[i]); |
| 71 | + strbuf_addch(&content, '\n'); |
| 72 | + } |
| 73 | + |
| 74 | + fprintf(out, "blob\nmark :%d\n", n); |
| 75 | + emit_data(out, content.buf, content.len); |
| 76 | + strbuf_release(&content); |
| 77 | +} |
| 78 | + |
| 79 | +static void emit_tag(FILE *out, const char *name, int mark) |
| 80 | +{ |
| 81 | + fprintf(out, "reset refs/tags/%s\nfrom :%d\n\n", name, mark); |
| 82 | +} |
| 83 | + |
| 84 | +static void emit_commit(FILE *out, struct strintmap *names, |
| 85 | + int argc, const char **argv, int seq) |
| 86 | +{ |
| 87 | + int n = resolve_mark(names, argv[1]); |
| 88 | + const char *branch = argv[2]; |
| 89 | + const char *subject = argv[3]; |
| 90 | + const char *rest; |
| 91 | + int i; |
| 92 | + |
| 93 | + fprintf(out, "commit refs/heads/%s\nmark :%d\n", branch, n); |
| 94 | + fprintf(out, "author A <a@e> %d +0000\n", 1700000000 + seq); |
| 95 | + fprintf(out, "committer A <a@e> %d +0000\n", 1700000000 + seq); |
| 96 | + emit_data(out, subject, strlen(subject)); |
| 97 | + |
| 98 | + /* |
| 99 | + * fast-import requires `from` and `merge` to precede all file |
| 100 | + * operations; emit them first regardless of argv ordering. |
| 101 | + */ |
| 102 | + for (i = 4; i < argc; i++) { |
| 103 | + if (skip_prefix(argv[i], "from=", &rest)) |
| 104 | + fprintf(out, "from :%d\n", resolve_mark(names, rest)); |
| 105 | + else if (skip_prefix(argv[i], "merge=", &rest)) |
| 106 | + fprintf(out, "merge :%d\n", resolve_mark(names, rest)); |
| 107 | + } |
| 108 | + |
| 109 | + /* |
| 110 | + * The PATH=BLOB list is the entire tree; wipe whatever the |
| 111 | + * implicit parent contributed before re-applying it. |
| 112 | + */ |
| 113 | + fprintf(out, "deleteall\n"); |
| 114 | + for (i = 4; i < argc; i++) { |
| 115 | + const char *eq; |
| 116 | + size_t key_len; |
| 117 | + char *path; |
| 118 | + |
| 119 | + if (skip_prefix(argv[i], "from=", &rest) || |
| 120 | + skip_prefix(argv[i], "merge=", &rest)) |
| 121 | + continue; |
| 122 | + eq = strchr(argv[i], '='); |
| 123 | + if (!eq) |
| 124 | + die("bad commit spec '%s'", argv[i]); |
| 125 | + key_len = eq - argv[i]; |
| 126 | + path = xmemdupz(argv[i], key_len); |
| 127 | + fprintf(out, "M 100644 :%d %s\n", |
| 128 | + resolve_mark(names, eq + 1), path); |
| 129 | + free(path); |
| 130 | + } |
| 131 | + |
| 132 | + fputc('\n', out); |
| 133 | + emit_tag(out, argv[1], n); |
| 134 | +} |
| 135 | + |
| 136 | +int cmd__historian(int argc, const char **argv UNUSED) |
| 137 | +{ |
| 138 | + struct child_process fi = CHILD_PROCESS_INIT; |
| 139 | + struct strintmap names = STRINTMAP_INIT; |
| 140 | + struct strbuf line = STRBUF_INIT; |
| 141 | + int seq = 0; |
| 142 | + int ret = 0; |
| 143 | + FILE *fi_in; |
| 144 | + |
| 145 | + if (argc != 1) |
| 146 | + die("usage: test-tool historian <input"); |
| 147 | + |
| 148 | + setup_git_directory(); |
| 149 | + |
| 150 | + strvec_pushl(&fi.args, "fast-import", "--quiet", "--force", NULL); |
| 151 | + fi.git_cmd = 1; |
| 152 | + fi.in = -1; |
| 153 | + fi.no_stdout = 1; |
| 154 | + if (start_command(&fi)) |
| 155 | + die("failed to start git fast-import"); |
| 156 | + fi_in = xfdopen(fi.in, "w"); |
| 157 | + |
| 158 | + while (strbuf_getline_lf(&line, stdin) != EOF) { |
| 159 | + const char **a = NULL; |
| 160 | + int n; |
| 161 | + |
| 162 | + strbuf_trim(&line); |
| 163 | + if (!line.len || line.buf[0] == '#') |
| 164 | + continue; |
| 165 | + |
| 166 | + n = split_cmdline(line.buf, &a); |
| 167 | + if (n < 0) |
| 168 | + die("split_cmdline failed: %s", |
| 169 | + split_cmdline_strerror(n)); |
| 170 | + |
| 171 | + if (n >= 2 && !strcmp(a[0], "blob")) |
| 172 | + emit_blob(fi_in, &names, n, a); |
| 173 | + else if (n >= 4 && !strcmp(a[0], "commit")) |
| 174 | + emit_commit(fi_in, &names, n, a, seq++); |
| 175 | + else |
| 176 | + die("unknown directive: %s", a[0]); |
| 177 | + |
| 178 | + free(a); |
| 179 | + } |
| 180 | + |
| 181 | + if (fclose(fi_in)) |
| 182 | + die_errno("close fast-import stdin"); |
| 183 | + if (finish_command(&fi)) |
| 184 | + ret = 1; |
| 185 | + |
| 186 | + strbuf_release(&line); |
| 187 | + strintmap_clear(&names); |
| 188 | + return ret; |
| 189 | +} |
0 commit comments