Skip to content

Commit

Permalink
feat: Support multiple Git protocols
Browse files Browse the repository at this point in the history
feat: Support multiple Git protocols
  • Loading branch information
vyfor authored Apr 28, 2024
2 parents 906007b + 2af4b85 commit 190e836
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ mod ipc;
mod json;
mod mappings;
mod rpc;
mod types;
mod utils;
mod util;

use rpc::activity::ActivityButton;
use std::{
ffi::{c_char, CString},
ptr::null,
time::UNIX_EPOCH,
};
use types::AssetType;
use utils::{
use util::types::AssetType;
use util::utils::{
build_activity, build_presence, find_workspace, get_asset, ptr_to_string,
validate_buttons,
};
Expand Down
2 changes: 2 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod types;
pub mod utils;
File renamed without changes.
33 changes: 22 additions & 11 deletions src/utils.rs → src/util/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn validate_buttons(
let mut buttons = Vec::with_capacity(2);

if first_url == "git" || second_url == "git" {
if let Some(repository) = find_repository(workspace) {
if let Some(repository) = find_git_repository(workspace) {
if first_url == "git" {
first_url = repository.clone();
}
Expand Down Expand Up @@ -282,7 +282,7 @@ fn lsp_manager_presence(
}

#[inline(always)]
fn find_repository(workspace_path: &str) -> Option<String> {
fn find_git_repository(workspace_path: &str) -> Option<String> {
let config_path = format!("{}/{}", workspace_path, ".git/config");

let file = match File::open(config_path) {
Expand All @@ -291,22 +291,33 @@ fn find_repository(workspace_path: &str) -> Option<String> {
};
let reader = BufReader::new(file);

let mut repo_url = String::new();

for line in reader.lines() {
let line = match line {
Ok(line) => line,
Err(_) => continue,
};
if line.trim_start().starts_with("url = ") {
repo_url = line[7..].trim().to_string();

if let Some(repo_url) = line.trim().strip_prefix("url = ") {
if repo_url.starts_with("http") {
return Some(
repo_url
.strip_suffix(".git")
.map(|url| url.to_string())
.unwrap_or_else(|| repo_url.to_string()),
);
} else if let Some((_protocol, repo_url)) = repo_url.split_once('@')
{
return Some(format!(
"https://{}",
repo_url
.replacen(':', "/", 1)
.strip_suffix(".git")
.unwrap_or(repo_url)
));
}
break;
}
}

if repo_url.is_empty() {
None
} else {
Some(repo_url)
}
None
}

0 comments on commit 190e836

Please sign in to comment.