improve symbolic-ref robustness
Subsystems (2)
cover improve symbolic-ref robustness
I noticed that an interrupt "git symbolic-ref" will not clean up "HEAD.lock". So I started this series as an attempt to convert create_symref() to "struct lock_file" to get the usual tempfile cleanup.
But I found a few other points of interest. The biggest is that git-symbolic-ref does not actually propagate errors in its exit code. Whoops. That's fixed in the first patch, which could go separately to "maint".
The rest of it is fairly dependent on master because of the refs/files-backend.c code movement. I can backport it if we really want it for maint.
[1/4]: symbolic-ref: propagate error code from create_symref() [2/4]: t1401: test reflog creation for git-symbolic-ref [3/4]: create_symref: modernize variable names [4/4]: create_symref: use existing ref-lock code
-Peff
PATCH 1/4 symbolic-ref: propagate error code from create_symref()
If create_symref() fails, git-symbolic-ref will still exit with code 0, and our caller has no idea that the command did nothing.
This appears to have been broken since the beginning of time (e.g., it is not a regression where create_symref() stopped calling die() or something similar).
--- builtin/symbolic-ref.c | 2 +- t/t1401-symbolic-ref.sh | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
builtin/symbolic-ref.c +1 -1
@@ -67,7 +67,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)if (!strcmp (argv[0 ],"HEAD" ) && !starts_with (argv[1 ],"refs/" ))die ("Refusing to point HEAD outside of refs/" ); -create_symref (argv[0 ], argv[1 ], msg); + ret = !!create_symref (argv[0 ], argv[1 ], msg);break ;default :usage_with_options (git_symbolic_ref_usage, options);
t/t1401-symbolic-ref.sh +6 -0
@@ -92,4 +92,10 @@ test_expect_success LONG_REF 'we can parse long symbolic ref' ' test_cmp expect actual ' +test_expect_success 'symbolic-ref reports failure in exit code' ' + test_when_finished "rm -f .git/HEAD.lock" && + >.git/HEAD.lock && + test_must_fail git symbolic-ref HEAD refs/heads/whatever +' + test_done
Signature · 1 line
2.7.0.rc1.350.g9acc0f4
PATCH 2/4 t1401: test reflog creation for git-symbolic-ref
In reply to Jeff KingThe current code writes a reflog entry whenever we update a symbolic ref, but we never test that this is so. Let's add a test to make sure upcoming refactoring doesn't cause a regression.
--- t/t1401-symbolic-ref.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
t/t1401-symbolic-ref.sh +16 -0
@@ -98,4 +98,20 @@ test_expect_success 'symbolic-ref reports failure in exit code' ' test_must_fail git symbolic-ref HEAD refs/heads/whatever ' +test_expect_success 'symbolic-ref writes reflog entry' ' + git checkout -b log1 && + test_commit one && + git checkout -b log2 && + test_commit two && + git checkout --orphan orphan && + git symbolic-ref -m create HEAD refs/heads/log1 && + git symbolic-ref -m update HEAD refs/heads/log2 && + cat >expect <<-\EOF && + update + create + EOF + git log --format=%gs -g >actual && + test_cmp expect actual +' + test_done
Signature · 1 line
2.7.0.rc1.350.g9acc0f4
PATCH 3/4 create_symref: modernize variable names
In reply to Jeff KingOnce upon a time, create_symref() was used only to point HEAD at a branch name, and the variable names reflect that (e.g., calling the path git_HEAD). However, it is much more generic these days (and has been for some time). Let's update the variable names to make it easier to follow:
- ref_target is now just ref, matching the declaration in cache.h (and hopefully making it clear that it is the symref itself, and not the target).
- git_HEAD is now ref_path; the on-disk path corresponding to ref.
- refs_heads_master is now just target; i.e., what the symref points at. This term also matches what is in the symlink(2) manpage (at least on Linux).
- the buffer to hold the symref file's contents was simply called ref. It's now buf (admittedly also generic, but at least not actively introducing confusion with the other variable holding the refname).
---
This could actually be squashed in with patch 4, as most of the changed instances just go away. I did find the new names easier to work with, though, so maybe they make the diff for that patch easier.
refs.h | 2 +- refs/files-backend.c | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 22 deletions(-)
refs.h +1 -1
@@ -292,7 +292,7 @@ extern char *shorten_unambiguous_ref(const char *refname, int strict);/** rename ref, return 0 on success **/ extern int rename_ref (const char *oldref,const char *newref,const char *logmsg); -extern int create_symref (const char *ref,const char *refs_heads_master,const char *logmsg); +extern int create_symref (const char *ref,const char *target,const char *logmsg);enum action_on_err {UPDATE_REFS_MSG_ON_ERR ,
refs/files-backend.c +20 -21
91 lines not on this page · raw message
Signature · 1 line
2.7.0.rc1.350.g9acc0f4
Re: [PATCH 3/4] create_symref: modernize variable names
Quoted 9 lines · Jeff King
Once upon a time,
create_symref()was used only to point HEAD at a branch name, and the variable names reflect that (e.g., calling the pathgit_HEAD). However, it is much more generic these days (and has been for some time). Let's update the variable names to make it easier to follow:-
ref_targetis now justref, matching the declaration incache.h(and hopefully making it clear that it is the symref itself, and not the target).
I've been trying to name variables that hold reference names "refname" to distinguish them clearly from other representations of references, like "struct ref_entry *".
Quoted 1 line
[...]
Otherwise LGTM.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 3/4] create_symref: modernize variable names
Quoted 7 lines · Michael Haggerty
Quoted 6 lines · Jeff King
Once upon a time,
create_symref()was used only to point HEAD at a branch name, and the variable names reflect that (e.g., calling the pathgit_HEAD). However, it is much more generic these days (and has been for some time). Let's update the variable names to make it easier to follow:
Quoted 7 lines
Quoted 3 lines
-
ref_targetis now justref, matching the declaration incache.h(and hopefully making it clear that it is the symref itself, and not the target).I've been trying to name variables that hold reference names "refname" to distinguish them clearly from other representations of references, like "struct
ref_entry*".
That makes sense. I've tweaked this patch to use "refname", and carried through the change to the following patch.
-Peff
PATCH 4/4 create_symref: use existing ref-lock code
In reply to Jeff KingThe create_symref() function predates the existence of "struct lock_file", let alone the more recent "struct ref_lock". Instead, it just does its own manual dot-locking. Besides being more code, this has a few downsides:
- if git is interrupted while holding the lock, we don't clean up the lockfile
- we don't do the usual directory/filename conflict check. So you can sometimes create a symref "refs/heads/foo/bar", even if "refs/heads/foo" exists (namely, if the refs are packed and we do not hit the d/f conflict in the filesystem).
This patch refactors create_symref() to use the "struct ref_lock" interface, which handles both of these things. There are a few bonus cleanups that come along with it:
- we leaked ref_path in some error cases
- the symref contents were stored in a fixed-size buffer, putting an artificial (albeit large) limitation on the length of the refname. We now write through fprintf, and handle refnames of any size.
- we called adjust_shared_perm only after the file was renamed into place, creating a potential race with readers in a shared repository. Now we fix the permissions first, and commit only if that succeeded. This also makes the update atomic with respect to our exit code (whereas previously, we might report failure even though we updated the ref).
- the legacy prefer_symlink_refs path did not do any locking at all. Admittedly, it is not atomic from a reader's perspective (and it cannot be; it has to unlink and then symlink, creating a race), but at least it cannot conflict with other writers now.
- the result of this patch is hopefully more readable. It eliminates three goto labels. Two were for error checking that is now simplified, and the third was to reach shared code that has been pulled into its own function.
--- refs/files-backend.c | 113 +++++++++++++++++++++++++----------------------- t/t1401-symbolic-ref.sh | 8 ++++ 2 files changed, 66 insertions(+), 55 deletions(-)
refs/files-backend.c +58 -55
@@ -2811,74 +2811,77 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -int create_symref (const char *ref,const char *target,const char *logmsg) +static int create_ref_symlink (struct ref_lock *lock,const char *target) { -char *lockpath =NULL ; -char buf[1000 ]; -int fd, len, written; -char *ref_path =git_pathdup ("%s" , ref); -unsigned char old_sha1[20 ], new_sha1[20 ]; -struct strbuf err =STRBUF_INIT ; - -if (logmsg &&read_ref (ref, old_sha1)) -hashclr (old_sha1); - -if (safe_create_leading_directories (ref_path) <0 ) -return error ("unable to create directory for %s" , ref_path); - +int ret =-1 ;#ifndef NO_SYMLINK_HEAD -if (prefer_symlink_refs) { -unlink (ref_path); -if (!symlink (target, ref_path)) - goto done; +char *ref_path =get_locked_file_path (lock->lk); +unlink (ref_path); + ret =symlink (target, ref_path); +free (ref_path); + +if (ret)fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); - }#endif +return ret; +} - len =snprintf (buf,sizeof (buf),"ref: %s\n" , target); -if (sizeof (buf) <= len) { -error ("refname too long: %s" , target); - goto error_free_return; - } - lockpath =mkpathdup ("%s.lock" , ref_path); - fd =open (lockpath,O_CREAT |O_EXCL |O_WRONLY ,0666 ); -if (fd <0 ) { -error ("Unable to open %s for writing" , lockpath); - goto error_free_return; - } - written =write_in_full (fd, buf, len); -if (close (fd) !=0 || written != len) { -error ("Unable to write to %s" , lockpath); - goto error_unlink_return; - } -if (rename (lockpath, ref_path) <0 ) { -error ("Unable to create %s" , ref_path); - goto error_unlink_return; - } -if (adjust_shared_perm (ref_path)) { -error ("Unable to fix permissions on %s" , lockpath); - error_unlink_return: -unlink_or_warn (lockpath); - error_free_return: -free (lockpath); -free (ref_path); -return -1 ; - } -free (lockpath); - -#ifndef NO_SYMLINK_HEAD - done: -#endif +static void update_symref_reflog (struct ref_lock *lock,const char *ref, +const char *target,const char *logmsg) +{ +struct strbuf err =STRBUF_INIT ; +unsigned char new_sha1[20 ];if (logmsg && !read_ref (target, new_sha1) && -log_ref_write (ref, old_sha1, new_sha1, logmsg,0 , &err)) { +log_ref_write (ref, lock->old_oid.hash, new_sha1, logmsg,0 , &err)) {error ("%s" , err.buf);strbuf_release (&err); } +} -free (ref_path); +static int create_symref_locked (struct ref_lock *lock,const char *ref, +const char *target,const char *logmsg) +{ +if (prefer_symlink_refs && !create_ref_symlink (lock, target)) { +update_symref_reflog (lock, ref, target, logmsg); +return 0 ; + } + +if (!fdopen_lock_file (lock->lk,"w" )) +return error ("unable to fdopen %s: %s" , + lock->lk->tempfile.filename.buf,strerror (errno)); + +if (adjust_shared_perm (lock->lk->tempfile.filename.buf)) +return error ("unable to fix permissions on %s: %s" , + lock->lk->tempfile.filename.buf,strerror (errno)); + +/* no error check; commit_ref will check ferror */ +fprintf (lock->lk->tempfile.fp,"ref: %s\n" , target); +if (commit_ref (lock) <0 ) +return error ("unable to write symref for %s: %s" , ref, +strerror (errno)); +update_symref_reflog (lock, ref, target, logmsg);return 0 ; } +int create_symref (const char *ref,const char *target,const char *logmsg) +{ +struct strbuf err =STRBUF_INIT ; +struct ref_lock *lock; +int ret; + + lock =lock_ref_sha1_basic (ref,NULL ,NULL ,NULL ,REF_NODEREF ,NULL , + &err); +if (!lock) { +error ("%s" , err.buf); +strbuf_release (&err); +return -1 ; + } + + ret =create_symref_locked (lock, ref, target, logmsg); +unlock_ref (lock); +return ret; +} +int reflog_exists (const char *refname) {struct stat st;
t/t1401-symbolic-ref.sh +8 -0
@@ -114,4 +114,12 @@ test_expect_success 'symbolic-ref writes reflog entry' ' test_cmp expect actual ' +test_expect_success 'symbolic-ref does not create ref d/f conflicts' ' + git checkout -b df && + test_commit df && + test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df && + git pack-refs --all --prune && + test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df +' + test_done
Signature · 1 line
2.7.0.rc1.350.g9acc0f4
Re: [PATCH 4/4] create_symref: use existing ref-lock code
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2833
Quoted 5 lines · Jeff King
#ifndef
NO_SYMLINK_HEADrefs/files-backend.c +0
-4-if (prefer_symlink_refs) { -unlink (ref_path); -if (!symlink (target, ref_path)) - goto done;
I see that the original was sloppy (most certainly my bad) ...
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2819
Quoted 2 lines
refs/files-backend.c +2
-0+char *ref_path =get_locked_file_path (lock->lk); +unlink (ref_path);
... and you inherited that. I see a few seemingly related helpers in wrapper.c, but none looks useful in this context X-<.
if (unlink_or_warn(ref_path))
return -1;is close enough, but it still lets the caller fallback to textual symref.
Quoted 7 lines
refs/files-backend.c +4
-1+ ret =symlink (target, ref_path); +free (ref_path); + +if (ret)fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); - }#endif
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2871
Quoted 10 lines
refs/files-backend.c +2
-7+return ret; +} - len =snprintf (buf,sizeof (buf),"ref: %s\n" , target); -... -free (lockpath); - -#ifndef NO_SYMLINK_HEAD - done: -#endif
This huge block is worth removing ;-) Thanks.
Re: [PATCH 4/4] create_symref: use existing ref-lock code
Quoted 16 lines · Junio C Hamano
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2833
Quoted 5 lines · Jeff King
#ifndef
NO_SYMLINK_HEADrefs/files-backend.c +0
-4-if (prefer_symlink_refs) { -unlink (ref_path); -if (!symlink (target, ref_path)) - goto done;I see that the original was sloppy (most certainly my bad) ...
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2819
Quoted 2 lines
refs/files-backend.c +2
-0+char *ref_path =get_locked_file_path (lock->lk); +unlink (ref_path);... and you inherited that. I see a few seemingly related helpers in
wrapper.c, but none looks useful in this context X-<.
Quoted 5 lines
if (unlink_or_warn(ref_path)) return -1;is close enough, but it still lets the caller fallback to textual symref.
I don't think the original is _wrong_; it's meant to be "unlink if needed", and the symlink call is what we really care about (if our unlink failed for anything but ENOENT, the symlink will, too). But I agree unlink_or_warn should do that and give us a nice warning (and cover up ENOENT). To fall through, I think we just want (in the original):
if (!unlink_or_warn(ref_path) && !symlink(target, ref_path))
goto done;
and in mine that becomes:
int ret = -1;
...
if (!unlink_or_warn(ref_path) && !symlink(target, ref_path))
ret = 0;
I must confess I considered a follow-on patch to drop the prefer_symlink_refs code path completely. I'm surprised it all still works with packed-refs, etc, but resolve_ref does take special care to use readlink().
-Peff
Re: [PATCH 4/4] create_symref: use existing ref-lock code
In reply to Jeff KingQuoted 81 lines · Jeff King
The
create_symref()function predates the existence of "structlock_file", let alone the more recent "structref_lock". Instead, it just does its own manual dot-locking. Besides being more code, this has a few downsides:- if git is interrupted while holding the lock, we don't clean up the lockfile
- we don't do the usual directory/filename conflict check. So you can sometimes create a symref "refs/heads/foo/bar", even if "refs/heads/foo" exists (namely, if the refs are packed and we do not hit the d/f conflict in the filesystem).
This patch refactors
create_symref()to use the "structref_lock" interface, which handles both of these things. There are a few bonus cleanups that come along with it:- we leaked
ref_pathin some error cases- the symref contents were stored in a fixed-size buffer, putting an artificial (albeit large) limitation on the length of the refname. We now write through fprintf, and handle refnames of any size.
- we called
adjust_shared_permonly after the file was renamed into place, creating a potential race with readers in a shared repository. Now we fix the permissions first, and commit only if that succeeded. This also makes the update atomic with respect to our exit code (whereas previously, we might report failure even though we updated the ref).- the legacy
prefer_symlink_refspath did not do any locking at all. Admittedly, it is not atomic from a reader's perspective (and it cannot be; it has to unlink and then symlink, creating a race), but at least it cannot conflict with other writers now.- the result of this patch is hopefully more readable. It eliminates three goto labels. Two were for error checking that is now simplified, and the third was to reach shared code that has been pulled into its own function.
Signed-off-by Jeff King <[email protected]>--- refs/files-backend.c | 113 +++++++++++++++++++++++++----------------------- t/t1401-symbolic-ref.sh | 8 ++++ 2 files changed, 66 insertions(+), 55 deletions(-)refs/files-backend.c +3
-18@@ -2811,74 +2811,77 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -int create_symref (const char *ref,const char *target,const char *logmsg) +static int create_ref_symlink (struct ref_lock *lock,const char *target) { -char *lockpath =NULL ; -char buf[1000 ]; -int fd, len, written; -char *ref_path =git_pathdup ("%s" , ref); -unsigned char old_sha1[20 ], new_sha1[20 ]; -struct strbuf err =STRBUF_INIT ; - -if (logmsg &&read_ref (ref, old_sha1)) -hashclr (old_sha1); - -if (safe_create_leading_directories (ref_path) <0 ) -return error ("unable to create directory for %s" , ref_path); - +int ret =-1 ;#ifndef NO_SYMLINK_HEAD -if (prefer_symlink_refs) { -unlink (ref_path); -if (!symlink (target, ref_path)) - goto done; +char *ref_path =get_locked_file_path (lock->lk);
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2827
Quoted 10 lines
refs/files-backend.c +7
-1+unlink (ref_path); + ret =symlink (target, ref_path); +free (ref_path); + +if (ret)fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); - }#endif +return ret; +}
This function is racy. A reader might see no reference at all in the moment between the unlink() and the symlink(). Moreover, if this process is killed at that moment, the symbolic ref would be gone forever.
I think that the semantics of rename() would allow this race to be fixed, though, since symlink() doesn't have the analogue of O_CREAT|O_EXCL, one would need a lockfile *and* a second temporary filename under which the new symlink is originally created.
However, this race has always been here, and symlink-based symrefs are obsolete, so it's probably not worth fixing.
Quoted 53 lines
refs/files-backend.c +12
-36- len =snprintf (buf,sizeof (buf),"ref: %s\n" , target); -if (sizeof (buf) <= len) { -error ("refname too long: %s" , target); - goto error_free_return; - } - lockpath =mkpathdup ("%s.lock" , ref_path); - fd =open (lockpath,O_CREAT |O_EXCL |O_WRONLY ,0666 ); -if (fd <0 ) { -error ("Unable to open %s for writing" , lockpath); - goto error_free_return; - } - written =write_in_full (fd, buf, len); -if (close (fd) !=0 || written != len) { -error ("Unable to write to %s" , lockpath); - goto error_unlink_return; - } -if (rename (lockpath, ref_path) <0 ) { -error ("Unable to create %s" , ref_path); - goto error_unlink_return; - } -if (adjust_shared_perm (ref_path)) { -error ("Unable to fix permissions on %s" , lockpath); - error_unlink_return: -unlink_or_warn (lockpath); - error_free_return: -free (lockpath); -free (ref_path); -return -1 ; - } -free (lockpath); - -#ifndef NO_SYMLINK_HEAD - done: -#endif +static void update_symref_reflog (struct ref_lock *lock,const char *ref, +const char *target,const char *logmsg) +{ +struct strbuf err =STRBUF_INIT ; +unsigned char new_sha1[20 ];if (logmsg && !read_ref (target, new_sha1) && -log_ref_write (ref, old_sha1, new_sha1, logmsg,0 , &err)) { +log_ref_write (ref, lock->old_oid.hash, new_sha1, logmsg,0 , &err)) {error ("%s" , err.buf);strbuf_release (&err); } +} -free (ref_path); +static int create_symref_locked (struct ref_lock *lock,const char *ref, +const char *target,const char *logmsg) +{ +if (prefer_symlink_refs && !create_ref_symlink (lock, target)) { +update_symref_reflog (lock, ref, target, logmsg);
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2855
Quoted 10 lines
refs/files-backend.c +10
-0+return 0 ; + } + +if (!fdopen_lock_file (lock->lk,"w" )) +return error ("unable to fdopen %s: %s" , + lock->lk->tempfile.filename.buf,strerror (errno)); + +if (adjust_shared_perm (lock->lk->tempfile.filename.buf)) +return error ("unable to fix permissions on %s: %s" , + lock->lk->tempfile.filename.buf,strerror (errno));
You can skip this step. lock_file() already calls adjust_shared_perm().
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2862
Quoted 6 lines
refs/files-backend.c +6
-0+/* no error check; commit_ref will check ferror */ +fprintf (lock->lk->tempfile.fp,"ref: %s\n" , target); +if (commit_ref (lock) <0 ) +return error ("unable to write symref for %s: %s" , ref, +strerror (errno)); +update_symref_reflog (lock, ref, target, logmsg);
Here is another problem that didn't originate with your changes:
The reflog should be written while holding the reference lock, to prevent two processes' trying to write new entries at the same time.
I think the problem would be solved if you move the call to update_symref_reflog() above the call to commit_ref().
Granted, this could case a reflog entry to be written for a reference update whose commit fails, but that's also a risk for non-symbolic references. Fixing this residual problem would require the ability to roll back reflog changes.
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2862
Quoted 3 lines
refs/files-backend.c +0
-0return 0 ; }
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 4/4] create_symref: use existing ref-lock code
Quoted 18 lines · Michael Haggerty
Quoted 1 line
+static int
create_ref_symlink(struct ref_lock *lock, const char *target)[...]
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2820
Quoted 3 lines
refs/files-backend.c +3
-0+char *ref_path =get_locked_file_path (lock->lk); +unlink (ref_path); + ret =symlink (target, ref_path);[...]
This function is racy. A reader might see no reference at all in the moment between the
unlink()and thesymlink(). Moreover, if this process is killed at that moment, the symbolic ref would be gone forever.I think that the semantics of
rename()would allow this race to be fixed, though, sincesymlink()doesn't have the analogue ofO_CREAT|O_EXCL, one would need a lockfile *and* a second temporary filename under which the new symlink is originally created.However, this race has always been here, and symlink-based symrefs are obsolete, so it's probably not worth fixing.
Quoted 5 lines · Yeah. In the commit message I
Quoted 5 lines
- the legacy
prefer_symlink_refspath did not do any locking at all. Admittedly, it is not atomic from a reader's perspective (and it cannot be; it has to unlink and then symlink, creating a race), but at least it cannot conflict with other writers now.
but I think you are right that we could do tricks with rename() on the symlink. That is in POSIX, though I wouldn't be surprised if it misbehaves on some obscure systems. Given that using symlinks is only triggered by an undocumented (!) option that presumably very few people use, I'm inclined to leave it as-is.
I was actually tempted to rip it out, as the option is mostly a hack from 2006:
http://article.gmane.org/gmane.comp.version-control.git/19402
It was done to let people bisect old kernel trees whose build system _depends_ on .git/HEAD being a symlink. My thought is:
- people are a lot less likely to bisect back that far anymore
- ...and if they do, their build system will be totally broken by the new ref-backend stuff
- ...and the right solution is not to put a hack in git, but for the bisecting user to "fix up" the tree before testing (by munging the symlink themselves, or applying a patch to the build system to use git rev-parse). This matches what people have to do for every other type of weird "the old version doesn't quite build" incompatibility that has nothing to do with git.
But that should probably come as a separate patch (it affects this only in that it makes this patch simpler to do that cleanup first :) ).
Quoted 5 lines
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2855
Quoted 3 lines
refs/files-backend.c +3
-0+if (adjust_shared_perm (lock->lk->tempfile.filename.buf)) +return error ("unable to fix permissions on %s: %s" , + lock->lk->tempfile.filename.buf,strerror (errno));You can skip this step.
lock_file()already callsadjust_shared_perm().
Thanks, fixed.
Quoted 12 lines
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2862
Quoted 6 lines
refs/files-backend.c +6
-0+/* no error check; commit_ref will check ferror */ +fprintf (lock->lk->tempfile.fp,"ref: %s\n" , target); +if (commit_ref (lock) <0 ) +return error ("unable to write symref for %s: %s" , ref, +strerror (errno)); +update_symref_reflog (lock, ref, target, logmsg);Here is another problem that didn't originate with your changes:
The reflog should be written while holding the reference lock, to prevent two processes' trying to write new entries at the same time.
Quoted 7 lines
I think the problem would be solved if you move the call to
update_symref_reflog()above the call tocommit_ref().Granted, this could case a reflog entry to be written for a reference update whose commit fails, but that's also a risk for non-symbolic references. Fixing this residual problem would require the ability to roll back reflog changes.
Thanks, I agree with your reasoning (and especially that we should err on the same side that normal ref-writing does). I'll do it in a follow-on patch, though, to make it more clear what is going on.
-Peff
Re: [PATCH 4/4] create_symref: use existing ref-lock code
Quoted 3 lines · Jeff King
Given that using symlinks is only
triggered by an undocumented (!) option that presumably very few people use, I'm inclined to leave it as-is.
Sorry, I am wrong here. We do document core.preferSymlinkRefs. I missed it because my grep was case-sensitive. :-/
-Peff
v2 cover improve symbolic-ref robustness
In reply to Jeff KingQuoted 3 lines · Jeff King
I noticed that an interrupt "git symbolic-ref" will not clean up "HEAD.lock". So I started this series as an attempt to convert
create_symref()to "structlock_file" to get the usual tempfile cleanup.
Here's version 2, based on comments from Michael. The first two patches were picked out separately for jk/symbolic-ref-maint, so I've dropped them here (so 1+2 here are the original 3+4).
The other differences from v1 are:
- use "refname" instead of "ref" to match surrounding code
- drop adjust_shared_perm, as lockfile does it for us
- adjust reflog writing order (done in a new patch)
The patches are:
[1/3]: create_symref: modernize variable names [2/3]: create_symref: use existing ref-lock code [3/3]: create_symref: write reflog while holding lock
-Peff
PATCH v2 1/3 create_symref: modernize variable names
Once upon a time, create_symref() was used only to point HEAD at a branch name, and the variable names reflect that (e.g., calling the path git_HEAD). However, it is much more generic these days (and has been for some time). Let's update the variable names to make it easier to follow:
- ref_target is now just refname. This is closer to the ref that is already in cache.h, but with the extra twist that "name" makes it clear this is the name and not a ref struct. Dropping "target" hopefully makes it clear that we are talking about the symref itself, not what it points to.
- git_HEAD is now ref_path; the on-disk path corresponding to ref.
- refs_heads_master is now just target; i.e., what the symref points at. This term also matches what is in the symlink(2) manpage (at least on Linux).
- the buffer to hold the symref file's contents was simply called ref. It's now buf (admittedly also generic, but at least not actively introducing confusion with the other variable holding the refname).
--- refs.h | 2 +- refs/files-backend.c | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 22 deletions(-)
refs.h +1 -1
@@ -292,7 +292,7 @@ extern char *shorten_unambiguous_ref(const char *refname, int strict);/** rename ref, return 0 on success **/ extern int rename_ref (const char *oldref,const char *newref,const char *logmsg); -extern int create_symref (const char *ref,const char *refs_heads_master,const char *logmsg); +extern int create_symref (const char *refname,const char *target,const char *logmsg);enum action_on_err {UPDATE_REFS_MSG_ON_ERR ,
refs/files-backend.c +20 -21
91 lines not on this page · raw message
Signature · 1 line
2.7.0.rc2.368.g1cbb535
PATCH v2 2/3 create_symref: use existing ref-lock code
In reply to Jeff KingThe create_symref() function predates the existence of "struct lock_file", let alone the more recent "struct ref_lock". Instead, it just does its own manual dot-locking. Besides being more code, this has a few downsides:
- if git is interrupted while holding the lock, we don't clean up the lockfile
- we don't do the usual directory/filename conflict check. So you can sometimes create a symref "refs/heads/foo/bar", even if "refs/heads/foo" exists (namely, if the refs are packed and we do not hit the d/f conflict in the filesystem).
This patch refactors create_symref() to use the "struct ref_lock" interface, which handles both of these things. There are a few bonus cleanups that come along with it:
- we leaked ref_path in some error cases
- the symref contents were stored in a fixed-size buffer, putting an artificial (albeit large) limitation on the length of the refname. We now write through fprintf, and handle refnames of any size.
- we called adjust_shared_perm only after the file was renamed into place, creating a potential race with readers in a shared repository. The lockfile code now handles this when creating the lockfile, making it atomic.
- the legacy prefer_symlink_refs path did not do any locking at all. Admittedly, it is not atomic from a reader's perspective (as it unlinks and re-creates the symlink to overwrite), but at least it cannot conflict with other writers now.
- the result of this patch is hopefully more readable. It eliminates three goto labels. Two were for error checking that is now simplified, and the third was to reach shared code that has been pulled into its own function.
--- refs/files-backend.c | 109 ++++++++++++++++++++++++------------------------ t/t1401-symbolic-ref.sh | 8 ++++ 2 files changed, 62 insertions(+), 55 deletions(-)
refs/files-backend.c +54 -55
@@ -2811,74 +2811,73 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -int create_symref (const char *refname,const char *target,const char *logmsg) +static int create_ref_symlink (struct ref_lock *lock,const char *target) { -char *lockpath =NULL ; -char buf[1000 ]; -int fd, len, written; -char *ref_path =git_pathdup ("%s" , refname); -unsigned char old_sha1[20 ], new_sha1[20 ]; -struct strbuf err =STRBUF_INIT ; - -if (logmsg &&read_ref (refname, old_sha1)) -hashclr (old_sha1); - -if (safe_create_leading_directories (ref_path) <0 ) -return error ("unable to create directory for %s" , ref_path); - +int ret =-1 ;#ifndef NO_SYMLINK_HEAD -if (prefer_symlink_refs) { -unlink (ref_path); -if (!symlink (target, ref_path)) - goto done; +char *ref_path =get_locked_file_path (lock->lk); +unlink (ref_path); + ret =symlink (target, ref_path); +free (ref_path); + +if (ret)fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); - }#endif +return ret; +} - len =snprintf (buf,sizeof (buf),"ref: %s\n" , target); -if (sizeof (buf) <= len) { -error ("refname too long: %s" , target); - goto error_free_return; - } - lockpath =mkpathdup ("%s.lock" , ref_path); - fd =open (lockpath,O_CREAT |O_EXCL |O_WRONLY ,0666 ); -if (fd <0 ) { -error ("Unable to open %s for writing" , lockpath); - goto error_free_return; - } - written =write_in_full (fd, buf, len); -if (close (fd) !=0 || written != len) { -error ("Unable to write to %s" , lockpath); - goto error_unlink_return; - } -if (rename (lockpath, ref_path) <0 ) { -error ("Unable to create %s" , ref_path); - goto error_unlink_return; - } -if (adjust_shared_perm (ref_path)) { -error ("Unable to fix permissions on %s" , lockpath); - error_unlink_return: -unlink_or_warn (lockpath); - error_free_return: -free (lockpath); -free (ref_path); -return -1 ; - } -free (lockpath); - -#ifndef NO_SYMLINK_HEAD - done: -#endif +static void update_symref_reflog (struct ref_lock *lock,const char *refname, +const char *target,const char *logmsg) +{ +struct strbuf err =STRBUF_INIT ; +unsigned char new_sha1[20 ];if (logmsg && !read_ref (target, new_sha1) && -log_ref_write (refname, old_sha1, new_sha1, logmsg,0 , &err)) { +log_ref_write (refname, lock->old_oid.hash, new_sha1, logmsg,0 , &err)) {error ("%s" , err.buf);strbuf_release (&err); } +} -free (ref_path); +static int create_symref_locked (struct ref_lock *lock,const char *refname, +const char *target,const char *logmsg) +{ +if (prefer_symlink_refs && !create_ref_symlink (lock, target)) { +update_symref_reflog (lock, refname, target, logmsg); +return 0 ; + } + +if (!fdopen_lock_file (lock->lk,"w" )) +return error ("unable to fdopen %s: %s" , + lock->lk->tempfile.filename.buf,strerror (errno)); + +/* no error check; commit_ref will check ferror */ +fprintf (lock->lk->tempfile.fp,"ref: %s\n" , target); +if (commit_ref (lock) <0 ) +return error ("unable to write symref for %s: %s" , refname, +strerror (errno)); +update_symref_reflog (lock, refname, target, logmsg);return 0 ; } +int create_symref (const char *refname,const char *target,const char *logmsg) +{ +struct strbuf err =STRBUF_INIT ; +struct ref_lock *lock; +int ret; + + lock =lock_ref_sha1_basic (refname,NULL ,NULL ,NULL ,REF_NODEREF ,NULL , + &err); +if (!lock) { +error ("%s" , err.buf); +strbuf_release (&err); +return -1 ; + } + + ret =create_symref_locked (lock, refname, target, logmsg); +unlock_ref (lock); +return ret; +} +int reflog_exists (const char *refname) {struct stat st;
t/t1401-symbolic-ref.sh +8 -0
@@ -114,4 +114,12 @@ test_expect_success 'symbolic-ref writes reflog entry' ' test_cmp expect actual ' +test_expect_success 'symbolic-ref does not create ref d/f conflicts' ' + git checkout -b df && + test_commit df && + test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df && + git pack-refs --all --prune && + test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df +' + test_done
Signature · 1 line
2.7.0.rc2.368.g1cbb535
PATCH v2 3/3 create_symref: write reflog while holding lock
In reply to Jeff KingWe generally hold a lock on the matching ref while writing to its reflog; this prevents two simultaneous writers from clobbering each other's reflog lines (it does not even have to be two symref updates; because we don't hold the lock, we could race with somebody writing to the pointed-to ref via HEAD, for example).
We can fix this by writing the reflog before we commit the lockfile. This runs the risk of writing the reflog but failing the final rename(), but at least we now err on the same side as the rest of the ref code.
--- refs/files-backend.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
refs/files-backend.c +2 -1
@@ -2850,12 +2850,13 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,return error ("unable to fdopen %s: %s" , lock->lk->tempfile.filename.buf,strerror (errno)); +update_symref_reflog (lock, refname, target, logmsg); +/* no error check; commit_ref will check ferror */ fprintf (lock->lk->tempfile.fp,"ref: %s\n" , target);if (commit_ref (lock) <0 )return error ("unable to write symref for %s: %s" , refname,strerror (errno)); -update_symref_reflog (lock, refname, target, logmsg);return 0 ; }
Signature · 1 line
2.7.0.rc2.368.g1cbb535
[RFC/PATCH 4/3] create_symref: drop support for writing symbolic links
In reply to Jeff KingI'm iffy on this one, just because it drops a user-visible feature with no deprecation. For the reasons given below, I doubt anybody is using it for the original intended purpose, but maybe there is some crazy person whose workflow revolves around core.preferSymlinkRefs.
A conservative choice would probably be to issue a deprecation warning when we see it defined, wait a few versions, and then apply the patch below.
-- >8 --
Long ago, in 2fabd21 (Disable USE_SYMLINK_HEAD by default, 2005-11-15), we switched git's default behavior to writing text symrefs instead of symbolic links. Any scripts accustomed to looking directly at .git/HEAD were updated to use rev-parse instead. The Linux kernel's setlocalversion script was one, and it was fixed in 117a93d (kbuild: Use git in scripts/setlocalversion, 2006-01-04).
However, the problem still happened when bisecting the kernel; pre-117a93d kernels would not build properly with a newer git, because they wanted to look directly at HEAD. To solve this, we added 9f0bb90 (core.prefersymlinkrefs: use symlinks for .git/HEAD, 2006-05-02), which lets the user turn on the old behavior, theoretically letting you bisect older kernel history.
But there are a few complications with this solution:
- packed-refs means you are limited in what you can do with .git/HEAD. If it is a symlink, you may readlink it to see where it points, but you cannot necessarily cat .git/HEAD to get the sha1, as the pointed-to ref may be packed.
In particular, this means that the pre-117a93d kbuild script would sometimes work and sometimes not.
- These days, we bisect on a detached HEAD. So during bisection, .git/HEAD _is_ a regular file with the sha1, and it works to cat it, whether or not you set core.preferSymlinkRefs.
Such scripts will all be broken again if we move to alternate ref backends. They should have learned to use rev-parse long ago, and it is only bisecting ancient history that is a problem.
Now that almost ten years have passed, it seems less likely that developers will bisect so far back in history. And moreover, this is but one of many possible problems developers run into when trying to build versions. The standard solution is to apply a "fixup" patch or other workaround while test-building the project, and that would work here, too.
This patch therefore drops core.preferSymlinkRefs completely. There are a few reasons to want to do so:
1. It drops some code that is probably exercised very rarely.
2. The symlink code is not up to the same standards as the rest of the ref code. In particular, it is racy with respect to simultaneous readers and writers.
3. If we want to eventually drop the symlink-reading code, this is a good first step to deprecating it.
--- Documentation/config.txt | 6 ------ cache.h | 1 - config.c | 5 ----- contrib/completion/git-completion.bash | 1 - environment.c | 1 - refs/files-backend.c | 20 -------------------- t/t7201-co.sh | 12 ------------ t/t9903-bash-prompt.sh | 9 --------- 8 files changed, 55 deletions(-)
Documentation/config.txt +0 -6
@@ -433,12 +433,6 @@ CIFS/Microsoft Windows. + False by default. -core.preferSymlinkRefs:: - Instead of the default "symref" format for HEAD - and other symbolic reference files, use symbolic links. - This is sometimes needed to work with old scripts that - expect HEAD to be a symbolic link. - core.bare:: If true this repository is assumed to be 'bare' and has no working directory associated with it. If this is the case a
cache.h +0 -1
@@ -625,7 +625,6 @@ extern int has_symlinks;extern int minimum_abbrev, default_abbrev;extern int ignore_case;extern int assume_unchanged; -extern int prefer_symlink_refs;extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int warn_on_object_refname_ambiguity;
config.c +0 -5
@@ -726,11 +726,6 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } -if (!strcmp (var,"core.prefersymlinkrefs" )) { - prefer_symlink_refs =git_config_bool (var, value); -return 0 ; - } -if (!strcmp (var,"core.logallrefupdates" )) { log_all_ref_updates =git_config_bool (var, value);return 0 ;
contrib/completion/git-completion.bash +0 -1
@@ -2046,7 +2046,6 @@ _git_config () core.packedGitLimit core.packedGitWindowSize core.pager - core.preferSymlinkRefs core.preloadindex core.quotepath core.repositoryFormatVersion
environment.c +0 -1
@@ -19,7 +19,6 @@ int has_symlinks = 1;int minimum_abbrev =4 , default_abbrev =7 ;int ignore_case;int assume_unchanged; -int prefer_symlink_refs;int is_bare_repository_cfg =-1 ;/* unspecified */ int log_all_ref_updates =-1 ;/* unspecified */ int warn_ambiguous_refs =1 ;
refs/files-backend.c +0 -20
@@ -2811,21 +2811,6 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -static int create_ref_symlink (struct ref_lock *lock,const char *target) -{ -int ret =-1 ; -#ifndef NO_SYMLINK_HEAD -char *ref_path =get_locked_file_path (lock->lk); -unlink (ref_path); - ret =symlink (target, ref_path); -free (ref_path); - -if (ret) -fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); -#endif -return ret; -} -static void update_symref_reflog (struct ref_lock *lock,const char *refname,const char *target,const char *logmsg) {
@@ -2841,11 +2826,6 @@ static void update_symref_reflog(struct ref_lock *lock, const char *refname,static int create_symref_locked (struct ref_lock *lock,const char *refname,const char *target,const char *logmsg) { -if (prefer_symlink_refs && !create_ref_symlink (lock, target)) { -update_symref_reflog (lock, refname, target, logmsg); -return 0 ; - } -if (!fdopen_lock_file (lock->lk,"w" ))return error ("unable to fdopen %s: %s" , lock->lk->tempfile.filename.buf,strerror (errno));
t/t7201-co.sh +0 -12
@@ -427,18 +427,6 @@ test_expect_success 'checkout w/--track from tag fails' ' test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)" ' -test_expect_success 'detach a symbolic link HEAD' ' - git checkout master && - git config --bool core.prefersymlinkrefs yes && - git checkout side && - git checkout master && - it=$(git symbolic-ref HEAD) && - test "z$it" = zrefs/heads/master && - here=$(git rev-parse --verify refs/heads/master) && - git checkout side^ && - test "z$(git rev-parse --verify refs/heads/master)" = "z$here" -' - test_expect_success \ 'checkout with --track fakes a sensible -b <name>' ' git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
t/t9903-bash-prompt.sh +0 -9
@@ -46,15 +46,6 @@ test_expect_success 'prompt - branch name' ' test_cmp expected "$actual" ' -test_expect_success SYMLINKS 'prompt - branch name - symlink symref' ' - printf " (master)" >expected && - test_when_finished "git checkout master" && - test_config core.preferSymlinkRefs true && - git checkout master && - __git_ps1 >"$actual" && - test_cmp expected "$actual" -' - test_expect_success 'prompt - unborn branch' ' printf " (unborn)" >expected && git checkout --orphan unborn &&
Signature · 1 line
2.7.0.rc2.368.g1cbb535
Re: [RFC/PATCH 4/3] create_symref: drop support for writing symbolic links
Quoted 8 lines · Jeff King
refs/files-backend.c +0
-2@@ -2811,21 +2811,6 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -static int create_ref_symlink (struct ref_lock *lock,const char *target) -{
refs/files-backend.c static int commit_ref_update(struct ref_lock *lock, · line 2825
Quoted 10 lines
refs/files-backend.c +0
-10-int ret =-1 ; -#ifndef NO_SYMLINK_HEAD -char *ref_path =get_locked_file_path (lock->lk); -unlink (ref_path); - ret =symlink (target, ref_path); -free (ref_path); - -if (ret) -fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); -#endif
I forgot the build-time knob, which becomes a noop after this patch. If we choose to apply this patch, we'd want to squash this in, too:
Makefile +0 -6
@@ -113,9 +113,6 @@ all:: # # Define NO_SYS_SELECT_H if you don't have sys/select.h. # -# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link. -# Enable it on Windows. By default, symrefs are still used. -# # Define NO_SVN_TESTS if you want to skip time-consuming SVN interoperability # tests. These tests take up a significant amount of the total test time # but are not needed unless you plan to talk to SVN repos.
@@ -1200,9 +1197,6 @@ ifdef FREAD_READS_DIRECTORIES COMPAT_CFLAGS += -DFREAD_READS_DIRECTORIES COMPAT_OBJS += compat/fopen.o endif -ifdef NO_SYMLINK_HEAD - BASIC_CFLAGS += -DNO_SYMLINK_HEAD -endif ifdef GETTEXT_POISON BASIC_CFLAGS += -DGETTEXT_POISON endif
config.mak.uname +0 -3
@@ -169,7 +169,6 @@ ifeq ($(uname_O),Cygwin) NO_STRCASESTR = YesPlease NO_MEMMEM = YesPlease NO_MKSTEMPS = YesPlease - NO_SYMLINK_HEAD = YesPlease NO_IPV6 = YesPlease OLD_ICONV = UnfortunatelyYes # There are conflicting reports about this.
@@ -338,7 +337,6 @@ ifeq ($(uname_S),Windows) NEEDS_CRYPTO_WITH_SSL = YesPlease NO_LIBGEN_H = YesPlease NO_POLL = YesPlease - NO_SYMLINK_HEAD = YesPlease NO_IPV6 = YesPlease NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease
@@ -491,7 +489,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NEEDS_CRYPTO_WITH_SSL = YesPlease NO_LIBGEN_H = YesPlease NO_POLL = YesPlease - NO_SYMLINK_HEAD = YesPlease NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease NO_STRCASESTR = YesPlease
configure.ac +0 -3
@@ -1096,9 +1096,6 @@ GIT_CONF_SUBST([HAVE_BSD_SYSCTL]) # Define USE_PIC if you need the main git objects to be built with -fPIC # in order to build and link perl/Git.so. x86-64 seems to need this. # -# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link. -# Enable it on Windows. By default, symrefs are still used. -# # Define NO_PTHREADS if we do not have pthreads. # # Define PTHREAD_LIBS to the linker flag used for Pthread support.
Re: [RFC/PATCH 4/3] create_symref: drop support for writing symbolic links
In reply to Jeff KingQuoted 3 lines · Jeff King
A conservative choice would probably be to issue a deprecation warning when we see it defined, wait a few versions, and then apply the patch below.
I agree with the analysis below. And I agree that in the ideal world, it would have been better not to add "prefer symlink refs" configuration in the first place. But we do not live in the ideal world, and we already have it, so deprecation would need the usual multi-step process.
Thanks.
Quoted 221 lines
-- >8 --
Long ago, in 2fabd21 (DisableUSE_SYMLINK_HEADby default, 2005-11-15), we switched git's default behavior to writing text symrefs instead of symbolic links. Any scripts accustomed to looking directly at .git/HEAD were updated to userev-parseinstead. The Linux kernel's setlocalversion script was one, and it was fixed in 117a93d (kbuild: Use git inscripts/setlocalversion, 2006-01-04).However, the problem still happened when bisecting the kernel; pre-117a93d kernels would not build properly with a newer git, because they wanted to look directly at HEAD. To solve this, we added 9f0bb90 (core.prefersymlinkrefs: use symlinks for .git/HEAD, 2006-05-02), which lets the user turn on the old behavior, theoretically letting you bisect older kernel history.
But there are a few complications with this solution:
- packed-refs means you are limited in what you can do with .git/HEAD. If it is a symlink, you may
readlinkit to see where it points, but you cannot necessarilycat .git/HEADto get the sha1, as the pointed-to ref may be packed.In particular, this means that the pre-117a93d kbuild script would sometimes work and sometimes not.
- These days, we bisect on a detached HEAD. So during bisection, .git/HEAD _is_ a regular file with the sha1, and it works to
catit, whether or not you set core.preferSymlinkRefs.Such scripts will all be broken again if we move to alternate ref backends. They should have learned to use
rev-parselong ago, and it is only bisecting ancient history that is a problem.Now that almost ten years have passed, it seems less likely that developers will bisect so far back in history. And moreover, this is but one of many possible problems developers run into when trying to build versions. The standard solution is to apply a "fixup" patch or other workaround while test-building the project, and that would work here, too.
This patch therefore drops core.preferSymlinkRefs completely. There are a few reasons to want to do so:
1. It drops some code that is probably exercised very rarely.
2. The symlink code is not up to the same standards as the rest of the ref code. In particular, it is racy with respect to simultaneous readers and writers.
3. If we want to eventually drop the symlink-reading code, this is a good first step to deprecating it.
Signed-off-by Jeff King <[email protected]>--- Documentation/config.txt | 6 ------ cache.h | 1 - config.c | 5 ----- contrib/completion/git-completion.bash | 1 - environment.c | 1 - refs/files-backend.c | 20 -------------------- t/t7201-co.sh | 12 ------------ t/t9903-bash-prompt.sh | 9 --------- 8 files changed, 55 deletions(-)Documentation/config.txt +0
-6@@ -433,12 +433,6 @@ CIFS/Microsoft Windows. + False by default. -core.preferSymlinkRefs:: - Instead of the default "symref" format for HEAD - and other symbolic reference files, use symbolic links. - This is sometimes needed to work with old scripts that - expect HEAD to be a symbolic link. - core.bare:: If true this repository is assumed to be 'bare' and has no working directory associated with it. If this is the case acache.h +0
-1@@ -625,7 +625,6 @@ extern int has_symlinks;extern int minimum_abbrev, default_abbrev;extern int ignore_case;extern int assume_unchanged; -extern int prefer_symlink_refs;extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int warn_on_object_refname_ambiguity;config.c +0
-5@@ -726,11 +726,6 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } -if (!strcmp (var,"core.prefersymlinkrefs" )) { - prefer_symlink_refs =git_config_bool (var, value); -return 0 ; - } -if (!strcmp (var,"core.logallrefupdates" )) { log_all_ref_updates =git_config_bool (var, value);return 0 ;contrib/completion/git-completion.bash +0
-1@@ -2046,7 +2046,6 @@ _git_config () core.packedGitLimit core.packedGitWindowSize core.pager - core.preferSymlinkRefs core.preloadindex core.quotepath core.repositoryFormatVersionenvironment.c +0
-1@@ -19,7 +19,6 @@ int has_symlinks = 1;int minimum_abbrev =4 , default_abbrev =7 ;int ignore_case;int assume_unchanged; -int prefer_symlink_refs;int is_bare_repository_cfg =-1 ;/* unspecified */ int log_all_ref_updates =-1 ;/* unspecified */ int warn_ambiguous_refs =1 ;refs/files-backend.c +0
-20@@ -2811,21 +2811,6 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -static int create_ref_symlink (struct ref_lock *lock,const char *target) -{ -int ret =-1 ; -#ifndef NO_SYMLINK_HEAD -char *ref_path =get_locked_file_path (lock->lk); -unlink (ref_path); - ret =symlink (target, ref_path); -free (ref_path); - -if (ret) -fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); -#endif -return ret; -} -static void update_symref_reflog (struct ref_lock *lock,const char *refname,const char *target,const char *logmsg) {@@ -2841,11 +2826,6 @@ static void update_symref_reflog(struct ref_lock *lock, const char *refname,static int create_symref_locked (struct ref_lock *lock,const char *refname,const char *target,const char *logmsg) { -if (prefer_symlink_refs && !create_ref_symlink (lock, target)) { -update_symref_reflog (lock, refname, target, logmsg); -return 0 ; - } -if (!fdopen_lock_file (lock->lk,"w" ))return error ("unable to fdopen %s: %s" , lock->lk->tempfile.filename.buf,strerror (errno));t/t7201-co.sh +0
-12@@ -427,18 +427,6 @@ test_expect_success 'checkout w/--track from tag fails' ' test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)" ' -test_expect_success 'detach a symbolic link HEAD' ' - git checkout master && - git config --bool core.prefersymlinkrefs yes && - git checkout side && - git checkout master && - it=$(git symbolic-ref HEAD) && - test "z$it" = zrefs/heads/master && - here=$(git rev-parse --verify refs/heads/master) && - git checkout side^ && - test "z$(git rev-parse --verify refs/heads/master)" = "z$here" -' - test_expect_success \ 'checkout with --track fakes a sensible -b <name>' ' git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&t/t9903-bash-prompt.sh +0
-9@@ -46,15 +46,6 @@ test_expect_success 'prompt - branch name' ' test_cmp expected "$actual" ' -test_expect_success SYMLINKS 'prompt - branch name - symlink symref' ' - printf " (master)" >expected && - test_when_finished "git checkout master" && - test_config core.preferSymlinkRefs true && - git checkout master && - __git_ps1 >"$actual" && - test_cmp expected "$actual" -' - test_expect_success 'prompt - unborn branch' ' printf " (unborn)" >expected && git checkout --orphan unborn &&
Re: [RFC/PATCH 4/3] create_symref: drop support for writing symbolic links
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Jeff King
A conservative choice would probably be to issue a deprecation warning when we see it defined, wait a few versions, and then apply the patch below.
I agree with the analysis below. And I agree that in the ideal world, it would have been better not to add "prefer symlink refs" configuration in the first place. But we do not live in the ideal world, and we already have it, so deprecation would need the usual multi-step process.
Here's the first step of that multi-step process. The commit message will look familiar, as the rationale for deprecating is the same as for dropping.
This is on top of what I posted earlier (it obviously could be independent, but I assume we're planning to merge the other bits, and I don't mind holding this topic hostage for a little while to save some annoying merging).
-- >8 --
Subject: [PATCH] create_symref: deprecate support for writing symbolic links
Long ago, in 2fabd21 (Disable USE_SYMLINK_HEAD by default, 2005-11-15), we switched git's default behavior to writing text symrefs instead of symbolic links. Any scripts accustomed to looking directly at .git/HEAD were updated to use rev-parse instead. The Linux kernel's setlocalversion script was one, and it was fixed in 117a93d (kbuild: Use git in scripts/setlocalversion, 2006-01-04).
However, the problem still happened when bisecting the kernel; pre-117a93d would not build properly with a newer git, because they wanted to look directly at HEAD. To solve this, we added 9f0bb90 (core.prefersymlinkrefs: use symlinks for .git/HEAD, 2006-05-02), which lets the user turn on the old behavior, theoretically letting you bisect older kernel history.
But there are a few complications with this solution:
- packed-refs means you are limited in what you can do with .git/HEAD. If it is a symlink, you may readlink it to see where it points, but you cannot necessarily cat .git/HEAD to get the sha1, as the pointed-to ref may be packed.
In particular, this means that the pre-117a93d kbuild script would sometimes work and sometimes not.
- These days, we bisect on a detached HEAD. So during bisection, .git/HEAD _is_ a regular file with the sha1, and it works to cat it, whether or not you set core.preferSymlinkRefs.
Such scripts will all be broken again if we move to alternate ref backends. They should have learned to use rev-parse long ago, and it is only bisecting ancient history that is a problem.
Now that almost ten years have passed, it seems less likely that developers will bisect so far back in history. And moreover, this is but one of many possible problems developers run into when trying to build versions. The standard solution is to apply a "fixup" patch or other workaround while test-building the project, and that would work here, too.
This patch therefore deprecates core.preferSymlinkRefs completely. There are a few reasons to want to do so:
1. It drops some code that is probably exercised very rarely.
2. The symlink code is not up to the same standards as the rest of the ref code. In particular, it is racy with respect to simultaneous readers and writers.
3. If we want to eventually drop the symlink-reading code, this is a good first step to deprecating it.
We print a deprecation warning anytime a symlink is created using this code. That prevents us from spamming the user with multiple warnings from read-only operations, but catches major operations like init, clone, and checkout.
---
There's no advice.* safety valve here. The solution is "fix your script", and the last-ditch effort is "come to the mailing list and tell us why it would be a bad thing to remove the feature".
refs/files-backend.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
refs/files-backend.c +15 -0
@@ -2860,12 +2860,27 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,return 0 ; } +static const char symlink_deprecation_warning[] = +"The core.preferSymlinkRefs configuration option has been\n" +"deprecated and will be removed in a future version of Git. If your\n" +"workflow or script depends on '.git/HEAD' being a symbolic link,\n" +"it should be adjusted to use:\n" +"\n" +" git rev-parse HEAD\n" +"\n" +" git rev-parse --symbolic-full-name HEAD\n" +"\n" +"to get the sha1 or branch name, respectively." ; +int create_symref (const char *refname,const char *target,const char *logmsg) {struct strbuf err =STRBUF_INIT ;struct ref_lock *lock;int ret; +if (prefer_symlink_refs) +warning ("%s" , symlink_deprecation_warning); + lock =lock_ref_sha1_basic (refname,NULL ,NULL ,NULL ,REF_NODEREF ,NULL , &err);if (!lock) {
Signature · 1 line
2.7.0.rc3.367.g09631da
Re: [RFC/PATCH 4/3] create_symref: drop support for writing symbolic links
Quoted 14 lines · Jeff King
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Jeff King
A conservative choice would probably be to issue a deprecation warning when we see it defined, wait a few versions, and then apply the patch below.
I agree with the analysis below. And I agree that in the ideal world, it would have been better not to add "prefer symlink refs" configuration in the first place. But we do not live in the ideal world, and we already have it, so deprecation would need the usual multi-step process.
Quoted 3 lines
Here's the first step of that multi-step process. The commit message will look familiar, as the rationale for deprecating is the same as for dropping.
And here's the second step, which would obviously want to wait a while before being merged.
A third possible step would be removing the reading side from resolve_ref(). But that would want to wait a few more cycles past this one. And I'm hesitant to do it while the ref code is in flux. It will be easier to just write the patch later on. :)
-- >8 --
Subject: [PATCH] create_symref: drop support for writing symbolic links
The parent commit deprecated the writing of symbolic links for symrefs. Now that some time has passed, we can follow through by dropping the code.
--- Documentation/config.txt | 6 ------ Makefile | 6 ------ cache.h | 1 - config.c | 5 ----- config.mak.uname | 3 --- configure.ac | 3 --- contrib/completion/git-completion.bash | 1 - environment.c | 1 - refs/files-backend.c | 35 ---------------------------------- t/t7201-co.sh | 12 ------------ t/t9903-bash-prompt.sh | 9 --------- 11 files changed, 82 deletions(-)
Documentation/config.txt +0 -6
@@ -433,12 +433,6 @@ CIFS/Microsoft Windows. + False by default. -core.preferSymlinkRefs:: - Instead of the default "symref" format for HEAD - and other symbolic reference files, use symbolic links. - This is sometimes needed to work with old scripts that - expect HEAD to be a symbolic link. - core.bare:: If true this repository is assumed to be 'bare' and has no working directory associated with it. If this is the case a
Makefile +0 -6
@@ -113,9 +113,6 @@ all:: # # Define NO_SYS_SELECT_H if you don't have sys/select.h. # -# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link. -# Enable it on Windows. By default, symrefs are still used. -# # Define NO_SVN_TESTS if you want to skip time-consuming SVN interoperability # tests. These tests take up a significant amount of the total test time # but are not needed unless you plan to talk to SVN repos.
@@ -1200,9 +1197,6 @@ ifdef FREAD_READS_DIRECTORIES COMPAT_CFLAGS += -DFREAD_READS_DIRECTORIES COMPAT_OBJS += compat/fopen.o endif -ifdef NO_SYMLINK_HEAD - BASIC_CFLAGS += -DNO_SYMLINK_HEAD -endif ifdef GETTEXT_POISON BASIC_CFLAGS += -DGETTEXT_POISON endif
cache.h +0 -1
@@ -625,7 +625,6 @@ extern int has_symlinks;extern int minimum_abbrev, default_abbrev;extern int ignore_case;extern int assume_unchanged; -extern int prefer_symlink_refs;extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int warn_on_object_refname_ambiguity;
config.c +0 -5
@@ -726,11 +726,6 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } -if (!strcmp (var,"core.prefersymlinkrefs" )) { - prefer_symlink_refs =git_config_bool (var, value); -return 0 ; - } -if (!strcmp (var,"core.logallrefupdates" )) { log_all_ref_updates =git_config_bool (var, value);return 0 ;
config.mak.uname +0 -3
@@ -169,7 +169,6 @@ ifeq ($(uname_O),Cygwin) NO_STRCASESTR = YesPlease NO_MEMMEM = YesPlease NO_MKSTEMPS = YesPlease - NO_SYMLINK_HEAD = YesPlease NO_IPV6 = YesPlease OLD_ICONV = UnfortunatelyYes # There are conflicting reports about this.
@@ -338,7 +337,6 @@ ifeq ($(uname_S),Windows) NEEDS_CRYPTO_WITH_SSL = YesPlease NO_LIBGEN_H = YesPlease NO_POLL = YesPlease - NO_SYMLINK_HEAD = YesPlease NO_IPV6 = YesPlease NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease
@@ -491,7 +489,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NEEDS_CRYPTO_WITH_SSL = YesPlease NO_LIBGEN_H = YesPlease NO_POLL = YesPlease - NO_SYMLINK_HEAD = YesPlease NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease NO_STRCASESTR = YesPlease
configure.ac +0 -3
@@ -1096,9 +1096,6 @@ GIT_CONF_SUBST([HAVE_BSD_SYSCTL]) # Define USE_PIC if you need the main git objects to be built with -fPIC # in order to build and link perl/Git.so. x86-64 seems to need this. # -# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link. -# Enable it on Windows. By default, symrefs are still used. -# # Define NO_PTHREADS if we do not have pthreads. # # Define PTHREAD_LIBS to the linker flag used for Pthread support.
contrib/completion/git-completion.bash +0 -1
@@ -2046,7 +2046,6 @@ _git_config () core.packedGitLimit core.packedGitWindowSize core.pager - core.preferSymlinkRefs core.preloadindex core.quotepath core.repositoryFormatVersion
environment.c +0 -1
@@ -19,7 +19,6 @@ int has_symlinks = 1;int minimum_abbrev =4 , default_abbrev =7 ;int ignore_case;int assume_unchanged; -int prefer_symlink_refs;int is_bare_repository_cfg =-1 ;/* unspecified */ int log_all_ref_updates =-1 ;/* unspecified */ int warn_ambiguous_refs =1 ;
refs/files-backend.c +0 -35
@@ -2811,21 +2811,6 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -static int create_ref_symlink (struct ref_lock *lock,const char *target) -{ -int ret =-1 ; -#ifndef NO_SYMLINK_HEAD -char *ref_path =get_locked_file_path (lock->lk); -unlink (ref_path); - ret =symlink (target, ref_path); -free (ref_path); - -if (ret) -fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); -#endif -return ret; -} -static void update_symref_reflog (struct ref_lock *lock,const char *refname,const char *target,const char *logmsg) {
@@ -2841,11 +2826,6 @@ static void update_symref_reflog(struct ref_lock *lock, const char *refname,static int create_symref_locked (struct ref_lock *lock,const char *refname,const char *target,const char *logmsg) { -if (prefer_symlink_refs && !create_ref_symlink (lock, target)) { -update_symref_reflog (lock, refname, target, logmsg); -return 0 ; - } -if (!fdopen_lock_file (lock->lk,"w" ))return error ("unable to fdopen %s: %s" , lock->lk->tempfile.filename.buf,strerror (errno));
@@ -2860,27 +2840,12 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,return 0 ; } -static const char symlink_deprecation_warning[] = -"The core.preferSymlinkRefs configuration option has been\n" -"deprecated and will be removed in a future version of Git. If your\n" -"workflow or script depends on '.git/HEAD' being a symbolic link,\n" -"it should be adjusted to use:\n" -"\n" -" git rev-parse HEAD\n" -"\n" -" git rev-parse --symbolic-full-name HEAD\n" -"\n" -"to get the sha1 or branch name, respectively." ; -int create_symref (const char *refname,const char *target,const char *logmsg) {struct strbuf err =STRBUF_INIT ;struct ref_lock *lock;int ret; -if (prefer_symlink_refs) -warning ("%s" , symlink_deprecation_warning); - lock =lock_ref_sha1_basic (refname,NULL ,NULL ,NULL ,REF_NODEREF ,NULL , &err);if (!lock) {
t/t7201-co.sh +0 -12
@@ -427,18 +427,6 @@ test_expect_success 'checkout w/--track from tag fails' ' test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)" ' -test_expect_success 'detach a symbolic link HEAD' ' - git checkout master && - git config --bool core.prefersymlinkrefs yes && - git checkout side && - git checkout master && - it=$(git symbolic-ref HEAD) && - test "z$it" = zrefs/heads/master && - here=$(git rev-parse --verify refs/heads/master) && - git checkout side^ && - test "z$(git rev-parse --verify refs/heads/master)" = "z$here" -' - test_expect_success \ 'checkout with --track fakes a sensible -b <name>' ' git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
t/t9903-bash-prompt.sh +0 -9
@@ -46,15 +46,6 @@ test_expect_success 'prompt - branch name' ' test_cmp expected "$actual" ' -test_expect_success SYMLINKS 'prompt - branch name - symlink symref' ' - printf " (master)" >expected && - test_when_finished "git checkout master" && - test_config core.preferSymlinkRefs true && - git checkout master && - __git_ps1 >"$actual" && - test_cmp expected "$actual" -' - test_expect_success 'prompt - unborn branch' ' printf " (unborn)" >expected && git checkout --orphan unborn &&
Signature · 1 line
2.7.0.rc3.367.g09631da
Quoted 16 lines · Jeff King
Quoted 3 lines · Jeff King
I noticed that an interrupt "git symbolic-ref" will not clean up "HEAD.lock". So I started this series as an attempt to convert
create_symref()to "structlock_file" to get the usual tempfile cleanup.Here's version 2, based on comments from Michael. The first two patches were picked out separately for jk/symbolic-ref-maint, so I've dropped them here (so 1+2 here are the original 3+4).
The other differences from v1 are:
- use "refname" instead of "ref" to match surrounding code
- drop
adjust_shared_perm, as lockfile does it for us
Quoted 7 lines
- adjust reflog writing order (done in a new patch)
The patches are:
[1/3]:
create_symref: modernize variable names [2/3]:create_symref: use existing ref-lock code [3/3]:create_symref: write reflog while holding lock
Thanks, Peff. The whole series is
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Quoted 8 lines · Michael Haggerty
Quoted 5 lines · Jeff King
The patches are:
[1/3]:
create_symref: modernize variable names [2/3]:create_symref: use existing ref-lock code [3/3]:create_symref: write reflog while holding lock
Quoted 5 lines
Thanks, Peff. The whole series is
Reviewed-by Michael Haggerty <[email protected]>Michael
Thanks, both.
Quoted 16 lines · Jeff King
Quoted 3 lines · Jeff King
I noticed that an interrupt "git symbolic-ref" will not clean up "HEAD.lock". So I started this series as an attempt to convert
create_symref()to "structlock_file" to get the usual tempfile cleanup.Here's version 2, based on comments from Michael. The first two patches were picked out separately for jk/symbolic-ref-maint, so I've dropped them here (so 1+2 here are the original 3+4).
The other differences from v1 are:
- use "refname" instead of "ref" to match surrounding code
- drop
adjust_shared_perm, as lockfile does it for us
Quoted 7 lines
- adjust reflog writing order (done in a new patch)
The patches are:
[1/3]:
create_symref: modernize variable names [2/3]:create_symref: use existing ref-lock code [3/3]:create_symref: write reflog while holding lock
This is queued as an early part of 'pu', and some refactoring in David's refs-backend-lmdb topic conflicts with it when merged to 'pu'. I think I resolved the conflicts correctly, but please double check the result when I push it out later today.
Thanks.
Quoted 10 lines · Junio C Hamano
Quoted 5 lines
The patches are:
[1/3]:
create_symref: modernize variable names [2/3]:create_symref: use existing ref-lock code [3/3]:create_symref: write reflog while holding lockThis is queued as an early part of 'pu', and some refactoring in David's refs-backend-lmdb topic conflicts with it when merged to 'pu'. I think I resolved the conflicts correctly, but please double check the result when I push it out later today.
Looks sane to me. It seems like the change was just the renaming of the functions. We _could_ match the renaming of the static-local functions (e.g., create_symref_locked would become files_create_symref_locked), but I don't think it's necessary for file-local functions.
-Peff