使用 JGit 修改 repo 即使在提交后也不会改变任何内容

问题描述 投票:0回答:2

这是我的代码:

                            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);

这段代码创建一个新文件,写入它,然后修改另一个文件并在它的末尾附加一些东西。所有这些都适用于本地克隆的仓库。但是,当我运行提交和推送时,在线仓库中没有任何变化。它说有一个新的提交,但它也说“显示 0 个更改的文件,其中有 0 个添加和 0 个删除。”基本上是一个空提交。我究竟做错了什么?我如何将本地存储库复制到在线存储库中? 另外,如果 git.add() 命令在错误的位置,那是因为我将它移到了更改的前面以尝试解决问题。它曾经在他们身后。

java git github jgit
2个回答
1
投票

看着

org.eclipse.jgit.api / Class AddCommand / addFilepattern()
,我明白了:

添加一个文件/目录的路径,其内容应该被添加。

目录名(例如

dir
添加
dir/file1
dir/file2
)也可以递归地添加目录中的所有文件。
尚不支持 Fileglobs(例如 *.c)。

参数:

filepattern
- 要添加的文件/目录的 存储库相对路径(以
/
作为分隔符)

addFilepattern() 方法文档中的术语“repository-relative”表示文件模式应相对于存储库的根目录指定。

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

如果要将 file1.txt 添加到暂存区,可以使用 addFilepattern("dir1/file1.txt"),即使当前工作目录是 dir1 或 dir2。这是因为路径是相对于存储库的根目录 (/repo),而不是当前工作目录。

如果您尝试添加存储库中不存在的文件(从根目录的角度来看),它将不会被暂存,并且在您提交时不会注册任何更改。

仔细检查

git.add().addFilepattern(postname).call();
中postname的值,以确保它是一个有效的参数。


0
投票

我最终决定使用

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

这是唯一有效的方法

© www.soinside.com 2019 - 2024. All rights reserved.