commit_builder: add accessors for most fields

I'd like to be able to access the current committer on a
`CommitBuilder`.
This commit is contained in:
Martin von Zweigbergk 2023-03-17 22:34:25 -07:00 committed by Martin von Zweigbergk
parent 8f1dc49039
commit f758b646a9
2 changed files with 61 additions and 8 deletions

View file

@ -79,22 +79,38 @@ impl CommitBuilder<'_> {
}
}
pub fn parents(&self) -> &[CommitId] {
&self.commit.parents
}
pub fn set_parents(mut self, parents: Vec<CommitId>) -> Self {
assert!(!parents.is_empty());
self.commit.parents = parents;
self
}
pub fn predecessors(&self) -> &[CommitId] {
&self.commit.predecessors
}
pub fn set_predecessors(mut self, predecessors: Vec<CommitId>) -> Self {
self.commit.predecessors = predecessors;
self
}
pub fn tree(&self) -> &TreeId {
&self.commit.root_tree
}
pub fn set_tree(mut self, tree_id: TreeId) -> Self {
self.commit.root_tree = tree_id;
self
}
pub fn change_id(&self) -> &ChangeId {
&self.commit.change_id
}
pub fn set_change_id(mut self, change_id: ChangeId) -> Self {
self.commit.change_id = change_id;
self
@ -107,16 +123,28 @@ impl CommitBuilder<'_> {
self
}
pub fn description(&self) -> &str {
&self.commit.description
}
pub fn set_description(mut self, description: impl Into<String>) -> Self {
self.commit.description = description.into();
self
}
pub fn author(&self) -> &Signature {
&self.commit.author
}
pub fn set_author(mut self, author: Signature) -> Self {
self.commit.author = author;
self
}
pub fn committer(&self) -> &Signature {
&self.commit.committer
}
pub fn set_committer(mut self, committer: Signature) -> Self {
self.commit.committer = committer;
self

View file

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use jujutsu_lib::backend::{ChangeId, MillisSinceEpoch, ObjectId, Signature, Timestamp};
use jujutsu_lib::matchers::EverythingMatcher;
use jujutsu_lib::repo::Repo;
use jujutsu_lib::repo_path::RepoPath;
@ -39,24 +40,48 @@ fn test_initial(use_git: bool) {
);
let mut tx = repo.start_transaction(&settings, "test");
let commit = tx
let author_signature = Signature {
name: "author name".to_string(),
email: "author email".to_string(),
timestamp: Timestamp {
timestamp: MillisSinceEpoch(100),
tz_offset: 60,
},
};
let committer_signature = Signature {
name: "committer name".to_string(),
email: "committer email".to_string(),
timestamp: Timestamp {
timestamp: MillisSinceEpoch(200),
tz_offset: -60,
},
};
let change_id = ChangeId::new(vec![100u8; 16]);
let builder = tx
.mut_repo()
.new_commit(
&settings,
vec![store.root_commit_id().clone()],
tree.id().clone(),
)
.write()
.unwrap();
.set_change_id(change_id.clone())
.set_description("description")
.set_author(author_signature.clone())
.set_committer(committer_signature.clone());
assert_eq!(builder.parents(), &[store.root_commit_id().clone()]);
assert_eq!(builder.predecessors(), &[]);
assert_eq!(builder.tree(), tree.id());
assert_eq!(builder.change_id(), &change_id);
assert_eq!(builder.author(), &author_signature);
assert_eq!(builder.committer(), &committer_signature);
let commit = builder.write().unwrap();
tx.commit();
assert_eq!(commit.parents(), vec![store.root_commit()]);
assert_eq!(commit.predecessors(), vec![]);
assert_eq!(commit.description(), "");
assert_eq!(commit.author().name, settings.user_name());
assert_eq!(commit.author().email, settings.user_email());
assert_eq!(commit.committer().name, settings.user_name());
assert_eq!(commit.committer().email, settings.user_email());
assert_eq!(commit.description(), "description");
assert_eq!(commit.author(), &author_signature);
assert_eq!(commit.committer(), &committer_signature);
assert_eq!(
store
.root_commit()