@@ -2719,19 +2719,23 @@ static int open_or_create_logfile(const char *path, void *cb)
2719
2719
}
2720
2720
2721
2721
/*
2722
- * Create a reflog for a ref. If force_create = 0, the reflog will
2723
- * only be created for certain refs (those for which
2724
- * should_autocreate_reflog returns non-zero. Otherwise, create it
2725
- * regardless of the ref name. Fill in *err and return -1 on failure.
2722
+ * Create a reflog for a ref. Store its path to *logfile. If
2723
+ * force_create = 0, only create the reflog for certain refs (those
2724
+ * for which should_autocreate_reflog returns non-zero). Otherwise,
2725
+ * create it regardless of the reference name. If the logfile already
2726
+ * existed or was created, return 0 and set *logfd to the file
2727
+ * descriptor opened for appending to the file. If no logfile exists
2728
+ * and we decided not to create one, return 0 and set *logfd to -1. On
2729
+ * failure, fill in *err, set *logfd to -1, and return -1.
2726
2730
*/
2727
- static int log_ref_setup (const char * refname , struct strbuf * logfile , struct strbuf * err , int force_create )
2731
+ static int log_ref_setup (const char * refname ,
2732
+ struct strbuf * logfile , int * logfd ,
2733
+ struct strbuf * err , int force_create )
2728
2734
{
2729
- int logfd ;
2730
-
2731
2735
strbuf_git_path (logfile , "logs/%s" , refname );
2732
2736
2733
2737
if (force_create || should_autocreate_reflog (refname )) {
2734
- if (raceproof_create_file (logfile -> buf , open_or_create_logfile , & logfd )) {
2738
+ if (raceproof_create_file (logfile -> buf , open_or_create_logfile , logfd )) {
2735
2739
if (errno == ENOENT )
2736
2740
strbuf_addf (err , "unable to create directory for '%s': "
2737
2741
"%s" , logfile -> buf , strerror (errno ));
@@ -2745,8 +2749,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
2745
2749
return -1 ;
2746
2750
}
2747
2751
} else {
2748
- logfd = open (logfile -> buf , O_APPEND | O_WRONLY , 0666 );
2749
- if (logfd < 0 ) {
2752
+ * logfd = open (logfile -> buf , O_APPEND | O_WRONLY , 0666 );
2753
+ if (* logfd < 0 ) {
2750
2754
if (errno == ENOENT || errno == EISDIR ) {
2751
2755
/*
2752
2756
* The logfile doesn't already exist,
@@ -2763,10 +2767,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
2763
2767
}
2764
2768
}
2765
2769
2766
- if (logfd >= 0 ) {
2770
+ if (* logfd >= 0 )
2767
2771
adjust_shared_perm (logfile -> buf );
2768
- close (logfd );
2769
- }
2770
2772
2771
2773
return 0 ;
2772
2774
}
@@ -2777,11 +2779,14 @@ static int files_create_reflog(struct ref_store *ref_store,
2777
2779
{
2778
2780
int ret ;
2779
2781
struct strbuf sb = STRBUF_INIT ;
2782
+ int fd ;
2780
2783
2781
2784
/* Check validity (but we don't need the result): */
2782
2785
files_downcast (ref_store , 0 , "create_reflog" );
2783
2786
2784
- ret = log_ref_setup (refname , & sb , err , force_create );
2787
+ ret = log_ref_setup (refname , & sb , & fd , err , force_create );
2788
+ if (fd >= 0 )
2789
+ close (fd );
2785
2790
strbuf_release (& sb );
2786
2791
return ret ;
2787
2792
}
@@ -2817,17 +2822,17 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
2817
2822
struct strbuf * logfile , int flags ,
2818
2823
struct strbuf * err )
2819
2824
{
2820
- int logfd , result , oflags = O_APPEND | O_WRONLY ;
2825
+ int logfd , result ;
2821
2826
2822
2827
if (log_all_ref_updates < 0 )
2823
2828
log_all_ref_updates = !is_bare_repository ();
2824
2829
2825
- result = log_ref_setup (refname , logfile , err , flags & REF_FORCE_CREATE_REFLOG );
2830
+ result = log_ref_setup (refname , logfile , & logfd , err ,
2831
+ flags & REF_FORCE_CREATE_REFLOG );
2826
2832
2827
2833
if (result )
2828
2834
return result ;
2829
2835
2830
- logfd = open (logfile -> buf , oflags );
2831
2836
if (logfd < 0 )
2832
2837
return 0 ;
2833
2838
result = log_ref_write_fd (logfd , old_sha1 , new_sha1 ,
0 commit comments