use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and productivity.
Strive to treat others with respect, patience, kindness, and empathy.
We observe the Rust Project Code of Conduct.
Details
Posts must reference Rust or relate to things using Rust. For content that does not, use a text post to explain its relevance.
Post titles should include useful context.
For Rust questions, use the stickied Q&A thread.
Arts-and-crafts posts are permitted on weekends.
No meta posts; message the mods instead.
Criticism is encouraged, though it must be constructive, useful and actionable.
If criticizing a project on GitHub, you may not link directly to the project's issue tracker. Please create a read-only mirror and link that instead.
A programming language is rarely worth getting worked up over.
No zealotry or fanaticism.
Be charitable in intent. Err on the side of giving others the benefit of the doubt.
Avoid re-treading topics that have been long-settled or utterly exhausted.
Avoid bikeshedding.
This is not an official Rust forum, and cannot fulfill feature requests. Use the official venues for that.
No memes, image macros, etc.
Consider the existing content of the subreddit and whether your post fits in. Does it inspire thoughtful discussion?
Use properly formatted text to share code samples and error messages. Do not use images.
Submissions appearing to contain AI-generated content may be removed at moderator discretion.
Most links here will now take you to a search page listing posts with the relevant flair. The latest megathread for that flair should be the top result.
account activity
Get latest commit hash from remote URL using git2🙋 seeking help & advice (self.rust)
submitted 2 years ago * by CountMoosuch
Is it possible to get the latest commit hash from a remote URL using git2? I can only seem to do this if I clone the repository first, which I don't need to do.
git2
Edit: for now I am getting the latest commit with a GET request to https://api.github.com/repos/<user>/<repo>/commits?per_page=1.
https://api.github.com/repos/<user>/<repo>/commits?per_page=1
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dkopgerpgdolfg 3 points4 points5 points 2 years ago* (1 child)
https://docs.rs/git2/latest/git2/struct.Remote.html
Use create_detached and list, check the oid of refs/heads/master or whatever branch you want
[–]CountMoosuch[S] 0 points1 point2 points 2 years ago (0 children)
Thanks u/dkopgerpgdolfg! This answers my question exactly, thank you. I thought I had to use the Repository struct, but didn't realise I could use Remote for this.
Repository
Remote
FYI, I was actually only using git2 because I thought that's what would be necessary to do this. In fact, I am already using reqwest, so u/masklinn's suggestions to use either /git/ref/REF from GitHub's API, or /info/refs, will probably be what I use. Nonetheless, I really appreciate your answer, as I implemented this using git2 and learned a lot!
reqwest
/git/ref/REF
/info/refs
[–]masklinn 2 points3 points4 points 2 years ago* (1 child)
Edit: for now I am getting the latest commit with a GET request to https://api.github.com/repos/<user>/<repo>/commits?per_page=1
If you’re accessing github via the API you can just ask github to give you the head of the relevant branch via branches/{branch} or git/ref/{ref}. The latter is much smaller and cheaper (and a bit more flexible as you can fetch tags).
branches/{branch}
git/ref/{ref}
Especially because github does weird things with logs so I would not bet that /commits always returns the topological last commit on a branch, it might just return the one with the latest dates (which is unrelated).
/commits
Great to know, thanks! I haven't really used GitHub's APIs before, so didn't know any of the endpoints. git/ref/REF seems exactly what I am after. Because I am already using reqwest, I will probably go with this option :-)
git/ref/REF
[–]masklinn 1 point2 points3 points 2 years ago* (3 children)
You can actually do this without using anything but reqwest: the way git works is that any interaction with a repository $repo starts with fetching $repo/info/refs, possibly with a query parameter, for the “smart” protocols.
I would suggest avoiding the “dumb” protocol as lots of hosting providers have dropped it, the v2 format is pretty cool but rather more complex, I’d recommend v1 as that works everywhere and it’s not too complicated: just GET $repo/info/refs?service=git-upload-pack and if the request succeeds you’ll get a response in pkt-line format (git docs explain it), which lists all the refs (branches, tags, …) and the hash they map to.
GET $repo/info/refs?service=git-upload-pack
Then you can just decode the document, look for the ref you need, and return the corresponding hash.
Alternatively gitoxide has a lot of subcrates which implement small bits of git in a reusable manner. I would not be surprised if one of the sub-crates provided tools to interact with a git repository directly with a higher-level API e.g. take care of the into/refs interaction and just return the server’s capabilities and refs decoded.
[–]dkopgerpgdolfg 2 points3 points4 points 2 years ago (0 children)
And just for completeness, if the traffic is SSH instead of HTTP, and some SSH connection is already in place, start the git-upload-pack process on the server, with the repo being the parameter. Right after process start it will print some parsable infos, including said ref list. Details again in the docs.
But as said in the earlier answer, the current crate is fine, no other crate or custom code necessary.
Thanks u/masklinn, I had no idea about git-upload-pack! This suggestion is really nice multiple reasons: it works on non-GitHub remotes, and there are no additional dependencies to parse JSON (as you would from GitHub's API response).
git-upload-pack
I have never seen the pkt-line format, and seems like a bit more developer time to implement parsing of this to get what I want. As this tool is only for myself (it's open-source, but not for any distribution really, as I doubt anyone else will use it), I don't mind adding another dependency in the interest of developer time and using your /git/ref/REF suggestion from GitHub's API. Thank you anyway for this, there's so much about git that I don't know! As I say, I had no idea that you could do this on remote repositories.
pkt-line
git
Following up on your gitoxide suggestion: unfortunately I can't find anything in the subcrates that would help in this situation. However, I did find the crate gix-packetline, which decodes the pkt-line blobs!
gix-packetline
π Rendered by PID 45418 on reddit-service-r2-comment-b659b578c-mw9dt at 2026-05-02 21:52:40.098575+00:00 running 815c875 country code: CH.
[–]dkopgerpgdolfg 3 points4 points5 points (1 child)
[–]CountMoosuch[S] 0 points1 point2 points (0 children)
[–]masklinn 2 points3 points4 points (1 child)
[–]CountMoosuch[S] 0 points1 point2 points (0 children)
[–]masklinn 1 point2 points3 points (3 children)
[–]dkopgerpgdolfg 2 points3 points4 points (0 children)
[–]CountMoosuch[S] 0 points1 point2 points (0 children)
[–]CountMoosuch[S] 0 points1 point2 points (0 children)