2020-11-07 23:06:42 +01:00
|
|
|
use std::collections::{HashMap, HashSet, VecDeque};
|
2020-10-05 19:49:44 +02:00
|
|
|
use std::fs;
|
2020-10-25 19:35:47 +01:00
|
|
|
use std::io;
|
|
|
|
use std::path::Path;
|
2020-11-23 23:48:06 +01:00
|
|
|
use walkdir::WalkDir;
|
2020-10-05 20:32:48 +02:00
|
|
|
#[path = "data.rs"]
|
|
|
|
mod data;
|
2020-10-05 19:49:44 +02:00
|
|
|
|
2020-11-27 01:11:30 +01:00
|
|
|
#[path = "diff.rs"]
|
|
|
|
mod diff;
|
|
|
|
|
2020-11-02 23:29:37 +01:00
|
|
|
pub struct Commit {
|
|
|
|
pub tree: String,
|
2020-11-28 22:41:04 +01:00
|
|
|
pub parents: Vec<String>,
|
2020-11-02 23:29:37 +01:00
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
2020-10-13 23:35:19 +02:00
|
|
|
pub fn write_tree(directory: String) -> String {
|
|
|
|
let mut entries: Vec<(String, String, String)> = vec![];
|
|
|
|
let mut name;
|
|
|
|
let mut type_: String;
|
|
|
|
let mut oid: String;
|
2020-10-05 19:49:44 +02:00
|
|
|
|
2020-10-13 23:35:19 +02:00
|
|
|
let it = fs::read_dir(&directory).unwrap();
|
|
|
|
for entry in it {
|
2020-10-05 19:49:44 +02:00
|
|
|
let item = entry.unwrap();
|
|
|
|
let metadata = item.metadata().unwrap();
|
2020-10-13 23:35:19 +02:00
|
|
|
name = item.file_name();
|
2020-10-05 19:49:44 +02:00
|
|
|
let full = format!("{}/{}", directory, name.to_str().unwrap());
|
2020-10-05 20:04:11 +02:00
|
|
|
if is_ignored(&full) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-05 19:49:44 +02:00
|
|
|
if metadata.is_file() {
|
2020-10-13 23:35:19 +02:00
|
|
|
type_ = "blob".to_owned();
|
|
|
|
oid = data::hash_object(&fs::read(&full).unwrap(), type_.clone());
|
|
|
|
println!("{} {}", oid, full);
|
2020-10-05 19:49:44 +02:00
|
|
|
} else if metadata.is_dir() {
|
2020-10-13 23:35:19 +02:00
|
|
|
type_ = "tree".to_owned();
|
|
|
|
oid = write_tree(full);
|
|
|
|
} else {
|
|
|
|
panic!("What is this?");
|
2020-10-05 19:49:44 +02:00
|
|
|
}
|
2020-10-13 23:35:19 +02:00
|
|
|
entries.push((
|
|
|
|
name.to_str().unwrap().to_owned().clone(),
|
|
|
|
oid.clone(),
|
|
|
|
type_.clone(),
|
|
|
|
));
|
2020-10-05 19:49:44 +02:00
|
|
|
}
|
2020-10-13 23:35:19 +02:00
|
|
|
|
|
|
|
entries.sort();
|
|
|
|
|
|
|
|
let mut tree = String::new();
|
|
|
|
for entry in entries {
|
|
|
|
tree.push_str(&format!("{} {} {}\n", entry.2, entry.1, entry.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return data::hash_object(&tree.into_bytes(), "tree".to_owned());
|
2020-10-05 19:49:44 +02:00
|
|
|
}
|
2020-10-05 20:04:11 +02:00
|
|
|
|
2020-10-25 19:35:47 +01:00
|
|
|
pub fn read_tree(oid: String) {
|
2020-10-27 00:22:17 +01:00
|
|
|
empty_current_directory(".").unwrap();
|
2020-10-25 19:35:47 +01:00
|
|
|
for (path, object_id) in get_tree(oid, "./".to_owned()).iter() {
|
|
|
|
let mut dirs = Path::new(path).ancestors();
|
|
|
|
dirs.next();
|
|
|
|
|
|
|
|
let dir = dirs.next().unwrap().to_str().unwrap();
|
|
|
|
|
|
|
|
fs::create_dir_all(dir).expect("Cannot create required dirs");
|
|
|
|
fs::write(path, data::get_object(object_id.clone(), "".to_owned()))
|
|
|
|
.expect("Cannot write required object");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 00:56:25 +01:00
|
|
|
pub fn commit(message: &str) -> String {
|
|
|
|
let mut commit = format!("tree {}\n", write_tree(".".to_owned()));
|
2020-11-02 00:01:24 +01:00
|
|
|
|
2020-11-08 14:09:17 +01:00
|
|
|
let head = data::get_ref("HEAD".to_owned(), true);
|
2020-11-08 13:49:39 +01:00
|
|
|
if head.value != "" {
|
2020-11-08 00:25:00 +01:00
|
|
|
commit += format!("parent {}\n", head.value).as_str();
|
2020-11-02 00:01:24 +01:00
|
|
|
}
|
2020-11-28 23:25:06 +01:00
|
|
|
let merge_head = data::get_ref("MERGE_HEAD".to_owned(), true);
|
|
|
|
if merge_head.value != "" {
|
|
|
|
commit += format!("parent {}\n", merge_head.value).as_str();
|
|
|
|
data::delete_ref("MERGE_HEAD".to_owned(), false);
|
|
|
|
}
|
2020-11-02 00:01:24 +01:00
|
|
|
|
2020-10-27 00:56:25 +01:00
|
|
|
commit += "\n";
|
|
|
|
commit += format!("{}\n", message).as_str();
|
|
|
|
|
2020-11-01 23:38:22 +01:00
|
|
|
let oid = data::hash_object(&commit.into_bytes(), "commit".to_owned());
|
2020-11-08 00:25:00 +01:00
|
|
|
data::update_ref(
|
|
|
|
"HEAD".to_owned(),
|
|
|
|
data::RefValue {
|
|
|
|
value: oid.clone(),
|
|
|
|
symbolic: false,
|
|
|
|
},
|
2020-11-08 14:09:17 +01:00
|
|
|
true,
|
2020-11-08 00:25:00 +01:00
|
|
|
);
|
2020-11-01 23:38:22 +01:00
|
|
|
return oid;
|
2020-10-27 00:56:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 23:29:37 +01:00
|
|
|
pub fn get_commit(oid: String) -> Commit {
|
|
|
|
let commit = data::get_object(oid, "commit".to_owned());
|
|
|
|
let tree: String;
|
2020-11-28 22:41:04 +01:00
|
|
|
let mut parents = vec![];
|
2020-11-02 23:29:37 +01:00
|
|
|
let message: String;
|
|
|
|
let mut message_start = 2;
|
|
|
|
|
|
|
|
let lines: Vec<&str> = commit.lines().collect();
|
|
|
|
let mut line_items: Vec<&str> = lines[0].splitn(2, " ").collect();
|
|
|
|
tree = line_items[1].to_owned();
|
|
|
|
|
|
|
|
line_items = lines[1].splitn(2, " ").collect();
|
|
|
|
if line_items[0] == "parent" {
|
2020-11-28 22:41:04 +01:00
|
|
|
parents.push(line_items[1].to_owned());
|
2020-11-02 23:29:37 +01:00
|
|
|
message_start = 3;
|
2020-11-28 23:25:06 +01:00
|
|
|
|
|
|
|
//Need to be refactored later
|
|
|
|
let other_parents: Vec<&str> = lines[2].splitn(2, " ").collect();
|
|
|
|
if other_parents[0] == "parent" {
|
|
|
|
parents.push(other_parents[1].to_owned());
|
|
|
|
message_start = 4;
|
|
|
|
}
|
2020-11-28 22:41:04 +01:00
|
|
|
} else {
|
|
|
|
parents.push("".to_owned());
|
2020-11-02 23:29:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message = lines[message_start..].join("\n");
|
|
|
|
|
|
|
|
return Commit {
|
|
|
|
tree,
|
2020-11-28 22:41:04 +01:00
|
|
|
parents,
|
2020-11-02 23:29:37 +01:00
|
|
|
message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-07 23:06:42 +01:00
|
|
|
pub fn iter_commits_and_parents(mut oids: VecDeque<String>) -> Vec<String> {
|
2020-11-07 20:45:58 +01:00
|
|
|
let mut visited: HashSet<String> = HashSet::new();
|
|
|
|
let mut oid_sequence = vec![];
|
|
|
|
|
|
|
|
while !oids.is_empty() {
|
2020-11-07 23:06:42 +01:00
|
|
|
let oid = oids.pop_front().unwrap();
|
2020-11-07 20:45:58 +01:00
|
|
|
if oid == "" || visited.contains(&oid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
visited.insert(oid.clone());
|
|
|
|
oid_sequence.push(oid.clone());
|
|
|
|
|
|
|
|
let commit = get_commit(oid);
|
2020-11-07 23:06:42 +01:00
|
|
|
// Deal with parent next
|
2020-11-28 22:41:04 +01:00
|
|
|
oids.push_front(commit.parents[0].clone());
|
|
|
|
// Deal with other parent later
|
|
|
|
if commit.parents.len() > 1 {
|
|
|
|
oids.push_back(commit.parents[1].clone());
|
|
|
|
}
|
2020-11-07 20:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return oid_sequence;
|
|
|
|
}
|
|
|
|
|
2020-11-13 21:17:20 +01:00
|
|
|
pub fn checkout(name: String) {
|
|
|
|
let oid = get_oid(name.clone());
|
2020-11-03 00:01:31 +01:00
|
|
|
let commit = get_commit(oid.clone());
|
2020-11-13 21:17:20 +01:00
|
|
|
let head;
|
2020-11-03 00:01:31 +01:00
|
|
|
read_tree(commit.tree);
|
2020-11-13 21:17:20 +01:00
|
|
|
|
|
|
|
if is_branch(name.clone()) {
|
|
|
|
head = data::RefValue {
|
|
|
|
symbolic: true,
|
|
|
|
value: format!("refs/heads/{}", name),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
head = data::RefValue {
|
2020-11-08 00:25:00 +01:00
|
|
|
symbolic: false,
|
2020-11-13 21:17:20 +01:00
|
|
|
value: oid,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
data::update_ref("HEAD".to_owned(), head, false);
|
2020-11-03 00:01:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:49:43 +01:00
|
|
|
pub fn create_tag(name: String, oid: String) {
|
2020-11-08 00:25:00 +01:00
|
|
|
data::update_ref(
|
|
|
|
format!("refs/tags/{}", name),
|
|
|
|
data::RefValue {
|
|
|
|
value: oid,
|
|
|
|
symbolic: false,
|
|
|
|
},
|
2020-11-08 14:09:17 +01:00
|
|
|
true,
|
2020-11-08 00:25:00 +01:00
|
|
|
);
|
2020-11-04 00:49:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 01:34:29 +01:00
|
|
|
pub fn get_oid(mut name: String) -> String {
|
|
|
|
if name == "@".to_owned() {
|
|
|
|
name = "HEAD".to_owned();
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:19:39 +01:00
|
|
|
let refs_to_try: [String; 4] = [
|
|
|
|
format!("{}", name),
|
|
|
|
format!("refs/{}", name),
|
|
|
|
format!("refs/tags/{}", name),
|
|
|
|
format!("refs/heads/{}", name),
|
|
|
|
];
|
|
|
|
|
2020-11-21 18:08:47 +01:00
|
|
|
for reference in refs_to_try.iter() {
|
2020-11-08 14:09:17 +01:00
|
|
|
let found = data::get_ref(reference.clone(), false);
|
2020-11-08 13:49:39 +01:00
|
|
|
if found.value != "" {
|
|
|
|
return found.value;
|
|
|
|
} else {
|
|
|
|
continue;
|
2020-11-06 01:19:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut is_hex = true;
|
|
|
|
for c in name.chars() {
|
|
|
|
if !c.is_ascii_hexdigit() {
|
|
|
|
is_hex = false;
|
|
|
|
}
|
2020-11-04 22:54:45 +01:00
|
|
|
}
|
2020-11-06 01:19:39 +01:00
|
|
|
|
|
|
|
if name.len() == 40 && is_hex {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!(format!("Unknown name {}", name));
|
2020-11-04 22:54:45 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 23:22:38 +01:00
|
|
|
pub fn create_branch(name: String, oid: String) {
|
2020-11-08 00:25:00 +01:00
|
|
|
data::update_ref(
|
|
|
|
format!("refs/heads/{}", name),
|
|
|
|
data::RefValue {
|
|
|
|
value: oid,
|
|
|
|
symbolic: false,
|
|
|
|
},
|
2020-11-08 14:09:17 +01:00
|
|
|
true,
|
2020-11-08 00:25:00 +01:00
|
|
|
);
|
2020-11-07 23:22:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 21:21:39 +01:00
|
|
|
pub fn init() -> std::io::Result<()> {
|
|
|
|
data::init()?;
|
|
|
|
data::update_ref(
|
|
|
|
"HEAD".to_owned(),
|
|
|
|
data::RefValue {
|
|
|
|
symbolic: true,
|
|
|
|
value: "refs/heads/master".to_owned(),
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-14 13:30:35 +01:00
|
|
|
pub fn get_branch_name() -> String {
|
|
|
|
let head = data::get_ref("HEAD".to_owned(), false);
|
|
|
|
if !head.symbolic {
|
|
|
|
return "".to_owned();
|
|
|
|
}
|
|
|
|
assert!(head.value.starts_with("refs/heads/"));
|
|
|
|
let ref_items: Vec<&str> = head.value.splitn(3, "/").collect();
|
|
|
|
return (*ref_items.last().unwrap()).to_owned();
|
|
|
|
}
|
|
|
|
|
2020-11-14 14:19:15 +01:00
|
|
|
pub fn iter_branch_names() -> Vec<String> {
|
|
|
|
let mut branches = vec![];
|
|
|
|
for entry in data::iter_refs("refs/heads/", true) {
|
|
|
|
let name: Vec<&str> = entry.0.splitn(2, "refs/heads/").collect();
|
|
|
|
branches.push((*name.last().unwrap()).to_owned());
|
|
|
|
}
|
|
|
|
branches
|
|
|
|
}
|
|
|
|
|
2020-11-17 00:25:25 +01:00
|
|
|
pub fn reset(oid: String) {
|
|
|
|
data::update_ref(
|
|
|
|
"HEAD".to_owned(),
|
|
|
|
data::RefValue {
|
|
|
|
symbolic: false,
|
|
|
|
value: oid,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-27 01:11:30 +01:00
|
|
|
pub fn merge(oid: String) {
|
|
|
|
let head = data::get_ref("HEAD".to_owned(), true);
|
|
|
|
assert!(head.value != "");
|
|
|
|
|
2020-12-01 00:22:00 +01:00
|
|
|
let merge_base = get_merge_base(oid.clone(), head.value.clone());
|
|
|
|
let c_base = get_commit(merge_base);
|
2020-11-27 01:11:30 +01:00
|
|
|
let c_head = get_commit(head.value);
|
2020-11-28 23:25:06 +01:00
|
|
|
let c_other = get_commit(oid.clone());
|
|
|
|
|
|
|
|
data::update_ref(
|
|
|
|
"MERGE_HEAD".to_owned(),
|
|
|
|
data::RefValue {
|
|
|
|
symbolic: false,
|
|
|
|
value: oid,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
2020-11-27 01:11:30 +01:00
|
|
|
|
2020-12-01 00:22:00 +01:00
|
|
|
read_tree_merged(c_base.tree, c_head.tree, c_other.tree);
|
2020-11-27 01:11:30 +01:00
|
|
|
println!("Merged in working tree");
|
2020-11-28 23:25:06 +01:00
|
|
|
println!("Please commit");
|
2020-11-27 01:11:30 +01:00
|
|
|
}
|
2020-11-24 00:20:18 +01:00
|
|
|
|
2020-11-29 22:33:25 +01:00
|
|
|
pub fn get_merge_base(commit1: String, commit2: String) -> String {
|
|
|
|
let mut commit1_deq = VecDeque::new();
|
|
|
|
commit1_deq.push_front(commit1);
|
|
|
|
|
|
|
|
let mut commit2_deq = VecDeque::new();
|
|
|
|
commit2_deq.push_front(commit2);
|
|
|
|
|
|
|
|
let parents1 = iter_commits_and_parents(commit1_deq);
|
|
|
|
|
|
|
|
for oid in iter_commits_and_parents(commit2_deq) {
|
|
|
|
if parents1.contains(&oid) {
|
|
|
|
return oid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "".to_owned();
|
|
|
|
}
|
|
|
|
|
2020-10-05 20:04:11 +02:00
|
|
|
fn is_ignored(path: &String) -> bool {
|
|
|
|
if path.contains(".rgit") {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 19:35:47 +01:00
|
|
|
|
|
|
|
fn tree_entries(oid: String) -> Vec<(String, String, String)> {
|
|
|
|
let mut entries: Vec<(String, String, String)> = vec![];
|
2020-11-21 18:08:47 +01:00
|
|
|
|
|
|
|
if oid == "".to_owned() {
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-10-25 19:35:47 +01:00
|
|
|
let tree_data = data::get_object(oid, "tree".to_owned());
|
|
|
|
for line in tree_data.split_terminator("\n") {
|
|
|
|
let items: Vec<&str> = line.splitn(3, " ").collect();
|
|
|
|
entries.push((
|
|
|
|
items[0].to_owned(), // _type
|
|
|
|
items[1].to_owned(), // oid
|
|
|
|
items[2].to_owned(), // name
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:08:47 +01:00
|
|
|
pub fn get_tree(oid: String, base_path: String) -> HashMap<String, String> {
|
2020-10-25 19:35:47 +01:00
|
|
|
let mut result = HashMap::new();
|
|
|
|
for entry in tree_entries(oid) {
|
|
|
|
// _type, oid, name
|
|
|
|
assert!(entry.2.find("/").is_none());
|
|
|
|
assert!(entry.2 != "..");
|
|
|
|
assert!(entry.2 != ".");
|
|
|
|
let path = base_path.clone() + entry.2.as_str();
|
|
|
|
if entry.0 == "blob".to_owned() {
|
|
|
|
result.insert(path.clone(), entry.1.clone());
|
|
|
|
} else if entry.0 == "tree".to_owned() {
|
|
|
|
result.extend(get_tree(entry.1, format!("{}/", path)));
|
|
|
|
} else {
|
|
|
|
panic!("Unknown tree entry: {}", entry.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2020-10-27 00:22:17 +01:00
|
|
|
|
2020-11-23 23:48:06 +01:00
|
|
|
pub fn get_working_tree() -> HashMap<String, String> {
|
|
|
|
let mut result = HashMap::new();
|
|
|
|
|
|
|
|
for entry in WalkDir::new(".") {
|
|
|
|
let item = entry.unwrap();
|
|
|
|
let relative_path = item.path().strip_prefix("./").unwrap();
|
|
|
|
let metadata = item.metadata().unwrap();
|
|
|
|
let path = item.path().to_str().unwrap().to_owned();
|
|
|
|
if metadata.is_file() && !is_ignored(&path.clone()) {
|
|
|
|
let content = fs::read(path.clone()).unwrap();
|
|
|
|
result.insert(
|
|
|
|
relative_path.to_str().unwrap().to_owned(),
|
|
|
|
data::hash_object(&content, "blob".to_owned()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-27 00:22:17 +01:00
|
|
|
fn empty_current_directory(dir: &str) -> io::Result<()> {
|
2020-11-01 23:38:22 +01:00
|
|
|
// Delete current directory, except the ignored directories and files
|
2020-10-27 00:22:17 +01:00
|
|
|
for entry in fs::read_dir(dir)? {
|
|
|
|
let entry = entry?;
|
|
|
|
let path = entry.path();
|
|
|
|
if is_ignored(&path.to_str().unwrap().to_owned()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.is_dir() {
|
|
|
|
empty_current_directory(path.to_str().unwrap())?;
|
|
|
|
match fs::remove_dir(&path) {
|
|
|
|
Ok(()) => (),
|
|
|
|
_ => println!("Unable to remove dir {}", path.clone().to_str().unwrap()),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
fs::remove_file(&path)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-11-13 21:17:20 +01:00
|
|
|
|
|
|
|
fn is_branch(name: String) -> bool {
|
|
|
|
return data::get_ref(name, true).value != "";
|
|
|
|
}
|
2020-11-27 01:11:30 +01:00
|
|
|
|
2020-12-01 00:22:00 +01:00
|
|
|
fn read_tree_merged(base_tree: String, head_tree: String, commit_tree: String) {
|
2020-11-27 01:11:30 +01:00
|
|
|
empty_current_directory(".").unwrap();
|
2020-12-01 00:22:00 +01:00
|
|
|
let base_tree = get_tree(base_tree, "".to_owned());
|
2020-11-27 01:11:30 +01:00
|
|
|
let head_tree = get_tree(head_tree, "".to_owned());
|
|
|
|
let commit_tree = get_tree(commit_tree, "".to_owned());
|
2020-12-01 00:22:00 +01:00
|
|
|
for (path, blob) in diff::merge_trees(base_tree, head_tree, commit_tree) {
|
2020-11-27 01:11:30 +01:00
|
|
|
let mut dirs = Path::new(&path).ancestors();
|
|
|
|
dirs.next();
|
|
|
|
|
|
|
|
let dir = dirs.next().unwrap().to_str().unwrap();
|
|
|
|
|
|
|
|
fs::create_dir_all(dir).expect("Cannot create required dirs");
|
|
|
|
fs::write(path, blob).expect("Cannot write required object");
|
|
|
|
}
|
|
|
|
}
|