liboxen 0.9.9-alpha

Oxen is a fast, unstructured data version control, to help version datasets, written in Rust.
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Based on: <https://git-scm.com/docs/git-rm>
//! Remove files matching pathspec from the index, or from the working tree and the index.
//! `oxen rm` will not remove a file from just your working directory.
//! (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.)
//! When --staged is given, the staged content has to match either the tip of the branch or the file on disk,
//! allowing the file to be removed from just the index.

use crate::api;
use crate::command;
use crate::config::UserConfig;
use crate::error::OxenError;
use crate::model::LocalRepository;
use crate::opts::RmOpts;
use crate::util;

use super::CommitEntryReader;
use super::Stager;

use pluralizer::pluralize;
use std::convert::TryInto;
use std::path::Path;
use std::path::PathBuf;

pub async fn rm(repo: &LocalRepository, opts: &RmOpts) -> Result<(), OxenError> {
    if opts.remote {
        return remove_remote(repo, opts).await;
    }

    // Check if it is a directory and -r was provided
    let path = &opts.path;
    let dir_exists = dir_is_staged_or_committed(repo, path)?;

    if dir_exists && opts.recursive {
        return rm_dir(repo, opts).await;
    }

    // Error if is a directory and -r was not provided
    if dir_exists && !opts.recursive {
        let error = format!("`oxen rm` on directory {path:?} requires -r");
        return Err(OxenError::basic_str(error));
    }

    rm_file(repo, opts)
}

async fn rm_dir(repo: &LocalRepository, opts: &RmOpts) -> Result<(), OxenError> {
    let path = opts.path.as_ref();
    if opts.staged {
        return remove_staged(repo, opts);
    }

    // We can only use `oxen rm` on directories that are committed
    if !dir_is_committed(repo, path)? {
        let error = format!("Directory {path:?} does not match any committed directories.");
        return Err(OxenError::basic_str(error));
    }

    // Make sure there are no modified files in directory
    let modifications = list_modified_files_in_dir(repo, path)?;
    if !modifications.is_empty() {
        let num_mods: isize = modifications.len().try_into().unwrap(); // should always be safe to go from usize -> isize
        let error = format!("There are {} with modifications within {path:?}\n\tUse `oxen status` to see the modified files.", pluralize("file", num_mods, true));
        return Err(OxenError::basic_str(error));
    }

    // Remove the directory from disk
    let full_path = repo.path.join(path);
    log::debug!("REMOVING DIRECTORY: {full_path:?}");
    if full_path.exists() {
        // user might have removed dir manually before using `oxen rm`
        util::fs::remove_dir_all(&full_path)?;
    }

    // Stage all the removed files
    command::add(repo, &full_path)?;

    Ok(())
}

fn list_modified_files_in_dir(
    repo: &LocalRepository,
    path: &Path,
) -> Result<Vec<PathBuf>, OxenError> {
    let status = command::status(repo)?;
    let modified: Vec<PathBuf> = status
        .modified_files
        .into_iter()
        .filter(|p| p.starts_with(path))
        .collect();
    Ok(modified)
}

fn dir_is_staged_or_committed(repo: &LocalRepository, path: &Path) -> Result<bool, OxenError> {
    Ok(dir_is_staged(repo, path)? || dir_is_committed(repo, path)?)
}

fn dir_is_staged(repo: &LocalRepository, path: &Path) -> Result<bool, OxenError> {
    let stager = Stager::new(repo)?;
    Ok(stager.has_staged_dir(path))
}

fn dir_is_committed(repo: &LocalRepository, path: &Path) -> Result<bool, OxenError> {
    let commit = api::local::commits::head_commit(repo)?;
    let commit_reader = CommitEntryReader::new(repo, &commit)?;
    Ok(commit_reader.has_dir(path))
}

fn file_is_committed(repo: &LocalRepository, path: &Path) -> Result<bool, OxenError> {
    let commit = api::local::commits::head_commit(repo)?;
    let commit_reader = CommitEntryReader::new(repo, &commit)?;
    Ok(commit_reader.has_file(path))
}

fn rm_file(repo: &LocalRepository, opts: &RmOpts) -> Result<(), OxenError> {
    let path = opts.path.as_ref();
    if opts.staged {
        return remove_staged_file(repo, path);
    }

    if !file_is_committed(repo, path)? {
        let error = format!("File {path:?} must be committed to use `oxen rm`");
        return Err(OxenError::basic_str(error));
    }

    // Remove file from disk
    let full_path = repo.path.join(path);
    log::debug!("REMOVING FILE: {full_path:?}");
    if full_path.exists() {
        // user might have removed file manually before using `oxen rm`
        util::fs::remove_file(&full_path)?;
    }

    // Stage the removed file
    command::add(repo, &full_path)?;

    Ok(())
}

async fn remove_remote(repo: &LocalRepository, opts: &RmOpts) -> Result<(), OxenError> {
    let path = opts.path.as_ref();

    if opts.recursive {
        Err(OxenError::basic_str(
            "`oxen remote rm` does not support removing directories yet",
        ))
    } else {
        remove_remote_staged_file(repo, path).await
    }
}

async fn remove_remote_staged_file(repo: &LocalRepository, path: &Path) -> Result<(), OxenError> {
    let branch = api::local::branches::current_branch(repo)?.expect("Must be on branch.");
    let branch_name = branch.name;
    let remote_repo = api::remote::repositories::get_default_remote(repo).await?;
    let user_id = UserConfig::identifier()?;
    api::remote::staging::rm_file(&remote_repo, &branch_name, &user_id, path.to_path_buf()).await
}

fn remove_staged(repo: &LocalRepository, opts: &RmOpts) -> Result<(), OxenError> {
    let path = opts.path.as_ref();

    if opts.recursive {
        remove_staged_dir(repo, path)
    } else {
        remove_staged_file(repo, path)
    }
}

fn remove_staged_file(repo: &LocalRepository, path: &Path) -> Result<(), OxenError> {
    let stager = Stager::new(repo)?;
    stager.remove_staged_file(path)
}

fn remove_staged_dir(repo: &LocalRepository, path: &Path) -> Result<(), OxenError> {
    let stager = Stager::new(repo)?;
    stager.remove_staged_dir(path)
}

// unit tests
#[cfg(test)]
mod tests {
    use std::path::Path;

    use crate::command;
    use crate::core::index::rm;
    use crate::core::index::CommitEntryReader;
    use crate::error::OxenError;
    use crate::model::StagedEntryStatus;
    use crate::opts::RmOpts;
    use crate::test;
    use crate::util;

    #[tokio::test]
    async fn test_rm_staged_file() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("README", |repo| async move {
            // Stage the README.md file
            let path = Path::new("README.md");
            command::add(&repo, repo.path.join(path))?;

            let status = command::status(&repo)?;
            assert_eq!(status.staged_files.len(), 1);
            assert!(status.staged_files.contains_key(path));

            let opts = RmOpts::from_staged_path(path);
            rm::rm(&repo, &opts).await?;

            let status = command::status(&repo)?;
            assert_eq!(status.staged_files.len(), 0);

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_staged_dir_without_recursive_flag_should_be_error() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("train", |repo| async move {
            // Stage the data
            let path = Path::new("train");
            command::add(&repo, repo.path.join(path))?;

            let status = command::status(&repo)?;
            status.print_stdout();
            assert_eq!(status.staged_dirs.len(), 1);

            let opts = RmOpts {
                path: path.to_path_buf(),
                staged: true,
                recursive: false, // This should be an error
                remote: false,
            };
            let result = rm::rm(&repo, &opts).await;
            assert!(result.is_err());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_staged_dir() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("train", |repo| async move {
            // Stage the data
            let path = Path::new("train");
            command::add(&repo, repo.path.join(path))?;

            let status = command::status(&repo)?;
            status.print_stdout();
            assert_eq!(status.staged_dirs.len(), 1);

            let opts = RmOpts {
                path: path.to_path_buf(),
                staged: true,
                recursive: true, // make sure to pass in recursive
                remote: false,
            };
            rm::rm(&repo, &opts).await?;

            let status = command::status(&repo)?;
            status.print_stdout();
            assert_eq!(status.staged_dirs.len(), 0);
            assert_eq!(status.staged_files.len(), 0);

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_staged_dir_with_slash() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("train", |repo| async move {
            // Stage the data
            let path = Path::new("train/");
            command::add(&repo, repo.path.join(path))?;

            let status = command::status(&repo)?;
            assert_eq!(status.staged_dirs.len(), 1);

            let opts = RmOpts {
                path: path.to_path_buf(),
                staged: true,
                recursive: true, // make sure to pass in recursive
                remote: false,
            };
            let result = rm::rm(&repo, &opts).await;
            assert!(result.is_ok());

            let status = command::status(&repo)?;
            status.print_stdout();
            assert_eq!(status.staged_dirs.len(), 0);
            assert_eq!(status.staged_files.len(), 0);

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_staged_rm_file() -> Result<(), OxenError> {
        test::run_select_data_repo_test_committed_async("README", |repo| async move {
            // Remove the readme
            let path = Path::new("README.md");

            let opts = RmOpts::from_path(path);
            rm::rm(&repo, &opts).await?;

            let status = command::status(&repo)?;
            status.print_stdout();

            assert_eq!(status.staged_files.len(), 1);
            assert_eq!(
                status.staged_files.get(path).unwrap().status,
                StagedEntryStatus::Removed
            );

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_dir_without_recursive_flag_should_be_error() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("train", |repo| async move {
            // Remove the train dir
            let path = Path::new("train");

            let opts = RmOpts {
                path: path.to_path_buf(),
                staged: false,
                recursive: false, // This should be an error
                remote: false,
            };

            let result = rm::rm(&repo, &opts).await;
            assert!(result.is_err());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_dir_that_is_not_committed_should_throw_error() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("train", |repo| async move {
            // The train dir is not committed, so should get an error trying to remove
            let train_dir = Path::new("train");

            let opts = RmOpts {
                path: train_dir.to_path_buf(),
                staged: false,
                recursive: true, // Need to specify recursive
                remote: false,
            };

            let result = rm::rm(&repo, &opts).await;
            assert!(result.is_err());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_dir_with_modifications_should_throw_error() -> Result<(), OxenError> {
        test::run_select_data_repo_test_committed_async("train", |repo| async move {
            // Remove the train dir
            let train_dir = Path::new("train");

            let opts = RmOpts {
                path: train_dir.to_path_buf(),
                staged: false,
                recursive: true, // Need to specify recursive
                remote: false,
            };

            // copy a cat into the dog image
            util::fs::copy(
                Path::new("data/test/images/cat_1.jpg"),
                repo.path.join(train_dir.join("dog_1.jpg")),
            )?;

            // There should be one modified file
            let status = command::status(&repo)?;
            status.print_stdout();
            assert_eq!(status.modified_files.len(), 1);

            let result = rm::rm(&repo, &opts).await;
            assert!(result.is_err());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_dir() -> Result<(), OxenError> {
        test::run_select_data_repo_test_committed_async("train", |repo| async move {
            // Remove the train dir
            let path = Path::new("train");

            let og_num_files = util::fs::rcount_files_in_dir(&repo.path.join(path));

            let opts = RmOpts {
                path: path.to_path_buf(),
                staged: false,
                recursive: true, // Must pass in recursive = true
                remote: false,
            };
            rm::rm(&repo, &opts).await?;

            let status = command::status(&repo)?;
            status.print_stdout();

            assert_eq!(status.staged_files.len(), og_num_files);
            for (_, staged_entry) in status.staged_files.iter() {
                assert_eq!(staged_entry.status, StagedEntryStatus::Removed);
            }

            // commit the removal
            let commit = command::commit(&repo, "removed train dir")?;

            // make sure the train dir is deleted from the commits db
            let commit_reader = CommitEntryReader::new(&repo, &commit)?;
            assert!(!commit_reader.has_dir(path));

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_dir_with_slash() -> Result<(), OxenError> {
        test::run_select_data_repo_test_committed_async("train", |repo| async move {
            // Remove the train dir
            let path = Path::new("train/");

            let og_num_files = util::fs::rcount_files_in_dir(&repo.path.join(path));

            let opts = RmOpts {
                path: path.to_path_buf(),
                staged: false,
                recursive: true, // Must pass in recursive = true
                remote: false,
            };
            rm::rm(&repo, &opts).await?;

            let status = command::status(&repo)?;
            status.print_stdout();

            assert_eq!(status.staged_files.len(), og_num_files);
            for (_, staged_entry) in status.staged_files.iter() {
                assert_eq!(staged_entry.status, StagedEntryStatus::Removed);
            }

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_rm_subdir() -> Result<(), OxenError> {
        test::run_select_data_repo_test_committed_async("annotations", |repo| async move {
            // Remove the annotations/train subdir
            let path = Path::new("annotations").join("train");
            let og_num_files = util::fs::rcount_files_in_dir(&repo.path.join(&path));

            let opts = RmOpts {
                path,
                staged: false,
                recursive: true, // Must pass in recursive = true
                remote: false,
            };
            rm::rm(&repo, &opts).await?;

            let status = command::status(&repo)?;
            status.print_stdout();

            assert_eq!(status.staged_files.len(), og_num_files);
            for (_, staged_entry) in status.staged_files.iter() {
                assert_eq!(staged_entry.status, StagedEntryStatus::Removed);
            }

            Ok(())
        })
        .await
    }
}