blob: 1bd88648795be00f32c802aa30ea45b26dba11eb (
plain)
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
|
use std::{
fs,
io::{Seek, Write},
path::Path,
};
use anyhow::Result;
use crate::{
files::{Locations, RepositoryPaths},
history::FileHistory,
};
use super::ActionOptions;
pub fn shift(command_options: ActionOptions, path: &str, new_cursor: usize) -> Result<()> {
let locations = Locations::from(&command_options);
match RepositoryPaths::from_tracked(&locations, Path::new(path))? {
RepositoryPaths::Tracked(tracked) => {
let mut history_file = tracked.load_history_file()?;
let mut file_history = FileHistory::from_file(&mut history_file)?;
file_history.set_cursor(new_cursor);
if file_history.file_is_deleted() {
fs::remove_file(path)?;
} else {
let new_content = file_history.get_content();
let mut tracked_file = tracked.create_tracked_file()?;
tracked_file.rewind()?;
tracked_file.set_len(0)?;
tracked_file.write_all(new_content.as_bytes())?;
}
file_history.write_to_file(&mut history_file)?;
}
RepositoryPaths::Deleted(deleted) => {
let mut history_file = deleted.load_history_file()?;
let mut file_history = FileHistory::from_file(&mut history_file)?;
file_history.set_cursor(new_cursor);
if !file_history.file_is_deleted() {
let mut new_tracked_file = deleted.create_tracked_file(&locations)?;
let new_content = file_history.get_content();
new_tracked_file.write_all(new_content.as_bytes())?;
}
}
RepositoryPaths::Untracked(_) => panic!("File is not tracked with Ka."),
}
Ok(())
}
|