Using JGit to modify a repo doesn't change anything even after committing

96 Views Asked by At

Here is my code:

                            postbody = usabled;
                            String pathToClone = "./repo";
                            Git git = Git.cloneRepository()
                                    .setURI("https://github.com/Glitch31415/rws.git")
                                    .setDirectory(new File(pathToClone))
                                    .call();
                            System.out.println("remade local repo");
                            if (windows == true) {
                                new File(git.getRepository().getDirectory().getParent() + "\\community\\", postname);
                            }
                            else {
                                new File(git.getRepository().getDirectory().getParent() + "/community/", postname);
                            }
                        
                            try {
                                FileWriter myWriter;
                                if (windows == true) {
                                    myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "\\community\\" + postname);
                                    System.out.println("writing the file to " + git.getRepository().getDirectory().getParent() + "\\community\\" + postname);
                                }
                                else {
                                    myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "/community/" + postname);
                                    System.out.println("writing the file to " + git.getRepository().getDirectory().getParent() + "/community/" + postname);
                                }

                                myWriter.write(postbody);
                                System.out.println("wrote " + postbody);
                                myWriter.close();
                              } catch (IOException e) {
                                e.printStackTrace();
                            }
                            git.add().addFilepattern(postname).call();
                            try {
                                try {
                                    File myObj;
                                    if (windows == true) {
                                          myObj = new File(git.getRepository().getDirectory().getParent() + "\\community\\index");
                                    }
                                    else {
                                          myObj = new File(git.getRepository().getDirectory().getParent() + "/community/index");
                                    }

                                      Scanner myReader = new Scanner(myObj);
                                      while (myReader.hasNextLine()) {
                                        st = st + myReader.nextLine() + "\n";
                                      }
                                      myReader.close();
                                    } catch (FileNotFoundException e) {
                                      e.printStackTrace();
                                    }
                                System.out.println("index was read as '" + st + "'");
                                FileWriter myWriter;
                                if (windows == true) {
                                    myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "\\community\\index");
                                }
                                else {
                                    myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "/community/index");
                                }

                                
                                myWriter.write(st+postname+"\n");
                                myWriter.close();
                              } catch (IOException e) {
                                e.printStackTrace();
                            }
                            git.add().addFilepattern("/community/index").call();
                            git.commit().setMessage("Committed from server").call();
                            PushCommand pushCommand = git.push();
                            pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("key", ""));
                            pushCommand.call();
                            Path directory = Path.of(pathToClone);
                            Files.walk(directory)
                            .sorted(Comparator.reverseOrder())
                            .map(Path::toFile)
                            .forEach(File::delete);

This code creates a new file, writes to it, then modifies another file and appends something onto the end of it. All this works in the local cloned repo. But, when I run commit and push, nothing changes in the online repo. It says there was a new commit, but it also says "Showing 0 changed files with 0 additions and 0 deletions." Basically an empty commit. What am I doing wrong? How do I just copy the local repo into the online one? Also, if the git.add() command is in the wrong spot, it's because I moved it in front of the changes to try to fix the problem. It used to be behind them.

2

There are 2 best solutions below

1
Glitch On BEST ANSWER

I eventually decided to use

git.add().addFilepattern("*").call();

which was the only thing that worked

0
VonC On

Looking at org.eclipse.jgit.api / Class AddCommand / addFilepattern(), I see:

Add a path to a file/directory whose content should be added.

A directory name (e.g. dir to add dir/file1 and dir/file2) can also be given to add all files in the directory, recursively.
Fileglobs (e.g. *.c) are not yet supported.

Parameters:
filepattern - repository-relative path of file/directory to add (with / as separator)

The term "repository-relative" in the documentation for the addFilepattern() method means that the file pattern should be specified relative to the root directory of the repository.

/repo
   /dir1
      file1.txt
   /dir2
      file2.txt

If you want to add file1.txt to the staging area, you would use addFilepattern("dir1/file1.txt"), even if the current working directory is dir1 or dir2. This is because the path is relative to the root of the repository (/repo), not the current working directory.

If you try to add a file that doesn't exist in the repository (from the root directory perspective), it won't be staged, and no changes will be registered when you commit.

Double-check the value of postname in git.add().addFilepattern(postname).call();, to make sure it is a valid paramter.