mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2025-01-08 19:06:38 +03:00
support updating branch ownership
This commit is contained in:
parent
87476939a8
commit
df935248c0
@ -257,8 +257,6 @@ mod tests {
|
||||
let mut ownership =
|
||||
Ownership::try_from("src/main.rs:100-200,200-300\nsrc/main.rs").unwrap();
|
||||
let taken = ownership.take(&FileOwnership::try_from("src/main.rs:100-200").unwrap());
|
||||
println!("{}", ownership);
|
||||
println!("{:?}", taken);
|
||||
assert_eq!(ownership.files.len(), 2);
|
||||
assert_eq!(
|
||||
ownership.files[0],
|
||||
|
@ -11,6 +11,10 @@ impl<'reader> BranchReader<'reader> {
|
||||
Self { reader }
|
||||
}
|
||||
|
||||
pub fn reader(&self) -> &dyn reader::Reader {
|
||||
self.reader
|
||||
}
|
||||
|
||||
pub fn read_selected(&self) -> Result<Option<String>, reader::Error> {
|
||||
match self.reader.read_string("branches/selected") {
|
||||
Ok(selected) => Ok(Some(selected)),
|
||||
|
@ -535,37 +535,72 @@ pub fn create_virtual_branch(
|
||||
pub fn update_branch(
|
||||
gb_repository: &gb_repository::Repository,
|
||||
branch_update: branch::BranchUpdateRequest,
|
||||
) -> Result<()> {
|
||||
let writer = branch::Writer::new(gb_repository);
|
||||
|
||||
) -> Result<branch::Branch> {
|
||||
let current_session = gb_repository
|
||||
.get_or_create_current_session()
|
||||
.context("failed to get or create currnt session")?;
|
||||
let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session)
|
||||
.context("failed to open current session")?;
|
||||
let branch_reader = branch::Reader::new(¤t_session_reader);
|
||||
let branch_writer = branch::Writer::new(gb_repository);
|
||||
|
||||
let virtual_branches = Iterator::new(¤t_session_reader)
|
||||
let mut branch = branch_reader
|
||||
.read(&branch_update.id)
|
||||
.context("failed to read branch")?;
|
||||
|
||||
if let Some(ownership) = branch_update.ownership {
|
||||
set_ownership(&branch_reader, &branch_writer, &mut branch, &ownership)
|
||||
.context("failed to set ownership")?;
|
||||
}
|
||||
|
||||
if let Some(name) = branch_update.name {
|
||||
branch.name = name;
|
||||
branch_writer
|
||||
.write(&branch)
|
||||
.context("failed to write branch")?;
|
||||
};
|
||||
|
||||
Ok(branch)
|
||||
}
|
||||
|
||||
fn set_ownership(
|
||||
branch_reader: &branch::Reader,
|
||||
branch_writer: &branch::Writer,
|
||||
target_branch: &mut branch::Branch,
|
||||
ownership: &branch::Ownership,
|
||||
) -> Result<()> {
|
||||
if target_branch.ownership.eq(ownership) {
|
||||
// nothing to update
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut virtual_branches = Iterator::new(branch_reader.reader())
|
||||
.context("failed to create branch iterator")?
|
||||
.collect::<Result<Vec<branch::Branch>, reader::Error>>()
|
||||
.context("failed to read virtual branches")?
|
||||
.into_iter()
|
||||
.filter(|branch| branch.applied)
|
||||
.filter(|branch| branch.id != target_branch.id)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut target_branch = virtual_branches
|
||||
.iter()
|
||||
.find(|b| b.id == branch_update.id)
|
||||
.context("failed to find target branch")?
|
||||
.clone();
|
||||
|
||||
match branch_update.name {
|
||||
Some(name) => {
|
||||
target_branch.name = name;
|
||||
writer.write(&target_branch)?;
|
||||
Ok(())
|
||||
for file_ownership in &ownership.files {
|
||||
for branch in &mut virtual_branches {
|
||||
let taken = branch.ownership.take(file_ownership);
|
||||
if !taken.is_empty() {
|
||||
branch_writer.write(branch).context(format!(
|
||||
"failed to write source branch for {}",
|
||||
file_ownership
|
||||
))?;
|
||||
}
|
||||
}
|
||||
None => Ok(()),
|
||||
}
|
||||
|
||||
target_branch.ownership = ownership.clone();
|
||||
branch_writer
|
||||
.write(target_branch)
|
||||
.context("failed to write target branch")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn move_files(
|
||||
|
Loading…
Reference in New Issue
Block a user