diff options
| author | Teddy Wing | 2020-07-24 00:41:19 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2020-07-24 00:42:53 +0200 | 
| commit | 4d89f0c16c61175042c47b493cb945618db8f72b (patch) | |
| tree | c2cd94703719000037e56ff9cf5498ee3ed3f305 | |
| parent | 55894d54c19d8eed751ef31b66698c0a85997cee (diff) | |
| download | git-suggestion-4d89f0c16c61175042c47b493cb945618db8f72b.tar.bz2 | |
Read a blob from a repository
Work out the code required to get a file blob's contents from a Git
repository using the 'git2' crate.
We'll be using this to get the original file for patching.
| -rw-r--r-- | Cargo.lock | 157 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/lib.rs | 22 | 
3 files changed, 180 insertions, 0 deletions
| @@ -81,6 +81,9 @@ name = "cc"  version = "1.0.58"  source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "f9a06fb2e53271d7c279ec1efea6ab691c35a2ae67ec0d91d7acec0caf13b518" +dependencies = [ + "jobserver", +]  [[package]]  name = "cfg-if" @@ -227,6 +230,7 @@ checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724"  name = "git-suggested-patch"  version = "0.0.1"  dependencies = [ + "git2",   "github-rs",   "regex",   "serde", @@ -236,6 +240,21 @@ dependencies = [  ]  [[package]] +name = "git2" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ac22e49b7d886b6802c66662b12609452248b1bc9e87d6d83ecea3db96f557" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "openssl-probe", + "openssl-sys", + "url", +] + +[[package]]  name = "github-rs"  version = "0.7.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -363,6 +382,17 @@ dependencies = [  ]  [[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]]  name = "indexmap"  version = "1.5.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -388,6 +418,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"  [[package]] +name = "jobserver" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c71313ebb9439f74b00d9d2dcec36440beaf57a6aa0623068441dd7cd81a7f2" +dependencies = [ + "libc", +] + +[[package]]  name = "kernel32-sys"  version = "0.2.2"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -410,6 +449,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701"  [[package]] +name = "libgit2-sys" +version = "0.12.9+1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b33bf3d9d4c45b48ae1ea7c334be69994624dc0a69f833d5d9f7605f24b552b" +dependencies = [ + "cc", + "libc", + "libssh2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", +] + +[[package]] +name = "libssh2-sys" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eafa907407504b0e683786d4aba47acf250f114d37357d56608333fd167dd0fc" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libz-sys" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]]  name = "lock_api"  version = "0.3.4"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -428,6 +507,12 @@ dependencies = [  ]  [[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + +[[package]]  name = "maybe-uninit"  version = "2.0.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -527,6 +612,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5"  [[package]] +name = "openssl-probe" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" + +[[package]] +name = "openssl-sys" +version = "0.9.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]]  name = "parking_lot"  version = "0.9.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -553,6 +657,18 @@ dependencies = [  ]  [[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pkg-config" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" + +[[package]]  name = "proc-macro2"  version = "1.0.18"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -792,6 +908,12 @@ dependencies = [  ]  [[package]] +name = "tinyvec" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53953d2d3a5ad81d9f844a32f14ebb121f50b650cd59d0ee2a07cf13c617efed" + +[[package]]  name = "tokio"  version = "0.1.22"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1022,6 +1144,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"  [[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +dependencies = [ + "matches", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fb19cf769fa8c6a80a162df694621ebeb4dafb606470b2b2fce0be40a98a977" +dependencies = [ + "tinyvec", +] + +[[package]]  name = "unicode-xid"  version = "0.2.1"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1045,6 +1185,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f"  [[package]] +name = "url" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" +dependencies = [ + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "vcpkg" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" + +[[package]]  name = "version_check"  version = "0.9.2"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4,6 +4,7 @@ version = "0.0.1"  edition = "2018"  [dependencies] +git2 = "0.13.8"  github-rs = "0.7.0"  regex = "1.3.9"  serde = { version = "1.0.114", features = ["derive"] } @@ -204,4 +204,26 @@ mod tests {      fn patches_file() {          // File::open("../testdata/");      } + +    #[test] +    fn read_git_blob() { +        use std::path::Path; + +        use git2::Repository; + +        let repo = Repository::open("./private/suggestion-test").unwrap(); +        let commit = repo.find_commit("b58be52880a0a0c0d397052351be31f19acdeca4".parse().unwrap()).unwrap(); + +        let object = commit +            .tree().unwrap() +            .get_path(Path::new("src/server.rs")).unwrap() +            .to_object(&repo).unwrap(); + +        let blob = object +            .as_blob().unwrap() +            .content(); + +        println!("{:?}", commit); +        println!("{}", std::str::from_utf8(blob).unwrap()); +    }  } | 
