0

I migrated my svn repository to git, however for some reason there are still trunk folders, which should not be the case. I want to remove these folders without damaging the history line.

This is the git repository I have at the moment:

- My repository
  -- Project1
     --- trunk
         ---- src
         ---- somefile.txt
         ---- someotherfile.cpp
         ---- andanotherfile.cpp

and this is what I want to have:

- My repository
  -- Project1
     --- src
     --- somefile.txt
     --- someotherfile.cpp
     --- andanotherfile.cpp

I got to know that the command filter-branch is used to for this, however I could not find an example which suits my case, apart from this and this, but then even there I felt a bit confused and not sure if I should try or not. Anyone ran into something similar and can confirm that those are the solutions?

Thanks in advance.

Update: So I got to know that the following command is used most of the time to do the deed:

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

indeed, I checked the sed line and it does tell me that it would remove the trunk and move whatever is inside, to the upper directory. However, the execution gives and error of:

../myrepository/.git-rewrite/t/../index.new': No such file or directory

I don't know why am I getting this error. I looked it up and it seems I need an addition to this command, a number before the HEAD, like 83df928c..HEAD. However, I don't know what this is for (I mean, revision number, most likely, but why?) or what is mine. Would appreciate help. Almost got it!

Community
  • 1
  • 1

2 Answers2

0

clone another repo to have a test

git filter-branch --tree-filter 'cd Project1;cp trunk/* ./; rm -rf trunk'
CherryDT
  • 25,571
  • 5
  • 49
  • 74
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Are you sure of this command? It gives error for me, saying that `cd: can't cd to Project1` and following `cp: cannot stat trunk/*': No such file or directory`, resulting with `/usr/lib/git-core/git-filter-branch: 361: /usr/lib/git-core/git-filter-branch: cannot open ../commit: No such file` and `../map/f4a19a767a46968b29e129c6d811b1cd3bd56eec: Directory nonexistent could not write rewritten commit`. –  May 17 '16 at 09:54
  • it seems that trunk is a feature of svn? Sorry, I know little about svn. What is the history like? Anyway, you could backup the repo and try every possible method. – ElpieKay May 17 '16 at 09:56
  • 1
    So the Project1 folder was not added into the repo from the root commit. When rewriting those commits which have not included Project1, cd,cp and rm would fail. I'll do more tests. – ElpieKay May 17 '16 at 09:59
  • I did git svn fetch, and then pushed all my projects to Git. Don't know what you mean by root commit? –  May 17 '16 at 10:00
  • 1
    In git, root commit is the first commit, or more precisely the commit that has no parent. – ElpieKay May 17 '16 at 10:03
  • @frageDE try `git filter-branch --tree-filter 'ls Project1/trunk | xargs -i -t mv Project1/trunk/{} Project1 | sh'` – ElpieKay May 18 '16 at 01:59
  • Will this effect other projects in the repository? Mind you, I have 30 some projects (with different names, obviously) and I don't want them to get effected. Also, I don't want a different branch. I want to stay in master, having these changes. –  May 18 '16 at 10:23
0

So, here it goes.

The command which does the magic is:

git filter-branch -f --index-filter \
    'git ls-files -s | sed "s-/trunk/-/-" |
            GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
            git update-index --index-info &&
     mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' af117ec86a30ba6676571cd1f73083c5cfbec0bd..HEAD

note the commit tag before ..HEAD, that should be either second or third earliest commit tag that you should enter. If you enter the first, you get an error.

To find out the commit tag of the earliest commit, simply use:

git log

and scroll down as much as you can, there you will see the earliest commits. Take the second or third. It's done.