From 4d56c753233f64534b83a7adbefeb2afa98fc6fa Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Fri, 23 Oct 2015 11:03:29 +0200 Subject: [PATCH] Add snippet for rebasing --- README.md | 2 +- .../jgit/porcelain/RebaseToOriginMaster.java | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/dstadler/jgit/porcelain/RebaseToOriginMaster.java diff --git a/README.md b/README.md index 4bf2eb86..cbf9caa5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/main/java/org/dstadler/jgit/porcelain/RebaseToOriginMaster.java b/src/main/java/org/dstadler/jgit/porcelain/RebaseToOriginMaster.java new file mode 100644 index 00000000..9c2d488a --- /dev/null +++ b/src/main/java/org/dstadler/jgit/porcelain/RebaseToOriginMaster.java @@ -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 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.."); + } + } + } +}