Skip to content

Commit b8b7201

Browse files
committed
Refactoring update_base_branch
1 parent b856862 commit b8b7201

File tree

17 files changed

+187
-369
lines changed

17 files changed

+187
-369
lines changed

crates/gitbutler-core/src/git/repository_ext.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use tracing::instrument;
66
use crate::{
77
config::git::{GbConfig, GitConfig},
88
error::Code,
9+
project_repository::LogUntil,
910
};
1011

1112
use super::{CommitBuffer, Refname};
@@ -19,6 +20,9 @@ use std::os::windows::process::CommandExt;
1920
///
2021
/// For now, it collects useful methods from `gitbutler-core::git::Repository`
2122
pub trait RepositoryExt {
23+
fn distance(&self, from: git2::Oid, to: git2::Oid) -> Result<u32>;
24+
fn list_commits(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git2::Commit>>;
25+
fn list_commit_oids(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git2::Oid>>;
2226
/// Open a new in-memory repository and executes the provided closure using it.
2327
/// This is useful when temporary objects are created for the purpose of comparing or getting a diff.
2428
/// Note that it's the odb that is in-memory, not the working directory.
@@ -346,6 +350,77 @@ impl RepositoryExt for Repository {
346350
}
347351
Err(anyhow::anyhow!("No signing key found"))
348352
}
353+
354+
// returns a list of commit oids from the first oid to the second oid
355+
fn list_commit_oids(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git2::Oid>> {
356+
match to {
357+
LogUntil::Commit(oid) => {
358+
let mut revwalk = self.revwalk().context("failed to create revwalk")?;
359+
revwalk
360+
.push(from)
361+
.context(format!("failed to push {}", from))?;
362+
revwalk
363+
.hide(oid)
364+
.context(format!("failed to hide {}", oid))?;
365+
revwalk
366+
.map(|oid| oid.map(Into::into))
367+
.collect::<Result<Vec<_>, _>>()
368+
}
369+
LogUntil::Take(n) => {
370+
let mut revwalk = self.revwalk().context("failed to create revwalk")?;
371+
revwalk
372+
.push(from)
373+
.context(format!("failed to push {}", from))?;
374+
revwalk
375+
.take(n)
376+
.map(|oid| oid.map(Into::into))
377+
.collect::<Result<Vec<_>, _>>()
378+
}
379+
LogUntil::When(cond) => {
380+
let mut revwalk = self.revwalk().context("failed to create revwalk")?;
381+
revwalk
382+
.push(from)
383+
.context(format!("failed to push {}", from))?;
384+
let mut oids: Vec<git2::Oid> = vec![];
385+
for oid in revwalk {
386+
let oid = oid.context("failed to get oid")?;
387+
oids.push(oid);
388+
389+
let commit = self.find_commit(oid).context("failed to find commit")?;
390+
391+
if cond(&commit).context("failed to check condition")? {
392+
break;
393+
}
394+
}
395+
Ok(oids)
396+
}
397+
LogUntil::End => {
398+
let mut revwalk = self.revwalk().context("failed to create revwalk")?;
399+
revwalk
400+
.push(from)
401+
.context(format!("failed to push {}", from))?;
402+
revwalk
403+
.map(|oid| oid.map(Into::into))
404+
.collect::<Result<Vec<_>, _>>()
405+
}
406+
}
407+
.context("failed to collect oids")
408+
}
409+
410+
// returns a list of commits from the first oid to the second oid
411+
fn list_commits(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git2::Commit>> {
412+
self.list_commit_oids(from, to)?
413+
.into_iter()
414+
.map(|oid| self.find_commit(oid))
415+
.collect::<Result<Vec<_>, _>>()
416+
.context("failed to collect commits")
417+
}
418+
419+
// returns the number of commits between the first oid to the second oid
420+
fn distance(&self, from: git2::Oid, to: git2::Oid) -> Result<u32> {
421+
let oids = self.list_commit_oids(from, LogUntil::Commit(to))?;
422+
Ok(oids.len().try_into()?)
423+
}
349424
}
350425

351426
/// Signs the buffer with the configured gpg key, returning the signature.

crates/gitbutler-core/src/project_repository/repository.rs

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -234,105 +234,6 @@ impl Repository {
234234
.context("failed to lookup reference")
235235
}
236236

237-
// returns a list of commit oids from the first oid to the second oid
238-
pub fn l(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git2::Oid>> {
239-
match to {
240-
LogUntil::Commit(oid) => {
241-
let mut revwalk = self
242-
.git_repository
243-
.revwalk()
244-
.context("failed to create revwalk")?;
245-
revwalk
246-
.push(from)
247-
.context(format!("failed to push {}", from))?;
248-
revwalk
249-
.hide(oid)
250-
.context(format!("failed to hide {}", oid))?;
251-
revwalk
252-
.map(|oid| oid.map(Into::into))
253-
.collect::<Result<Vec<_>, _>>()
254-
}
255-
LogUntil::Take(n) => {
256-
let mut revwalk = self
257-
.git_repository
258-
.revwalk()
259-
.context("failed to create revwalk")?;
260-
revwalk
261-
.push(from)
262-
.context(format!("failed to push {}", from))?;
263-
revwalk
264-
.take(n)
265-
.map(|oid| oid.map(Into::into))
266-
.collect::<Result<Vec<_>, _>>()
267-
}
268-
LogUntil::When(cond) => {
269-
let mut revwalk = self
270-
.git_repository
271-
.revwalk()
272-
.context("failed to create revwalk")?;
273-
revwalk
274-
.push(from)
275-
.context(format!("failed to push {}", from))?;
276-
let mut oids: Vec<git2::Oid> = vec![];
277-
for oid in revwalk {
278-
let oid = oid.context("failed to get oid")?;
279-
oids.push(oid);
280-
281-
let commit = self
282-
.git_repository
283-
.find_commit(oid)
284-
.context("failed to find commit")?;
285-
286-
if cond(&commit).context("failed to check condition")? {
287-
break;
288-
}
289-
}
290-
Ok(oids)
291-
}
292-
LogUntil::End => {
293-
let mut revwalk = self
294-
.git_repository
295-
.revwalk()
296-
.context("failed to create revwalk")?;
297-
revwalk
298-
.push(from)
299-
.context(format!("failed to push {}", from))?;
300-
revwalk
301-
.map(|oid| oid.map(Into::into))
302-
.collect::<Result<Vec<_>, _>>()
303-
}
304-
}
305-
.context("failed to collect oids")
306-
}
307-
308-
// returns a list of oids from the first oid to the second oid
309-
pub fn list(&self, from: git2::Oid, to: git2::Oid) -> Result<Vec<git2::Oid>> {
310-
self.l(from, LogUntil::Commit(to))
311-
}
312-
313-
pub fn list_commits(&self, from: git2::Oid, to: git2::Oid) -> Result<Vec<git2::Commit>> {
314-
Ok(self
315-
.list(from, to)?
316-
.into_iter()
317-
.map(|oid| self.git_repository.find_commit(oid))
318-
.collect::<Result<Vec<_>, _>>()?)
319-
}
320-
321-
// returns a list of commits from the first oid to the second oid
322-
pub fn log(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git2::Commit>> {
323-
self.l(from, to)?
324-
.into_iter()
325-
.map(|oid| self.git_repository.find_commit(oid))
326-
.collect::<Result<Vec<_>, _>>()
327-
.context("failed to collect commits")
328-
}
329-
330-
// returns the number of commits between the first oid to the second oid
331-
pub fn distance(&self, from: git2::Oid, to: git2::Oid) -> Result<u32> {
332-
let oids = self.l(from, LogUntil::Commit(to))?;
333-
Ok(oids.len().try_into()?)
334-
}
335-
336237
pub fn commit(
337238
&self,
338239
user: Option<&users::User>,

crates/gitbutler-core/src/rebase/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn cherry_rebase(
1414
end_commit_oid: git2::Oid,
1515
) -> Result<Option<git2::Oid>> {
1616
// get a list of the commits to rebase
17-
let mut ids_to_rebase = project_repository.l(
17+
let mut ids_to_rebase = project_repository.repo().list_commit_oids(
1818
end_commit_oid,
1919
project_repository::LogUntil::Commit(start_commit_oid),
2020
)?;

0 commit comments

Comments
 (0)