Search code examples
javagitgithubjgit

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


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.


Solution

  • I eventually decided to use

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

    which was the only thing that worked