]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fast-export: add a --show-original-ids option to show original names
authorElijah Newren <newren@gmail.com>
Fri, 16 Nov 2018 07:59:56 +0000 (23:59 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sat, 17 Nov 2018 09:43:52 +0000 (18:43 +0900)
Knowing the original names (hashes) of commits can sometimes enable
post-filtering that would otherwise be difficult or impossible.  In
particular, the desire to rewrite commit messages which refer to other
prior commits (on top of whatever other filtering is being done) is
very difficult without knowing the original names of each commit.

In addition, knowing the original names (hashes) of blobs can allow
filtering by blob-id without requiring re-hashing the content of the
blob, and is thus useful as a small optimization.

Once we add original ids for both commits and blobs, we may as well
add them for tags too for completeness.  Perhaps someone will have a
use for them.

This commit teaches a new --show-original-ids option to fast-export
which will make it add a 'original-oid <hash>' line to blob, commits,
and tags.  It also teaches fast-import to parse (and ignore) such
lines.

Signed-off-by: Elijah Newren <newren@gmail.com>
Acked-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-fast-export.txt
Documentation/git-fast-import.txt
builtin/fast-export.c
fast-import.c
t/t9350-fast-export.sh

index f65026662af8b332b6f3e9383c68c0c987992a05..64c01ba91884df1ec8e49ddc8fe852f1fb2a9425 100644 (file)
@@ -122,6 +122,13 @@ marks the same across runs.
        repository which already contains the necessary parent
        commits.
 
+--show-original-ids::
+       Add an extra directive to the output for commits and blobs,
+       `original-oid <SHA1SUM>`.  While such directives will likely be
+       ignored by importers such as git-fast-import, it may be useful
+       for intermediary filters (e.g. for rewriting commit messages
+       which refer to older commits, or for stripping blobs by id).
+
 --refspec::
        Apply the specified refspec to each ref exported. Multiple of them can
        be specified.
index 7ab97745a60daec812a286f61b9680f4714e2c38..43ab3b1637b50462ff62aa9c33c87184ef8ebc47 100644 (file)
@@ -385,6 +385,7 @@ change to the project.
 ....
        'commit' SP <ref> LF
        mark?
+       original-oid?
        ('author' (SP <name>)? SP LT <email> GT SP <when> LF)?
        'committer' (SP <name>)? SP LT <email> GT SP <when> LF
        data
@@ -741,6 +742,19 @@ New marks are created automatically.  Existing marks can be moved
 to another object simply by reusing the same `<idnum>` in another
 `mark` command.
 
+`original-oid`
+~~~~~~~~~~~~~~
+Provides the name of the object in the original source control system.
+fast-import will simply ignore this directive, but filter processes
+which operate on and modify the stream before feeding to fast-import
+may have uses for this information
+
+....
+       'original-oid' SP <object-identifier> LF
+....
+
+where `<object-identifer>` is any string not containing LF.
+
 `tag`
 ~~~~~
 Creates an annotated tag referring to a specific commit.  To create
@@ -749,6 +763,7 @@ lightweight (non-annotated) tags see the `reset` command below.
 ....
        'tag' SP <name> LF
        'from' SP <commit-ish> LF
+       original-oid?
        'tagger' (SP <name>)? SP LT <email> GT SP <when> LF
        data
 ....
@@ -823,6 +838,7 @@ assigned mark.
 ....
        'blob' LF
        mark?
+       original-oid?
        data
 ....
 
index 78fc67b03aad7ede3e796d61a52fc4a194270c76..36c2575de5251c38b29173af9362f52dd161191d 100644 (file)
@@ -38,6 +38,7 @@ 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;
@@ -271,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 %lu\n", size);
        if (size && fwrite(buf, size, 1, stdout) != 1)
                die_errno("could not write blob '%s'", oid_to_hex(oid));
        printf("\n");
@@ -635,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
@@ -814,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 : "");
@@ -1096,6 +1104,8 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
                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()
        };
index 555d49ad23b9de847858aa0b918b54da54184fea..71b6cba00f96af4abb2dd628344d78be9640ee88 100644 (file)
@@ -1814,6 +1814,13 @@ static void parse_mark(void)
                next_mark = 0;
 }
 
+static void parse_original_identifier(void)
+{
+       const char *v;
+       if (skip_prefix(command_buf.buf, "original-oid ", &v))
+               read_next_command();
+}
+
 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
 {
        const char *data;
@@ -1956,6 +1963,7 @@ static void parse_new_blob(void)
 {
        read_next_command();
        parse_mark();
+       parse_original_identifier();
        parse_and_store_blob(&last_blob, NULL, next_mark);
 }
 
@@ -2579,6 +2587,7 @@ static void parse_new_commit(const char *arg)
 
        read_next_command();
        parse_mark();
+       parse_original_identifier();
        if (skip_prefix(command_buf.buf, "author ", &v)) {
                author = parse_ident(v);
                read_next_command();
@@ -2711,6 +2720,9 @@ static void parse_new_tag(const char *arg)
                die("Invalid ref name or SHA1 expression: %s", from);
        read_next_command();
 
+       /* original-oid ... */
+       parse_original_identifier();
+
        /* tagger ... */
        if (skip_prefix(command_buf.buf, "tagger ", &v)) {
                tagger = parse_ident(v);
index d7d73061d0d95ef667753ca6f7be9a82c56ac779..5690fe28106624f3d666ccdda1dbcdd58673fb13 100755 (executable)
@@ -77,6 +77,23 @@ test_expect_success 'fast-export --reference-excluded-parents master~2..master'
                 test $MASTER = $(git rev-parse --verify refs/heads/rewrite))
 '
 
+test_expect_success 'fast-export --show-original-ids' '
+
+       git fast-export --show-original-ids master >output &&
+       grep ^original-oid output| sed -e s/^original-oid.// | sort >actual &&
+       git rev-list --objects master muss >objects-and-names &&
+       awk "{print \$1}" objects-and-names | sort >commits-trees-blobs &&
+       comm -23 actual commits-trees-blobs >unfound &&
+       test_must_be_empty unfound
+'
+
+test_expect_success 'fast-export --show-original-ids | git fast-import' '
+
+       git fast-export --show-original-ids master muss | git fast-import --quiet &&
+       test $MASTER = $(git rev-parse --verify refs/heads/master) &&
+       test $MUSS = $(git rev-parse --verify refs/tags/muss)
+'
+
 test_expect_success 'iso-8859-1' '
 
        git config i18n.commitencoding ISO8859-1 &&