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
//! Platform detection for remote Git repositories.
//!
//! Identifies the hosting platform (GitHub, GitLab, or `GitCode`) from a
//! remote URL so that the CLI can select the correct API and workflow.
/// The Git platform hosting a remote repository.
///
/// Used to determine which API client and workflow to use for
/// platform-specific operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Platform {
/// GitHub (github.com or self-hosted GitHub Enterprise).
GitHub,
/// GitLab (gitlab.com or self-hosted GitLab).
GitLab,
/// `GitCode` (gitcode.com or self-hosted `GitCode`).
GitCode,
}
impl Platform {
/// Detects the platform from a remote Git URL.
///
/// Performs a case-insensitive substring match against known platform
/// domain patterns. Supports both HTTPS and SSH remote formats, as
/// well as self-hosted instances.
///
/// # Detection Strategy
///
/// - `github.com` or `github.*` → GitHub
/// - `gitcode.com` or `gitcode.*` → `GitCode`
/// - All other domains → GitLab (default, including self-hosted)
///
/// This approach assumes that any unrecognized domain is a GitLab instance,
/// which covers custom domains like `xyun.git.nyuncloud.com`.
///
/// # Examples
///
/// ```ignore
/// // Doctest skipped: crate name hyphen-to-underscore mapping
/// // prevents `use gitflow_core::Platform` from resolving.
/// use gitflow_core::Platform;
///
/// assert_eq!(
/// Platform::detect_from_remote_url("https://github.com/owner/repo.git"),
/// Some(Platform::GitHub),
/// );
/// assert_eq!(
/// Platform::detect_from_remote_url("git@gitlab.mycorp.com:group/project.git"),
/// Some(Platform::GitLab),
/// );
/// assert_eq!(
/// Platform::detect_from_remote_url("git@xyun.git.nyuncloud.com:fusion-cdn/bff/admin.git"),
/// Some(Platform::GitLab),
/// );
/// ```
#[must_use]
pub fn detect_from_remote_url(url: &str) -> Option<Self> {
let url_lower = url.to_lowercase();
if url_lower.contains("github.com") || url_lower.contains("github.") {
Some(Self::GitHub)
} else if url_lower.contains("gitcode.com") || url_lower.contains("gitcode.") {
Some(Self::GitCode)
} else {
// Default to GitLab for all other domains (including self-hosted GitLab)
Some(Self::GitLab)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_detect_github_from_https_url() {
assert_eq!(
Platform::detect_from_remote_url("https://github.com/owner/repo.git"),
Some(Platform::GitHub),
);
}
#[test]
fn test_should_detect_github_from_ssh_url() {
assert_eq!(
Platform::detect_from_remote_url("git@github.com:owner/repo.git"),
Some(Platform::GitHub),
);
}
#[test]
fn test_should_detect_gitlab_from_https_url() {
assert_eq!(
Platform::detect_from_remote_url("https://gitlab.com/group/project.git"),
Some(Platform::GitLab),
);
}
#[test]
fn test_should_detect_gitlab_from_self_hosted_url() {
assert_eq!(
Platform::detect_from_remote_url("git@gitlab.mycorp.com:group/project.git"),
Some(Platform::GitLab),
);
}
#[test]
fn test_should_detect_gitcode() {
assert_eq!(
Platform::detect_from_remote_url("https://gitcode.com/owner/repo.git"),
Some(Platform::GitCode),
);
}
#[test]
fn test_should_detect_gitlab_from_custom_domain() {
// Custom GitLab domains should be detected as GitLab
assert_eq!(
Platform::detect_from_remote_url("git@xyun.git.nyuncloud.com:fusion-cdn/bff/admin.git"),
Some(Platform::GitLab),
);
assert_eq!(
Platform::detect_from_remote_url("https://gitlab.mycorp.com/group/project.git"),
Some(Platform::GitLab),
);
}
#[test]
fn test_should_default_to_gitlab_for_unrecognized_url() {
// Unrecognized domains default to GitLab
assert_eq!(
Platform::detect_from_remote_url("https://example.com/repo.git"),
Some(Platform::GitLab),
);
}
#[test]
fn test_should_be_case_insensitive() {
assert_eq!(
Platform::detect_from_remote_url("HTTPS://GITHUB.COM/Owner/Repo.git"),
Some(Platform::GitHub),
);
assert_eq!(
Platform::detect_from_remote_url("GIT@GITLAB.COM:Group/Project.git"),
Some(Platform::GitLab),
);
assert_eq!(
Platform::detect_from_remote_url("HTTPS://GITCODE.COM/Owner/Repo.git"),
Some(Platform::GitCode),
);
}
}