match-trees: compute buffer offset correctly when splicing
[gitweb.git] / builtin / fast-export.c
index 7d50f5414e2f3b1506ecbdfcfceff80696fbf055..9e283482efcfa6de0376cc9306061cea149b12df 100644 (file)
@@ -37,7 +37,10 @@ static int fake_missing_tagger;
 static int use_done_feature;
 static int no_data;
 static int full_tree;
+static int reference_excluded_commits;
+static int show_original_ids;
 static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
+static struct string_list tag_refs = STRING_LIST_INIT_NODUP;
 static struct refspec refspecs = REFSPEC_INIT_FETCH;
 static int anonymize;
 static struct revision_sources revision_sources;
@@ -187,6 +190,22 @@ static int get_object_mark(struct object *object)
        return ptr_to_mark(decoration);
 }
 
+static struct commit *rewrite_commit(struct commit *p)
+{
+       for (;;) {
+               if (p->parents && p->parents->next)
+                       break;
+               if (p->object.flags & UNINTERESTING)
+                       break;
+               if (!(p->object.flags & TREESAME))
+                       break;
+               if (!p->parents)
+                       return NULL;
+               p = p->parents->item;
+       }
+       return p;
+}
+
 static void show_progress(void)
 {
        static int counter = 0;
@@ -253,7 +272,10 @@ static void export_blob(const struct object_id *oid)
 
        mark_next_object(object);
 
-       printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
+       printf("blob\nmark :%"PRIu32"\n", last_idnum);
+       if (show_original_ids)
+               printf("original-oid %s\n", oid_to_hex(oid));
+       printf("data %"PRIuMAX"\n", (uintmax_t)size);
        if (size && fwrite(buf, size, 1, stdout) != 1)
                die_errno("could not write blob '%s'", oid_to_hex(oid));
        printf("\n");
@@ -580,7 +602,8 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
                message += 2;
 
        if (commit->parents &&
-           get_object_mark(&commit->parents->item->object) != 0 &&
+           (get_object_mark(&commit->parents->item->object) != 0 ||
+            reference_excluded_commits) &&
            !full_tree) {
                parse_commit_or_die(commit->parents->item);
                diff_tree_oid(get_commit_tree_oid(commit->parents->item),
@@ -596,6 +619,13 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
                        export_blob(&diff_queued_diff.queue[i]->two->oid);
 
        refname = *revision_sources_at(&revision_sources, commit);
+       /*
+        * FIXME: string_list_remove() below for each ref is overall
+        * O(N^2).  Compared to a history walk and diffing trees, this is
+        * just lost in the noise in practice.  However, theoretically a
+        * repo may have enough refs for this to become slow.
+        */
+       string_list_remove(&extra_refs, refname, 0);
        if (anonymize) {
                refname = anonymize_refname(refname);
                anonymize_ident_line(&committer, &committer_end);
@@ -609,8 +639,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
                reencoded = reencode_string(message, "UTF-8", encoding);
        if (!commit->parents)
                printf("reset %s\n", refname);
-       printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
-              refname, last_idnum,
+       printf("commit %s\nmark :%"PRIu32"\n", refname, last_idnum);
+       if (show_original_ids)
+               printf("original-oid %s\n", oid_to_hex(&commit->object.oid));
+       printf("%.*s\n%.*s\ndata %u\n%s",
               (int)(author_end - author), author,
               (int)(committer_end - committer), committer,
               (unsigned)(reencoded
@@ -621,13 +653,21 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
        unuse_commit_buffer(commit, commit_buffer);
 
        for (i = 0, p = commit->parents; p; p = p->next) {
-               int mark = get_object_mark(&p->item->object);
-               if (!mark)
+               struct object *obj = &p->item->object;
+               int mark = get_object_mark(obj);
+
+               if (!mark && !reference_excluded_commits)
                        continue;
                if (i == 0)
-                       printf("from :%d\n", mark);
+                       printf("from ");
+               else
+                       printf("merge ");
+               if (mark)
+                       printf(":%d\n", mark);
                else
-                       printf("merge :%d\n", mark);
+                       printf("%s\n", oid_to_hex(anonymize ?
+                                                 anonymize_oid(&obj->oid) :
+                                                 &obj->oid));
                i++;
        }
 
@@ -767,21 +807,12 @@ static void handle_tag(const char *name, struct tag *tag)
                                    oid_to_hex(&tag->object.oid),
                                    type_name(tagged->type));
                        }
-                       p = (struct commit *)tagged;
-                       for (;;) {
-                               if (p->parents && p->parents->next)
-                                       break;
-                               if (p->object.flags & UNINTERESTING)
-                                       break;
-                               if (!(p->object.flags & TREESAME))
-                                       break;
-                               if (!p->parents) {
-                                       printf("reset %s\nfrom %s\n\n",
-                                              name, oid_to_hex(&null_oid));
-                                       free(buf);
-                                       return;
-                               }
-                               p = p->parents->item;
+                       p = rewrite_commit((struct commit *)tagged);
+                       if (!p) {
+                               printf("reset %s\nfrom %s\n\n",
+                                      name, oid_to_hex(&null_oid));
+                               free(buf);
+                               return;
                        }
                        tagged_mark = get_object_mark(&p->object);
                }
@@ -789,8 +820,10 @@ static void handle_tag(const char *name, struct tag *tag)
 
        if (starts_with(name, "refs/tags/"))
                name += 10;
-       printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
-              name, tagged_mark,
+       printf("tag %s\nfrom :%d\n", name, tagged_mark);
+       if (show_original_ids)
+               printf("original-oid %s\n", oid_to_hex(&tag->object.oid));
+       printf("%.*s%sdata %d\n%.*s\n",
               (int)(tagger_end - tagger), tagger,
               tagger == tagger_end ? "" : "\n",
               (int)message_size, (int)message_size, message ? message : "");
@@ -808,7 +841,7 @@ static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
                /* handle nested tags */
                while (tag && tag->object.type == OBJ_TAG) {
                        parse_object(the_repository, &tag->object.oid);
-                       string_list_append(&extra_refs, full_name)->util = tag;
+                       string_list_append(&tag_refs, full_name)->util = tag;
                        tag = (struct tag *)tag->tagged;
                }
                if (!tag)
@@ -867,25 +900,30 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info)
                }
 
                /*
-                * This ref will not be updated through a commit, lets make
-                * sure it gets properly updated eventually.
+                * Make sure this ref gets properly updated eventually, whether
+                * through a commit or manually at the end.
                 */
-               if (*revision_sources_at(&revision_sources, commit) ||
-                   commit->object.flags & SHOWN)
+               if (e->item->type != OBJ_TAG)
                        string_list_append(&extra_refs, full_name)->util = commit;
+
                if (!*revision_sources_at(&revision_sources, commit))
                        *revision_sources_at(&revision_sources, commit) = full_name;
        }
+
+       string_list_sort(&extra_refs);
+       string_list_remove_duplicates(&extra_refs, 0);
 }
 
-static void handle_tags_and_duplicates(void)
+static void handle_tags_and_duplicates(struct string_list *extras)
 {
        struct commit *commit;
        int i;
 
-       for (i = extra_refs.nr - 1; i >= 0; i--) {
-               const char *name = extra_refs.items[i].string;
-               struct object *object = extra_refs.items[i].util;
+       for (i = extras->nr - 1; i >= 0; i--) {
+               const char *name = extras->items[i].string;
+               struct object *object = extras->items[i].util;
+               int mark;
+
                switch (object->type) {
                case OBJ_TAG:
                        handle_tag(name, (struct tag *)object);
@@ -894,9 +932,45 @@ static void handle_tags_and_duplicates(void)
                        if (anonymize)
                                name = anonymize_refname(name);
                        /* create refs pointing to already seen commits */
-                       commit = (struct commit *)object;
-                       printf("reset %s\nfrom :%d\n\n", name,
-                              get_object_mark(&commit->object));
+                       commit = rewrite_commit((struct commit *)object);
+                       if (!commit) {
+                               /*
+                                * Neither this object nor any of its
+                                * ancestors touch any relevant paths, so
+                                * it has been filtered to nothing.  Delete
+                                * it.
+                                */
+                               printf("reset %s\nfrom %s\n\n",
+                                      name, oid_to_hex(&null_oid));
+                               continue;
+                       }
+
+                       mark = get_object_mark(&commit->object);
+                       if (!mark) {
+                               /*
+                                * Getting here means we have a commit which
+                                * was excluded by a negative refspec (e.g.
+                                * fast-export ^master master).  If we are
+                                * referencing excluded commits, set the ref
+                                * to the exact commit.  Otherwise, the user
+                                * wants the branch exported but every commit
+                                * in its history to be deleted, which basically
+                                * just means deletion of the ref.
+                                */
+                               if (!reference_excluded_commits) {
+                                       /* delete the ref */
+                                       printf("reset %s\nfrom %s\n\n",
+                                              name, oid_to_hex(&null_oid));
+                                       continue;
+                               }
+                               /* set ref to commit using oid, not mark */
+                               printf("reset %s\nfrom %s\n\n", name,
+                                      oid_to_hex(&commit->object.oid));
+                               continue;
+                       }
+
+                       printf("reset %s\nfrom :%d\n\n", name, mark
+                              );
                        show_progress();
                        break;
                }
@@ -1028,6 +1102,11 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
                OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
                             N_("Apply refspec to exported refs")),
                OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
+               OPT_BOOL(0, "reference-excluded-parents",
+                        &reference_excluded_commits, N_("Reference parents which are not in fast-export stream by object id")),
+               OPT_BOOL(0, "show-original-ids", &show_original_ids,
+                           N_("Show original object ids of blobs/commits")),
+
                OPT_END()
        };
 
@@ -1084,7 +1163,8 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
                }
        }
 
-       handle_tags_and_duplicates();
+       handle_tags_and_duplicates(&extra_refs);
+       handle_tags_and_duplicates(&tag_refs);
        handle_deletes();
 
        if (export_filename && lastimportid != last_idnum)