-2

I am learning to push code to git using jgit. I am able to push all the files to git, but I only want to add new files or update the changes to git. Please suggest:

Below is my code:

Git git = Git.open(new File(repoPath)); // connect to github
// I want some code which recognizes the new additions or finds the files changed

git.add().addFilepattern(".").call(); // Want to add only the delta
git.commit().setMessage("commit - 1").call(); 
git.push().setCredentialsProvider(cp).call(); 
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • `git.add().addFilepattern(".").call()` adds new and changed files from the working directory to the index. Please tell us what you expect and what the actual outcome is. – Rüdiger Herrmann Oct 07 '19 at 16:32
  • git.add().addFilepattern(".").call() adds all the files. If we see the org.eclipse.jgit.api.AddCommand.call(){ boolean addAll = filepatterns.contains("."); } thus it adds all the files and not the delta. I need a parameter that can be passed to addFilepattern. – Saurabh Jhunjhunwala Oct 07 '19 at 16:38

3 Answers3

0

Unchanged files are never added to the index, they are already contained in the index. Hence, use the file pattern "." if you want to add all new and changed files to the index.

The addAll variable you are referring to, is used to control whether to scan all files in the working directory or just the ones in the contained in the filepatterns list.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
0
//add files which have been modified or deleted
git.add().addFilepattern(".").setUpdate(true).call();
Status status = git.status().call();
//add new files
if (!CollectionUtils.isEmpty(status.getUntracked())) {
   AddCommand addCommand = git.add();
   for (String s : status.getUntracked()) {
        addCommand.addFilepattern(s);
   }
   addCommand.call();
}
What Dong
  • 19
  • 4
-2

Got it !!!

Status status = git.status().call();
System.out.println("Added: " + status.getAdded());
System.out.println("Changed: " + status.getChanged());
System.out.println("Conflicting: " + status.getConflicting());
System.out.println("ConflictingStageState: " + status.getConflictingStageState());
System.out.println("IgnoredNotInIndex: " + status.getIgnoredNotInIndex());
System.out.println("Missing: " + status.getMissing());
System.out.println("Modified: " + status.getModified());
System.out.println("Removed: " + status.getRemoved());
System.out.println("Untracked: " + status.getUntracked());
System.out.println("UntrackedFolders: " + status.getUntrackedFolders());
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57