Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not possible to pass multiple files per commit per GitHub API?

Tags:

java

git

github

api

I already use the GitHub API to make automated commits.
For this I'm using a Java Library from Kohsuke
It works with this API command:

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

But is it possible to include multiple files in 1 Commit per GitHub API?

like image 212
Ernst Robert Avatar asked Nov 04 '15 22:11

Ernst Robert


People also ask

How do I commit multiple files in github?

To add multiple files to a single commit, you will need to clone the repository locally, edit the files, then commit and push.

What is Github API limit?

User-to-server requests are limited to 5,000 requests per hour and per authenticated user.

How do I commit multiple files in bitbucket?

Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed. Enter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository.


1 Answers

The following code snippit will allow you to prepare a commit with multiple files, then associate a new branch to it, using the Java library from Kohsuke

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

like image 194
Dan Dowma Avatar answered Oct 02 '22 03:10

Dan Dowma