mod init;
pub use init::Init;

mod clone;
pub use clone::Clone;

mod pushpull;
pub use pushpull::*;

mod log;
pub use self::log::Log;

mod record;
pub use record::Record;

mod diff;
pub use diff::Diff;

mod change;
pub use change::Change;

mod protocol;
pub use protocol::Protocol;

#[cfg(feature = "git")]
mod git;
#[cfg(feature = "git")]
pub use git::Git;

mod channel;
pub use channel::*;

mod reset;
pub use reset::*;

mod fork;
pub use fork::*;

mod unrecord;
pub use unrecord::*;

mod file_operations;
pub use file_operations::*;

mod apply;
pub use apply::*;

mod archive;
pub use archive::*;

mod credit;
pub use credit::*;

#[cfg(debug_assertions)]
mod debug;
#[cfg(debug_assertions)]
pub use debug::*;

/// Record the pending change (i.e. any unrecorded modifications in
/// the working copy), returning its hash.
fn pending<T: libpijul::MutTxnTExt + libpijul::TxnT>(
    txn: &mut T,
    channel: &mut libpijul::ChannelRef<T>,
    repo: &mut crate::repository::Repository,
) -> Result<Option<libpijul::Hash>, anyhow::Error> {
    use libpijul::changestore::ChangeStore;
    let recorded = txn.record_all(
        libpijul::Algorithm::default(),
        channel,
        &mut repo.working_copy,
        &repo.changes,
        "",
    )?;
    if recorded.actions.is_empty() {
        return Ok(None);
    }
    let actions = recorded
        .actions
        .into_iter()
        .map(|rec| rec.globalize(txn))
        .collect();
    let mut pending_change = libpijul::change::Change::make_change(
        txn,
        channel,
        actions,
        recorded.contents,
        libpijul::change::ChangeHeader::default(),
        Vec::new(),
    );
    let (dependencies, extra_known) =
        libpijul::change::dependencies(txn, channel, pending_change.changes.iter());
    pending_change.dependencies = dependencies;
    pending_change.extra_known = extra_known;
    let hash = repo.changes.save_change(&pending_change).unwrap();
    txn.apply_local_change(channel, &pending_change, hash, &recorded.updatables)?;
    Ok(Some(hash))
}

#[cfg(unix)]
fn pager() {
    pager::Pager::with_pager("less -RF").setup()
}

#[cfg(not(unix))]
fn pager() {}
