Re: [PATCHv1 2/2] git-p4: work with a detached head

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Luke Diamand <luke@xxxxxxxxxxx> writes:

> On 9 September 2015 at 22:52, Junio C Hamano <gitster@xxxxxxxxx> wrote:
>> Luke Diamand <luke@xxxxxxxxxxx> writes:
>>
>>>      def run(self, args):
>>>          if len(args) == 0:
>>>              self.master = currentGitBranch()
>>> -            if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
>>> -                die("Detecting current git branch failed!")
>>> +            if self.master == "undefined":
>>> +                self.master = None
>>
>> The comparison with textual "undefined" smelled fishy and I ended up
>> looking at the implementation of currentGitBranch().
>>
>>     def currentGitBranch():
>>         return read_pipe("git name-rev HEAD").split(" ")[1].strip()
>>
>> Yuck.  I know it is not entirely the fault of this patch, but
>> shouldn't it be reading from
>>
>>     $ git symbolic-ref HEAD
>>
>> and catch the error "fatal: ref HEAD is not a symbolic ref" and use
>> it as a signal to tell that the HEAD is detached?
>
> That sounds much nicer. I'll redo the patch accordingly.

While "symbolic-ref" _is_ the right way to learn what the currently
checked out branch is, I think you'd need to be a bit careful while
analysing the ramifications of that fix.

Notice:

    $ git checkout ld/p4-detached-head
    $ git symbolic-ref -q HEAD; echo $?
    refs/heads/ld/p4-detached-head
    0
    $ git checkout HEAD^0
    $ git symbolic-ref -q HEAD; echo $?
    1
    $ git name-rev HEAD
    HEAD ld/p4-detached-head

A few implications of the above observation:

 * The fact that the code used to use 'name-rev HEAD' means that it
   behaved as if you are on some branch when you are in a detached
   HEAD state, if your current commit happened to be at the tip of
   some branch.  Users could be relying on this behaviour, i.e. 
   you can detach (perhaps because you do not want to accidentally
   advance the history of the real branch) at the tip of a branch,
   and have "git p4" still apply the configuration based on the name
   of the original branch.

 * If there were multiple branches that point at your current
   commit, then what is returned by currentGitBranch based on
   name-rev is unpredictable.  So in that sense, the workflow that
   relies on the existing "use the configuration based on the branch
   name returned by name-rev" behaviour is already broken, but not
   many people have two or more branches pointing at the same commit
   very often, so they may perceive existing breakage of their
   workflow a non-issue.  To them, fixing the implementation of
   currentGitBranch may appear to be a regression.

 * Of course, if the user happens to have a branch whose name is
   "undefined", you may have detached the HEAD at a totally
   unrelated commit, and the existing code in run() would first set
   self.master to "undefined", notices "refs/heads/undefined"
   exists, and without even noticing that these two are not related
   to each other at all, happily goes ahead and uses "undefined"
   branch.  I don't know what happens in that case---perhaps it is
   the same as the second case where configuration for an unrelated
   branch is applied and no other damage is done.  Perhaps the code
   gets confused and sometimes updates HEAD and sometimes updates
   the tip of self.master branch.  In either case, the existing
   behaviour cannot be something the users have sensibly relied on.

A good write-up of the bug in the externally visible behaviour that
is corrected by fixing currentGitBranch implementation in the
Release Notes (when this fix hits a release) should be sufficient, I
think.

Thanks.

   
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]