use bytes::Bytes;
use packfile::{
low_level::{PackFile, PackFileEntry},
PktLine,
};
use thrussh::{server::Session, ChannelId};
use tracing::instrument;
use crate::{Handler, PackageProvider, UserProvider};
#[instrument(skip(handle, session, channel, metadata, packfile_entries), err)]
pub fn handle<U: UserProvider + PackageProvider + Send + Sync + 'static>(
handle: &mut Handler<U>,
session: &mut Session,
channel: ChannelId,
metadata: &[Bytes],
packfile_entries: &[PackFileEntry],
) -> Result<(), anyhow::Error> {
let done = metadata.iter().any(|v| v.as_ref() == b"done");
if !done {
handle.write(PktLine::Data(b"acknowledgments\n"))?;
handle.write(PktLine::Data(b"ready\n"))?;
handle.write(PktLine::Delimiter)?;
}
handle.write(PktLine::Data(b"packfile\n"))?;
let packfile = PackFile::new(packfile_entries);
handle.write(PktLine::SidebandData(packfile))?;
handle.write(PktLine::Flush)?;
handle.flush(session, channel);
session.exit_status_request(channel, 0);
session.eof(channel);
session.close(channel);
Ok(())
}