Merge branch 'maint'
authorShawn O. Pearce <spearce@spearce.org>
Mon, 5 Mar 2007 17:49:02 +0000 (12:49 -0500)
committerShawn O. Pearce <spearce@spearce.org>
Mon, 5 Mar 2007 17:50:29 +0000 (12:50 -0500)
* maint:
fast-import: Fail if a non-existant commit is used for merge
fast-import: Avoid infinite loop after reset

[sp: Minor evil merge to deal with type_names array moving
to be private in 'master'.]

1  2 
fast-import.c
diff --combined fast-import.c
index 65e99c2e8b14840f70c027deb360d5d3c5292959,d9492b9884fbf78a5986f7701c79136c6f83798f..cc3347b23d0c7dba1bc05c65cb58215c92bb8547
@@@ -220,7 -220,8 +220,8 @@@ struct branc
        const char *name;
        struct tree_entry branch_tree;
        uintmax_t last_commit;
-       unsigned int pack_id;
+       unsigned active : 1;
+       unsigned pack_id : PACK_ID_BITS;
        unsigned char sha1[20];
  };
  
@@@ -528,6 -529,7 +529,7 @@@ static struct branch *new_branch(const 
        b->table_next_branch = branch_table[hc];
        b->branch_tree.versions[0].mode = S_IFDIR;
        b->branch_tree.versions[1].mode = S_IFDIR;
+       b->active = 0;
        b->pack_id = MAX_PACK_ID;
        branch_table[hc] = b;
        branch_count++;
@@@ -891,7 -893,7 +893,7 @@@ static int store_object
        SHA_CTX c;
        z_stream s;
  
 -      hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
 +      hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
                (unsigned long)datlen) + 1;
        SHA1_Init(&c);
        SHA1_Update(&c, hdr, hdrlen);
@@@ -1008,11 -1010,11 +1010,11 @@@ static void *gfi_unpack_entry
        struct object_entry *oe,
        unsigned long *sizep)
  {
 -      static char type[20];
 +      enum object_type type;
        struct packed_git *p = all_packs[oe->pack_id];
        if (p == pack_data)
                p->pack_size = pack_size + 20;
 -      return unpack_entry(p, oe->offset, type, sizep);
 +      return unpack_entry(p, oe->offset, &type, sizep);
  }
  
  static const char *get_mode(const char *str, uint16_t *modep)
@@@ -1049,9 -1051,9 +1051,9 @@@ static void load_tree(struct tree_entr
                t->delta_depth = 0;
                buf = gfi_unpack_entry(myoe, &size);
        } else {
 -              char type[20];
 -              buf = read_sha1_file(sha1, type, &size);
 -              if (!buf || strcmp(type, tree_type))
 +              enum object_type type;
 +              buf = read_sha1_file(sha1, &type, &size);
 +              if (!buf || type != OBJ_TREE)
                        die("Can't load tree %s", sha1_to_hex(sha1));
        }
  
@@@ -1312,7 -1314,7 +1314,7 @@@ static int update_branch(struct branch 
                        return error("Branch %s is missing commits.", b->name);
                }
  
 -              if (!in_merge_bases(old_cmit, new_cmit)) {
 +              if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
                        unlock_ref(lock);
                        warn("Not updating %s"
                                " (new tip %s does not contain %s)",
@@@ -1392,7 -1394,7 +1394,7 @@@ static void read_next_command(void
  
  static void cmd_mark(void)
  {
 -      if (!strncmp("mark :", command_buf.buf, 6)) {
 +      if (!prefixcmp(command_buf.buf, "mark :")) {
                next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
                read_next_command();
        }
@@@ -1405,10 -1407,10 +1407,10 @@@ static void *cmd_data (size_t *size
        size_t length;
        char *buffer;
  
 -      if (strncmp("data ", command_buf.buf, 5))
 +      if (prefixcmp(command_buf.buf, "data "))
                die("Expected 'data n' command, found: %s", command_buf.buf);
  
 -      if (!strncmp("<<", command_buf.buf + 5, 2)) {
 +      if (!prefixcmp(command_buf.buf + 5, "<<")) {
                char *term = xstrdup(command_buf.buf + 5 + 2);
                size_t sz = 8192, term_len = command_buf.len - 5 - 2;
                length = 0;
@@@ -1547,6 -1549,7 +1549,7 @@@ static void unload_one_branch(void
                        e = active_branches;
                        active_branches = e->active_next_branch;
                }
+               e->active = 0;
                e->active_next_branch = NULL;
                if (e->branch_tree.tree) {
                        release_tree_content_recursive(e->branch_tree.tree);
  static void load_branch(struct branch *b)
  {
        load_tree(&b->branch_tree);
-       b->active_next_branch = active_branches;
-       active_branches = b;
-       cur_active_branches++;
-       branch_load_count++;
+       if (!b->active) {
+               b->active = 1;
+               b->active_next_branch = active_branches;
+               active_branches = b;
+               cur_active_branches++;
+               branch_load_count++;
+       }
  }
  
  static void file_change_m(struct branch *b)
        struct object_entry *oe = oe;
        unsigned char sha1[20];
        uint16_t mode, inline_data = 0;
 -      char type[20];
  
        p = get_mode(p, &mode);
        if (!p)
                oe = find_mark(strtoumax(p + 1, &x, 10));
                hashcpy(sha1, oe->sha1);
                p = x;
 -      } else if (!strncmp("inline", p, 6)) {
 +      } else if (!prefixcmp(p, "inline")) {
                inline_data = 1;
                p += 6;
        } else {
        } else if (oe) {
                if (oe->type != OBJ_BLOB)
                        die("Not a blob (actually a %s): %s",
 -                              command_buf.buf, type_names[oe->type]);
 +                              command_buf.buf, typename(oe->type));
        } else {
 -              if (sha1_object_info(sha1, type, NULL))
 +              enum object_type type = sha1_object_info(sha1, NULL);
 +              if (type < 0)
                        die("Blob not found: %s", command_buf.buf);
 -              if (strcmp(blob_type, type))
 +              if (type != OBJ_BLOB)
                        die("Not a blob (actually a %s): %s",
 -                              command_buf.buf, type);
 +                          typename(type), command_buf.buf);
        }
  
        tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
@@@ -1668,7 -1674,7 +1674,7 @@@ static void cmd_from(struct branch *b
        const char *from;
        struct branch *s;
  
 -      if (strncmp("from ", command_buf.buf, 5))
 +      if (prefixcmp(command_buf.buf, "from "))
                return;
  
        if (b->branch_tree.tree) {
                        char *buf;
  
                        buf = read_object_with_reference(b->sha1,
 -                              type_names[OBJ_COMMIT], &size, b->sha1);
 +                              commit_type, &size, b->sha1);
                        if (!buf || size < 46)
                                die("Not a valid commit: %s", from);
                        if (memcmp("tree ", buf, 5)
@@@ -1734,7 -1740,7 +1740,7 @@@ static struct hash_list *cmd_merge(unsi
        struct branch *s;
  
        *count = 0;
 -      while (!strncmp("merge ", command_buf.buf, 6)) {
 +      while (!prefixcmp(command_buf.buf, "merge ")) {
                from = strchr(command_buf.buf, ' ') + 1;
                n = xmalloc(sizeof(*n));
                s = lookup_branch(from);
                        if (oe->type != OBJ_COMMIT)
                                die("Mark :%" PRIuMAX " not a commit", idnum);
                        hashcpy(n->sha1, oe->sha1);
-               } else if (get_sha1(from, n->sha1))
+               } else if (!get_sha1(from, n->sha1)) {
+                       unsigned long size;
+                       char *buf = read_object_with_reference(n->sha1,
 -                              type_names[OBJ_COMMIT], &size, n->sha1);
++                              commit_type, &size, n->sha1);
+                       if (!buf || size < 46)
+                               die("Not a valid commit: %s", from);
+                       free(buf);
+               } else
                        die("Invalid ref name or SHA1 expression: %s", from);
  
                n->next = NULL;
@@@ -1780,11 -1793,11 +1793,11 @@@ static void cmd_new_commit(void
  
        read_next_command();
        cmd_mark();
 -      if (!strncmp("author ", command_buf.buf, 7)) {
 +      if (!prefixcmp(command_buf.buf, "author ")) {
                author = parse_ident(command_buf.buf + 7);
                read_next_command();
        }
 -      if (!strncmp("committer ", command_buf.buf, 10)) {
 +      if (!prefixcmp(command_buf.buf, "committer ")) {
                committer = parse_ident(command_buf.buf + 10);
                read_next_command();
        }
        for (;;) {
                if (1 == command_buf.len)
                        break;
 -              else if (!strncmp("M ", command_buf.buf, 2))
 +              else if (!prefixcmp(command_buf.buf, "M "))
                        file_change_m(b);
 -              else if (!strncmp("D ", command_buf.buf, 2))
 +              else if (!prefixcmp(command_buf.buf, "D "))
                        file_change_d(b);
                else if (!strcmp("deleteall", command_buf.buf))
                        file_change_deleteall(b);
@@@ -1877,7 -1890,7 +1890,7 @@@ static void cmd_new_tag(void
        read_next_command();
  
        /* from ... */
 -      if (strncmp("from ", command_buf.buf, 5))
 +      if (prefixcmp(command_buf.buf, "from "))
                die("Expected from command, got %s", command_buf.buf);
        from = strchr(command_buf.buf, ' ') + 1;
        s = lookup_branch(from);
                char *buf;
  
                buf = read_object_with_reference(sha1,
 -                      type_names[OBJ_COMMIT], &size, sha1);
 +                      commit_type, &size, sha1);
                if (!buf || size < 46)
                        die("Not a valid commit: %s", from);
                free(buf);
        read_next_command();
  
        /* tagger ... */
 -      if (strncmp("tagger ", command_buf.buf, 7))
 +      if (prefixcmp(command_buf.buf, "tagger "))
                die("Expected tagger command, got %s", command_buf.buf);
        tagger = parse_ident(command_buf.buf + 7);
  
        size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
        sp = new_data.buffer;
        sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
 -      sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
 +      sp += sprintf(sp, "type %s\n", commit_type);
        sp += sprintf(sp, "tag %s\n", t->name);
        sp += sprintf(sp, "tagger %s\n", tagger);
        *sp++ = '\n';
@@@ -1981,7 -1994,7 +1994,7 @@@ int main(int argc, const char **argv
  
                if (*a != '-' || !strcmp(a, "--"))
                        break;
 -              else if (!strncmp(a, "--date-format=", 14)) {
 +              else if (!prefixcmp(a, "--date-format=")) {
                        const char *fmt = a + 14;
                        if (!strcmp(fmt, "raw"))
                                whenspec = WHENSPEC_RAW;
                        else
                                die("unknown --date-format argument %s", fmt);
                }
 -              else if (!strncmp(a, "--max-pack-size=", 16))
 +              else if (!prefixcmp(a, "--max-pack-size="))
                        max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
 -              else if (!strncmp(a, "--depth=", 8))
 +              else if (!prefixcmp(a, "--depth="))
                        max_depth = strtoul(a + 8, NULL, 0);
 -              else if (!strncmp(a, "--active-branches=", 18))
 +              else if (!prefixcmp(a, "--active-branches="))
                        max_active_branches = strtoul(a + 18, NULL, 0);
 -              else if (!strncmp(a, "--export-marks=", 15))
 +              else if (!prefixcmp(a, "--export-marks="))
                        mark_file = a + 15;
 -              else if (!strncmp(a, "--export-pack-edges=", 20)) {
 +              else if (!prefixcmp(a, "--export-pack-edges=")) {
                        if (pack_edges)
                                fclose(pack_edges);
                        pack_edges = fopen(a + 20, "a");
                        break;
                else if (!strcmp("blob", command_buf.buf))
                        cmd_new_blob();
 -              else if (!strncmp("commit ", command_buf.buf, 7))
 +              else if (!prefixcmp(command_buf.buf, "commit "))
                        cmd_new_commit();
 -              else if (!strncmp("tag ", command_buf.buf, 4))
 +              else if (!prefixcmp(command_buf.buf, "tag "))
                        cmd_new_tag();
 -              else if (!strncmp("reset ", command_buf.buf, 6))
 +              else if (!prefixcmp(command_buf.buf, "reset "))
                        cmd_reset_branch();
                else if (!strcmp("checkpoint", command_buf.buf))
                        cmd_checkpoint();