Skip to content

Commit 6c0f43d

Browse files
committed
Detect repository name and url
1 parent a0e2d0a commit 6c0f43d

File tree

3 files changed

+214
-3
lines changed

3 files changed

+214
-3
lines changed

Cargo.lock

+157
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ authors = ["o2sh <[email protected]>"]
55

66
[dependencies]
77
colored= "1.6.1"
8-
tokei = "8.0"
8+
git2 = "0.7.5"
9+
tokei = "8.0"

src/main.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
extern crate colored;
2+
extern crate git2;
23
extern crate tokei;
34

45
use colored::*;
6+
use git2::Error;
7+
use git2::Repository;
58
use std::fmt;
69
use std::process::{Command, Stdio};
710

@@ -148,12 +151,16 @@ fn main() {
148151
}
149152

150153
let authors = get_authors(3);
154+
let config: Configuration = match get_configuration() {
155+
Ok(config) => config,
156+
Err(_) => panic!("Could not retrieve git configuration data"),
157+
};
151158

152159
let info = Info {
153-
project_name: String::from("onefetch"),
160+
project_name: config.repository_name,
154161
language: language,
155162
authors: authors,
156-
repo: String::from("https://github.com/02sh/onefetch"),
163+
repo: config.repository_url,
157164
number_of_lines: get_total_loc(&tokei_langs),
158165
license: String::from("MIT"),
159166
};
@@ -176,6 +183,52 @@ fn is_git_installed() -> bool {
176183
.is_ok()
177184
}
178185

186+
#[derive(Debug)]
187+
struct Configuration {
188+
pub repository_name: String,
189+
pub repository_url: String,
190+
}
191+
192+
fn get_configuration() -> Result<Configuration, Error> {
193+
let repo = Repository::open("./")?;
194+
let config = repo.config()?;
195+
let mut remote_url = String::new();
196+
let mut repository_name = String::new();
197+
let mut remote_upstream: Option<String> = None;
198+
199+
for entry in &config.entries(None).unwrap() {
200+
let entry = entry.unwrap();
201+
match entry.name().unwrap() {
202+
"remote.origin.url" => remote_url = entry.value().unwrap().to_string(),
203+
"remote.upstream.url" => remote_upstream = Some(entry.value().unwrap().to_string()),
204+
_ => (),
205+
}
206+
}
207+
208+
match remote_upstream {
209+
Some(url) => remote_url = url.clone(),
210+
None => (),
211+
};
212+
213+
let url = remote_url.clone();
214+
let mut name_parts: Vec<&str> = url.split("/").collect();
215+
216+
for (i, part) in name_parts.clone().iter().enumerate() {
217+
if part.contains(".git") {
218+
let git_parts: Vec<&str> = part.split(".").collect();
219+
repository_name = git_parts[0].to_string();
220+
name_parts[i] = git_parts[0];
221+
222+
break;
223+
}
224+
}
225+
226+
Ok(Configuration {
227+
repository_name: repository_name,
228+
repository_url: name_parts.join("/"),
229+
})
230+
}
231+
179232
// Return first n most active commiters as authors within this project.
180233
fn get_authors(n: usize) -> Vec<String> {
181234
use std::collections::HashMap;

0 commit comments

Comments
 (0)