5
5
#include "config.h"
6
6
#include "gettext.h"
7
7
#include "hash.h"
8
+ #include "hex.h"
8
9
#include "refs.h"
9
10
#include "object-name.h"
10
11
#include "parse-options.h"
13
14
static const char * const git_update_ref_usage [] = {
14
15
N_ ("git update-ref [<options>] -d <refname> [<old-oid>]" ),
15
16
N_ ("git update-ref [<options>] <refname> <new-oid> [<old-oid>]" ),
16
- N_ ("git update-ref [<options>] --stdin [-z]" ),
17
+ N_ ("git update-ref [<options>] --stdin [-z] [--batch-updates] " ),
17
18
NULL
18
19
};
19
20
@@ -565,6 +566,49 @@ static void parse_cmd_abort(struct ref_transaction *transaction,
565
566
report_ok ("abort" );
566
567
}
567
568
569
+ static void print_rejected_refs (const char * refname ,
570
+ const struct object_id * old_oid ,
571
+ const struct object_id * new_oid ,
572
+ const char * old_target ,
573
+ const char * new_target ,
574
+ enum ref_transaction_error err ,
575
+ void * cb_data UNUSED )
576
+ {
577
+ struct strbuf sb = STRBUF_INIT ;
578
+ const char * reason = "" ;
579
+
580
+ switch (err ) {
581
+ case REF_TRANSACTION_ERROR_NAME_CONFLICT :
582
+ reason = "refname conflict" ;
583
+ break ;
584
+ case REF_TRANSACTION_ERROR_CREATE_EXISTS :
585
+ reason = "reference already exists" ;
586
+ break ;
587
+ case REF_TRANSACTION_ERROR_NONEXISTENT_REF :
588
+ reason = "reference does not exist" ;
589
+ break ;
590
+ case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE :
591
+ reason = "incorrect old value provided" ;
592
+ break ;
593
+ case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE :
594
+ reason = "invalid new value provided" ;
595
+ break ;
596
+ case REF_TRANSACTION_ERROR_EXPECTED_SYMREF :
597
+ reason = "expected symref but found regular ref" ;
598
+ break ;
599
+ default :
600
+ reason = "unkown failure" ;
601
+ }
602
+
603
+ strbuf_addf (& sb , "rejected %s %s %s %s\n" , refname ,
604
+ new_oid ? oid_to_hex (new_oid ) : new_target ,
605
+ old_oid ? oid_to_hex (old_oid ) : old_target ,
606
+ reason );
607
+
608
+ fwrite (sb .buf , sb .len , 1 , stdout );
609
+ strbuf_release (& sb );
610
+ }
611
+
568
612
static void parse_cmd_commit (struct ref_transaction * transaction ,
569
613
const char * next , const char * end UNUSED )
570
614
{
@@ -573,6 +617,10 @@ static void parse_cmd_commit(struct ref_transaction *transaction,
573
617
die ("commit: extra input: %s" , next );
574
618
if (ref_transaction_commit (transaction , & error ))
575
619
die ("commit: %s" , error .buf );
620
+
621
+ ref_transaction_for_each_rejected_update (transaction ,
622
+ print_rejected_refs , NULL );
623
+
576
624
report_ok ("commit" );
577
625
ref_transaction_free (transaction );
578
626
}
@@ -609,15 +657,15 @@ static const struct parse_cmd {
609
657
{ "commit" , parse_cmd_commit , 0 , UPDATE_REFS_CLOSED },
610
658
};
611
659
612
- static void update_refs_stdin (void )
660
+ static void update_refs_stdin (unsigned int flags )
613
661
{
614
662
struct strbuf input = STRBUF_INIT , err = STRBUF_INIT ;
615
663
enum update_refs_state state = UPDATE_REFS_OPEN ;
616
664
struct ref_transaction * transaction ;
617
665
int i , j ;
618
666
619
667
transaction = ref_store_transaction_begin (get_main_ref_store (the_repository ),
620
- 0 , & err );
668
+ flags , & err );
621
669
if (!transaction )
622
670
die ("%s" , err .buf );
623
671
@@ -685,7 +733,7 @@ static void update_refs_stdin(void)
685
733
*/
686
734
state = cmd -> state ;
687
735
transaction = ref_store_transaction_begin (get_main_ref_store (the_repository ),
688
- 0 , & err );
736
+ flags , & err );
689
737
if (!transaction )
690
738
die ("%s" , err .buf );
691
739
@@ -701,6 +749,8 @@ static void update_refs_stdin(void)
701
749
/* Commit by default if no transaction was requested. */
702
750
if (ref_transaction_commit (transaction , & err ))
703
751
die ("%s" , err .buf );
752
+ ref_transaction_for_each_rejected_update (transaction ,
753
+ print_rejected_refs , NULL );
704
754
ref_transaction_free (transaction );
705
755
break ;
706
756
case UPDATE_REFS_STARTED :
@@ -727,6 +777,8 @@ int cmd_update_ref(int argc,
727
777
struct object_id oid , oldoid ;
728
778
int delete = 0 , no_deref = 0 , read_stdin = 0 , end_null = 0 ;
729
779
int create_reflog = 0 ;
780
+ unsigned int flags = 0 ;
781
+
730
782
struct option options [] = {
731
783
OPT_STRING ( 'm' , NULL , & msg , N_ ("reason" ), N_ ("reason of the update" )),
732
784
OPT_BOOL ('d' , NULL , & delete , N_ ("delete the reference" )),
@@ -735,6 +787,8 @@ int cmd_update_ref(int argc,
735
787
OPT_BOOL ('z' , NULL , & end_null , N_ ("stdin has NUL-terminated arguments" )),
736
788
OPT_BOOL ( 0 , "stdin" , & read_stdin , N_ ("read updates from stdin" )),
737
789
OPT_BOOL ( 0 , "create-reflog" , & create_reflog , N_ ("create a reflog" )),
790
+ OPT_BIT ('0' , "batch-updates" , & flags , N_ ("batch reference updates" ),
791
+ REF_TRANSACTION_ALLOW_FAILURE ),
738
792
OPT_END (),
739
793
};
740
794
@@ -756,8 +810,10 @@ int cmd_update_ref(int argc,
756
810
usage_with_options (git_update_ref_usage , options );
757
811
if (end_null )
758
812
line_termination = '\0' ;
759
- update_refs_stdin ();
813
+ update_refs_stdin (flags );
760
814
return 0 ;
815
+ } else if (flags & REF_TRANSACTION_ALLOW_FAILURE ) {
816
+ die ("--batch-updates can only be used with --stdin" );
761
817
}
762
818
763
819
if (end_null )
0 commit comments