Skip to content

Conversation

JanLikar
Copy link
Contributor

Add --version switch, which prints a short hash of the current commit. Fix #612

Add --version switch, which prints a short hash of the current
commit. Fix rust-lang#612
@sanxiyn
Copy link
Contributor

sanxiyn commented Nov 16, 2015

Doesn't this print the commit when rustfmt is run, not when rustfmt is compiled? That seems wrong.

@nrc
Copy link
Member

nrc commented Nov 16, 2015

Yeah, I think the right approach is to get the commit hash at compile time. Probably the best way to do this is to make a small syntax extension, however, this would mean we can't use stable Rust :-( Alternatively, we could use a Cargo build script - I remember we used to have one, but it was removed because it was annoying for various reasons. I wonder how rustc gets its version hash - I assume some combination of unstable Rust and its horrific system of make files - but it might be worth checking out to see if there is some way of passing the data in.

@sanxiyn
Copy link
Contributor

sanxiyn commented Nov 16, 2015

rustc sets CFG_VER_HASH environment variable in makefile and uses env! macro in source. env! is stable.

@nrc
Copy link
Member

nrc commented Nov 16, 2015

Ah cool, I wonder if Cargo lets you set environment variables without having to use a build script?

@JanLikar
Copy link
Contributor Author

You're right, my commit is wrong. Nonetheless, I have learned that this approach might be problematic.
huon: JanLikar: the build script approach will be the easiest with stable Rust, however note that it won't always work (e.g. if the crate is published to crates.io there's no longer any git info) So if someone would install rustfmt from Cargo, he wouldn't even be able to get the hash. We could hardcode the hash into some file or have a git hook do it when we commit, but that's not really great. Could we just use crate versioning?

@nrc
Copy link
Member

nrc commented Nov 17, 2015

Using a git hook to save the hash into a file and then #includeing that file to get the hash sounds like a non-awful option if we can get the git hook working well. I don't know much about git hooks and afaik GH does not support them. Maybe worth looking into? Otherwise we could just return 0.0.1 for now and think about proper versioning sooner rather than later.

Print version acquired from Cargo when building. If built using rustc
directly, print X.X.X
@JanLikar
Copy link
Contributor Author

Github only has webhooks, so we would need a server to process each of the commits. I think that would be an overkill. "--version" or "-V" now prints version as defined in Cargo.toml. Now we need to agree on a versioning scheme.

@mbrt
Copy link

mbrt commented Dec 1, 2015

Why to use commit hashes? Isn't better to use the Cargo version? With std::option_env (as already done by @JanLikar) you get that version at compile time. It's simpler and other projects already followed that path (e.g. clap).

@nrc
Copy link
Member

nrc commented Dec 6, 2015

@JanLikar sorry for the lack of attention, I have been on vacation the last week, but this has been sitting even longer than that. I think we should land this, the PR looks good and we can bump versions when we feel like it. Thanks for the PR, and sorry again it took so long to land.

@mrbt that is the current state of the PR

nrc added a commit that referenced this pull request Dec 6, 2015
@nrc nrc merged commit 26297c5 into rust-lang:master Dec 6, 2015
@JanLikar
Copy link
Contributor Author

JanLikar commented Dec 6, 2015

@nrc It's ok, no harm done. I hope you had a great vacation!

@JanLikar JanLikar deleted the version branch December 6, 2015 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants