Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error combining git repositories into subdirs

Tags:

git

history

I have looked at several threads addressing this.

Combining multiple git repositories

Combining multiple git repositories having a space in their name

I also looked at the git filter-branch manpage.


update

I have changed to a 2 script system:

#!/bin/bash
git filter-branch --index-filter '~/doit.sh' HEAD

and doit.sh

#!/bin/bash
git ls-files -s | \ 
    sed "s-\t-&data/perl_modules/-" | \ 
    GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info && \
    mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"

This avoids the previous error, but now gets this (replaced path with [...]):

Rewrite c35e4ef0626fb2045f57fa5b605e7663b8d06196 (1/10977)mv: cannot stat `[...]/.git-rewrite/t/../index.new': No such file or directory
index filter failed: ~/doit.sh

When I run the

ls-files- s | sed ... | git update-index ...

I get the index file it should generate. As well when I change the doit.sh file to output the result of sed instead of piping it to git update-index it appears to produce the proper output... it seems git update-index is simply not creating the file when run under --index-filter....


Update again:

When I change

mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"

to

mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" || true

It fails the first mv, but all the others (so far) are working.


All of this culminated in this script:

git filter-branch --index-filter \
    'git ls-files -s | sed "s-\t\"*-&data/perl_modules/-" |
            GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                    git update-index --index-info &&
     mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

Theoretically this script should take all the files in the repository, shove them into data/perl_modules/, and rewrite history so that it appears the files have always been in that directory.

However I get this error:

fatal: ambiguous argument 'ls-files': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

Not sure how to proceed, I don't understand the script well enough to debug it, and it is taken directly from the git filter-branch manpage.

I have tried this both before and after manually moving the files to the subdir in case it required they be moved, or required they not be moved.

like image 939
Exodist Avatar asked Oct 17 '11 18:10

Exodist


People also ask

Can I merge two git repositories?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.

How do I allow unrelated histories?

Option 1: Use '–allow-unrelated-histories' One way to solve the issue is to use the --allow-unrelated-histories git flag. Here the git command will look something like this: git pull origin master --allow-unrelated-histories . You can substitute origin with the remote repository you are pulling from.

How do I merge two branches?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.


2 Answers

There were a couple problems.

1) git filter-branch --index filter was not using the '...' properly.

I am not sure why this is, a bug with git-filter, an environment problem? who knows. But I was able to fix it by moving everything out of the '...' and putting it into a script file. I then called the script inside the ''

git filter-branch --index-filter '~/doit.sh' HEAD

And doit.sh:

#!/bin/bash

git ls-files -s | \ 
    sed "s-\t-&data/perl_modules/-" | \ 
    GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info

mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" || true

2) The first commit was empty, so there was no index.new to mv

This git repo was imported from svn using git-svn. As such the first commit was completely empty simply saying initial svn repo initialization. As such there were no files moved, and no index.new to move. This was solved by adding || true to the mv command. Note however that if more than one mv command fails you have other problems.

like image 124
Exodist Avatar answered Oct 03 '22 21:10

Exodist


looks like you have \ missing in some of the multiline breaks.

like image 37
Adam Dymitruk Avatar answered Oct 03 '22 22:10

Adam Dymitruk