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

Re: [libvirt] [PATCH] maint: ignore unsaved emacs files



Eric Blake wrote:
> On 10/25/2012 05:12 PM, Laine Stump wrote:
>>
>> What would be *really* nice is if git could give a *warning* if you
>> tried to do git add . and it found any of those files - it would prevent
>> the cases where you forget to save a file that you've modified.

If you write ChangeLog entries in advance, vc-dwim may do precisely
what you want:

    http://www.gnu.org/s/vc-dwim/

in that it cross-checks that what you say you're going add, change, or
remove via ChangeLog with what the VC diffs say you're actually doing.

It also checks for editor temporaries, which usually indicate
your editor knows about a modified version that you have not
yet "saved".

> I suppose it IS possible to make 'git commit' complain loudly if you
> have unsaved files, or if you tried to add a temporary file as part of
> the commit, by modifying .git/hooks/pre-commit, but I'm not sure I have
> the best formula for doing that off-hand, and it's the sort of thing
> that has to be done in each copy of the tree (and thus, the sort of
> thing you would want to automate during bootstrap).
>
> CC'ing Jim, who has done just that in coreutils' bootstrap.conf, in
> bootstrap_epilogue():
>
>   # Install our git hooks, as long as "cp" accepts the --backup option,
>   # so that we can back up any existing files.
>   case $(cp --help) in *--backup*) backup=1;; *) backup=0;; esac
>   if test $backup = 1; then
>     hooks=$(cd scripts/git-hooks && git ls-files)
>     for f in $hooks; do
>       # If it is identical, skip it.
>       cmp scripts/git-hooks/$f .git/hooks/$f > /dev/null \
>         && continue
>       cp --backup=numbered scripts/git-hooks/$f .git/hooks
>       chmod a-w .git/hooks/$f
>     done
>   fi

Yep, that's how we install coreutils' custom log-checking
local commit hook script.

It sounds like you want a combination of that and the piece
of vc-dwim that looks for editor temporaries.  It uses the names
of files that it's diffing or about to commit, and searches for
the each corresponding editor temporary file name (emacs and vi*).
If it finds a matching temporary file name, it complains.

Here's the Perl function from vc-dwim.pl:

# For emacs, the temporary is a symlink named "$dir/.#$base",
# with useful information in the link name part.
# For Vim, the temporary is a regular file named "$dir/.$base.swp".
# Vim temporaries can also be named .$base.swo, .$base.swn, .$base.swm, etc.
# so test for a few of those, in the unusual event that one of those
# exists, but the .swp file does not.
sub exists_editor_backup ($)
{
  my ($f) = @_;

  # If $f is a symlink, use its referent.
  -l $f
    and $f = readlink $f;

  my $d = dirname $f;
  $f = basename $f;
  my @candidate_tmp =
    (
     "$d/.#$f", "$d/#$f#",                      # Emacs
     map { "$d/.$f.sw$_" } qw (p o n m l k),    # Vim
    );
  foreach my $c (@candidate_tmp)
    {
      -l $c or -f _
        and return $c; # Vim
    }
  return undef;
}


>> (For that matter, it would be nice if make did that :-)
>
> I run make all the time with unsaved files; in that scenario, it would
> have to be only a warning and not an error.  I'm more comfortable with
> it being fatal only in a git hook (which happens when you try to commit
> a mess) and not during development (where a mess might be normal, as
> part of testing a half-baked theory).  But indeed, if there were a way
> to make maint.mk (or cfg.mk) install some GNU make constructs to add a
> loud warning on the presence of any editor temporary files, as evidence
> that we have sniffed an unsaved file so the build may fail in relation
> to what you have just typed into your editor, that might be a nice
> improvement.


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