Content AND Presentation

2018-06-16 ivy-use-selectable-prompt

It’s been a long-standing problem in Ivy that entering a string which was not on the list of candidates, but was a substring of one of the candidates, was non-trivial. You had to remember to press C-M-j instead of RET, and that requires some finger acrobatics.

Well, it’s no longer a problem. In one of the recent versions of Ivy, a new variable called ivy-use-selectable-prompt was introduced. With it, you can just press the up arrow when on the first candidate to select the contents of the prompt line. Very nice!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2018-06-10 Git diff in Eshell

I try to use Eshell more, as part of a general drive towards moving everything into Emacs. One thing that annoyed me a bit was issuing a git diff or something similar in Eshell. Invariably, I received a WARNING: terminal is not fully functional.

I am not sure, but I suspect that the reason is that Eshell does not really like less, which seems to be the default Git pager.

Now there exist quite a lot of ways to overcome this issue. One is having

(setenv "PAGER" "cat")

in your init.el. This is, however, an all-or-nothing type solution, since it affects all programs started from Emacs. (Though maybe this is the right way?) I have discovered another, truly remarkable solution of this problem which this blog post is too short to contain;-), and hence it must wait for another time.

Problem solved – at least partially. The diff is not colored at all! Again, this is easy to overcome: just put

[color]
	ui = always

in your .git/config and you’re done.

(Please let me know if this has any undesirable side-effects.)

Of course, I don’t use git diff in Eshell very often, since there is Magit – but sometimes I want to e.g. show someone what it looks like to use Git, and then I start with the command line.

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryEshell

Comments on this page

2018-06-04 Collaborating with non-Git-users - Git hook

In the first part, I showed how I can make Git add information about the commit in every file I want it to in such a way that it is updated every time I Git-archive the project. The second part was devoted to integrating this workflow in Emacs and mu4e. Today I finish with showing my Git hook protecting me from accidentally messing up.

One issue with this setup is that I can accidentally commit the values (like the commit hash etc.) I got from one of the other authors instead of the placeholder. Using Magit’s magit-discard on a region is easy, but forgetting to do it is even easier. So, git hooks to the rescue – this is perfectly suited to a Git pre-commit hook:

#!/bin/bash

git diff --name-only --cached | while read -r filename; do
    if ! grep -q "\$Format:" <(head -n3 "$filename")
    then
	echo File "$filename" contains the commit data, please revert to the placeholders!
	exit 1
    fi
done

Notice that I used a few tricks here:

  • git diff --name-only --cached gives us the list of files about to be committed,
  • we pipe it to bash while loop, which reads it line by line
  • we use grep -q to quiet grep and only make it return a non-zero code when nothing is found,
  • we use ! to negate the exit status of grep,
  • we use head to only search the first few (10 by default) lines of each file,
  • and we use the famous <(...) bash trick to avoid fussing around with temp files ourselves.

Notice also that the above is not very robust: if the filenames contain spaces, things might break down. (Use IFS= read in that case.) In my case, this is not a concern, so I didn’t bother.

The only remaining thing to do is to ensure that I won’t accidentally commit things from other people as myself. I know two methods of doing this: one is an extremely dirty hack, and the other one relies on a not-so-well-documented feature of Git. Go figure.

The dirty hack is to set the user name in the per-repository Git configuration to empty strings. This makes it impossible to commit anything (even if you use git commit --author), since Git complains about empty ident name (for <email@example.com>) not allowed. The trick is to set the environment variables GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL (for some very strange reason, they override even the local Git settings). Now, if you don’t specify your author data, the commit will fail, but if you do, everything will be fine.

The more elegant way is to use a client-side Git hook. In this case, the pre-commit hook is again our best bet. The problem is, it does not get any parameters. Happily for us, it has an undocumented feature: it has access to some environment variables, one of them being GIT_AUTHOR_EMAIL. (I learned about it here; it is actually described in the docs, but that seems not to imply that the pre-commit hook is going to get it.) So, we can use this pre-commit hook:

#!/bin/bash

if ! grep "@allowed.domain$" <(echo $GIT_AUTHOR_EMAIL)
then
    echo Please correctly set the commit author!
    exit 1
fi

(Of course, I merged both the above scripts to one pre-commit script.)

Problem solved!

CategoryEnglish, CategoryBlog, CategoryGit

Comments on this page

More...

CategoryEnglish, CategoryBlog