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
use uuid::Uuid;
use crate::backend;
use crate::backend::{ChangeId, CommitId, Signature, TreeId};
use crate::commit::Commit;
use crate::repo::MutableRepo;
use crate::settings::UserSettings;
#[derive(Debug)]
pub struct CommitBuilder {
commit: backend::Commit,
rewrite_source: Option<Commit>,
}
pub fn new_change_id() -> ChangeId {
ChangeId::from_bytes(Uuid::new_v4().as_bytes())
}
impl CommitBuilder {
pub fn for_new_commit(
settings: &UserSettings,
parents: Vec<CommitId>,
tree_id: TreeId,
) -> CommitBuilder {
let signature = settings.signature();
assert!(!parents.is_empty());
let commit = backend::Commit {
parents,
predecessors: vec![],
root_tree: tree_id,
change_id: new_change_id(),
description: String::new(),
author: signature.clone(),
committer: signature,
};
CommitBuilder {
commit,
rewrite_source: None,
}
}
pub fn for_rewrite_from(settings: &UserSettings, predecessor: &Commit) -> CommitBuilder {
let mut commit = predecessor.store_commit().clone();
commit.predecessors = vec![predecessor.id().clone()];
commit.committer = settings.signature();
if commit.author.name == UserSettings::user_name_placeholder() {
commit.author.name = commit.committer.name.clone();
}
if commit.author.email == UserSettings::user_email_placeholder() {
commit.author.email = commit.committer.email.clone();
}
CommitBuilder {
commit,
rewrite_source: Some(predecessor.clone()),
}
}
pub fn set_parents(mut self, parents: Vec<CommitId>) -> Self {
assert!(!parents.is_empty());
self.commit.parents = parents;
self
}
pub fn set_predecessors(mut self, predecessors: Vec<CommitId>) -> Self {
self.commit.predecessors = predecessors;
self
}
pub fn set_tree(mut self, tree_id: TreeId) -> Self {
self.commit.root_tree = tree_id;
self
}
pub fn set_change_id(mut self, change_id: ChangeId) -> Self {
self.commit.change_id = change_id;
self
}
pub fn generate_new_change_id(mut self) -> Self {
self.commit.change_id = new_change_id();
self
}
pub fn set_description(mut self, description: String) -> Self {
self.commit.description = description;
self
}
pub fn set_author(mut self, author: Signature) -> Self {
self.commit.author = author;
self
}
pub fn set_committer(mut self, committer: Signature) -> Self {
self.commit.committer = committer;
self
}
pub fn write_to_repo(self, repo: &mut MutableRepo) -> Commit {
let mut rewrite_source_id = None;
if let Some(rewrite_source) = self.rewrite_source {
if *rewrite_source.change_id() == self.commit.change_id {
rewrite_source_id.replace(rewrite_source.id().clone());
}
}
let commit = repo.write_commit(self.commit);
if let Some(rewrite_source_id) = rewrite_source_id {
repo.record_rewritten_commit(rewrite_source_id, commit.id().clone())
}
commit
}
}