gps/ps/public/
backup_stack.rs

1use super::super::private::git;
2use std::result::Result;
3
4#[derive(Debug)]
5pub enum BackupStackError {
6    OpenRepositoryFailed(git::CreateCwdRepositoryError),
7    CurrentBranchNameMissing,
8    GetUpstreamBranchNameFailed,
9    GetRemoteNameFailed,
10    ConvertStringToStrFailed,
11    PushFailed(git::ExtForcePushError),
12}
13
14pub fn backup_stack(branch_name: String) -> Result<(), BackupStackError> {
15    let repo = git::create_cwd_repo().map_err(BackupStackError::OpenRepositoryFailed)?;
16
17    let cur_branch_name =
18        git::get_current_branch(&repo).ok_or(BackupStackError::CurrentBranchNameMissing)?;
19    let branch_upstream_name = git::branch_upstream_name(&repo, cur_branch_name.as_str())
20        .map_err(|_| BackupStackError::GetUpstreamBranchNameFailed)?;
21    let remote_name = repo
22        .branch_remote_name(&branch_upstream_name)
23        .map_err(|_| BackupStackError::GetRemoteNameFailed)?;
24    let remote_name_str = remote_name
25        .as_str()
26        .ok_or(BackupStackError::ConvertStringToStrFailed)?;
27
28    // e.g. git push <remote> <stack-branch>:<branch-name>
29    git::ext_push(true, remote_name_str, &cur_branch_name, &branch_name)
30        .map_err(BackupStackError::PushFailed)?;
31
32    Ok(())
33}