Repository: dathere/qsv (language: rust)

Issue:
add more polars info when invoking `qsv --version`
Right now, we show the semantic version of polars, e.g.

```
$ qsv --version
qsv 0.131.1-mimalloc-apply;fetch;foreach;geocode;Luau 0.635;to;polars-0.41.3;self_update-8-8;19.20 GiB-1.24 GiB-0 B-24.00 GiB (aarch64-apple-darwin compiled with Rust 1.80.1) prebuilt
```

in addition, we should also show the tag or commit rev given qsv's polars dependency policy.

https://github.com/jqnatividad/qsv/blob/a9e079df5d1de5584b4de8ced2c9ee29beb7cb7d/Cargo.toml#L270-L277

For example:
```
# when pinning polars to a specific rev
$ qsv --version
qsv 0.131.1-mimalloc-apply;fetch;foreach;geocode;Luau 0.635;to;polars-0.41.3-9dd9569;self_update-8-8;19.20 GiB-1.24 GiB-0 B-24.00 GiB (aarch64-apple-darwin compiled with Rust 1.80.1) prebuilt

# when pinning polars to a tag
$ qsv --version
qsv 0.131.1-mimalloc-apply;fetch;foreach;geocode;Luau 0.635;to;polars-0.41.3-py-1.4.1;self_update-8-8;19.20 GiB-1.24 GiB-0 B-24.00 GiB (aarch64-apple-darwin compiled with Rust 1.80.1) prebuilt
```


Discussion / hints:

Fix patch:
diff --git a/build.rs b/build.rs
index 903491efe..dd9e48d45 100644
--- a/build.rs
+++ b/build.rs
@@ -1,3 +1,8 @@
+use std::{fs, path::Path};
+
+// This is the string that is searched for in Cargo.toml to find the Polars revision
+const QSV_POLARS_REV: &str = "# QSV_POLARS_REV=";
+
 fn main() {
     // we use TARGET in --version and user-agent strings
     println!(
@@ -10,4 +15,25 @@ fn main() {
         "cargo:rustc-env=QSV_KIND={}",
         std::env::var("QSV_KIND").unwrap_or_else(|_| "compiled".to_string())
     );
+
+    // QSV_POLARS_REV contains either the commit id short hash or the git tag
+    // of the Polars version qsv was built against
+    let cargo_toml_path = Path::new("Cargo.toml");
+    let cargo_toml_content =
+        fs::read_to_string(cargo_toml_path).expect("Failed to read Cargo.toml");
+    let polars_rev = cargo_toml_content
+        .find(QSV_POLARS_REV)
+        .map_or_else(String::new, |index| {
+            let start_index = index + QSV_POLARS_REV.len();
+            let end_index = cargo_toml_content[start_index..]
+                .find('\n')
+                .map_or(cargo_toml_content.len(), |i| start_index + i);
+            cargo_toml_content[start_index..end_index]
+                .trim()
+                .to_string()
+        });
+    println!(
+        "cargo:rustc-env=QSV_POLARS_REV={}",
+        std::env::var("QSV_POLARS_REV").unwrap_or(polars_rev)
+    );
 }
diff --git a/src/util.rs b/src/util.rs
index 93f1a8894..2f7040a77 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -90,6 +90,12 @@ const QSV_KIND: &str = match option_env!("QSV_KIND") {
     None => "installed",
 };
 
+#[cfg(feature = "polars")]
+const QSV_POLARS_REV: &str = match option_env!("QSV_POLARS_REV") {
+    Some(rev) => rev,
+    None => "",
+};
+
 fn default_user_agent() -> String {
     let unknown_command = "Unknown".to_string();
     let current_command = CURRENT_COMMAND.get().unwrap_or(&unknown_command);
@@ -219,7 +225,7 @@ pub fn version() -> String {
     #[cfg(all(feature = "to", not(feature = "lite")))]
     enabled_features.push_str("to;");
     #[cfg(all(feature = "polars", not(feature = "lite")))]
-    enabled_features.push_str(format!("polars-{};", polars::VERSION).as_str());
+    enabled_features.push_str(format!("polars-{}-{};", polars::VERSION, QSV_POLARS_REV).as_str());
     #[cfg(feature = "self_update")]
     enabled_features.push_str("self_update");
     enabled_features.push('-');


Test patch: