pijul 0.3.2

A patch-based distributed version control system, easy to use and fast. Command-line interface.
use clap;
pub type StaticSubcommand = clap::App<'static, 'static>;

mod fs_operation;
mod remote;
mod ask;

pub mod info;
pub mod init;
pub mod record;
pub mod add;
pub mod pull;
pub mod push;
pub mod apply;
pub mod clone;
pub mod remove;
pub mod mv;
pub mod ls;
pub mod revert;
pub mod unrecord;
pub mod changes;
pub mod patch;
pub mod fork;
pub mod branches;
pub mod delete_branch;
pub mod checkout;

#[cfg(test)]
mod test;
use std::fs::{File, canonicalize};
use std::path::{Path, PathBuf};
use std::env::current_dir;

use error::Error;
use std::io::{Read, Write};
use libpijul::DEFAULT_BRANCH;

pub fn all_command_invocations() -> Vec<StaticSubcommand> {
    return vec![changes::invocation(),
                info::invocation(),
                init::invocation(),
                record::invocation(),
                unrecord::invocation(),
                add::invocation(),
                pull::invocation(),
                push::invocation(),
                apply::invocation(),
                clone::invocation(),
                remove::invocation(),
                mv::invocation(),
                ls::invocation(),
                revert::invocation(),
                patch::invocation(),
                fork::invocation(),
                branches::invocation(),
                delete_branch::invocation(),
                checkout::invocation(),
    ];
}

pub fn get_wd(repository_path: Option<&Path>) -> Result<PathBuf, Error> {
    match repository_path {
        None => {
            let p = try!(canonicalize(try!(current_dir())));
            Ok(p)
        }
        Some(a) if a.is_relative() => {
            let mut p = try!(canonicalize(try!(current_dir())));
            p.push(a);
            Ok(p)
        }
        Some(a) => {
            let p = try!(canonicalize(a));
            Ok(p)
        }
    }
}


pub fn get_current_branch(root: &Path) -> Result<String, Error> {
    let path = root.join("current_branch");
    if let Ok(mut f) = File::open(&path) {
        let mut s = String::new();
        f.read_to_string(&mut s)?;
        Ok(s.trim().to_string())
    } else {
        Ok(DEFAULT_BRANCH.to_string())
    }
}

pub fn set_current_branch(root: &Path, branch: &str) -> Result<(), Error> {
    let path = root.join("current_branch");
    let mut f = File::create(&path)?;
    f.write_all(branch.trim().as_ref())?;
    f.write_all(b"\n")?;
    Ok(())
}