git_next_core/git/
push.rs

1//
2use crate::{git, git::repository::open::OpenRepositoryLike, BranchName};
3
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub enum Force {
6    No,
7    From(git::GitRef),
8}
9impl std::fmt::Display for Force {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        match self {
12            Self::No => write!(f, "fast-forward"),
13            Self::From(from) => write!(f, "force-if-from:{from}"),
14        }
15    }
16}
17
18pub type Result<T> = core::result::Result<T, Error>;
19
20#[derive(Debug, thiserror::Error)]
21pub enum Error {
22    #[error("io")]
23    Io(#[from] std::io::Error),
24
25    #[error("network: {0}")]
26    Network(#[from] kxio::net::Error),
27
28    #[error("fetch: {0}")]
29    Fetch(#[from] git::fetch::Error),
30
31    #[error("lock")]
32    Lock,
33
34    #[error("gix open: {0}")]
35    Open(#[from] Box<gix::open::Error>),
36
37    #[error("gix iter: {0}")]
38    GixIter(#[from] gix::reference::iter::Error),
39
40    #[error("gix iter init: {0}")]
41    GixIterInit(#[from] gix::reference::iter::init::Error),
42
43    #[cfg(test)]
44    #[error("test")]
45    TestResult(#[from] Box<dyn std::error::Error>),
46}
47
48/// Resets the position of a branch in the remote repo
49///
50/// Performs a 'git fetch' first to ensure we have up-to-date branch positions before
51/// performing `git push`.
52///
53/// # Errors
54///
55/// Will return an `Err` if their is no remote fetch defined in .git/config, or
56/// if there are any network connectivity issues with the remote server.
57pub fn reset(
58    open_repository: &dyn OpenRepositoryLike,
59    repo_details: &git::RepoDetails,
60    branch_name: &BranchName,
61    to_commit: &git::GitRef,
62    force: &git::push::Force,
63) -> Result<()> {
64    open_repository.fetch()?;
65    open_repository.push(repo_details, branch_name, to_commit, force)
66}