@@ -1840,12 +1840,17 @@ static int verify_lock(struct ref_lock *lock,
1840
1840
if (read_ref_full (lock -> ref_name ,
1841
1841
mustexist ? RESOLVE_REF_READING : 0 ,
1842
1842
lock -> old_oid .hash , NULL )) {
1843
- int save_errno = errno ;
1844
- strbuf_addf (err , "can't verify ref %s" , lock -> ref_name );
1845
- errno = save_errno ;
1846
- return -1 ;
1843
+ if (old_sha1 ) {
1844
+ int save_errno = errno ;
1845
+ strbuf_addf (err , "can't verify ref %s" , lock -> ref_name );
1846
+ errno = save_errno ;
1847
+ return -1 ;
1848
+ } else {
1849
+ hashclr (lock -> old_oid .hash );
1850
+ return 0 ;
1851
+ }
1847
1852
}
1848
- if (hashcmp (lock -> old_oid .hash , old_sha1 )) {
1853
+ if (old_sha1 && hashcmp (lock -> old_oid .hash , old_sha1 )) {
1849
1854
strbuf_addf (err , "ref %s is at %s but expected %s" ,
1850
1855
lock -> ref_name ,
1851
1856
sha1_to_hex (lock -> old_oid .hash ),
@@ -1882,7 +1887,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1882
1887
const char * orig_refname = refname ;
1883
1888
struct ref_lock * lock ;
1884
1889
int last_errno = 0 ;
1885
- int type , lflags ;
1890
+ int type ;
1891
+ int lflags = 0 ;
1886
1892
int mustexist = (old_sha1 && !is_null_sha1 (old_sha1 ));
1887
1893
int resolve_flags = 0 ;
1888
1894
int attempts_remaining = 3 ;
@@ -1893,10 +1899,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1893
1899
1894
1900
if (mustexist )
1895
1901
resolve_flags |= RESOLVE_REF_READING ;
1896
- if (flags & REF_DELETING ) {
1902
+ if (flags & REF_DELETING )
1897
1903
resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME ;
1898
- if (flags & REF_NODEREF )
1899
- resolve_flags |= RESOLVE_REF_NO_RECURSE ;
1904
+ if (flags & REF_NODEREF ) {
1905
+ resolve_flags |= RESOLVE_REF_NO_RECURSE ;
1906
+ lflags |= LOCK_NO_DEREF ;
1900
1907
}
1901
1908
1902
1909
refname = resolve_ref_unsafe (refname , resolve_flags ,
@@ -1932,6 +1939,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1932
1939
1933
1940
goto error_return ;
1934
1941
}
1942
+
1943
+ if (flags & REF_NODEREF )
1944
+ refname = orig_refname ;
1945
+
1935
1946
/*
1936
1947
* If the ref did not exist and we are creating it, make sure
1937
1948
* there is no existing packed ref whose name begins with our
@@ -1947,11 +1958,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1947
1958
1948
1959
lock -> lk = xcalloc (1 , sizeof (struct lock_file ));
1949
1960
1950
- lflags = 0 ;
1951
- if (flags & REF_NODEREF ) {
1952
- refname = orig_refname ;
1953
- lflags |= LOCK_NO_DEREF ;
1954
- }
1955
1961
lock -> ref_name = xstrdup (refname );
1956
1962
lock -> orig_ref_name = xstrdup (orig_refname );
1957
1963
strbuf_git_path (& ref_file , "%s" , refname );
@@ -1985,7 +1991,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1985
1991
goto error_return ;
1986
1992
}
1987
1993
}
1988
- if (old_sha1 && verify_lock (lock , old_sha1 , mustexist , err )) {
1994
+ if (verify_lock (lock , old_sha1 , mustexist , err )) {
1989
1995
last_errno = errno ;
1990
1996
goto error_return ;
1991
1997
}
@@ -2811,73 +2817,72 @@ static int commit_ref_update(struct ref_lock *lock,
2811
2817
return 0 ;
2812
2818
}
2813
2819
2814
- int create_symref (const char * ref_target , const char * refs_heads_master ,
2815
- const char * logmsg )
2820
+ static int create_ref_symlink (struct ref_lock * lock , const char * target )
2816
2821
{
2817
- char * lockpath = NULL ;
2818
- char ref [1000 ];
2819
- int fd , len , written ;
2820
- char * git_HEAD = git_pathdup ("%s" , ref_target );
2821
- unsigned char old_sha1 [20 ], new_sha1 [20 ];
2822
- struct strbuf err = STRBUF_INIT ;
2823
-
2824
- if (logmsg && read_ref (ref_target , old_sha1 ))
2825
- hashclr (old_sha1 );
2826
-
2827
- if (safe_create_leading_directories (git_HEAD ) < 0 )
2828
- return error ("unable to create directory for %s" , git_HEAD );
2829
-
2822
+ int ret = -1 ;
2830
2823
#ifndef NO_SYMLINK_HEAD
2831
- if (prefer_symlink_refs ) {
2832
- unlink (git_HEAD );
2833
- if (!symlink (refs_heads_master , git_HEAD ))
2834
- goto done ;
2824
+ char * ref_path = get_locked_file_path (lock -> lk );
2825
+ unlink (ref_path );
2826
+ ret = symlink (target , ref_path );
2827
+ free (ref_path );
2828
+
2829
+ if (ret )
2835
2830
fprintf (stderr , "no symlink - falling back to symbolic ref\n" );
2836
- }
2837
2831
#endif
2832
+ return ret ;
2833
+ }
2838
2834
2839
- len = snprintf (ref , sizeof (ref ), "ref: %s\n" , refs_heads_master );
2840
- if (sizeof (ref ) <= len ) {
2841
- error ("refname too long: %s" , refs_heads_master );
2842
- goto error_free_return ;
2843
- }
2844
- lockpath = mkpathdup ("%s.lock" , git_HEAD );
2845
- fd = open (lockpath , O_CREAT | O_EXCL | O_WRONLY , 0666 );
2846
- if (fd < 0 ) {
2847
- error ("Unable to open %s for writing" , lockpath );
2848
- goto error_free_return ;
2849
- }
2850
- written = write_in_full (fd , ref , len );
2851
- if (close (fd ) != 0 || written != len ) {
2852
- error ("Unable to write to %s" , lockpath );
2853
- goto error_unlink_return ;
2854
- }
2855
- if (rename (lockpath , git_HEAD ) < 0 ) {
2856
- error ("Unable to create %s" , git_HEAD );
2857
- goto error_unlink_return ;
2858
- }
2859
- if (adjust_shared_perm (git_HEAD )) {
2860
- error ("Unable to fix permissions on %s" , lockpath );
2861
- error_unlink_return :
2862
- unlink_or_warn (lockpath );
2863
- error_free_return :
2864
- free (lockpath );
2865
- free (git_HEAD );
2866
- return -1 ;
2835
+ static void update_symref_reflog (struct ref_lock * lock , const char * refname ,
2836
+ const char * target , const char * logmsg )
2837
+ {
2838
+ struct strbuf err = STRBUF_INIT ;
2839
+ unsigned char new_sha1 [20 ];
2840
+ if (logmsg && !read_ref (target , new_sha1 ) &&
2841
+ log_ref_write (refname , lock -> old_oid .hash , new_sha1 , logmsg , 0 , & err )) {
2842
+ error ("%s" , err .buf );
2843
+ strbuf_release (& err );
2867
2844
}
2868
- free ( lockpath );
2845
+ }
2869
2846
2870
- #ifndef NO_SYMLINK_HEAD
2871
- done :
2872
- #endif
2873
- if (logmsg && !read_ref (refs_heads_master , new_sha1 ) &&
2874
- log_ref_write (ref_target , old_sha1 , new_sha1 , logmsg , 0 , & err )) {
2847
+ static int create_symref_locked (struct ref_lock * lock , const char * refname ,
2848
+ const char * target , const char * logmsg )
2849
+ {
2850
+ if (prefer_symlink_refs && !create_ref_symlink (lock , target )) {
2851
+ update_symref_reflog (lock , refname , target , logmsg );
2852
+ return 0 ;
2853
+ }
2854
+
2855
+ if (!fdopen_lock_file (lock -> lk , "w" ))
2856
+ return error ("unable to fdopen %s: %s" ,
2857
+ lock -> lk -> tempfile .filename .buf , strerror (errno ));
2858
+
2859
+ update_symref_reflog (lock , refname , target , logmsg );
2860
+
2861
+ /* no error check; commit_ref will check ferror */
2862
+ fprintf (lock -> lk -> tempfile .fp , "ref: %s\n" , target );
2863
+ if (commit_ref (lock ) < 0 )
2864
+ return error ("unable to write symref for %s: %s" , refname ,
2865
+ strerror (errno ));
2866
+ return 0 ;
2867
+ }
2868
+
2869
+ int create_symref (const char * refname , const char * target , const char * logmsg )
2870
+ {
2871
+ struct strbuf err = STRBUF_INIT ;
2872
+ struct ref_lock * lock ;
2873
+ int ret ;
2874
+
2875
+ lock = lock_ref_sha1_basic (refname , NULL , NULL , NULL , REF_NODEREF , NULL ,
2876
+ & err );
2877
+ if (!lock ) {
2875
2878
error ("%s" , err .buf );
2876
2879
strbuf_release (& err );
2880
+ return -1 ;
2877
2881
}
2878
2882
2879
- free (git_HEAD );
2880
- return 0 ;
2883
+ ret = create_symref_locked (lock , refname , target , logmsg );
2884
+ unlock_ref (lock );
2885
+ return ret ;
2881
2886
}
2882
2887
2883
2888
int reflog_exists (const char * refname )
0 commit comments