Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I preserve tags when filtering a repository?

Is there a way to move a repository, and all its tags, to be a subdirectory of another repository in an automated fashion?

For reasons, a project was split into multiple repositories. Now we see that wasn't such a hot idea and want to bring it back together again. Submodules were considered, but they really are one project and we need to move a lot of files between them.

Let's say we have repository A, B and C. And we want one project with each of them as a subdirectory:

project/
    A/
    B/
    C/

That can be accomplished with git-filter-branch --tree-filter. Done, no problem. And it even does the branches correctly too, which was a pleasant surprise.

The trouble is, A, B and C have tags, and they all need to be converted, too.

Can this be automated?

like image 710
Schwern Avatar asked Apr 07 '11 00:04

Schwern


1 Answers

I think I can answer my own question. Adding --tag-name-filter cat to the git-filter-branch call. -- --all does all the branches and tags at once speeding up the conversion considerably. Here's the full call.

 git filter-branch --tree-filter 'move-to-subdir $subdir' --prune-empty --tag-name-filter cat -- --all

With move-to-subdir being a little program I wrote to make the subdirectory and move the files into it.

#!/usr/bin/env perl

use Path::Class;
use autodie;

my $subdir = shift;

mkdir $subdir unless -d $subdir;

my $cwd = dir(".");
my @kids = grep { $_ ne $subdir } $cwd->children;
for my $dir (@kids) {
    rename $dir, dir("$subdir/$dir")->cleanup;
}

This information is all in the git-filter-branch man page, but it's kind of obscured.

like image 131
Schwern Avatar answered Oct 15 '22 16:10

Schwern