1+ use std:: collections:: HashSet ;
12use std:: path:: Path ;
23use std:: str;
34
45use duct:: cmd;
56use git2:: { Config , Error , Repository } ;
6- use log:: { debug, trace} ;
7+ use log:: { debug, trace, warn } ;
78use shellexpand;
89
910/// Update old `req.key` config format to include remote name, i.e, `req.remote_name.key`
@@ -23,6 +24,15 @@ fn slugify_domain(domain: &str) -> String {
2324 str:: replace ( domain, "." , "|" )
2425}
2526
27+ /// Get the remotes for the repository
28+ pub fn get_remotes ( ) -> HashSet < String > {
29+ let repo = Repository :: open_from_env ( ) . expect ( "Couldn't find repository" ) ;
30+ match repo. remotes ( ) {
31+ Ok ( remotes) => remotes. into_iter ( ) . filter_map ( |rem| rem) . map ( |rem| String :: from ( rem) ) . collect ( ) ,
32+ Err ( _) => HashSet :: new ( )
33+ }
34+ }
35+
2636/// Get the URL of the given remote
2737pub fn get_remote_url ( remote : & str ) -> String {
2838 let repo = Repository :: open_from_env ( ) . expect ( "Couldn't find repository" ) ;
@@ -47,7 +57,7 @@ pub fn get_config(field_name: &str, remote_name: &str) -> Option<String> {
4757 }
4858}
4959
50- /// Set a value for the project-local git-req configuration
60+ /// Set a value for the project remote -local git-req configuration
5161pub fn set_config ( field_name : & str , remote_name : & str , value : & str ) {
5262 migrate_legacy ( field_name, "origin" ) ;
5363 let repo = Repository :: open_from_env ( ) . expect ( "Couldn't find repository" ) ;
@@ -65,6 +75,24 @@ pub fn delete_config(field_name: &str, remote_name: &str) {
6575 . unwrap ( ) ;
6676}
6777
78+ /// Get a value for the given project-local git-req config
79+ pub fn get_project_config ( field_name : & str ) -> Option < String > {
80+ let key = format ! ( "req.{}" , field_name) ;
81+ match get_repo_info ( & key) {
82+ Ok ( val) => Some ( val) ,
83+ Err ( _) => None ,
84+ }
85+ }
86+
87+ /// Set a value for the project-local git-req configuration. Consider using `set_config` unless
88+ /// absolutely necessary.
89+ pub fn set_project_config ( field_name : & str , value : & str ) {
90+ let repo = Repository :: open_from_env ( ) . expect ( "Couldn't find repository" ) ;
91+ let mut cfg = repo. config ( ) . unwrap ( ) ;
92+ cfg. set_str ( & format ! ( "req.{}" , field_name) , value)
93+ . unwrap ( ) ;
94+ }
95+
6896/// Get a value for the given global git-req config
6997pub fn get_req_config ( domain : & str , field : & str ) -> Option < String > {
7098 let slug = slugify_domain ( domain) ;
@@ -99,9 +127,9 @@ pub fn delete_req_config(domain: &str, field: &str) -> Result<(), Error> {
99127 cfg. remove ( & format ! ( "req.{}.{}" , slug, field) )
100128}
101129
102- /// Get the name of the default remote
130+ /// Guess the name of the default remote
103131#[ allow( clippy:: match_wild_err_arm) ]
104- pub fn get_default_remote_name ( ) -> Result < String , String > {
132+ pub fn guess_default_remote_name ( ) -> Result < String , String > {
105133 let repo = Repository :: open_from_env ( ) . expect ( "Couldn't find repository" ) ;
106134 let remotes = repo. remotes ( ) . expect ( "Couldn't fetch the list of remotes" ) ;
107135 match remotes. len ( ) {
@@ -122,8 +150,8 @@ pub fn checkout_branch(
122150 is_virtual_remote_branch : bool ,
123151) -> Result < bool , String > {
124152 let repo = Repository :: open_from_env ( ) . expect ( "Couldn't find repository" ) ;
125- let local_branch_name = match get_default_remote_name ( ) {
126- Ok ( default_remote_name) => {
153+ let local_branch_name = match get_project_config ( "defaultremote" ) {
154+ Some ( default_remote_name) => {
127155 if remote_name != default_remote_name {
128156 trace ! ( "Non-default remote name requested: {}" , remote_name) ;
129157 format ! ( "{}/{}" , remote_name, local_branch_name)
@@ -132,9 +160,9 @@ pub fn checkout_branch(
132160 String :: from ( local_branch_name)
133161 }
134162 }
135- Err ( _ ) => {
136- trace ! (
137- "Multiple remotes found, but none named origin: {}" ,
163+ None => {
164+ warn ! (
165+ "No default remote found. Using {}" ,
138166 remote_name
139167 ) ;
140168 format ! ( "{}/{}" , remote_name, local_branch_name)
@@ -172,6 +200,7 @@ pub fn checkout_branch(
172200 } else {
173201 & origin_with_remote
174202 } ;
203+ trace ! ( "Checking '{}' as '{}'" , remote_ref, local_branch_name) ;
175204 match cmd ! ( "git" , "checkout" , "-b" , & local_branch_name, & remote_ref) . run ( ) {
176205 Ok ( _) => Ok ( true ) ,
177206 Err ( err) => Err ( format ! ( "Could not check out local branch: {}" , err) ) ,
0 commit comments