Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » I'm new in Jgit(How to I merge a branch and master?)
I'm new in Jgit [message #1773085] Thu, 21 September 2017 09:41 Go to next message
robert motala is currently offline robert motalaFriend
Messages: 1
Registered: September 2017
Junior Member
Here is my code:


import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import junit.framework.TestCase;
import org.eclipse.jgit.api.*;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.*;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;


public class Http {

private static final File localPath = new File("/localpath");
private static final String REMOTE_URL = "url";

private static void deleteDir(File file) {
File[] contents = file.listFiles();
if (contents != null) {
for (File f : contents) {
deleteDir(f);
}
}
file.delete();
}

public static void main(String[] args) throws IOException, GitAPIException {


SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {

@Override

protected void configure(OpenSshConfig.Host host, Session session) {
session.setUserInfo(new UserInfo() {
@Override

public String getPassphrase() {

return "ssh-key";

}

@Override

public String getPassword() {
return null;
}

@Override

public boolean promptPassword(String message) {
return false;
}

@Override

public boolean promptPassphrase(String message) {
return true;
}

@Override

public boolean promptYesNo(String message) {
return false;
}

@Override

public void showMessage(String message) {
}

});

}

};



//
// Git result = Git.cloneRepository()
//
// .setURI(REMOTE_URL)
//
// .setTransportConfigCallback(transport -> {
//
// SshTransport sshTransport = (SshTransport) transport;
//
// sshTransport.setSshSessionFactory(sshSessionFactory);
//
// })
//
// .setDirectory(localPath)
//
// .call();
//


Git git = Git.init().setDirectory(localPath).call();


System.out.println(" Results " + git.getRepository().getBranch());

git.getRepository().getRef(Constants.HEAD);
git.status().call().isClean();

System.out.println(" Check the status " + git.getRepository().getRef(Constants.HEAD) + " status "
+ git.status().call().isClean());


Ref headRef = git.getRepository().getRef(Constants.HEAD);
System.out.println("Head of ref" + headRef);
if (headRef == null || headRef.getObjectId() == null) {
// no commit yet
}


List<Ref> call = git.branchList().call();
for (Ref ref : call) {
System.out.println("Branch-Before: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
}

// setCreateBranch(true)
Ref ref = git.checkout().setName("newbranch")
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint("origin/newbranch").call();
//
System.out.println("Checkout an existing branch " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());


git.diff().setOutputStream(System.out).call();
git.add().addFilepattern(".").call();
git.commit().setMessage("commiy all changes").call();

PushCommand pushCommand = git.push();
pushCommand.setRefSpecs(new RefSpec("newbranch"));
pushCommand.setRemote(REMOTE_URL)
.setTransportConfigCallback(transport -> {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
});
pushCommand.call();




CheckoutCommand coCmd = git.checkout();
// Commands are part of the api module, which include git-like calls
coCmd.setName("master");
coCmd.call(); // switch to "master" branch

Ref master = git.getRepository().getRef(Constants.HEAD);


ObjectId masterTip = master.getTarget().getObjectId();

System.out.println("master Tip" + masterTip);


MergeCommand mgCmd = git.merge();
System.out.println("repository " + mgCmd.getRepository().getBranch());
mgCmd.include(ref).setCommit(true).setFastForward(MergeCommand.FastForwardMode.FF_ONLY); // "foo" is considered as a Ref to a branch
MergeResult res = mgCmd.call(); // actually do the merge
//
//
System.out.println("merge results " + res);
//
// if (res.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING)) {
// System.out.println(res.getConflicts().toString());
// // inform the user he has to handle the conflicts
// }







}}





Results master
Check the status SymbolicRef[HEAD -> refs/heads/master=7905440a4a6e3993383f807ba1510216a7bf21fe] status true
Head of refSymbolicRef[HEAD -> refs/heads/master=7905440a4a6e3993383f807ba1510216a7bf21fe]
Branch-Before: Ref[refs/heads/master=7905440a4a6e3993383f807ba1510216a7bf21fe] refs/heads/master 7905440a4a6e3993383f807ba1510216a7bf21fe
Branch-Before: Ref[refs/heads/newbranch=f557134f0e0b4ccc714c5c62e22a87c0bb1b61ee] refs/heads/newbranch f557134f0e0b4ccc714c5c62e22a87c0bb1b61ee
Checkout an existing branch Ref[refs/heads/newbranch=f557134f0e0b4ccc714c5c62e22a87c0bb1b61ee] refs/heads/newbranch f557134f0e0b4ccc714c5c62e22a87c0bb1b61ee
master TipAnyObjectId[7905440a4a6e3993383f807ba1510216a7bf21fe]
repository master
merge results Merge of revisions 7905440a4a6e3993383f807ba1510216a7bf21fe, f557134f0e0b4ccc714c5c62e22a87c0bb1b61ee with base f557134f0e0b4ccc714c5c62e22a87c0bb1b61ee using strategy recursive resulted in: Fast-forward.

Process finished with exit code 0
Re: I'm new in Jgit [message #1773102 is a reply to message #1773085] Thu, 21 September 2017 14:14 Go to previous message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1032
Registered: July 2009
Senior Member
see my response in the bug you opened earlier
https://bugs.eclipse.org/bugs/show_bug.cgi?id=522558
Previous Topic:Create Eclipse project in existing Git repository
Next Topic:LFS - .lfsconfig ignored on initial clone?
Goto Forum:
  


Current Time: Wed Sep 19 22:32:54 GMT 2018

Powered by FUDForum. Page generated in 0.01667 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top