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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! hosted-git-info is a [Rust] port of the original [hosted-git-info] project on [npm].
//!
//! [Rust]: https://www.rustlang.org/
//! [hosted-git-info]: https://github.com/npm/hosted-git-info
//! [npm]: https://www.npmjs.com
//!
//! It provides metadata and conversions from repository urls for [GitHub], [Bitbucket]
//! and [GitLab].
//!
//! [GitHub]: https://github.com/
//! [Bitbucket]: https://www.bitbucket.org/
//! [GitLab]: https://www.gitlab.com/
//!
//! It will let you identify and transform various git hosts URLs between
//! protocols. It also can tell you what the URL is for the raw path for
//! particular file for direct access without git.
//!
//! # Usage
//!
//! First, URL parsing may fail for various reasons and therefore returns a `Result`:
//!
//! ```
//! use hosted_git_info::{HostedGitInfo, ParseError};
//!
//! assert!(HostedGitInfo::from_url("https://www.rustlang.org/") == Err(ParseError::UnknownUrl));
//! ```
//!
//! Let’s parse a valid URL and look at its components.
//!
//! ```
//! use hosted_git_info::{HostedGitInfo, Provider};
//!
//! let url = "https://github.com/foo/bar.git#branch";
//! let info = HostedGitInfo::from_url(url).unwrap();
//! assert_eq!(info.provider(), Provider::GitHub);
//! assert_eq!(info.user(), Some("foo"));
//! assert_eq!(info.project(), "bar");
//! assert_eq!(info.committish(), Some("branch"));
//! assert_eq!(info.auth(), None);
//! ```

#![deny(clippy::unwrap_used)]

#[cfg(feature = "derive_builder")]
use derive_builder::Builder;
use percent_encoding::percent_decode_str;
use std::str;
use thiserror::Error;
use url::Url;

mod parser;

static AUTH_SCHEMES: [&str; 5] = ["git", "https", "git+https", "http", "git+http"];
static KNOWN_SCHEMES: [&str; 10] = [
    "http",
    "https",
    "git",
    "git+ssh",
    "git+https",
    "ssh",
    "bitbucket",
    "gist",
    "github",
    "gitlab",
];

/// Enum of supported git hosting providers.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Provider {
    /// see <https://www.bitbucket.org/>
    BitBucket,
    /// see <https://gist.github.com/>
    Gist,
    /// see <https://github.com/>
    GitHub,
    /// see <https://www.gitlab.com/>
    GitLab,
}

/// Enum of the original URL types (shortcut, https, ssh, ...)
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum DefaultRepresentation {
    /// Example: `Turbo87/hosted-git-info-rs`
    Shortcut,
    /// Example: `git://github.com/Turbo87/hosted-git-info-rs`
    Git,
    /// Example: `https://github.com/Turbo87/hosted-git-info-rs.git`
    Https,
    /// Example: `git+ssh://git@github.com:Turbo87/hosted-git-info-rs.git`
    Ssh,
    /// anything else 🤷‍
    Other,
}

impl DefaultRepresentation {
    fn from_scheme(scheme: &str) -> DefaultRepresentation {
        use DefaultRepresentation::*;

        match scheme {
            "git" => Git,
            "git+https" => Https,
            "git+ssh" => Ssh,
            "https" => Https,
            "ssh" => Ssh,
            _ => Other,
        }
    }
}

/// Errors that can occur during parsing.
#[derive(PartialEq, Eq, Clone, Copy, Debug, Error)]
pub enum ParseError {
    /// Failed to parse the URL with the `url` crate.
    #[error("Failed to parse URL")]
    InvalidUrl(#[from] url::ParseError),

    /// Failed to parse a part of the URL with the `percent_encoding` crate.
    #[error("Failed to parse percent-encoded URI component")]
    InvalidUriEncoding(#[from] str::Utf8Error),

    /// The URL could not be recognized.
    #[error("Failed to recognize URL")]
    UnknownUrl,
}

/// The parsed information from a git hosting URL.
#[derive(Debug, Eq, PartialEq, Clone)]
#[cfg_attr(feature = "derive_builder", derive(Builder))]
pub struct HostedGitInfo {
    provider: Provider,

    #[cfg_attr(
        feature = "derive_builder",
        builder(setter(into, strip_option), default)
    )]
    user: Option<String>,

    #[cfg_attr(
        feature = "derive_builder",
        builder(setter(into, strip_option), default)
    )]
    auth: Option<String>,

    #[cfg_attr(feature = "derive_builder", builder(setter(into)))]
    project: String,

    #[cfg_attr(
        feature = "derive_builder",
        builder(setter(into, strip_option), default)
    )]
    committish: Option<String>,

    #[cfg_attr(feature = "derive_builder", builder(setter(name = "repr")))]
    default_representation: DefaultRepresentation,
}

impl HostedGitInfo {
    /// Parses a URL string and returns a [HostedGitInfo] struct, if successful.
    /// If parsing fails, a [ParseError] will be returned.
    pub fn from_url(giturl: &str) -> Result<Self, ParseError> {
        // if (!giturl) {
        //   return
        // }

        // const url = isGitHubShorthand(giturl) ? 'github:' + giturl : correctProtocol(giturl)
        let url = if is_github_shorthand(giturl) {
            format!("github:{}", giturl)
        } else {
            // correctProtocol(giturl)
            correct_protocol(giturl)
        };

        // const parsed = parseGitUrl(url)
        // if (!parsed) {
        //   return parsed
        // }
        let parsed = parse_git_url(&url)?;

        // const gitHostShortcut = gitHosts.byShortcut[parsed.protocol]
        let parser_from_shortcut = parser::parser_from_shortcut(parsed.scheme());

        // const gitHostDomain = gitHosts.byDomain[parsed.hostname.startsWith('www.') ? parsed.hostname.slice(4) : parsed.hostname]
        let simplified_domain = parsed
            .domain()
            .map(|domain| domain.strip_prefix("www.").unwrap_or(domain));
        let parser_from_domain =
            simplified_domain.and_then(|domain| parser::parser_from_domain(domain));

        // const gitHostName = gitHostShortcut || gitHostDomain
        let parser = parser_from_shortcut
            .as_ref()
            .or_else(|| parser_from_domain.as_ref());

        // if (!gitHostName) {
        //   return
        // }
        //
        // const gitHostInfo = gitHosts[gitHostShortcut || gitHostDomain]
        let parser = match parser {
            Some(parser) => parser,
            None => return Err(ParseError::UnknownUrl),
        };

        // let auth = null
        // if (authProtocols[parsed.protocol] && (parsed.username || parsed.password)) {
        //   auth = `${parsed.username}${parsed.password ? ':' + parsed.password : ''}`
        // }
        let username = match parsed.username() {
            username if !username.is_empty() => Some(username),
            _ => None,
        };
        let password = parsed.password();
        let auth = if AUTH_SCHEMES.contains(&parsed.scheme()) {
            match (username, password) {
                (Some(username), Some(password)) => Some(format!("{}:{}", username, password)),
                (Some(username), None) => Some(username.to_string()),
                (None, Some(password)) => Some(format!(":{}", password)),
                (None, None) => None,
            }
        } else {
            None
        };

        // let committish = null
        // let user = null
        // let project = null
        // let defaultRepresentation = null
        //
        // try {
        //   if (gitHostShortcut) {
        if parser_from_shortcut.is_some() {
            // let pathname = parsed.pathname.startsWith('/') ? parsed.pathname.slice(1) : parsed.pathname
            let path = parsed.path();
            let mut pathname = path.strip_prefix('/').unwrap_or(path);

            // const firstAt = pathname.indexOf('@')
            let first_at = pathname.find('@');
            // we ignore auth for shortcuts, so just trim it out
            // if (firstAt > -1) {
            //   pathname = pathname.slice(firstAt + 1)
            // }
            if let Some(first_at) = first_at {
                pathname = &pathname[first_at + 1..];
            }

            // const lastSlash = pathname.lastIndexOf('/')
            let last_slash = pathname.rfind('/');
            let (user, project) = if let Some(last_slash) = last_slash {
                // user = decodeURIComponent(pathname.slice(0, lastSlash))
                let user = percent_decode_str(&pathname[0..last_slash]).decode_utf8()?;

                // we want nulls only, never empty strings
                // if (!user) {
                //   user = null
                // }
                let user = if user.is_empty() { None } else { Some(user) };

                // project = decodeURIComponent(pathname.slice(lastSlash + 1))
                let project = percent_decode_str(&pathname[last_slash + 1..]).decode_utf8()?;
                (user, project)
            } else {
                // project = decodeURIComponent(pathname)
                let project = percent_decode_str(&pathname).decode_utf8()?;
                (None, project)
            };

            let project = project
                .strip_suffix(".git")
                .unwrap_or_else(|| project.as_ref());

            // if (parsed.hash) {
            //   committish = decodeURIComponent(parsed.hash.slice(1))
            // }
            let committish = parsed
                .fragment()
                .map(|committish| percent_decode_str(&committish).decode_utf8())
                .transpose()?;

            // defaultRepresentation = 'shortcut'
            Ok(Self {
                provider: parser.provider(),
                user: user.map(|s| s.to_string()),
                auth,
                project: project.to_string(),
                committish: committish.map(|s| s.to_string()),
                default_representation: DefaultRepresentation::Shortcut,
            })
        } else {
            // if (!gitHostInfo.protocols.includes(parsed.protocol)) {
            //   return
            // }
            if !parser.supports_scheme(parsed.scheme()) {
                return Err(ParseError::UnknownUrl);
            }

            // const segments = gitHostInfo.extract(parsed)
            // if (!segments) {
            //   return
            // }
            let segments = parser.extract(&parsed)?;

            // user = segments.user && decodeURIComponent(segments.user)
            let user = segments
                .user
                .map(|user| percent_decode_str(&user).decode_utf8())
                .transpose()?;

            // project = decodeURIComponent(segments.project)
            let project = segments
                .project
                .map(|project| percent_decode_str(&project).decode_utf8())
                .transpose()?
                .ok_or(ParseError::UnknownUrl)?;

            // committish = decodeURIComponent(segments.committish)
            let committish = segments
                .committish
                .map(|committish| percent_decode_str(&committish).decode_utf8())
                .transpose()?;

            // defaultRepresentation = protocolToRepresentation(parsed.protocol)
            Ok(Self {
                provider: parser.provider(),
                user: user.map(|s| s.to_string()),
                auth,
                project: project.to_string(),
                committish: committish.map(|s| s.to_string()),
                default_representation: DefaultRepresentation::from_scheme(parsed.scheme()),
            })
        }
        //   }
        // } catch (err) {
        //   /* istanbul ignore else */
        //   if (err instanceof URIError) {
        //     return
        //   } else {
        //     throw err
        //   }
        // }
        //
        // return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts)
    }

    /// The type of hosting provider. (GitHub, Gitlab, Bitbucket, ...)
    pub fn provider(&self) -> Provider {
        self.provider
    }

    /// The name of the user or organization on the git host.
    ///
    /// This is using an [Option] because some hosting providers allow projects
    /// that are not scoped to a particular user or organization.
    ///
    /// Example: `https://github.com/Turbo87/hosted-git-info-rs.git` → `Turbo87`
    pub fn user(&self) -> Option<&str> {
        self.user.as_deref()
    }

    /// The authentication part of the URL, if it exists.
    ///
    /// Format: `<USER>[:<PASSWORD>]`
    ///
    /// Example: `https://user:password@github.com/foo/bar.git` → `user:password`
    pub fn auth(&self) -> Option<&str> {
        self.auth.as_deref()
    }

    /// The name of the project on the git host.
    ///
    /// Example: `https://github.com/Turbo87/hosted-git-info-rs.git` → `hosted-git-info-rs`
    pub fn project(&self) -> &str {
        &self.project
    }

    /// The [branch, tag, commit, ...](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefcommit-ishacommit-ishalsocommittish)
    /// part of the URL, if it exists.
    ///
    /// Example: `https://github.com/Turbo87/hosted-git-info-rs.git#rust-is-awesome` → `rust-is-awesome`
    pub fn committish(&self) -> Option<&str> {
        self.committish.as_deref()
    }

    /// The original URL type (shortcut, https, ssh, ...).
    ///
    /// Example: `https://github.com/Turbo87/hosted-git-info-rs.git` → `Https`
    pub fn default_representation(&self) -> DefaultRepresentation {
        self.default_representation
    }
}

// look for github shorthand inputs, such as npm/cli
fn is_github_shorthand(arg: &str) -> bool {
    // it cannot contain whitespace before the first #
    // it cannot start with a / because that's probably an absolute file path
    // but it must include a slash since repos are username/repository
    // it cannot start with a . because that's probably a relative file path
    // it cannot start with an @ because that's a scoped package if it passes the other tests
    // it cannot contain a : before a # because that tells us that there's a protocol
    // a second / may not exist before a #

    // const firstHash = arg.indexOf('#')
    let first_hash = arg.find('#');
    // const firstSlash = arg.indexOf('/')
    let first_slash = arg.find('/');
    // const secondSlash = arg.indexOf('/', firstSlash + 1)
    let second_slash = first_slash.and_then(|first_slash| arg[first_slash + 1..].find('/'));
    // const firstColon = arg.indexOf(':')
    let first_colon = arg.find(':');
    // const firstSpace = /\s/.exec(arg)
    let first_space = arg.find(char::is_whitespace);
    // const firstAt = arg.indexOf('@')
    let first_at = arg.find('@');

    // const spaceOnlyAfterHash = !firstSpace || (firstHash > -1 && firstSpace.index > firstHash)
    let space_only_after_hash =
        first_space.is_none() || (first_hash.is_some() && first_space > first_hash);
    // const atOnlyAfterHash = firstAt === -1 || (firstHash > -1 && firstAt > firstHash)
    let at_only_after_hash = first_at.is_none() || (first_hash.is_some() && first_at > first_hash);
    // const colonOnlyAfterHash = firstColon === -1 || (firstHash > -1 && firstColon > firstHash)
    let colon_only_after_hash =
        first_colon.is_none() || (first_hash.is_some() && first_colon > first_hash);
    // const secondSlashOnlyAfterHash = secondSlash === -1 || (firstHash > -1 && secondSlash > firstHash)
    let second_slash_only_after_hash =
        second_slash.is_none() || (first_hash.is_some() && second_slash > first_hash);
    // const hasSlash = firstSlash > 0
    let has_slash = matches!(first_slash, Some(first_slash) if first_slash > 0);

    // if a # is found, what we really want to know is that the character immediately before # is not a /

    // const doesNotEndWithSlash = firstHash > -1 ? arg[firstHash - 1] !== '/' : !arg.endsWith('/')
    let does_not_end_with_slash = if let Some(first_hash) = first_hash {
        arg.as_bytes().get(first_hash - 1) != Some(&b'/')
    } else {
        !arg.ends_with('/')
    };
    // const doesNotStartWithDot = !arg.startsWith('.')
    let does_not_start_with_dot = !arg.starts_with('.');

    // return spaceOnlyAfterHash && hasSlash && doesNotEndWithSlash && doesNotStartWithDot && atOnlyAfterHash && colonOnlyAfterHash && secondSlashOnlyAfterHash
    space_only_after_hash
        && has_slash
        && does_not_end_with_slash
        && does_not_start_with_dot
        && at_only_after_hash
        && colon_only_after_hash
        && second_slash_only_after_hash
}

// accepts input like git:github.com:user/repo and inserts the // after the first :
fn correct_protocol(arg: &str) -> String {
    // const firstColon = arg.indexOf(':')
    if let Some(first_colon) = arg.find(':') {
        // const proto = arg.slice(0, firstColon + 1)
        let proto = &arg[0..first_colon];

        // if (knownProtocols.includes(proto)) {
        //   return arg
        // }
        if KNOWN_SCHEMES.contains(&proto) {
            return arg.to_string();
        }

        // const firstAt = arg.indexOf('@')
        // if (firstAt > -1) {
        if let Some(first_at) = arg.find('@') {
            // if (firstAt > firstColon) {
            return if first_at > first_colon {
                // return `git+ssh://${arg}`
                format!("git+ssh://{}", arg)
            } else {
                arg.to_string()
            };
        }

        // const doubleSlash = arg.indexOf('//')
        let double_slash = arg.find("//");
        // if (doubleSlash === firstColon + 1) {
        if double_slash == Some(first_colon + 1) {
            return arg.to_string();
        }

        // return arg.slice(0, firstColon + 1) + '//' + arg.slice(firstColon + 1)
        format!("{}//{}", &arg[0..first_colon + 1], &arg[first_colon + 1..])
    } else {
        arg.to_string()
    }
}

// try to parse the url as its given to us, if that throws
// then we try to clean the url and parse that result instead
fn parse_git_url(giturl: &str) -> Result<Url, url::ParseError> {
    // let result
    // try {
    //   result = new url.URL(giturl)
    // } catch (err) {}
    //
    // if (result) {
    //   return result
    // }
    Url::parse(giturl).or_else(|_error| {
        // const correctedUrl = correctUrl(giturl)
        let corrected_url = correct_url(giturl).ok_or(_error)?;

        // try {
        //   result = new url.URL(correctedUrl)
        // } catch (err) {}
        //
        // return result
        Url::parse(&corrected_url)
    })
}

// attempt to correct an scp style url so that it will parse with `new URL()`
fn correct_url(giturl: &str) -> Option<String> {
    // const firstAt = giturl.indexOf('@')
    let first_at = giturl.find('@');
    // const lastHash = giturl.lastIndexOf('#')
    let last_hash = giturl.rfind('#');
    // let firstColon = giturl.indexOf(':')
    let mut first_colon = giturl.find(':');
    // let lastColon = giturl.lastIndexOf(':', lastHash > -1 ? lastHash : Infinity)
    let last_colon = last_hash
        .map(|last_hash| &giturl[..last_hash])
        .unwrap_or(giturl)
        .rfind(':');

    // let corrected
    let mut corrected = None;

    // if (lastColon > firstAt) {
    if let Some(last_colon_) = last_colon {
        if last_colon > first_at {
            // the last : comes after the first @ (or there is no @)
            // like it would in:
            // proto://hostname.com:user/repo
            // username@hostname.com:user/repo
            // :password@hostname.com:user/repo
            // username:password@hostname.com:user/repo
            // proto://username@hostname.com:user/repo
            // proto://:password@hostname.com:user/repo
            // proto://username:password@hostname.com:user/repo
            // then we replace the last : with a / to create a valid path

            //   corrected = giturl.slice(0, lastColon) + '/' + giturl.slice(lastColon + 1)
            let corrected_ = format!("{}/{}", &giturl[0..last_colon_], &giturl[last_colon_ + 1..]);

            // and we find our new : positions

            // firstColon = corrected.indexOf(':')
            first_colon = corrected_.find(':');
            // lastColon = corrected.lastIndexOf(':')
            // last_colon = corrected_.rfind(':'); // this appears to be a bug in the original?

            corrected = Some(corrected_);
        }
    }

    // if (firstColon === -1 && giturl.indexOf('//') === -1) {
    if first_colon.is_none() && !giturl.contains("//") {
        // we have no : at all
        // as it would be in:
        // username@hostname.com/user/repo
        // then we prepend a protocol

        // corrected = `git+ssh://${corrected}`
        corrected = corrected.map(|corrected| format!("git+ssh://{}", corrected));
    }

    // return corrected
    corrected
}