Use commit from cargo lock file for git repositories (#2490)

Previously the cargo bazel lock file would be created with the branch or
tag in the cargo lock file, rather than using the pinned commit. This
would result in non-reproducible builds. This makes the pinned commit
preferred, falling back to the branch or tag if this doesn't exist.

Fixes #2073

Co-authored-by: UebelAndre <github@uebelandre.com>
diff --git a/crate_universe/src/metadata/metadata_annotation.rs b/crate_universe/src/metadata/metadata_annotation.rs
index 990c7aa..f1f957c 100644
--- a/crate_universe/src/metadata/metadata_annotation.rs
+++ b/crate_universe/src/metadata/metadata_annotation.rs
@@ -245,7 +245,10 @@
 
             return Ok(SourceAnnotation::Git {
                 remote: source.url().to_string(),
-                commitish: Commitish::from(git_ref.clone()),
+                commitish: source
+                    .precise()
+                    .map(|rev| Commitish::Rev(rev.to_string()))
+                    .unwrap_or(Commitish::from(git_ref.clone())),
                 shallow_since: None,
                 strip_prefix,
                 patch_args: None,
@@ -547,6 +550,27 @@
     }
 
     #[test]
+    fn resolves_commit_from_branches_and_tags() {
+        let crates =
+            LockfileAnnotation::new(test::lockfile::git_repos(), &test::metadata::git_repos())
+                .unwrap()
+                .crates;
+
+        let package_id = PackageId { repr: "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)".into() };
+        let annotation = crates.get(&package_id).unwrap();
+
+        let commitish = match annotation {
+            SourceAnnotation::Git { commitish, .. } => commitish,
+            _ => panic!("Unexpected annotation type"),
+        };
+
+        assert_eq!(
+            *commitish,
+            Commitish::Rev("1e09e50e8d15580b5929adbade9c782a6833e4a0".into())
+        );
+    }
+
+    #[test]
     fn detect_unused_annotation() {
         // Create a config with some random annotation
         let mut config = Config::default();
diff --git a/crate_universe/test_data/metadata/git_repos/Cargo.lock b/crate_universe/test_data/metadata/git_repos/Cargo.lock
index 58405f4..3ac58d3 100644
--- a/crate_universe/test_data/metadata/git_repos/Cargo.lock
+++ b/crate_universe/test_data/metadata/git_repos/Cargo.lock
@@ -59,7 +59,7 @@
 [[package]]
 name = "tracing"
 version = "0.2.0"
-source = "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0"
+source = "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0"
 dependencies = [
  "cfg-if",
  "pin-project-lite",
@@ -70,7 +70,7 @@
 [[package]]
 name = "tracing-attributes"
 version = "0.2.0"
-source = "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0"
+source = "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -80,7 +80,7 @@
 [[package]]
 name = "tracing-core"
 version = "0.2.0"
-source = "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0"
+source = "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0"
 dependencies = [
  "lazy_static",
 ]
diff --git a/crate_universe/test_data/metadata/git_repos/Cargo.toml b/crate_universe/test_data/metadata/git_repos/Cargo.toml
index 6516f98..ecfb389 100644
--- a/crate_universe/test_data/metadata/git_repos/Cargo.toml
+++ b/crate_universe/test_data/metadata/git_repos/Cargo.toml
@@ -9,4 +9,5 @@
 path = "lib.rs"
 
 [dependencies]
-tracing = { git = "https://github.com/tokio-rs/tracing.git", rev = "1e09e50e8d15580b5929adbade9c782a6833e4a0" }
+tracing = { git = "https://github.com/tokio-rs/tracing.git", branch = "master" }
+
diff --git a/crate_universe/test_data/metadata/git_repos/metadata.json b/crate_universe/test_data/metadata/git_repos/metadata.json
index 725191f..db4f006 100644
--- a/crate_universe/test_data/metadata/git_repos/metadata.json
+++ b/crate_universe/test_data/metadata/git_repos/metadata.json
@@ -107,7 +107,7 @@
                     "registry": null,
                     "rename": null,
                     "req": "*",
-                    "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0",
+                    "source": "git+https://github.com/tokio-rs/tracing.git?branch=master",
                     "target": null,
                     "uses_default_features": true
                 }
@@ -1527,7 +1527,7 @@
                     "registry": null,
                     "rename": null,
                     "req": "^0.2",
-                    "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0",
+                    "source": "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0",
                     "target": null,
                     "uses_default_features": true
                 },
@@ -1539,7 +1539,7 @@
                     "registry": null,
                     "rename": null,
                     "req": "^0.2",
-                    "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0",
+                    "source": "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0",
                     "target": null,
                     "uses_default_features": false
                 },
@@ -1647,7 +1647,7 @@
                 ]
             },
             "homepage": "https://tokio.rs",
-            "id": "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
+            "id": "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
             "keywords": [
                 "logging",
                 "tracing",
@@ -1674,7 +1674,7 @@
             "readme": "README.md",
             "repository": "https://github.com/tokio-rs/tracing",
             "rust_version": null,
-            "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0",
+            "source": "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0",
             "targets": [
                 {
                     "crate_types": [
@@ -1979,7 +1979,7 @@
                     "registry": null,
                     "rename": null,
                     "req": "^0.2",
-                    "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0",
+                    "source": "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0",
                     "target": null,
                     "uses_default_features": true
                 },
@@ -1991,7 +1991,7 @@
                     "registry": null,
                     "rename": null,
                     "req": "^0.2",
-                    "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0",
+                    "source": "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0",
                     "target": null,
                     "uses_default_features": true
                 }
@@ -2001,7 +2001,7 @@
             "edition": "2018",
             "features": {},
             "homepage": "https://tokio.rs",
-            "id": "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
+            "id": "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
             "keywords": [
                 "logging",
                 "tracing",
@@ -2019,7 +2019,7 @@
             "readme": "README.md",
             "repository": "https://github.com/tokio-rs/tracing",
             "rust_version": null,
-            "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0",
+            "source": "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0",
             "targets": [
                 {
                     "crate_types": [
@@ -2205,7 +2205,7 @@
                 ]
             },
             "homepage": "https://tokio.rs",
-            "id": "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
+            "id": "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
             "keywords": [
                 "logging",
                 "tracing",
@@ -2231,7 +2231,7 @@
             "readme": "README.md",
             "repository": "https://github.com/tokio-rs/tracing",
             "rust_version": null,
-            "source": "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0",
+            "source": "git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0",
             "targets": [
                 {
                     "crate_types": [
@@ -2397,7 +2397,7 @@
             },
             {
                 "dependencies": [
-                    "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                    "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
                 ],
                 "deps": [
                     {
@@ -2408,7 +2408,7 @@
                             }
                         ],
                         "name": "tracing",
-                        "pkg": "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                        "pkg": "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
                     }
                 ],
                 "features": [],
@@ -2525,8 +2525,8 @@
                 "dependencies": [
                     "cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
                     "pin-project-lite 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
-                    "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
-                    "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                    "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)",
+                    "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
                 ],
                 "deps": [
                     {
@@ -2557,7 +2557,7 @@
                             }
                         ],
                         "name": "tracing_attributes",
-                        "pkg": "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                        "pkg": "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
                     },
                     {
                         "dep_kinds": [
@@ -2567,7 +2567,7 @@
                             }
                         ],
                         "name": "tracing_core",
-                        "pkg": "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                        "pkg": "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
                     }
                 ],
                 "features": [
@@ -2577,7 +2577,7 @@
                     "std",
                     "tracing-attributes"
                 ],
-                "id": "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                "id": "tracing 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
             },
             {
                 "dependencies": [
@@ -2618,7 +2618,7 @@
                     }
                 ],
                 "features": [],
-                "id": "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                "id": "tracing-attributes 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
             },
             {
                 "dependencies": [
@@ -2641,7 +2641,7 @@
                     "lazy_static",
                     "std"
                 ],
-                "id": "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
+                "id": "tracing-core 0.2.0 (git+https://github.com/tokio-rs/tracing.git?branch=master#1e09e50e8d15580b5929adbade9c782a6833e4a0)"
             },
             {
                 "dependencies": [],