Using JGit来修改一个存储库,即使在提交后也没有任何更改。

huangapple go评论113阅读模式
英文:

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

问题

这段代码创建一个新文件,向其写入内容,然后修改另一个文件并在其末尾添加内容。所有这些操作都在本地克隆的存储库中进行。但是,当我运行提交和推送时,在在线存储库中没有任何更改。它显示有一个新的提交,但也显示“显示0个更改文件,0个添加和0个删除”。基本上是一个空提交。我做错了什么?我如何将本地存储库复制到在线存储库中?

另外,如果git.add()命令放错位置了,那是因为我将它移到更改之前来尝试修复问题。它以前在更改之后。

这部分代码似乎是在Java中使用JGit库进行Git操作的代码。要解决问题,您需要确保以下几点:

  1. 在更改文件后,请确保将更改添加到Git的暂存区(staging area)。在您的代码中,您使用了git.add()命令,但要确保它们在您的更改之后调用,以便正确地将更改添加到暂存区。

  2. 在进行提交(commit)之前,请确保在本地存储库中进行了更改。您可以使用git.status().call()检查是否有未提交的更改,以确保更改已成功添加到本地存储库。

  3. 确保您设置了正确的凭据来执行推送(push)操作。在您的代码中,您使用了pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("key", ""));,确保替换“key”和空字符串为您的实际凭据信息。

  4. 检查您的网络连接是否正常,以确保可以将更改推送到在线存储库。

通过这些步骤,您应该能够确保将本地更改正确地推送到在线存储库中。如果问题仍然存在,请检查错误消息以获取更多信息,以便更精确地诊断问题。

英文:

Here is my code:

  1. postbody = usabled;
  2. String pathToClone = "./repo";
  3. Git git = Git.cloneRepository()
  4. .setURI("https://github.com/Glitch31415/rws.git")
  5. .setDirectory(new File(pathToClone))
  6. .call();
  7. System.out.println("remade local repo");
  8. if (windows == true) {
  9. new File(git.getRepository().getDirectory().getParent() + "\\community\\", postname);
  10. }
  11. else {
  12. new File(git.getRepository().getDirectory().getParent() + "/community/", postname);
  13. }
  14. try {
  15. FileWriter myWriter;
  16. if (windows == true) {
  17. myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "\\community\\" + postname);
  18. System.out.println("writing the file to " + git.getRepository().getDirectory().getParent() + "\\community\\" + postname);
  19. }
  20. else {
  21. myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "/community/" + postname);
  22. System.out.println("writing the file to " + git.getRepository().getDirectory().getParent() + "/community/" + postname);
  23. }
  24. myWriter.write(postbody);
  25. System.out.println("wrote " + postbody);
  26. myWriter.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. git.add().addFilepattern(postname).call();
  31. try {
  32. try {
  33. File myObj;
  34. if (windows == true) {
  35. myObj = new File(git.getRepository().getDirectory().getParent() + "\\community\\index");
  36. }
  37. else {
  38. myObj = new File(git.getRepository().getDirectory().getParent() + "/community/index");
  39. }
  40. Scanner myReader = new Scanner(myObj);
  41. while (myReader.hasNextLine()) {
  42. st = st + myReader.nextLine() + "\n";
  43. }
  44. myReader.close();
  45. } catch (FileNotFoundException e) {
  46. e.printStackTrace();
  47. }
  48. System.out.println("index was read as '" + st + "'");
  49. FileWriter myWriter;
  50. if (windows == true) {
  51. myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "\\community\\index");
  52. }
  53. else {
  54. myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "/community/index");
  55. }
  56. myWriter.write(st+postname+"\n");
  57. myWriter.close();
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. git.add().addFilepattern("/community/index").call();
  62. git.commit().setMessage("Committed from server").call();
  63. PushCommand pushCommand = git.push();
  64. pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("key", ""));
  65. pushCommand.call();
  66. Path directory = Path.of(pathToClone);
  67. Files.walk(directory)
  68. .sorted(Comparator.reverseOrder())
  69. .map(Path::toFile)
  70. .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.

答案1

得分: 1

I see the text you provided.

英文:

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.

  1. /repo
  2. /dir1
  3. file1.txt
  4. /dir2
  5. 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.

答案2

得分: 1

  1. 我最终决定使用

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

  1. 这是唯一有效的方法。
英文:

I eventually decided to use

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

which was the only thing that worked

huangapple
  • 本文由 发表于 2023年5月18日 05:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定