@@ -3,12 +3,14 @@ extern crate open;
33extern crate regex;
44extern crate structopt;
55
6- use git2:: { ErrorCode , Repository } ;
76use regex:: Regex ;
87use std:: env;
98use std:: process:: Command ;
109use structopt:: StructOpt ;
1110
11+ mod git;
12+ mod logger;
13+
1214#[ derive( Debug , StructOpt ) ]
1315// Rename all will use the name of the field
1416#[ structopt( rename_all = "kebab-case" ) ]
@@ -30,82 +32,39 @@ pub struct Opt {
3032 verbose : bool ,
3133}
3234
33- # [ cfg ( target_os = "linux" ) ]
35+ /// Function to open the browser using the system shell.
3436fn open_browser ( browser : & String , url : & String ) {
3537 Command :: new ( browser)
3638 . arg ( url)
3739 . output ( )
3840 . expect ( "failed to execute process" ) ;
3941}
4042
41- #[ cfg( target_os = "windows" ) ]
42- fn open_browser ( browser : & String , url : & String ) {
43- Command :: new ( browser)
44- . arg ( url)
45- . output ( )
46- . expect ( "failed to execute process" ) ;
47- }
48-
49- fn get_repo ( ) -> Repository {
50- return match Repository :: open ( "." ) {
51- Ok ( repo) => repo,
52- Err ( e) => panic ! ( "failed to open: {}" , e) ,
53- } ;
54- }
55-
56- fn get_branch ( repo : & Repository , verbose : & bool ) -> String {
57- let head = match repo. head ( ) {
58- Ok ( head) => Some ( head) ,
59- Err ( ref e) if e. code ( ) == ErrorCode :: UnbornBranch || e. code ( ) == ErrorCode :: NotFound => {
60- None
61- }
62- Err ( e) => panic ! ( "failed to get head ref {}" , e) ,
63- } ;
64-
65- let head = head. as_ref ( ) . and_then ( |h| h. shorthand ( ) ) ;
66- print_verbose (
67- format ! (
68- "# On branch {}" ,
69- head. unwrap_or( "Not currently on any branch" )
70- )
71- . as_str ( ) ,
72- verbose,
73- ) ;
74-
75- String :: from ( head. unwrap_or ( "master" ) )
76- }
77-
78- fn print_verbose ( string : & str , verbose : & bool ) {
79- if * verbose {
80- println ! ( "{}" , string)
81- }
82- }
43+ const BROWSER : & str = "BROWSER" ;
8344
8445fn main ( ) {
8546 // Get the command line options
8647 let opt = Opt :: from_args ( ) ;
48+ let logger = logger:: Logger :: new ( opt. verbose ) ;
8749
88- print_verbose ( "Verbose is active" , & opt . verbose ) ;
50+ logger . print ( "Verbose is active" ) ;
8951
9052 // Check that the user is in a git repository.
91- let repo = get_repo ( ) ;
53+ let repo = git :: get_repo ( ) ;
9254
9355 // Get the branch to show in the browser.
9456 let branch = match opt. branch {
9557 Some ( branch) => branch,
9658 None => {
97- print_verbose ( "No branch given, getting current one" , & opt . verbose ) ;
59+ logger . print ( "No branch given, getting current one" ) ;
9860
99- get_branch ( & repo, & opt . verbose )
61+ git :: get_branch ( & repo, & logger )
10062 }
10163 } ;
10264
103- let remote_name = & opt. remote . unwrap_or ( "origin" . to_string ( ) ) ;
65+ let remote_name = & opt. remote . unwrap_or ( "origin" . to_owned ( ) ) ;
10466
105- print_verbose (
106- format ! ( "Getting remote for {}" , remote_name) . as_str ( ) ,
107- & opt. verbose ,
108- ) ;
67+ logger. print ( format ! ( "Getting remote for {}" , remote_name) . as_str ( ) ) ;
10968
11069 let optional_remote = match repo. find_remote ( remote_name) {
11170 Ok ( remote) => remote,
@@ -136,23 +95,17 @@ fn main() {
13695 if opt. browser . is_some ( ) {
13796 let option_browser = opt. browser . unwrap ( ) ;
13897
139- print_verbose (
140- format ! ( "Browser {} given as option" , option_browser) . as_str ( ) ,
141- & opt. verbose ,
142- ) ;
98+ logger. print ( format ! ( "Browser {} given as option" , option_browser) . as_str ( ) ) ;
14399
144100 open_browser ( & option_browser, & url) ;
145101 } else {
146- match env:: var ( " BROWSER" ) {
102+ match env:: var ( BROWSER ) {
147103 // If the environment variable is available, open the web browser.
148104 Ok ( browser) => open_browser ( & browser, & url) ,
105+ // Else, open the default browser of the system.
149106 Err ( e) => {
150- print_verbose (
151- format ! ( "BROWSER variable not available : {}" , e) . as_str ( ) ,
152- & opt. verbose ,
153- ) ;
154-
155- print_verbose ( "Opening default browser" , & opt. verbose ) ;
107+ logger. print ( format ! ( "{} variable not available : {}" , BROWSER , e) . as_str ( ) ) ;
108+ logger. print ( "Opening default browser" ) ;
156109
157110 // Open the default web browser on the current system.
158111 match open:: that ( url) {
0 commit comments