JavaYouth
JavaYouth

Reputation: 1552

Get modified files from locally cloned Git repository using Java

In a cloned git repository, I want to pick only the files that are modified (i.e, files that are ready to commit or which are shown as 'modified' if I run the command 'git status'). I do not want to do it on date change comparison as files could have been modified on any day over a period of time.

I need the collection of file names with their absolute file paths.

Is there any such git utility in Java available? Or what will be the better approach?

Upvotes: 3

Views: 1378

Answers (1)

JavaYouth
JavaYouth

Reputation: 1552

import java.io.File;
import java.util.Set;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;

public class GitModifiedFileExtractor {

    public static void main(String[] args) throws IllegalStateException, GitAPIException {
        Git myGitRepo = Git.init().setDirectory(new File("C:\\myClonedGitRepo")).call();
        Status status = myGitRepo.status().call();
        Set<String> modifiedFiles = status.getModified();
        for (String modifiedFile : modifiedFiles) {
            System.out.println("Modified File - " + modifiedFile);
        }
    }
    // Similarly we can get files - added, missing, removed, untracked, etc., 
    // from status object.
}

Upvotes: 1

Related Questions