1
1
extern crate colored;
2
+ extern crate git2;
2
3
extern crate tokei;
3
4
4
5
use colored:: * ;
6
+ use git2:: Error ;
7
+ use git2:: Repository ;
5
8
use std:: fmt;
6
9
use std:: process:: { Command , Stdio } ;
7
10
@@ -148,12 +151,16 @@ fn main() {
148
151
}
149
152
150
153
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
+ } ;
151
158
152
159
let info = Info {
153
- project_name : String :: from ( "onefetch" ) ,
160
+ project_name : config . repository_name ,
154
161
language : language,
155
162
authors : authors,
156
- repo : String :: from ( "https://github.com/02sh/onefetch" ) ,
163
+ repo : config . repository_url ,
157
164
number_of_lines : get_total_loc ( & tokei_langs) ,
158
165
license : String :: from ( "MIT" ) ,
159
166
} ;
@@ -176,6 +183,52 @@ fn is_git_installed() -> bool {
176
183
. is_ok ( )
177
184
}
178
185
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
+
179
232
// Return first n most active commiters as authors within this project.
180
233
fn get_authors ( n : usize ) -> Vec < String > {
181
234
use std:: collections:: HashMap ;
0 commit comments