1+ use crate :: OutputFormat ;
12use git_features:: progress:: Progress ;
23use git_odb:: pack;
34use std:: { fs, io, path:: PathBuf , str:: FromStr } ;
@@ -47,24 +48,11 @@ impl From<IterationMode> for pack::data::iter::Mode {
4748 }
4849}
4950
50- pub struct Context {
51+ pub struct Context < W : io :: Write > {
5152 pub thread_limit : Option < usize > ,
5253 pub iteration_mode : IterationMode ,
53- }
54-
55- impl From < Context > for pack:: bundle:: write:: Options {
56- fn from (
57- Context {
58- thread_limit,
59- iteration_mode,
60- } : Context ,
61- ) -> Self {
62- pack:: bundle:: write:: Options {
63- thread_limit,
64- iteration_mode : iteration_mode. into ( ) ,
65- index_kind : pack:: index:: Kind :: default ( ) ,
66- }
67- }
54+ pub format : OutputFormat ,
55+ pub out : W ,
6856}
6957
7058pub fn stream_len ( mut s : impl io:: Seek ) -> io:: Result < u64 > {
@@ -77,28 +65,47 @@ pub fn stream_len(mut s: impl io::Seek) -> io::Result<u64> {
7765 Ok ( len)
7866}
7967
80- pub fn from_pack < P > (
68+ pub fn from_pack < P , W : io :: Write > (
8169 pack : Option < PathBuf > ,
8270 directory : Option < PathBuf > ,
8371 progress : P ,
84- context : Context ,
72+ ctx : Context < W > ,
8573) -> anyhow:: Result < ( ) >
8674where
8775 P : Progress ,
8876 <<P as Progress >:: SubProgress as Progress >:: SubProgress : Send ,
8977{
9078 use anyhow:: Context ;
79+ let options = pack:: bundle:: write:: Options {
80+ thread_limit : ctx. thread_limit ,
81+ iteration_mode : ctx. iteration_mode . into ( ) ,
82+ index_kind : pack:: index:: Kind :: default ( ) ,
83+ } ;
84+ let out = ctx. out ;
85+ let format = ctx. format ;
9186 match pack {
9287 Some ( pack) => {
9388 let pack_len = pack. metadata ( ) ?. len ( ) ;
9489 let pack_file = fs:: File :: open ( pack) ?;
95- pack:: Bundle :: write_to_directory ( pack_file, Some ( pack_len) , directory, progress, context . into ( ) )
90+ pack:: Bundle :: write_to_directory ( pack_file, Some ( pack_len) , directory, progress, options )
9691 }
9792 None => {
9893 let stdin = io:: stdin ( ) ;
99- pack:: Bundle :: write_to_directory ( stdin. lock ( ) , None , directory, progress, context . into ( ) )
94+ pack:: Bundle :: write_to_directory ( stdin. lock ( ) , None , directory, progress, options )
10095 }
10196 }
10297 . with_context ( || "Failed to write pack and index" )
103- . map ( |_| ( ) )
98+ . map ( |res| {
99+ match format {
100+ OutputFormat :: Human => drop ( human_output ( out, res) ) ,
101+ #[ cfg( feature = "serde1" ) ]
102+ OutputFormat :: Json => serde_json:: to_writer_pretty ( out, & res) ?,
103+ } ;
104+ ( )
105+ } )
106+ }
107+
108+ fn human_output ( mut out : impl io:: Write , res : pack:: bundle:: write:: Outcome ) -> io:: Result < ( ) > {
109+ writeln ! ( & mut out, "index: {}" , res. index. index_hash) ?;
110+ writeln ! ( & mut out, "pack: {}" , res. index. pack_hash)
104111}
0 commit comments