step forty nine: merge, support multiple parents

This commit is contained in:
Gonçalo Valério 2020-11-28 21:41:04 +00:00
parent 8a8b028150
commit 2e9b5f6a4d
2 changed files with 17 additions and 11 deletions

View File

@ -11,7 +11,7 @@ mod diff;
pub struct Commit {
pub tree: String,
pub parent: String,
pub parents: Vec<String>,
pub message: String,
}
@ -98,7 +98,7 @@ pub fn commit(message: &str) -> String {
pub fn get_commit(oid: String) -> Commit {
let commit = data::get_object(oid, "commit".to_owned());
let tree: String;
let mut parent: String = "".to_owned();
let mut parents = vec![];
let message: String;
let mut message_start = 2;
@ -108,15 +108,17 @@ pub fn get_commit(oid: String) -> Commit {
line_items = lines[1].splitn(2, " ").collect();
if line_items[0] == "parent" {
parent = line_items[1].to_owned();
parents.push(line_items[1].to_owned());
message_start = 3;
} else {
parents.push("".to_owned());
}
message = lines[message_start..].join("\n");
return Commit {
tree,
parent,
parents,
message,
};
}
@ -135,7 +137,11 @@ pub fn iter_commits_and_parents(mut oids: VecDeque<String>) -> Vec<String> {
let commit = get_commit(oid);
// Deal with parent next
oids.push_front(commit.parent);
oids.push_front(commit.parents[0].clone());
// Deal with other parent later
if commit.parents.len() > 1 {
oids.push_back(commit.parents[1].clone());
}
}
return oid_sequence;

View File

@ -173,7 +173,7 @@ fn log_commits(matches: ArgMatches) {
print_commit(oid, &commit, refs.clone());
if commit.parent == "" {
if commit.parents[0] == "" {
break;
}
}
@ -214,9 +214,9 @@ fn k() {
oid,
&oid[0..10]
));
if commit.parent != "" {
println!("Parent: {}", commit.parent);
dot.push_str(&format!("\"{}\" -> \"{}\"\n", oid, commit.parent));
if commit.parents[0] != "" {
println!("Parent: {}", commit.parents[0]);
dot.push_str(&format!("\"{}\" -> \"{}\"\n", oid, commit.parents[0]));
}
}
dot.push_str("}");
@ -287,8 +287,8 @@ fn show(matches: ArgMatches) {
let oid = base::get_oid(cmd_matches.value_of("oid").unwrap().to_owned());
let commit = base::get_commit(oid.clone());
let refs: HashMap<String, Vec<String>> = HashMap::new();
let parent_tree = if commit.parent != "".to_owned() {
base::get_commit(commit.parent.clone()).tree
let parent_tree = if commit.parents[0] != "".to_owned() {
base::get_commit(commit.parents[0].clone()).tree
} else {
"".to_owned()
};