pijul_org / pijul

refactor: Provide a standalone function to create repo archive

The previous implementation was very dependent on the dist command CLI. To reduce this dependency, in particular in order to be able to reuse this feature elsewhere in the code, the new implementation tries to be unrelated to BasicOpts.

In addition, a new test is provided in order to verify that pijul dist correctly fails when an incorrect path is submitted.

By lthms on May 10, 2018
This patch is not signed.
76G8xVjw2JQ6ZMfrpVGWKzL1vWGSBfxQhwVo5CVAjzLqqkqShzQ54j8b32aW7uM6NiadPZzbgN4e4QY7Wev8Hdyi
This patch is in the following branches:
latest
master
testing






































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48







49
50
51
52
53
54
55
56






57
58
59
60
61
62


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90





91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

155
156
use commands::{BasicOptions, StaticSubcommand, default_explain};
use commands::{BasicOptions, StaticSubcommand, default_explain, ScanScope};
use error::{Result};
use libpijul::{ROOT_KEY, Branch, Txn, Key, PatchId, Edge, Repository};
pub fn dist<W: Write>(repo: Repository,
                      branch_name: &str,
                      scope: ScanScope,
                      archive_name: &str,
                      encoder: GzEncoder<W>
                     ) -> Result<()> {
    let branch = txn.get_branch(branch_name)
        .ok_or(error::ErrorKind::NoSuchBranch)?;
    let mut buffer = Vec::new();
        ScanScope::FromRoot => {
        },
            let inode = txn.find_inode(prefix.as_ref())?;
                .ok_or(error::ErrorKind::InvalidPath(user_input))?
        },

    archive_rec(&txn, &branch, key, &mut archive, &mut buffer, &mut forward,
                &mut current_path)?;
    archive.into_inner()?
            .finish()?
            .flush()
            .map_err(|x| x.into())
pub fn run(args: &ArgMatches) -> Result<()> {
    let archive_path = PathBuf::from(archive_name.to_string() + ".tar.gz");
    let encoder = GzEncoder::new(File::create(&archive_path)?, Compression::best());
    dist(repo, &opts.branch(), scan, archive_name, encoder)
fn archive_rec<W:Write> (txn: &Txn,
                         branch: &Branch,
                         key: Key<PatchId>,
                         builder: &mut Builder<W>,
                         buffer: &mut Vec<u8>,
                         forward: &mut Vec<(Key<PatchId>, Edge)>,
                         current_path: &mut PathBuf,
                        ) -> Result<()> {
pub fn explain(res: Result<()>) {
use clap::{Arg, ArgMatches, SubCommand};
use commands::{default_explain, BasicOptions, ScanScope, StaticSubcommand};
use error::Error;
use flate2::write::GzEncoder;
use flate2::Compression;
use libpijul::{graph, Branch, Edge, Key, PatchId, Repository, Txn, ROOT_KEY};
use std::fs::{remove_file, File};
use std::io::{stdout, Write};
use std::path::{Path, PathBuf};
use tar::{Builder, Header};

pub fn invocation() -> StaticSubcommand {
    return SubCommand::with_name("dist")
        .about("Produces a tar.gz archive of the repository")
        .arg(
            Arg::with_name("archive")
                .short("d")
                .takes_value(true)
                .required(true)
                .help("File name of the output archive."),
        )
        .arg(
            Arg::with_name("branch")
                .long("branch")
                .help("The branch from which to make the archive, defaults to the current branch.")
                .takes_value(true)
                .required(false),
        )
        .arg(
            Arg::with_name("repository")
                .long("repository")
                .help("Repository where to work.")
                .takes_value(true),
        )
        .arg(
            Arg::with_name("stdout")
                .long("stdout")
                .short("s")
                .help("Prints the resulting archive to stdout")
                .takes_value(false),
        )
        .arg(
            Arg::with_name("dir")
                .help("Directory (or file) to archive, defaults to the whole repository.")
                .takes_value(true),
        );
}

pub fn run(args: &ArgMatches) -> Result<(), error::Error> {
    let opts = BasicOptions::from_args(args)?;
    let archive_name = args.value_of("archive").unwrap();
    let archive_path = {
        PathBuf::from(archive_name.to_string() + ".tar.gz")
    };
    let repo = opts.open_repo()?;
pub fn dist<W: Write>(
    repo: Repository,
    branch_name: &str,
    scope: ScanScope,
    archive_name: &str,
    encoder: GzEncoder<W>,
) -> Result<(), Error> {
    let txn = repo.txn_begin()?;
    if let Some(branch) = txn.get_branch(&opts.branch()) {
        let encoder = GzEncoder::new(File::create(&archive_path)?, Compression::best());
        let mut archive = Builder::new(encoder);
        let mut buffer = Vec::new();
        let mut forward = Vec::new();
        let mut current_path = Path::new(archive_name).to_path_buf();
    let branch = txn.get_branch(branch_name).ok_or(Error::NoSuchBranch)?;
    let mut current_path = Path::new(archive_name).to_path_buf();
    let mut archive = Builder::new(encoder);
    let mut buffer = graph::Writer::new(Vec::new());
    let mut forward = Vec::new();

        let inode = opts.dir_inode(&txn)?;
        let key = if inode.is_root() {
    let key = match scope {
        ScanScope::FromRoot => ROOT_KEY,
        ScanScope::WithPrefix(prefix, user_input) => {
            let inode = txn.find_inode(&prefix)?;
            txn.get_inodes(inode)
                .map(|key| key.key.to_owned())
                .ok_or(Error::InvalidPath {
                    path: PathBuf::from(user_input),
                })?
        }
    };
    archive_rec(
        &txn,
        &branch,
        key,
        &mut archive,
        &mut buffer,
        &mut forward,
        &mut current_path,
    )?;

    archive
        .into_inner()?
        .finish()?
        .flush()
        .map_err(|x| x.into())
}

pub fn archive_rec<W:Write>(
    txn: &Txn, branch: &Branch, key: Key<PatchId>,
    builder: &mut Builder<W>, buffer: &mut Vec<u8>,
    forward: &mut Vec<(Key<PatchId>, Edge)>, current_path: &mut PathBuf
) -> Result<(), error::Error> {
pub fn run(args: &ArgMatches) -> Result<(), Error> {
    let opts = BasicOptions::from_args(args)?;

    let archive_name = args.value_of("archive").unwrap();

    let repo = opts.open_repo()?;
    let scan = opts.scan_scope()?;

    if args.is_present("stdout") {
        let encoder = GzEncoder::new(stdout(), Compression::best());

        dist(repo, &opts.branch(), scan, archive_name, encoder)
    } else {
        let archive_path = PathBuf::from(archive_name.to_string() + ".tar.gz");

        let encoder = GzEncoder::new(File::create(&archive_path)?, Compression::best());

        dist(repo, &opts.branch(), scan, archive_name, encoder).map_err(|err| {
            // The creation of the archive has failed, we should try to
            // remove it, but we ignore the error if we cannot.
            // This should not happen, because either we could not create
            // the file, or we have enough permission to do it, as we are
            // its creator.
            let _ = remove_file(archive_path);
            err
        })
    }
}

fn archive_rec<W: Write>(
    txn: &Txn,
    branch: &Branch,
    key: Key<PatchId>,
    builder: &mut Builder<W>,
    buffer: &mut graph::Writer<Vec<u8>>,
    forward: &mut Vec<(Key<PatchId>, Edge)>,
    current_path: &mut PathBuf,
) -> Result<(), Error> {
    let files = txn.list_files_under_node(branch, key);

    for (key, names) in files {
        debug!("archive_rec: {:?} {:?}", key, names);
        if names.len() > 1 {
            error!("file has several names: {:?}", names);
        }
        current_path.push(names[0].1);
        if names[0].0.is_dir() {
            archive_rec(txn, branch, key, builder, buffer, forward, current_path)?;
        } else {
            buffer.clear();
            let mut graph = txn.retrieve(&branch, key);
            txn.output_file(branch, buffer, &mut graph, forward)?;
            let mut header = Header::new_gnu();
            header.set_path(&current_path)?;
            header.set_size(buffer.len() as u64);
            header.set_mode(names[0].0.permissions() as u32);
            header.set_cksum();
            builder.append(&header, &buffer[..])?;
        }
        current_path.pop();
    }
    Ok(())
}

pub fn explain(res: Result<(), error::Error>) {
pub fn explain(res: Result<(), Error>) {
    default_explain(res)











1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

202


205


208
209
210


213


216
217
218

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    WithPrefix(PathBuf, String),
    pub fn scan_scope(&self) -> Result<ScanScope> {
            let root = self.args.value_of("repository")
                           .map(|root| Path::new(root).to_path_buf())
                           .unwrap_or(current_dir()?);
            Ok(ScanScope::WithPrefix(relative_repo_path(&self.repo_root, &root, prefix)?,
                                     prefix.into()))
}

pub fn relative_repo_path(repo_root: &PathBuf, base: &PathBuf, dir: &str) -> Result<PathBuf> {
    Ok(dir.strip_prefix(&repo_root)?.to_owned())
use clap;
use clap::ArgMatches;
pub type StaticSubcommand = clap::App<'static, 'static>;

mod ask;
mod fs_operation;
pub mod remote;
mod ssh_auth_attempts;

pub mod add;
pub mod apply;
pub mod branches;
pub mod checkout;
pub mod clone;
pub mod credit;
pub mod dependencies;
pub mod diff;
pub mod dist;
pub mod fork;
pub mod generate_completions;
pub mod grep;
pub mod hooks;
pub mod info;
pub mod init;
pub mod key;
pub mod log;
pub mod ls;
pub mod mv;
pub mod patch;
pub mod prune;
pub mod pull;
pub mod push;
pub mod record;
pub mod remove;
pub mod revert;
pub mod rollback;
pub mod sign;
pub mod status;
pub mod tag;
pub mod unrecord;

mod fold_until;

use error::Error;
use libpijul::fs_representation::{RepoPath, RepoRoot};
use libpijul::Hash;
use libpijul::{fs_representation, Inode, Repository, Txn, DEFAULT_BRANCH};
use rand;
use std::borrow::Cow;
use std::env::current_dir;
use std::env::var;
use std::fs::{canonicalize, create_dir, metadata};
use std::io::{stderr, Write};
use std::path::{Path, PathBuf};
use std::process::exit;

pub fn all_command_invocations() -> Vec<StaticSubcommand> {
    return vec![
        log::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(),
        prune::invocation(),
        checkout::invocation(),
        diff::invocation(),
        credit::invocation(),
        dist::invocation(),
        key::invocation(),
        rollback::invocation(),
        status::invocation(),
        dependencies::invocation(),
        tag::invocation(),
        sign::invocation(),
        generate_completions::invocation(),
        grep::invocation(),
    ];
}

pub fn get_wd(repository_path: Option<&Path>) -> Result<PathBuf, Error> {
    debug!("get_wd: {:?}", repository_path);
    match repository_path {
        None => Ok(canonicalize(current_dir()?)?),
        Some(a) if a.is_relative() => Ok(canonicalize(current_dir()?.join(a))?),
        Some(a) => Ok(canonicalize(a)?),
    }
}

/// Returns an error if the `dir` is contained in a repository.
pub fn assert_no_containing_repo(dir: &Path) -> Result<(), Error> {
    if metadata(dir).is_ok() {
        if fs_representation::find_repo_root(&canonicalize(dir)?).is_some() {
            return Err(Error::InARepository {
                path: dir.to_owned(),
            });
        }
    }
    Ok(())
}

/// Creates an empty pijul repository in the given directory.
pub fn create_repo(dir: &Path) -> Result<(), Error> {
    // Check that a repository does not already exist.
    if metadata(dir).is_err() {
        create_dir(dir)?;
    }
    let dir = canonicalize(dir)?;
    if fs_representation::find_repo_root(&dir).is_some() {
        return Err(Error::InARepository {
            path: dir.to_owned(),
        });
    }

    let repo_root = fs_representation::create(&dir, rand::thread_rng())?;
    let repo = repo_root.open_repo(None)?;
    repo.mut_txn_begin(rand::thread_rng())?.commit()?;
    Ok(())
}

fn default_explain<R>(command_result: Result<R, Error>) {
    debug!("default_explain");
    match command_result {
        Ok(_) => (),
        Err(e) => {
            writeln!(stderr(), "error: {}", e).unwrap();
            exit(1)
        }
    }
}

fn validate_base58(x: String) -> ::std::result::Result<(), String> {
    if Hash::from_base58(&x).is_some() {
        Ok(())
    } else {
        Err(format!("\"{}\" is invalid base58", x))
    }
}

/// Almost all commands want to know the current directory and the repository root.  This struct
/// fills that need, and also provides methods for other commonly-used tasks.
pub struct BasicOptions<'a> {
    /// This isn't 100% the same as the actual current working directory, so pay attention: this
    /// will be the current directory, unless the user specifies `--repository`, in which case
    /// `cwd` will actually be the path of the repository root. In other words, specifying
    /// `--repository` has the same effect as changing directory to the repository root before
    /// running `pijul`.
    pub cwd: PathBuf,
    pub repo_root: RepoRoot<PathBuf>,
    args: &'a ArgMatches<'a>,
}


pub enum ScanScope {
    FromRoot,
    WithPrefix(RepoPath<PathBuf>, String),
}

impl<'a> BasicOptions<'a> {
    /// Reads the options from command line arguments.
    pub fn from_args(args: &'a ArgMatches<'a>) -> Result<BasicOptions<'a>, Error> {
        let wd = get_wd(args.value_of("repository").map(Path::new))?;
        let repo_root = if let Some(r) = fs_representation::find_repo_root(&canonicalize(&wd)?) {
            r
        } else {
            return Err(Error::NotInARepository);
        };
        Ok(BasicOptions {
            cwd: wd,
            repo_root: repo_root,
            args: args,
        })
    }

    /// Gets the name of the desired branch.
    pub fn branch(&self) -> String {
        if let Some(b) = self.args.value_of("branch") {
            b.to_string()
        } else if let Ok(b) = self.repo_root.get_current_branch() {
            b
        } else {
            DEFAULT_BRANCH.to_string()
        }
    }

    pub fn repo_root(&self) -> PathBuf {
        self.repo_root.repo_root.clone()
    }

    pub fn open_repo(&self) -> Result<Repository, Error> {

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        self.repo_root.open_repo(None).map_err(|e| e.into())

================================

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        fs_representation::open_repo(&self.repo_root, None).map_err(|e| e.into())

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    }

    pub fn open_and_grow_repo(&self, increase: u64) -> Result<Repository, Error> {

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        fs_representation::open_repo(&self.repo_root, Some(increase)).map_err(|e| e.into())

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

================================
        self.repo_root
            .open_repo(Some(increase))
            .map_err(|e| e.into())

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    }

    pub fn pristine_dir(&self) -> PathBuf {
        self.repo_root.pristine_dir()
    }

    pub fn patches_dir(&self) -> PathBuf {
        self.repo_root.patches_dir()
    }

    pub fn scan_scope(&self) -> Result<ScanScope, Error> {
        if let Some(prefix) = self.args.value_of("dir") {
            let root = self
                .args
                .value_of("repository")
                .map(|root| Path::new(root).to_path_buf())
                .unwrap_or(current_dir()?);

            Ok(ScanScope::WithPrefix(
                relative_repo_path(&self.repo_root, &root, prefix)?,
                prefix.into(),
            ))
        } else {
            Ok(ScanScope::FromRoot)
        }
    }

    fn dir_inode(&self, txn: &Txn) -> Result<Inode, Error> {
        use libpijul::ROOT_INODE;
        if let Some(dir) = self.args.value_of("dir") {
            let dir = if Path::new(dir).is_relative() {
                let root = if let Some(root) = self.args.value_of("repository") {
                    Path::new(root).to_path_buf()
                } else {
                    current_dir()?
                };
                root.join(&dir).canonicalize()?
            } else {
                Path::new(dir).canonicalize()?
            };
            let dir = self.repo_root.relativize(&dir)?;
            debug!("{:?}", dir);
            let inode = txn.find_inode(&dir)?;
            debug!("{:?}", inode);
            Ok(inode)
        } else {
            Ok(ROOT_INODE)
        }
    }
}

fn remote_pijul_cmd() -> Cow<'static, str> {
    if let Ok(cmd) = var("REMOTE_PIJUL") {
        Cow::Owned(cmd)
    } else {
        Cow::Borrowed("pijul")
    }
}

pub fn relative_repo_path(
    repo_root: &RepoRoot<PathBuf>,
    base: &PathBuf,
    dir: &str,
) -> Result<RepoPath<PathBuf>, Error> {
    let dir = if Path::new(dir).is_relative() {
        base.join(&dir).canonicalize()?
    } else {
        Path::new(dir).canonicalize()?
    };

    Ok(repo_root.relativize(&dir)?.to_owned())
}

pub fn pretty_repo_path(
    repo_root: &RepoRoot<impl AsRef<Path>>,
    path: &RepoPath<impl AsRef<Path>>,
    cwd: &Path,
) -> PathBuf {
    let abs_path = repo_root.absolutize(path);
    pathdiff::diff_paths(&abs_path, cwd).unwrap_or(abs_path)
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      assert_failure "pijul dist should have failed"
#!/usr/bin/env bats

load ../test_helper

@test "dist works" {
    make_repo toto
    cd toto
    touch act1 act2 act3
    mkdir d
    touch d/act4 d/act5
    pijul add act1 act2 d d/act4
    pijul record -am "bunch of files" -A "Élisabeth Jacquet de la Guerre"
    pijul dist -d "cephale-et-procris-1.0"
    test -e cephale-et-procris-1.0.tar.gz
    run tar ztvf cephale-et-procris-1.0.tar.gz
    assert_output "cephale-et-procris-1.0/act1"
    assert_output "cephale-et-procris-1.0/act2"
    assert_output "cephale-et-procris-1.0/d/act4"
    [[ ! ( $output =~ act3 ) ]]
    [[ ! ( $output =~ d/act5 ) ]]
}

@test "dist fails with incorrect path" {
    make_repo toto
    cd toto
    touch act1 act2 act3
    mkdir d
    touch d/act4 d/act5
    pijul add act1 act2 d d/act4
    pijul record -am "bunch of files" -A "Élisabeth Jacquet de la Guerre"
    if $(pijul dist -d "cephale-et-procris-1.0" incorrect_path/); then
        assert_failure "pijul dist should have failed"
    fi

    if [ -f cephale-et-procris-1.0.tar.gz ]; then
        assert_failure "The archive was not correctly removed after failure"
    fi
}

@test "dist to file and dist to stdout gives the same result" {
    make_repo toto
    cd toto
    touch act1 act2 act3
    mkdir d
    touch d/act4 d/act5
    pijul add act1 act2 d d/act4
    pijul record -am "bunch of files" -A "Élisabeth Jacquet de la Guerre"
    pijul dist -d "cephale-et-procris-1.0"
    pijul dist -d "cephale-et-procris-1.0" --stdout > stdout.tar.gz

    diff cephale-et-procris-1.0.tar.gz stdout.tar.gz
}