|
1 | 1 | use anyhow::{Context, Result}; |
2 | 2 | use git_features::progress::Progress; |
3 | | -use git_odb::pack; |
| 3 | +use git_object::{owned, HashKind}; |
| 4 | +use git_odb::{loose, pack}; |
| 5 | +use std::io::Read; |
4 | 6 | use std::path::Path; |
5 | 7 |
|
6 | 8 | #[derive(PartialEq, Debug)] |
@@ -52,21 +54,65 @@ impl From<SafetyCheck> for pack::index::traverse::SafetyCheck { |
52 | 54 | } |
53 | 55 | } |
54 | 56 |
|
| 57 | +use quick_error::quick_error; |
| 58 | + |
| 59 | +quick_error! { |
| 60 | + #[derive(Debug)] |
| 61 | + enum Error { |
| 62 | + Io(err: std::io::Error) { |
| 63 | + source(err) |
| 64 | + from() |
| 65 | + } |
| 66 | + Odb(err: loose::db::write::Error) { |
| 67 | + source(err) |
| 68 | + from() |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +struct OutputWriter(Option<loose::Db>); |
| 74 | + |
| 75 | +impl git_odb::Write for OutputWriter { |
| 76 | + type Error = Error; |
| 77 | + |
| 78 | + fn write_stream( |
| 79 | + &self, |
| 80 | + kind: git_object::Kind, |
| 81 | + size: u64, |
| 82 | + from: impl Read, |
| 83 | + hash: HashKind, |
| 84 | + ) -> Result<owned::Id, Self::Error> { |
| 85 | + match self.0.as_ref() { |
| 86 | + Some(db) => db.write_stream(kind, size, from, hash).map_err(Into::into), |
| 87 | + None => git_odb::sink().write_stream(kind, size, from, hash).map_err(Into::into), |
| 88 | + } |
| 89 | + } |
| 90 | + fn write_buf(&self, kind: git_object::Kind, from: &[u8], hash: HashKind) -> Result<owned::Id, Self::Error> { |
| 91 | + match self.0.as_ref() { |
| 92 | + Some(db) => db.write_buf(kind, from, hash).map_err(Into::into), |
| 93 | + None => git_odb::sink().write_buf(kind, from, hash).map_err(Into::into), |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
55 | 98 | pub fn pack_or_pack_index<P>( |
56 | | - path: impl AsRef<Path>, |
| 99 | + pack_path: impl AsRef<Path>, |
| 100 | + object_path: Option<impl AsRef<Path>>, |
57 | 101 | _check: SafetyCheck, |
58 | 102 | _progress: Option<P>, |
59 | 103 | _delete_pack: bool, |
60 | 104 | ) -> Result<()> |
61 | 105 | where |
62 | 106 | P: Progress, |
63 | 107 | { |
64 | | - let path = path.as_ref(); |
| 108 | + let path = pack_path.as_ref(); |
65 | 109 | let _bundle = pack::Bundle::at(path).with_context(|| { |
66 | 110 | format!( |
67 | 111 | "Could not find .idx or .pack file from given file at '{}'", |
68 | 112 | path.display() |
69 | 113 | ) |
70 | 114 | })?; |
| 115 | + |
| 116 | + let _out = OutputWriter(object_path.map(|path| loose::Db::at(path.as_ref()))); |
71 | 117 | Ok(()) |
72 | 118 | } |
0 commit comments