@@ -89,6 +89,11 @@ static int proto_is_http(const char *s)
8989
9090static void credential_apply_config (struct credential * c )
9191{
92+ if (!c -> host )
93+ die (_ ("refusing to work with credential missing host field" ));
94+ if (!c -> protocol )
95+ die (_ ("refusing to work with credential missing protocol field" ));
96+
9297 if (c -> configured )
9398 return ;
9499 git_config (credential_config_callback , c );
@@ -191,8 +196,11 @@ int credential_read(struct credential *c, FILE *fp)
191196 return 0 ;
192197}
193198
194- static void credential_write_item (FILE * fp , const char * key , const char * value )
199+ static void credential_write_item (FILE * fp , const char * key , const char * value ,
200+ int required )
195201{
202+ if (!value && required )
203+ BUG ("credential value for %s is missing" , key );
196204 if (!value )
197205 return ;
198206 if (strchr (value , '\n' ))
@@ -202,11 +210,11 @@ static void credential_write_item(FILE *fp, const char *key, const char *value)
202210
203211void credential_write (const struct credential * c , FILE * fp )
204212{
205- credential_write_item (fp , "protocol" , c -> protocol );
206- credential_write_item (fp , "host" , c -> host );
207- credential_write_item (fp , "path" , c -> path );
208- credential_write_item (fp , "username" , c -> username );
209- credential_write_item (fp , "password" , c -> password );
213+ credential_write_item (fp , "protocol" , c -> protocol , 1 );
214+ credential_write_item (fp , "host" , c -> host , 1 );
215+ credential_write_item (fp , "path" , c -> path , 0 );
216+ credential_write_item (fp , "username" , c -> username , 0 );
217+ credential_write_item (fp , "password" , c -> password , 0 );
210218}
211219
212220static int run_credential_helper (struct credential * c ,
@@ -352,8 +360,11 @@ int credential_from_url_gently(struct credential *c, const char *url,
352360 * (3) proto://<user>:<pass>@<host>/...
353361 */
354362 proto_end = strstr (url , "://" );
355- if (!proto_end )
356- return 0 ;
363+ if (!proto_end || proto_end == url ) {
364+ if (!quiet )
365+ warning (_ ("url has no scheme: %s" ), url );
366+ return -1 ;
367+ }
357368 cp = proto_end + 3 ;
358369 at = strchr (cp , '@' );
359370 colon = strchr (cp , ':' );
@@ -374,10 +385,8 @@ int credential_from_url_gently(struct credential *c, const char *url,
374385 host = at + 1 ;
375386 }
376387
377- if (proto_end - url > 0 )
378- c -> protocol = xmemdupz (url , proto_end - url );
379- if (slash - host > 0 )
380- c -> host = url_decode_mem (host , slash - host );
388+ c -> protocol = xmemdupz (url , proto_end - url );
389+ c -> host = url_decode_mem (host , slash - host );
381390 /* Trim leading and trailing slashes from path */
382391 while (* slash == '/' )
383392 slash ++ ;
@@ -401,8 +410,6 @@ int credential_from_url_gently(struct credential *c, const char *url,
401410
402411void credential_from_url (struct credential * c , const char * url )
403412{
404- if (credential_from_url_gently (c , url , 0 ) < 0 ) {
405- warning (_ ("skipping credential lookup for url: %s" ), url );
406- credential_clear (c );
407- }
413+ if (credential_from_url_gently (c , url , 0 ) < 0 )
414+ die (_ ("credential url cannot be parsed: %s" ), url );
408415}
0 commit comments