0

I am trying to get list of commits during a specfic period and trying to get the list of files in each commit . Tried below code

    gitRepo = Git.cloneRepository().setURI("****").setCredentialsProvider(
            new UsernamePasswordCredentialsProvider("PRIVATE-TOKEN", "***")).setDirectory(new File("test")).setNoCheckout(true).call();

    ObjectId masterId = gitRepo.getRepository().exactRef("refs/remotes/origin/master").getObjectId();
    Date since = new SimpleDateFormat("yyyy-MM-dd").parse("2021-12-08");
    Date until = new SimpleDateFormat("yyyy-MM-dd").parse("2021-12-10");
    RevFilter between = CommitTimeRevFilter.between(since, until);
    for (RevCommit commit : gitRepo.log().add(masterId).setRevFilter(between).call()) {
        System.out.println(  "* "
                           + commit.getId().getName()
                           + " "
                           + commit.getShortMessage()
                           + " "
                           + commit.getAuthorIdent().getName());
        
    ObjectId lastCommitId = gitRepo.getRepository().resolve(commit.getId().getName());
    RevTree tree = commit.getTree();
    TreeWalk treeWalk = new TreeWalk(gitRepo.getRepository());
    treeWalk.addTree(tree);
    treeWalk.setRecursive(false);
    while(treeWalk.next()){
        System.out.println("File Name =   "+treeWalk.getPathString());
    }
    }

Its getting all files in the repo instead of the files changed as part of the specific commit I am passing in last commit . Not sure what I am missing ?

Harikrishnan R
  • 11
  • 1
  • 2
  • 10
  • It seems you are looking for the diff between a commit and its parent commit (note, that there may be more than one parent). This has been answered here: https://stackoverflow.com/questions/39935160/how-to-use-jgit-to-get-list-of-changes-in-files and here: https://stackoverflow.com/questions/28785364/list-of-files-changed-between-commits-with-jgit – Rüdiger Herrmann Dec 15 '21 at 10:33
  • Does this answer your question? [List of files changed between commits with JGit](https://stackoverflow.com/questions/28785364/list-of-files-changed-between-commits-with-jgit) – Rüdiger Herrmann Dec 15 '21 at 10:34
  • Hello Rudiger , thanks for response . I am trying to get file changes as part of single commit not between commits what changed , like how many files are part of that single commit and the file names – Harikrishnan R Dec 15 '21 at 16:23
  • This is exactly what the linked post answers. Git stores a tree of all files that constitute the repository at the time the commit was made. This is what the code in your question prints. To get a list of changed files, you need to compare two commits. – Rüdiger Herrmann Dec 16 '21 at 10:47
  • Thanks , will try it out, also is there a way when I get the list of commits using RevCommits and filters in above code to be sorted by commit date - right now they are in random order in the above code – Harikrishnan R Dec 17 '21 at 15:25

0 Answers0