不可能每个GitHub API每次提交传递多个文件



我已经使用GitHub API进行自动提交
为此,我使用Kohsuke的Java库
它与API命令一起工作:

Create a file
This method creates a new file in a repository
PUT /repos/:owner/:repo/contents/:path

但是,是否可以在每个GitHub API的1个Commit中包含多个文件?

下面的代码snippit将允许您准备一个带有多个文件的提交,然后使用Kohsuke 的Java库将一个新分支关联到它

// start with a Repository ref
GHRepository repo = ...
// get a sha to represent the root branch to start your commit from.  
// this can come from any number of classes:
//   GHBranch, GHCommit, GHTree, etc...
// for this example, we'll start from the master branch
GHBranch masterBranch = repo.getBranch("master");
// get a tree builder object to build up into the commit. 
// the base of the tree will be the master branch
GHTreeBuilder treeBuilder = repo.createTree().baseTree(masterBranch.getSHA1());
// add entries to the tree in various ways.  
// the nice thing about GHTreeBuilder.textEntry() is that it is an "upsert" (create new or update existing)
treeBuilder = treeBuilder.textEntry(pathToFile, contentOfFile, executable);
// repeat calls of treeBuider.textEntry() or .shaEntry() or .entry() ....
// perform the commit
GHCommit commit = repo.createCommit()
  // base the commit on the tree we built
  .tree(treeBuilder.create().getSha())
  // set the parent of the commit as the master branch
  .parent(masterBranch.getSHA1()).message("multi-file commit").create();
// create a new branch from that commit
String newBranchName = "myNewBranchName";
GHRef newRef = repo.createRef("/refs/heads/" + newBranchName, commit.getSHA1();
GHBranch newBranch = repo.getBranch(newBranchName);

根据Dan Dowma的回答,可以将这些更改推送到现有的分支


final String branchName = "main";
// start with a Repository ref
GHRepository repo = ...
// get a sha to represent the root branch to start your commit from.  
// this can come from any number of classes:
//   GHBranch, GHCommit, GHTree, etc...
// for this example, we'll start from the selected branch
GHBranch selectedBranch = repo.getBranch(branchName);
// get a tree builder object to build up into the commit. 
// the base of the tree will be the master branch
GHTreeBuilder treeBuilder = repo.createTree().baseTree(selectedBranch.getSHA1());
// add entries to the tree.
treeBuilder = treeBuilder.add(pathToFile, contentOfFile, executable);
// perform the commit
GHCommit commit = repo.createCommit()
  // base the commit on the tree we built
  .tree(treeBuilder.create().getSha())
  // set the parent of the commit as the master branch
  .parent(selectedBranch.getSHA1()).message("multi-file commit").create();
// Update an existing branch from that commit
GHRef existingBranchRef = repo.getRef("/refs/heads/" + branchName);
existingBranchRef.updateTo(commit.getSHA1());

不确定这是否是您想要的,也许更多的描述会有所帮助,但请看一下:https://developer.github.com/v3/git/

最新更新