3

I have used below code to do push.

 public static void main(String[] args) throws IOException,GitAPIException
{ 
  Repository localRepo = new 
  FileRepository("C:\\Users\\Joshi\\Desktop\\demo");
  Git git = new Git(localRepo);

// add remote repo:
  RemoteAddCommand remoteAddCommand = git.remoteAdd();
  remoteAddCommand.setName("origin");
  try {
    remoteAddCommand.setUri(new 
    URIish("https://bitbucket.org/nidhi011/bxc"));
    System.out.println("file Added");
    } catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
// you can add more settings here if needed
remoteAddCommand.call();
git.commit().setMessage( "commited" ).call();

// push to remote:
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"));
// you can add more settings here if needed
pushCommand.call();
}

and my error is

file Added

Exception in thread "main" 
org.eclipse.jgit.api.errors.WrongRepositoryStateException: Cannot commit on 
a repo with state: BARE
at org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:171)
at maven_git.push.main(push.java:38)

After running the above code I got this exception error please help me to solve out the jgit push command. And yes One more thing when I exceutes this code it make config file in my local directory " demo" folder I can't understand that.

NikitaJ
  • 53
  • 1
  • 7
  • 1
    **Caused by: org.eclipse.jgit.errors.TransportException: Nothing to push.** Maybe you need a git.add and a git.commit first? – jonhid Mar 15 '18 at 06:08
  • @jonhid sir yes I have done commit then I also get error of "Exception in thread "main" org.eclipse.jgit.api.errors.WrongRepositoryStateException: Cannot commit on a repo with state: BARE at org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:171) at maven_git.push.main(push.java:38)" – NikitaJ Mar 15 '18 at 06:16
  • Is your directory `demo` a Git repository before launching your Java program? – Mincong Huang Mar 15 '18 at 08:39
  • @MincongHuang yes it is , having .git folder – NikitaJ Mar 15 '18 at 14:26

1 Answers1

3

There're several lines that are incorrect in your code. Let's take a look together.

Open Git Repository

Opening a Git repository using FileRepository is tricky. This is an internal API, where the given string is the location of the repository metadata (the .git folder). In other words, it is used to construct a Git bare repository. You can prove it by calling the Repository#isBare():

Repository localRepo = new FileRepository("C:\\Users\\Joshi\\Desktop\\demo");
System.out.println(localRepo.isBare()); // true

After using this API, the created repository is a BARE repository. You cannot commit to a bare repository because it doesn't have a workspace. That's why you've exception saying:

org.eclipse.jgit.api.errors.WrongRepositoryStateException: Cannot commit on a repo with state: BARE

A better way is to use Git#open(). Note that you should close the Git repository after using it. So I'm using the try-with-resources statement here:

try (Git git = Git.open(new File("C:\\Users\\Joshi\\Desktop\\demo"))) {
    // Add your logic here ...
}

Add Files to Git

Before commit the changes, you need to add them to the index, to prepare the content staged for the next commit. For example:

git.add().addFilepattern(".").call();

Notice that this is completely different from RemoteAddCommand: we are adding file content, while RemoteAddCommand adds a new remote URL. In native Git commands, they're respectively:

git add .
git remote add origin https://bitbucket.org/nidhi011/bxc

Commit

You're correct about this part.

Push

If the local branch is not checked out from a tracking branch, then you would need to precise the remote branch name in your push-command.

git.push().setRemote("origin").add("master").call();

If the credentials were incorrect, user would not be authorized for pushing the changes. In this case, a TransportException will be thrown. You can add additional logic for exception handling:

try {
    git.push().setRemote("origin").add("master").call();
} catch (TransportException e) {
    // Add your own logic here, for example:
    System.out.println("Username or password incorrect.");
}
Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
  • what to write in tempDir sir ? – NikitaJ Mar 17 '18 at 18:06
  • Oops, sorry @NidhiJoshi. I updated my answer now. It is the file path of your repository. – Mincong Huang Mar 17 '18 at 19:54
  • okk thank you so much Iam completed with my coding but one thing I need to do is to add validation what are the steps to be followed – NikitaJ Mar 19 '18 at 14:45
  • Could you precise what do you want to validate, @NikitaJ? – Mincong Huang Mar 19 '18 at 15:03
  • like for url it is should be authenticate otherwise should pass error message and for credentials like username and password if it is not matching it should pass message like "Incorrect password" like this can you help me in it using coding explaination – NikitaJ Mar 20 '18 at 09:20
  • okay sir but can you write the syntax to take user input at run time like how to take url, username and password by user – NikitaJ Mar 21 '18 at 11:32
  • I recommend the article [JGit Authentication Explained](http://www.codeaffine.com/2014/12/09/jgit-authentication/) for different auth modes in JGit and [Java tutorial: Java Scanner class](https://www.javatpoint.com/Scanner-class) for scanning the user inputs. I hope they're useful for you. @NikitaJ – Mincong Huang Mar 21 '18 at 13:08
  • okk, Earliarly I have taken reference from the code affine authentication but im not getting particular ans that how to take input from user at run time – NikitaJ Mar 22 '18 at 05:26