Re: Feature request: Provide porcelain to manage symbolic references as branch aliases

2018-09-22 Thread Phil Sainty
Updating the proof-of-concept script for this feature request.

(See attachment.)

I'm quoting the entire original message for reference, just because
it's been a while since I proposed this.

-Phil


On 30/04/14 00:51, Phil Sainty wrote:
> Most of the plumbing for having branch name aliases already exists
> in the form of symbolic references, and people do use them for this
> purpose; but I get the impression that it's not really supported
> officially, and I'm not aware of any porcelain features to
> facilitate this use-case.
>
> I'd like to propose that such porcelain be added. I feel that a new
> argument to 'git branch' would make the most sense:
>
> git branch --alias  []
>
> For reference/testing, I'm attaching a wrapper script I wrote to
> implement the same functionality (as a separate command). I did this
> primarily to provide the error-checking I felt was needed to make it
> practical to use branch aliases -- git symbolic-ref will happily
> trash your branch references if you make a mistake, whereas it's
> pretty difficult to mess anything up with my git-branch-alias script.
>
> Thus far it's worked nicely for me. Examples:
>
> $ git branch-alias   # create alias
> $ git branch-alias  # create alias for current branch
> $ git branch # view branches and branch aliases
> $ git log 
> $ git checkout 
> $ git push origin  # pushes the branch, not the alias/reference
> $ git branch-alias -d  # delete an alias safely
>
> n.b. For my proposed porcelain change, these examples would become:
> $ git branch --alias   # creates
alias
> $ git branch --alias  # create alias for current branch
> $ git branch --delete  # works since 1.8.0.1, but see below.
>
>
> Since using this script, the only thing I've spotted that I'd like
> to be different is the commit message if I "git merge ". The
> commit message indicates that I've merged  rather than the
>  that it points at. I'd prefer that it was dereferenced
> when the message was generated, so that the real branch name was
> printed instead.
>
>
> That niggle aside, significant things I noted along the way were:
>
> 1. Git 1.8.0.1 fixed the problem whereby git branch -d 
>used to dereference  and therefore delete the branch
>it pointed at, rather than the reference.
>
> 2. HOWEVER if you have  checked out at the time you
>delete it, you're in trouble -- git allows you to do it, and
>you're then left with an invalid HEAD reference.
>
>(I think this ought to be considered a current bug in git.)
>
> 3. I resolved that situation (2) by using "git symbolic-ref HEAD"
>to find the target ref, and setting HEAD to that target. Nothing
>changes for the user, but we can now delete the reference safely.
>
>HOWEVER, there's a problem with detecting that situation (2)
>in the first place:
>
> 4. Chains of references are de-referenced atomically -- the entire
>reference chain is followed to its end; and I could find no
>facility to obtain ONLY the "next link of the chain".
>
>This means we can't use "git symbolic-ref HEAD" to check whether
>it points to another reference. In my script I had to resort to
>inspecting HEAD manually, which obviously isn't desirable.
>
>I think a new argument is warranted here, perhaps something like:
>"git symbolic-ref --max-deref-count=1"
>
>I'll justify that on the assumption that (2) needs fixing in git
>regardless, either by:
>
>(i) Not allowing the user to delete the checked-out symref (which
>would be consistent with the behaviour if the user attempts to
>"git branch -d " (for an actual branch name) when that
>is the currently checked-out branch.
>
>or,
>(ii) Using the approach I've taken: silently setting HEAD to the
> branch to which the symref points before deleting that symref.
> (I couldn't see any reason not to use this approach.)
>
>But as in both cases we need to detect that HEAD is the symref
>being deleted, which means that we need the ability to explicitly
>dereference only a single step of a reference chain.
>
>
> -Phil
>

#!/bin/sh
# git branch-alias
# Author: Phil S.
# Version 1.11
version=1.11

# Creates branch aliases, so that you can refer to a long branch name
# by a convenient short alias.  This is particularly useful for branch
# names beginning with bug-tracker ID numbers (or similar), where the
# benefits of tab-completion are greatly reduced.

# This is mostly a "do what I mean" wrapper around "git symbolic-ref",
# with numerous safety measures included in order to eliminate the
# (otherwise considerable) risk of trashing a branch if you get your
# arguments wrong.

# Installation:
# Place this script somewhere in your PATH and name it "git-branch-alias"
# and you will be able to invoke it with "git branch-alias" as per the
# following examples.

# Examples:
# git branch-alias   # create alias
# git branch-alias  # create alias for current branch
# git branch # view branches and 

Feature request: Provide porcelain to manage symbolic references as branch aliases

2014-04-29 Thread Phil Sainty
Most of the plumbing for having branch name aliases already exists
in the form of symbolic references, and people do use them for this
purpose; but I get the impression that it's not really supported
officially, and I'm not aware of any porcelain features to
facilitate this use-case.

I'd like to propose that such porcelain be added. I feel that a new
argument to 'git branch' would make the most sense:

git branch --alias alias [branch]

For reference/testing, I'm attaching a wrapper script I wrote to
implement the same functionality (as a separate command). I did this
primarily to provide the error-checking I felt was needed to make it
practical to use branch aliases -- git symbolic-ref will happily
trash your branch references if you make a mistake, whereas it's
pretty difficult to mess anything up with my git-branch-alias script.

Thus far it's worked nicely for me. Examples:

$ git branch-alias short some-overly-long-branch-name # creates alias
$ git branch-alias short # creates alias for current branch
$ git log short
$ git checkout short
$ git push origin short # pushes the branch, not the alias/reference
$ git branch-alias --delete short

n.b. For my proposed porcelain change, these examples would become:
$ git branch --alias short some-overly-long-branch-name # creates alias
$ git branch --alias short # creates alias for current branch
$ git branch --delete short # works since 1.8.0.1, but see below.


Since using this script, the only thing I've spotted that I'd like
to be different is the commit message if I git merge alias. The
commit message indicates that I've merged alias rather than the
branch that it points at. I'd prefer that it was dereferenced
when the message was generated, so that the real branch name was
printed instead.


That niggle aside, significant things I noted along the way were:

1. Git 1.8.0.1 fixed the problem whereby git branch -d symref
   used to dereference symref and therefore delete the branch
   it pointed at, rather than the reference.

2. HOWEVER if you have symref checked out at the time you
   delete it, you're in trouble -- git allows you to do it, and
   you're then left with an invalid HEAD reference.

   (I think this ought to be considered a current bug in git.)

3. I resolved that situation (2) by using git symbolic-ref HEAD
   to find the target ref, and setting HEAD to that target. Nothing
   changes for the user, but we can now delete the reference safely.

   HOWEVER, there's a problem with detecting that situation (2)
   in the first place:

4. Chains of references are de-referenced atomically -- the entire
   reference chain is followed to its end; and I could find no
   facility to obtain ONLY the next link of the chain.

   This means we can't use git symbolic-ref HEAD to check whether
   it points to another reference. In my script I had to resort to
   inspecting HEAD manually, which obviously isn't desirable.

   I think a new argument is warranted here, perhaps something like:
   git symbolic-ref --max-deref-count=1

   I'll justify that on the assumption that (2) needs fixing in git
   regardless, either by:

   (i) Not allowing the user to delete the checked-out symref (which
   would be consistent with the behaviour if the user attempts to
   git branch -d branch (for an actual branch name) when that
   is the currently checked-out branch.

   or,
   (ii) Using the approach I've taken: silently setting HEAD to the
branch to which the symref points before deleting that symref.
(I couldn't see any reason not to use this approach.)

   But as in both cases we need to detect that HEAD is the symref
   being deleted, which means that we need the ability to explicitly
   dereference only a single step of a reference chain.


-Phil


#!/bin/sh
# git branch-alias
# Version 1.07
# Author: Phil S.

# Creates branch aliases, so that you can refer to a long branch name
# by a convenient short alias. This is just a do what I mean wrapper
# around git symbolic-ref, but without the (considerable) risk of
# trashing a branch if you get your arguments wrong

# Examples:
# git branch-alias short some-overly-long-branch-name # creates alias
# git branch-alias short # creates alias for current branch
# git log short
# git checkout short
# git push origin short # pushes the branch, not the alias/reference
# git branch-alias --delete short

# Caveats:
# Although everything else I've tried works seamlessly, I note that
# git merge alias will cause the alias name to be mentioned in the
# commit message, rather than the real branch. It would be nicer if
# the branch name appeared.

# Compatibility:
# Developed with git version 1.7.12.4
# Tested with git version 1.9.0
#
# Related git changes between versions 1.7.12.4 and 1.9.0:
#
# 1.8.0.1
#  * A symbolic ref refs/heads/SYM was not correctly removed with git
#branch -d SYM; the command removed the ref pointed by SYM
#instead.
#
# 1.8.1
#  * git symbolic-ref learned the -d $symref