@matheusbsbs / matheus-bagliano / commits / 72303d44e9
Fork 0

Implemented 'tag' command in fast-import.

Tags received from the frontend are generated in memory in a simple linked list in the order that the tag commands were sent by the frontend. If multiple different tag objects for the same tag name get generated the last one sent by the frontend will be the one that gets written out at termination. Multiple tag objects for the same name will cause all older tags of the same name to be lost. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 24, 2006 at 03:12 UTC 72303d44e9d8f3fc9bef039b472a2bd259509420
1 file changed +125
fast-import.c
+125
@@ -185,6 +185,13 @@ struct branch
185 unsigned char sha1[20];
186 };
187
188 +struct tag
189 +{
190 + struct tag *next_tag;
191 + const char *name;
192 + unsigned char sha1[20];
193 +};
194 +
195
196 /* Stats and misc. counters */
197 static unsigned long max_depth = 10;
@@ -234,6 +241,10 @@ static unsigned long branch_table_sz = 1039;
241 static struct branch **branch_table;
242 static struct branch *active_branches;
243
244 +/* Tag data */
245 +static struct tag *first_tag;
246 +static struct tag *last_tag;
247 +
248 /* Input stream parsing */
249 static struct strbuf command_buf;
250 static unsigned long next_mark;
@@ -970,6 +981,21 @@ static void dump_branches()
981 }
982 }
983
984 +static void dump_tags()
985 +{
986 + static const char *msg = "fast-import";
987 + struct tag *t;
988 + struct ref_lock *lock;
989 + char path[PATH_MAX];
990 +
991 + for (t = first_tag; t; t = t->next_tag) {
992 + sprintf(path, "refs/tags/%s", t->name);
993 + lock = lock_any_ref_for_update(path, NULL, 0);
994 + if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
995 + die("Can't write %s", path);
996 + }
997 +}
998 +
999 static void read_next_command()
1000 {
1001 read_line(&command_buf, stdin, '\n');
@@ -1306,6 +1332,102 @@ static void cmd_new_branch()
1332 die("An lf did not terminate the branch command as expected.");
1333 }
1334
1335 +static void cmd_new_tag()
1336 +{
1337 + char *str_uq;
1338 + const char *endp;
1339 + char *sp;
1340 + const char *from;
1341 + char *tagger;
1342 + struct branch *s;
1343 + void *msg;
1344 + size_t msglen;
1345 + char *body;
1346 + struct tag *t;
1347 + unsigned char sha1[20];
1348 +
1349 + /* Obtain the new tag name from the rest of our command */
1350 + sp = strchr(command_buf.buf, ' ') + 1;
1351 + str_uq = unquote_c_style(sp, &endp);
1352 + if (str_uq) {
1353 + if (*endp)
1354 + die("Garbage after tag name in: %s", command_buf.buf);
1355 + sp = str_uq;
1356 + }
1357 + t = pool_alloc(sizeof(struct tag));
1358 + t->next_tag = NULL;
1359 + t->name = pool_strdup(sp);
1360 + if (last_tag)
1361 + last_tag->next_tag = t;
1362 + else
1363 + first_tag = t;
1364 + last_tag = t;
1365 + if (str_uq)
1366 + free(str_uq);
1367 + read_next_command();
1368 +
1369 + /* from ... */
1370 + if (strncmp("from ", command_buf.buf, 5))
1371 + die("Expected from command, got %s", command_buf.buf);
1372 +
1373 + from = strchr(command_buf.buf, ' ') + 1;
1374 + str_uq = unquote_c_style(from, &endp);
1375 + if (str_uq) {
1376 + if (*endp)
1377 + die("Garbage after string in: %s", command_buf.buf);
1378 + from = str_uq;
1379 + }
1380 +
1381 + s = lookup_branch(from);
1382 + if (s) {
1383 + memcpy(sha1, s->sha1, 20);
1384 + } else if (*from == ':') {
1385 + unsigned long idnum = strtoul(from + 1, NULL, 10);
1386 + struct object_entry *oe = find_mark(idnum);
1387 + if (oe->type != OBJ_COMMIT)
1388 + die("Mark :%lu not a commit", idnum);
1389 + memcpy(sha1, oe->sha1, 20);
1390 + } else if (!get_sha1(from, sha1)) {
1391 + unsigned long size;
1392 + char *buf;
1393 +
1394 + buf = read_object_with_reference(sha1,
1395 + type_names[OBJ_COMMIT], &size, sha1);
1396 + if (!buf || size < 46)
1397 + die("Not a valid commit: %s", from);
1398 + free(buf);
1399 + } else
1400 + die("Invalid ref name or SHA1 expression: %s", from);
1401 +
1402 + if (str_uq)
1403 + free(str_uq);
1404 + read_next_command();
1405 +
1406 + /* tagger ... */
1407 + if (strncmp("tagger ", command_buf.buf, 7))
1408 + die("Expected tagger command, got %s", command_buf.buf);
1409 + tagger = strdup(command_buf.buf);
1410 +
1411 + /* tag payload/message */
1412 + read_next_command();
1413 + msg = cmd_data(&msglen);
1414 +
1415 + /* build the tag object */
1416 + body = xmalloc(67 + strlen(t->name) + strlen(tagger) + msglen);
1417 + sp = body;
1418 + sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1419 + sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1420 + sp += sprintf(sp, "tag %s\n", t->name);
1421 + sp += sprintf(sp, "%s\n\n", tagger);
1422 + memcpy(sp, msg, msglen);
1423 + sp += msglen;
1424 + free(tagger);
1425 + free(msg);
1426 +
1427 + store_object(OBJ_TAG, body, sp - body, NULL, t->sha1, 0);
1428 + free(body);
1429 +}
1430 +
1431 static const char fast_import_usage[] =
1432 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] temp.pack";
1433
@@ -1367,6 +1489,8 @@ int main(int argc, const char **argv)
1489 cmd_new_branch();
1490 else if (!strncmp("commit ", command_buf.buf, 7))
1491 cmd_new_commit();
1492 + else if (!strncmp("tag ", command_buf.buf, 4))
1493 + cmd_new_tag();
1494 else
1495 die("Unsupported command: %s", command_buf.buf);
1496 }
@@ -1375,6 +1499,7 @@ int main(int argc, const char **argv)
1499 close(pack_fd);
1500 write_index(idx_name);
1501 dump_branches();
1502 + dump_tags();
1503
1504 fprintf(stderr, "%s statistics:\n", argv[0]);
1505 fprintf(stderr, "---------------------------------------------------\n");