2

I am trying to push a project into gitlab from java. I cloned it successfully but I am not able to push with changes(adding some more files or adding more information to existing file.). The code does not contain any error but it is not updated in gitlab.

Here is my code

File localPath = File.createTempFile("TestGitRepository", "");
    if(!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + 
localPath);
    }

    Git git = Git.cloneRepository()
            .setURI( REMOTE_URL ) 
            .setDirectory(localPath) 
            .setCredentialsProvider( cp )
            .call();
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath); 


            // Git git=Git.open(dir);
             File file = new File( git.getRepository().getWorkTree(), "file" + new Object().hashCode() ); 
             System.out.println("hi");
             file.createNewFile(); 

             git.add().addFilepattern( file.getName() ).call();

             git.commit().setMessage( "Add file " + file.getName() ).call();

             git.push() .setCredentialsProvider( cp ) .call();

             System.out.println("Pushed from repository: " + localPath + " to remote repository at " + REMOTE_URL);
Ash
  • 129
  • 1
  • 1
  • 12

1 Answers1

2

This is using JGit

For the commit syntax, use, as in the jgit/porcelain/CommitAll.java cookbook example:

git.commit().setAll(true).setMessage("a message").call();

That will be the equivalent of git commit -a -m "a message"

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • @Ash Don't forget, for your other questions, to read https://stackoverflow.com/help/accepted-answer – VonC Jun 03 '17 at 05:48
  • Now after adding that line of code it is showing in gitlab that updated less than minute ago. But how to edit a cloned file and then push it into gitlab. Because in my code it is cloning and immediately it is pushing, so I cannot edit. My usecase is I want clone it and make some changes and push back to git. I tried a separate clone method but I don't know how to call cloned folder in push method. Is there any way to overcome this problem? – Ash Jun 03 '17 at 05:59
  • @Ash one approach is to wait for the user to press a key (https://stackoverflow.com/a/15440036/6309, https://stackoverflow.com/a/26184535/6309), giving you time to edit. Another approach is the generate 2 executable: one for cloning, one for adding/pushing. You would call the second executable only when you are ready. – VonC Jun 03 '17 at 06:03
  • @Ash It is best to ask a new question, with the details of the error message in it (and your exact version number of JGit) – VonC Jun 03 '17 at 16:54