Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: How to automate interactive rebase / replace it by equivalent git commands

I need to automate a interactive rebase or replace it by other commands. Just let me explain my current situation:

In an svn->git transition i need to rebase the newly created git repository to fix a "history cut-offs" made during SVN. Here is my manual workflow to fix the problem.

branchNEW: containing history from SOMEDAY until now
branchOLD: containing history from past to SOMEDAY

EDIT or as ascii:

branchNEW:        Y - Z
branchOLD: W - X

Both branches have no common commits.

The basic idea now is to just rebase branchNEW onto branchOLD. Unfortunately there have been some refactoring SOMEDAY: Some files were moved to another directory. The result of the rebase is now, that each moved file exists in both places.

EDIT

some file exist in X 
the (nearly) same files also exist in Y, just on another path

branchNEW: W - X - Y - Z
(after rebase)

After the rebase, HEAD now contains the files of X and of Y. I also tried to add a new commit to branchOLD which removes the old files. After the rebase SVN-HEAD and git-HEAD are binary identical, but the "git log --follow" does not work.

Now to the main problem: I am able to fix this by using a second, interactive rebase:

git rebase -i SHA

SHA is the sha-id of the old root commit in branchNEW. Now in the editor i have to change "pick" to "edit" for the topmost commit. After exiting the editor i now have to remove the wrong files

git rm -f fileA fileB
git commit --amend
git rebase --continue

After this HEAD of git is binary identical to head of SVN and in addition, git has the complete history and also "git log --follow" works for the moved files.

As this step is just a small part of a huge VCS transition in the future, i need to script the complete process. But how to automate the above steps?

(i know that SHAs won't stay the same, but i am able to get the required SHA from the svn-id which is embedded in each commit message)

like image 877
Henning Avatar asked Sep 19 '14 16:09

Henning


1 Answers

I found a possible solution:

git rebase --interactive sends the "list of rebase commits" (the list where you can pick, stash, edit, ...) to an editor. Which kind of editor can be configured. So the solution is to configure an alternative "editor" just for this interactive rebase. This can be done using the environment variable GIT_SEQUENCE_EDITOR:

GIT_SEQUENCE_EDITOR="command" git rebase -i SHA

command could be a shell script or just a simple sed:

GIT_SEQUENCE_EDITOR="sed -i 's/^pick ce5efdb /edit ce5efdb /;/^pick ce6efdb /d'" git rebase -i SHA

Important: The "list of rebase commits" is passed as file to the command. So the command must accept a filename as parameter and have to write the result to same file. "sed -i" is exactly doing this.

like image 122
Henning Avatar answered Sep 19 '22 23:09

Henning