elect
elect

Reputation: 7180

is it possible to squash commits via Github API?

I've successfully implemented automatic file creation/update via Github V3 Rest API, however the only downside is that for each file I have a commit.

There is a possibility to do the same for multiple files, but unfortunately it involves some concepts I still haven't mastered and it will take me some time until I get there since I have more urgents stuff on my TODO list.

In the meanwhile, I can totally live with that or simply squash the last N commits, which I imagined should be relatively easy, since locally it's just a matter of

git reset --soft HEAD~N
git commit -m ".."

but I couldn't find anything on the docs or online

So my question is: how can I squash the last N commits, non interactively, using Github API?

Ps: I don't care if it's v3 or v4 as long as I get it done

Upvotes: 0

Views: 1890

Answers (4)

Nikolai Ehrhardt
Nikolai Ehrhardt

Reputation: 736

I consider to receive bad reputation with this answer as it does not provide any working example and is also superficial until now. Iam putting some notes, that are to much for a comment.

But I claim that accepted answer is wrong.

You basically can squash commits via GitHub Trees API, but the operation is quite complex. The steps are basically same with previous answers.

Then Checkout how to create a tree: https://docs.github.com/de/rest/git/trees?apiVersion=2022-11-28

and create new tree with base-tree of HEAD-commit without any changes of the blobs, means to keep list empty.

Now it should be possible to create a new commit from newly created tree. the parent of this new commit should be HEAD~N's sha. For creating a commit checkout: https://docs.github.com/de/rest/git/commits?apiVersion=2022-11-28

At Last create a reference (branch) from this commit: https://docs.github.com/de/rest/git/refs?apiVersion=2022-11-28#create-a-reference

Upvotes: 0

Julian Heise
Julian Heise

Reputation: 11

Meanwhile it seems you can squash via API by setting merge_method to squash

see https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#merge-a-pull-request

Upvotes: 1

Joachim Breitner
Joachim Breitner

Reputation: 25782

I created a small service (https://squasher.nomeata.de/) that squashes commits on a branch, and it uses the Github API to do so:

  • It gets information about the PR via repos/{owner}/{repo}/pulls/{pr}, in particular base and head ref.
  • It gets the merge base using repos/{head_repo_owner}/{head_repo_name}/compare/{base_label}...{head_label}
  • It gets the tree of the head branch using repos/{head_repo_owner}/{head_repo_name}/git/commits/{head_sha}
  • It creates a new, squashed commit using repos/{head_repo_owner}/{head_repo_name}/git/commits
  • It force-updates the head branch to that commit using repos/{head_repo_owner}/{head_repo_name}/git/refs/heads/{head_ref}

Full code at https://github.com/nomeata/squasher/blob/1ccc24a5768e663199f8805b59120a6160592cae/squasher.py#L44-L109

Upvotes: 1

VonC
VonC

Reputation: 1328522

I did not see a squash feature directly through API.

You would need to:

Pretty convoluted, but it should work (entirely through script, without having to clone the repository).

Upvotes: 3

Related Questions