6
6
#include "string-list.h"
7
7
#include "strbuf.h"
8
8
#include "run-command.h"
9
+ #include "rebase.h"
9
10
#include "refs.h"
10
11
#include "refspec.h"
11
12
#include "object-store.h"
@@ -248,9 +249,8 @@ static int add(int argc, const char **argv)
248
249
struct branch_info {
249
250
char * remote_name ;
250
251
struct string_list merge ;
251
- enum {
252
- NO_REBASE , NORMAL_REBASE , INTERACTIVE_REBASE , REBASE_MERGES
253
- } rebase ;
252
+ enum rebase_type rebase ;
253
+ char * push_remote_name ;
254
254
};
255
255
256
256
static struct string_list branch_list = STRING_LIST_INIT_NODUP ;
@@ -264,59 +264,69 @@ static const char *abbrev_ref(const char *name, const char *prefix)
264
264
265
265
static int config_read_branches (const char * key , const char * value , void * cb )
266
266
{
267
- if (starts_with (key , "branch." )) {
268
- const char * orig_key = key ;
269
- char * name ;
270
- struct string_list_item * item ;
271
- struct branch_info * info ;
272
- enum { REMOTE , MERGE , REBASE } type ;
273
- size_t key_len ;
274
-
275
- key += 7 ;
276
- if (strip_suffix (key , ".remote" , & key_len )) {
277
- name = xmemdupz (key , key_len );
278
- type = REMOTE ;
279
- } else if (strip_suffix (key , ".merge" , & key_len )) {
280
- name = xmemdupz (key , key_len );
281
- type = MERGE ;
282
- } else if (strip_suffix (key , ".rebase" , & key_len )) {
283
- name = xmemdupz (key , key_len );
284
- type = REBASE ;
285
- } else
286
- return 0 ;
267
+ const char * orig_key = key ;
268
+ char * name ;
269
+ struct string_list_item * item ;
270
+ struct branch_info * info ;
271
+ enum { REMOTE , MERGE , REBASE , PUSH_REMOTE } type ;
272
+ size_t key_len ;
287
273
288
- item = string_list_insert (& branch_list , name );
274
+ if (!starts_with (key , "branch." ))
275
+ return 0 ;
289
276
290
- if (!item -> util )
291
- item -> util = xcalloc (1 , sizeof (struct branch_info ));
292
- info = item -> util ;
293
- if (type == REMOTE ) {
294
- if (info -> remote_name )
295
- warning (_ ("more than one %s" ), orig_key );
296
- info -> remote_name = xstrdup (value );
297
- } else if (type == MERGE ) {
298
- char * space = strchr (value , ' ' );
299
- value = abbrev_branch (value );
300
- while (space ) {
301
- char * merge ;
302
- merge = xstrndup (value , space - value );
303
- string_list_append (& info -> merge , merge );
304
- value = abbrev_branch (space + 1 );
305
- space = strchr (value , ' ' );
306
- }
307
- string_list_append (& info -> merge , xstrdup (value ));
308
- } else {
309
- int v = git_parse_maybe_bool (value );
310
- if (v >= 0 )
311
- info -> rebase = v ;
312
- else if (!strcmp (value , "preserve" ))
313
- info -> rebase = NORMAL_REBASE ;
314
- else if (!strcmp (value , "merges" ))
315
- info -> rebase = REBASE_MERGES ;
316
- else if (!strcmp (value , "interactive" ))
317
- info -> rebase = INTERACTIVE_REBASE ;
277
+ key += strlen ("branch." );
278
+ if (strip_suffix (key , ".remote" , & key_len ))
279
+ type = REMOTE ;
280
+ else if (strip_suffix (key , ".merge" , & key_len ))
281
+ type = MERGE ;
282
+ else if (strip_suffix (key , ".rebase" , & key_len ))
283
+ type = REBASE ;
284
+ else if (strip_suffix (key , ".pushremote" , & key_len ))
285
+ type = PUSH_REMOTE ;
286
+ else
287
+ return 0 ;
288
+ name = xmemdupz (key , key_len );
289
+
290
+ item = string_list_insert (& branch_list , name );
291
+
292
+ if (!item -> util )
293
+ item -> util = xcalloc (1 , sizeof (struct branch_info ));
294
+ info = item -> util ;
295
+ switch (type ) {
296
+ case REMOTE :
297
+ if (info -> remote_name )
298
+ warning (_ ("more than one %s" ), orig_key );
299
+ info -> remote_name = xstrdup (value );
300
+ break ;
301
+ case MERGE : {
302
+ char * space = strchr (value , ' ' );
303
+ value = abbrev_branch (value );
304
+ while (space ) {
305
+ char * merge ;
306
+ merge = xstrndup (value , space - value );
307
+ string_list_append (& info -> merge , merge );
308
+ value = abbrev_branch (space + 1 );
309
+ space = strchr (value , ' ' );
318
310
}
311
+ string_list_append (& info -> merge , xstrdup (value ));
312
+ break ;
313
+ }
314
+ case REBASE :
315
+ /*
316
+ * Consider invalid values as false and check the
317
+ * truth value with >= REBASE_TRUE.
318
+ */
319
+ info -> rebase = rebase_parse_value (value );
320
+ break ;
321
+ case PUSH_REMOTE :
322
+ if (info -> push_remote_name )
323
+ warning (_ ("more than one %s" ), orig_key );
324
+ info -> push_remote_name = xstrdup (value );
325
+ break ;
326
+ default :
327
+ BUG ("unexpected type=%d" , type );
319
328
}
329
+
320
330
return 0 ;
321
331
}
322
332
@@ -605,6 +615,55 @@ static int migrate_file(struct remote *remote)
605
615
return 0 ;
606
616
}
607
617
618
+ struct push_default_info
619
+ {
620
+ const char * old_name ;
621
+ enum config_scope scope ;
622
+ struct strbuf origin ;
623
+ int linenr ;
624
+ };
625
+
626
+ static int config_read_push_default (const char * key , const char * value ,
627
+ void * cb )
628
+ {
629
+ struct push_default_info * info = cb ;
630
+ if (strcmp (key , "remote.pushdefault" ) || strcmp (value , info -> old_name ))
631
+ return 0 ;
632
+
633
+ info -> scope = current_config_scope ();
634
+ strbuf_reset (& info -> origin );
635
+ strbuf_addstr (& info -> origin , current_config_name ());
636
+ info -> linenr = current_config_line ();
637
+
638
+ return 0 ;
639
+ }
640
+
641
+ static void handle_push_default (const char * old_name , const char * new_name )
642
+ {
643
+ struct push_default_info push_default = {
644
+ old_name , CONFIG_SCOPE_UNKNOWN , STRBUF_INIT , -1 };
645
+ git_config (config_read_push_default , & push_default );
646
+ if (push_default .scope >= CONFIG_SCOPE_COMMAND )
647
+ ; /* pass */
648
+ else if (push_default .scope >= CONFIG_SCOPE_LOCAL ) {
649
+ int result = git_config_set_gently ("remote.pushDefault" ,
650
+ new_name );
651
+ if (new_name && result && result != CONFIG_NOTHING_SET )
652
+ die (_ ("could not set '%s'" ), "remote.pushDefault" );
653
+ else if (!new_name && result && result != CONFIG_NOTHING_SET )
654
+ die (_ ("could not unset '%s'" ), "remote.pushDefault" );
655
+ } else if (push_default .scope >= CONFIG_SCOPE_SYSTEM ) {
656
+ /* warn */
657
+ warning (_ ("The %s configuration remote.pushDefault in:\n"
658
+ "\t%s:%d\n"
659
+ "now names the non-existent remote '%s'" ),
660
+ config_scope_name (push_default .scope ),
661
+ push_default .origin .buf , push_default .linenr ,
662
+ old_name );
663
+ }
664
+ }
665
+
666
+
608
667
static int mv (int argc , const char * * argv )
609
668
{
610
669
struct option options [] = {
@@ -680,6 +739,11 @@ static int mv(int argc, const char **argv)
680
739
strbuf_addf (& buf , "branch.%s.remote" , item -> string );
681
740
git_config_set (buf .buf , rename .new_name );
682
741
}
742
+ if (info -> push_remote_name && !strcmp (info -> push_remote_name , rename .old_name )) {
743
+ strbuf_reset (& buf );
744
+ strbuf_addf (& buf , "branch.%s.pushremote" , item -> string );
745
+ git_config_set (buf .buf , rename .new_name );
746
+ }
683
747
}
684
748
685
749
if (!refspec_updated )
@@ -735,6 +799,9 @@ static int mv(int argc, const char **argv)
735
799
die (_ ("creating '%s' failed" ), buf .buf );
736
800
}
737
801
string_list_clear (& remote_branches , 1 );
802
+
803
+ handle_push_default (rename .old_name , rename .new_name );
804
+
738
805
return 0 ;
739
806
}
740
807
@@ -781,6 +848,13 @@ static int rm(int argc, const char **argv)
781
848
die (_ ("could not unset '%s'" ), buf .buf );
782
849
}
783
850
}
851
+ if (info -> push_remote_name && !strcmp (info -> push_remote_name , remote -> name )) {
852
+ strbuf_reset (& buf );
853
+ strbuf_addf (& buf , "branch.%s.pushremote" , item -> string );
854
+ result = git_config_set_gently (buf .buf , NULL );
855
+ if (result && result != CONFIG_NOTHING_SET )
856
+ die (_ ("could not unset '%s'" ), buf .buf );
857
+ }
784
858
}
785
859
786
860
/*
@@ -813,6 +887,8 @@ static int rm(int argc, const char **argv)
813
887
strbuf_addf (& buf , "remote.%s" , remote -> name );
814
888
if (git_config_rename_section (buf .buf , NULL ) < 1 )
815
889
return error (_ ("Could not remove config section '%s'" ), buf .buf );
890
+
891
+ handle_push_default (remote -> name , NULL );
816
892
}
817
893
818
894
return result ;
@@ -943,7 +1019,7 @@ static int add_local_to_show_info(struct string_list_item *branch_item, void *cb
943
1019
return 0 ;
944
1020
if ((n = strlen (branch_item -> string )) > show_info -> width )
945
1021
show_info -> width = n ;
946
- if (branch_info -> rebase )
1022
+ if (branch_info -> rebase >= REBASE_TRUE )
947
1023
show_info -> any_rebase = 1 ;
948
1024
949
1025
item = string_list_insert (show_info -> list , branch_item -> string );
@@ -960,16 +1036,16 @@ static int show_local_info_item(struct string_list_item *item, void *cb_data)
960
1036
int width = show_info -> width + 4 ;
961
1037
int i ;
962
1038
963
- if (branch_info -> rebase && branch_info -> merge .nr > 1 ) {
1039
+ if (branch_info -> rebase >= REBASE_TRUE && branch_info -> merge .nr > 1 ) {
964
1040
error (_ ("invalid branch.%s.merge; cannot rebase onto > 1 branch" ),
965
1041
item -> string );
966
1042
return 0 ;
967
1043
}
968
1044
969
1045
printf (" %-*s " , show_info -> width , item -> string );
970
- if (branch_info -> rebase ) {
1046
+ if (branch_info -> rebase >= REBASE_TRUE ) {
971
1047
const char * msg ;
972
- if (branch_info -> rebase == INTERACTIVE_REBASE )
1048
+ if (branch_info -> rebase == REBASE_INTERACTIVE )
973
1049
msg = _ ("rebases interactively onto remote %s" );
974
1050
else if (branch_info -> rebase == REBASE_MERGES )
975
1051
msg = _ ("rebases interactively (with merges) onto "
0 commit comments