Skip to contentSkip to conversation

Support gitlinks in fast-import.

RFC PATCH In review Alexander Gavrilov 2 participants 2 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 Support gitlinks in fast-import.

Currently fast-import/export cannot be used for repositories with submodules. This patch extends the relevant programs to make them correctly process gitlinks.

Links can be represented by two forms of the Modify command:

M 160000 SHA1 some/path

which sets the link target explicitly, or

M 160000 :mark some/path

where the mark refers to a commit. The latter form can be used by importing tools to build all submodules simultaneously in one physical repository, and then simply fetch them apart.

---

Changes:
1) Fixed the typo in the documentation.
2) Gitlinks are always exported as SHA.
3) Added some comments.
4) Added tests.

-- Alexander

 Documentation/git-fast-import.txt |    3 +
 builtin-fast-export.c             |   17 +++-
 fast-import.c                     |   14 +++-
 t/t9300-fast-import.sh            |  152 +++++++++++++++++++++++++++++++++++++
 t/t9301-fast-export.sh            |   42 ++++++++++
 5 files changed, 223 insertions(+), 5 deletions(-)
Documentation/git-fast-import.txt +3 -0
index 395c055..d510ddb 100644
@@ -481,6 +481,9 @@ in octal.  Git only supports the following modes:
   what you want.
 * `100755` or `755`: A normal, but executable, file.
 * `120000`: A symlink, the content of the file will be the link target.
+* `160000`: A gitlink, SHA-1 of the object refers to a commit in
+  another repository. Git links can only be specified by SHA or through
+  a commit mark. They are used to implement submodules.
 
 In both formats `<path>` is the complete path of the file to be added
 (if not already existing) or modified (if already existing).
builtin-fast-export.c +13 -4
index d0a462f..30ccbd5 100644
@@ -122,9 +122,16 @@ static void show_filemodify(struct diff_queue_struct *q,
 		if (is_null_sha1(spec->sha1))
 			printf("D %s\n", spec->path);
 		else {
-			struct object *object = lookup_object(spec->sha1);
-			printf("M %06o :%d %s\n", spec->mode,
-			       get_object_mark(object), spec->path);
+			/* Links refer to objects in another repositories, so
+			   output the SHA-1 verbatim */
+			if (S_ISGITLINK(spec->mode))
+				printf("M %06o %s %s\n", spec->mode,
+				       sha1_to_hex(spec->sha1), spec->path);
+			else {
+				struct object *object = lookup_object(spec->sha1);
+				printf("M %06o :%d %s\n", spec->mode,
+				       get_object_mark(object), spec->path);
+			}
 		}
 	}
 }
@@ -182,8 +189,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
 		diff_root_tree_sha1(commit->tree->object.sha1,
 				    "", &rev->diffopt);
 
+	/* Export the referenced blobs, and remember the marks */
 	for (i = 0; i < diff_queued_diff.nr; i++)
-		handle_object(diff_queued_diff.queue[i]->two->sha1);
+		if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
+			handle_object(diff_queued_diff.queue[i]->two->sha1);
 
 	mark_object(&commit->object);
 	if (!is_encoding_utf8(encoding))
fast-import.c +13 -1
index e72b286..cb6d1ee 100644
@@ -1868,6 +1868,7 @@ static void file_change_m(struct branch *b)
 	case S_IFREG | 0644:
 	case S_IFREG | 0755:
 	case S_IFLNK:
+	case S_IFGITLINK:
 	case 0644:
 	case 0755:
 		/* ok */
@@ -1900,7 +1901,18 @@ static void file_change_m(struct branch *b)
 		p = uq.buf;
 	}
 
-	if (inline_data) {
+	if (S_ISGITLINK(mode)) {
+		if (inline_data)
+			die("Git links cannot be specified 'inline': %s",
+				command_buf.buf);
+		else if (oe) {
+			if (oe->type != OBJ_COMMIT)
+				die("Not a commit (actually a %s): %s",
+					typename(oe->type), command_buf.buf);
+		}
+		/* else: accept the sha1 without check, as its object
+		         is expected to be in another repository */
+	} else if (inline_data) {
 		static struct strbuf buf = STRBUF_INIT;
 
 		if (p != uq.buf) {
t/t9300-fast-import.sh +152 -0

156 lines not on this page · raw message

t/t9301-fast-export.sh +42 -0

46 lines not on this page · raw message

Signature · 1 line
1.5.6.3.18.gfe82
#1 lore raw

Hi,

Quoted 5 lines · Alexander Gavrilov

Links can be represented by two forms of the Modify command:

M 160000 SHA1 some/path

Quoted 8 lines

which sets the link target explicitly, or

M 160000 :mark some/path

where the mark refers to a commit. The latter form can be used by importing tools to build all submodules simultaneously in one physical repository, and then simply fetch them apart.

It seems that people like to do this, have their submodule branches in the superproject. I wonder if we should bless this by installing an alternate in the "git submodule init"ed submodules, or even do it during a "git submodule update".

builtin-fast-export.c static void show_filemodify(struct diff_queue_struct *q, · line 125

Quoted 1 line
builtin-fast-export.c +1 -0
+			/* Links refer to objects in another repositories, so

s/another/other/

Thanks, especially for the tests,
Dscho