Skip to content

Commit

Permalink
Add snippet for rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Oct 23, 2015
1 parent 25d6cfa commit 4d56c75
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ This project tries to provide a collection of ready-to-run snippets which try to
* [List remote heads/tags without a local clone](https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ListRemoteRepository.java)
* [Fetch from remote repositories](https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/FetchRemoteCommits.java)
* [Clone a remote reppository via SSH protocol and username/password credentials](https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/CloneRemoteRepositoryWithAuthentication.java)
* [Rebase onto an upstream branch](https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/RebaseToOriginMaster.java)

##### Low-level API

Expand Down Expand Up @@ -101,7 +102,6 @@ This project tries to provide a collection of ready-to-run snippets which try to
* Git repo on Amazon S3: http://stackoverflow.com/questions/8744611/git-repository-on-s3-as-origin-not-as-backup http://stackoverflow.com/questions/7031729/publish-to-s3-using-git http://www.fancybeans.com/blog/2012/08/24/how-to-use-s3-as-a-private-git-repository/
* Doing stuff without local storage: Using org.eclipse.jgit.storage.dfs.InMemoryRepository to clone a Git repo in-memory and work from there
* CherryPick
* Rebase - http://stackoverflow.com/questions/22945257/jgit-how-to-squash-commits
* Submodules - http://www.codeaffine.com/2014/04/16/how-to-manage-git-submodules-with-jgit/

#### Sources
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.dstadler.jgit.porcelain;

/*
Copyright 2013, 2014 Dominik Stadler
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import java.io.IOException;
import java.util.List;

import org.dstadler.jgit.helper.CookbookHelper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RebaseCommand.InteractiveHandler;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.IllegalTodoFileModification;
import org.eclipse.jgit.lib.RebaseTodoLine;
import org.eclipse.jgit.lib.RebaseTodoLine.Action;
import org.eclipse.jgit.lib.Repository;

/**
* Snippet which shows how to rebase local changes onto a remote branch.
*
* See also http://stackoverflow.com/questions/22945257/jgit-how-to-squash-commits
*
* @author dominik.stadler at gmx.at
*/
public class RebaseToOriginMaster {

public static void main(String[] args) throws IOException, GitAPIException {
try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
// all refs
try (Git git = new Git(repository)) {
InteractiveHandler handler = new InteractiveHandler() {
@Override
public void prepareSteps(List<RebaseTodoLine> steps) {
for(RebaseTodoLine step : steps) {
try {
step.setAction(Action.EDIT);
} catch (IllegalTodoFileModification e) {
throw new IllegalStateException(e);
}
}
}

@Override
public String modifyCommitMessage(String oldMessage) {
return oldMessage;
}
};

git.rebase().setUpstream("origin/master").runInteractively(handler).call();
System.out.println("Rebased..");
}
}
}
}

0 comments on commit 4d56c75

Please sign in to comment.