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

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

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:

							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.

答案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.

/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.

答案2

得分: 1

我最终决定使用

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

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

I eventually decided to use

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:

确定