0

I am using Jgit api. I am able to create files in git remote location using the cloneRepository method.

Similarly I can add files using the addCommand. The requirement now is to create folders or update an existing folder with files in local and to push, commit the same to remote repository - https://github.com. I tried modifying the same code that we use for creating a file, but it is not working.

How can I achieve this? Code used for adding a file:

File file = new File(Trial.localPath + "\\" + fileName);
file.createNewFile();
UsernamePasswordCredentialsProvider upc = new UsernamePasswordCredentialsProvider(
                    userName, password);
git = Git.cloneRepository().setURI(remotePath).setDirectory(puppetDirFile).setCredentialsProvider(upc).call();
git.add().addFilepattern(fileName).call();  
git.commit().setMessage("Added "+ fileName).call();

PushCommand pc = git.push();
pc.setCredentialsProvider(upc).setForce(true).setPushAll();
pc.call();
git.getRepository().close();

modified for adding folder :

File file = new File(Trial.localPath+"\\ProjectA");
        if (!file.exists()) {
            if (file.mkdir()) {
                System.out.println("Directory is created!");
            }
        }
git.add().addFilepattern("projectA").call();    
git.commit().setMessage("Added "+ "ProjectA").call();

then rest pushed as the same above code.

centic
  • 15,565
  • 9
  • 68
  • 125
ABC
  • 1
  • 2
  • "I tried modifying the same code that we use for creating a file, but it is not working." You'll need to share that code with us and tell is what "not working" means if you hope to get a useful answer. – ChrisGPT was on strike Jan 18 '16 at 12:42
  • File file = new File(localDir + "\\" + fileName); file.createNewFile(); Writer writer = null; writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), "utf-8")); writer.write(contents); writer.close(); git.add().addFilepattern(fileName).call(); git.commit().setMessage("Added "+ fileName).call(); PushCommand pc = git.push(); UsernamePasswordCredentialsProvider upc = new UsernamePasswordCredentialsProvider(userName, password); pc.setCredentialsProvider(upc).setForce(true).setPushAll(); pc.call(); git.getRepository().close(); – ABC Jan 18 '16 at 12:59
  • in the above code after cloning remote repository to localDir added a new File named "fileName". then have commited and pushed the same to remote repository. Able to view the file in github.com. but when trying to do the same using a folder as in below code its failing.. It is just added as file with a different format. – ABC Jan 18 '16 at 13:03
  • File file = new File(Trial.localPath+"\\ProjectA"); if (!file.exists()) { if (file.mkdir()) { System.out.println("Directory is created!"); git.add().addFilepattern("projectA").call(); – ABC Jan 18 '16 at 13:04
  • It is very difficult to read code in comments. Please use the **edit** link at the bottom-left corner of your question and add the code there instead. After pasting the code, select it and click the **{}** button (or press Ctrl+K) to indent it all by four spaces, which will cause SO to format it as code. – ChrisGPT was on strike Jan 18 '16 at 13:29
  • Again, please don't put code into comments. Use the **edit** link at the bottom-left of your question, as requested in my previous comment. – ChrisGPT was on strike Jan 19 '16 at 04:05
  • Updated the code.. Thanks a lot for helping me with this Chris. – ABC Jan 19 '16 at 04:10

1 Answers1

1

Git does not support adding empty folders to git. So even though you have added it, it is not going to be in the Git index till you add a file. See associated stack overflow question. How can I add an empty directory to a Git repository?

Community
  • 1
  • 1
Yogesh_D
  • 17,656
  • 10
  • 41
  • 55