Skip to contentSkip to conversation

Rough draft: Split a git repo into superproject and submodule

PATCH New Eric Kidd 1 participant 1 message Jun 15, 2016 Kbuild

lore
Share & tools
lore

Eric Kidd

author
#0 lore raw

PATCH 1/1 Rough draft: Split a git repo into superproject and submodule

I have some extremely large git repositories which I want to split into modules. This could be done using 'git filter-branch' and the following steps:

1) Create a submodule using --subdirectory-filter.
2) Create a superproject using an index filter to delete the submodule.
3) Commit the submodule to the latest version of the superproject.

Unfortunately, this approach loses all the historical connections between the superproject and the submodule, breaking tools like 'git bisect', and making it difficult to recover old releases.

Ideally, each version of the newly created superproject would be linked to the correct version of the submodule (and all the .gitmodules entries would be set up correctly, too, throughout the project's history).

The attached patch contains a _very_ rough draft of a git submodule-split command. You can run it as follows:

  git submodule-split original-repo superproject directory-to-split
  git clone superproject.git superproject
  cd superproject
  git submodule update --init

It will output two repositories named superproject.git and subproject.git. Internally, it runs 'git filter-branch' on a bare repository.

There's a test suite, too.

I still have quite a few things left to do before this script is actually useful:

Rename submodule-split to something more appropriate Update .gitmodules using git config
Merge new entries with existing .gitmodules
Add support for directory and repository names which differ Add support for multiple possible directory names
Add support for directories which move around the tree Add support for directories which are missing in some revisions

I'm releasing this version for feedback on the general design and the coding style. I don't often write shell scripts of this magnitude, and there are almost certainly some portability and style problems. So please let me know what needs improvement, and I'll try to fix it.

Thank you for your feedback!

---
 .gitignore                 |    1 +
 Makefile                   |    1 +
 git-submodule-split.sh     |  113 ++++++++++++++++++++++++++++++++++++++++++++
 t/t7404-submodule-split.sh |   36 ++++++++++++++
 4 files changed, 151 insertions(+), 0 deletions(-)
 create mode 100644 git-submodule-split.sh
 create mode 100755 t/t7404-submodule-split.sh
.gitignore +1 -0
index 13311f1..fa6ed07 100644
@@ -119,6 +119,7 @@ git-show
 git-show-branch
 git-show-index
 git-show-ref
+git-submodule-split
 git-stage
 git-stash
 git-status
Makefile +1 -0
index 27b9569..aceac8f 100644
@@ -277,6 +277,7 @@ SCRIPT_SH += git-sh-setup.sh
 SCRIPT_SH += git-stash.sh
 SCRIPT_SH += git-submodule.sh
 SCRIPT_SH += git-web--browse.sh
+SCRIPT_SH += git-submodule-split.sh
 
 SCRIPT_PERL += git-add--interactive.perl
 SCRIPT_PERL += git-archimport.perl
git-submodule-split.sh new file +113 -0

113 lines not on this page · raw message

t/t7404-submodule-split.sh new file +36 -0
new file mode 100755
index 0000000..b490c60
@@ -0,0 +1,36 @@
+#!/bin/sh
+#
+# Copyright 2009 Eric Kidd
+
+test_description='git submodule-split tests'
+. ./test-lib.sh
+
+rm -rf .git
+test_create_repo original
+
+test_expect_success \
+	'create original repository' \
+	'(cd original &&
+	  echo "In main project" > main-file &&
+	  mkdir sub1 &&
+	  echo "In sub1" > sub1/sub1-file &&
+	  git add . &&
+	  git commit -m "Original project and sub1")'
+
+test_expect_success \
+	'split out sub1' \
+	'git submodule-split original split1 sub1 &&
+	 git clone split1.git split1 &&
+	 test -f split1/main-file &&
+	 ! test -f split1/sub1/sub1-file &&
+	 git clone sub1.git sub1 &&
+	 test -f sub1/sub1-file'
+
+test_expect_success \
+	'compare split repositories with original' \
+	'rm -rf split1 &&
+	 git clone split1.git split1 &&
+	 (cd split1 && git submodule init sub1 && git submodule update) &&
+	 diff -uNr -x .git -x .gitmodules original split1'
+
+test_done
Signature · 1 line
1.6.0.4