aboutsummaryrefslogtreecommitdiffstats
path: root/src/owner_repo.rs
blob: ea5a51077de23706a58cb924ef15b3ece238b281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2020  Teddy Wing
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.


use std::str::FromStr;

use git2::Repository;
use thiserror::Error;
use url;
use url::Url;


/// Errors getting an `OwnerRepo` from a remote in a Git repository.
#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    Git(#[from] git2::Error),

    #[error(transparent)]
    OwnerRepo(#[from] OwnerRepoError),

    #[error("Unable to find remote '{0}'")]
    NoRemote(String),
}

/// Errors parsing an `OwnerRepo`.
#[derive(Debug, Error)]
pub enum OwnerRepoError {
    #[error("Unable to parse URL")]
    Url(#[from] url::ParseError),

    #[error("URL has no path")]
    NoPath,

    #[error("Unable to parse owner or repo")]
    NoOwnerRepo,
}


#[derive(Debug)]
pub struct OwnerRepo {
    pub owner: String,
    pub repo: String,
}

/// Parse an owner-repo pair from a Git remote. Can be either an HTTP URL
/// (`https://github.com/teddywing/github-suggestion.git`) or an SSH-style
/// reference (`git@github.com:teddywing/github-suggestion.git`).
impl FromStr for OwnerRepo {
    type Err = OwnerRepoError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let url = match Url::parse(s) {
            Err(url::ParseError::RelativeUrlWithoutBase) =>
                return OwnerRepo::from_ssh(s),

            r => r,
        }?;
        let path = url.path_segments()
            .ok_or(OwnerRepoError::NoPath)?
            .collect::<Vec<_>>();

        if path.len() < 2 {
            return Err(OwnerRepoError::NoOwnerRepo);
        }

        Ok(OwnerRepo {
            owner: path[0].to_owned(),
            repo: path[1].to_owned(),
        })
    }
}

impl OwnerRepo {
    /// Parse an `OwnerRepo` from the URL for `remote_name` in the current
    /// repository.
    pub fn from_remote(
        remote_name: Option<&str>,
    ) -> Result<OwnerRepo, Error> {
        let repo = Repository::open(".")?;

        let remote_name = match remote_name {
            Some(r) => r,
            None => "origin",
        };

        let remote = repo.find_remote(remote_name)?;
        let url = remote.url()
            .ok_or_else(|| Error::NoRemote(remote_name.to_owned()))?;

        Ok(url.parse()?)
    }

    /// Parse an `OwnerRepo` from an SSH-style reference
    /// (`git@github.com:teddywing/github-suggestion.git`).
    pub fn from_ssh(ssh: &str) -> Result<Self, OwnerRepoError> {
        let address_path: Vec<_> = ssh.splitn(2, ':').collect();
        let path = address_path.get(1)
            .ok_or(OwnerRepoError::NoOwnerRepo)?;

        let path = path
            .strip_suffix(".git")
            .unwrap_or(path);

        let segments: Vec<_> = path.split('/').collect();

        if segments.len() < 2 {
            return Err(OwnerRepoError::NoOwnerRepo);
        }

        Ok(OwnerRepo {
            owner: segments[0].to_owned(),
            repo: segments[1].to_owned(),
        })
    }
}