Reputation: 11
I'm trying to get the few latest revision SHA without checking out any code.
I know you can get the list if you have a local copy with git log
git log --pretty=format:"%H" --first-parent origin/master | head -10
and you can get only the latest revision with git ls-remote
git ls-remote [email protected]:foo/foo.git master
But I need to get a list of the last 10 revisions, not just the last.
Can anyone help?
Upvotes: 1
Views: 86
Reputation: 11
Thanks for the suggestions.
git-bisect doesn't really work either because the test is run on another system (teamcity) and takes more work to report back the result to git.
The solution I finally took was to use github API to get the list of recent commits and parse the first 10 revisions
https://api.github.com/repos/foo/foo/commits?sha=master
Upvotes: 0
Reputation: 164939
I basically need to get the latest code revision that passes certain tests -- unfortunately, there's no way to get the latest successful rev from the test directly. I can only go down the list one by one and check if that revision passes or not, until I find one that does.
The tool you're looking for is git-bisect
. It does a binary search to find which commit broke the tests. Any test you give it. It's much more efficient than going through them one by one.
Clone the repository and then...
$ git bisect start
$ git bisect bad # the current version is bad
$ git bisect good v1.2.3 # v1.2.3 was good
It will then check out a commit between those two. You can run your tests and tell it if it's good or bad with git bisect bad
or git bisect good
. It will then move on to the next commit in its binary search. Eventually it will narrow the failure down to one commit. This generally takes log(n)
tries. So if there are 100 commits to search it will find it in about 10 tries.
You can automate the process with git bisect run <command>
. It will run the command
on each commit and use the exit status to determine if it's good or bad.
This, btw, is why it's important to avoid making commits which fail tests.
Generally Git goesn't like to operate via remotes. It's just not built for that. Simplest thing to do is clone the repository and use git log
. Most repositories are very small. If they aren't, that's going to cause all sorts of problems and you should look into git-lfs
to fix that.
You can do a shallow clone and fetch just the last 10 commits with git clone --depth=10
. That will save you some time, but it probably isn't worth it unless the repository is enormous.
If it's on Github, you can use the Github V3 API to request the tip of the branch and then walk backwards using the parents
link. For example...
$ curl https://api.github.com/repos/evalEmpire/perl5i/branches/master
{
"name": "master",
"commit": {
"sha": "aa1124b89f38eed793e2b9f2d2b2ba5d80a27a20",
...
"parents": [
{
"sha": "d100dd2a118c425c730c17e395371e2cd84b43a3",
"url": "https://api.github.com/repos/evalEmpire/perl5i/commits/d100dd2a118c425c730c17e395371e2cd84b43a3",
"html_url": "https://github.com/evalEmpire/perl5i/commit/d100dd2a118c425c730c17e395371e2cd84b43a3"
}
]
},
...
There's the new Github API v4 GraphQL API which allows you to craft queries. With that you can conceivably get all 10 commits in one query, but I have no experience with it.
Upvotes: 2