Content AND Presentation

2017-11-19 How to trip up yourself using Git

Everyone and their mother seem to be using Git these days. And I have to admit that it is really a clever piece of code. However, sometimes it may not work like you want it to do. Let me share a personal story.

Assume we have this piece of code.

if (1 > 0) {
	if (2 > 1) {
		console.log('2 is greater than 1.');
	} else {
		console.log('Something is wrong!');
	}
} else {
	console.log('Something is really wrong!');
}

Not that it makes a lot of sense, but let us commit it to our repo anyway:

git init
git add git-trip-up.js
git commit -m "Initial commit"

Now assume that we want to add something to both then- and else-branches of the outer if:

if (1 > 0) {
	console.log('1>0');
	if (2 > 1) {
		console.log('2 is greater than 1.');
	} else {
		console.log('1<=0');
		console.log('Something is wrong!');
	}
} else {
	console.log('Something is really wrong!');
}

Being a nice programmer, we do it on a separate branch. Having a moment of stupidity, we do it wrong (the second console.log is in the wrong else-branch).

git checkout -b new_branch
git commit -am "Bad commit"

We make a PR, and it accidentally gets merged. Then the team leader notices that something went wrong, reverts the merged commit and tells the author that he screwed up.

git checkout master
git merge new_branch
git revert --no-edit HEAD

The author fixes that on his branch.

git checkout new_branch
if (1 > 0) {
	console.log('1>0');
	if (2 > 1) {
		console.log('2 is greater than 1.');
	} else {
		console.log('Something is wrong!');
	}
} else {
	console.log('1<=0');
	console.log('Something is really wrong!');
}

Then he wants to make sure that there are no conflicts, and merges master into feature-branch. Boom! The console.log('1>0'); magically disappeared!

git commit -am "Fix a stupid mistake"
git merge --no-edit master

Well, of course this is not unexpected in technical terms. But the workflow seems reasonable, merging does not actually signal any conflicts, so one might think everything is fine.

I don’t know how to prevent such mistakes, other than being alert. So be alert when merging things that involve reverting. Probably.

CategoryEnglish, CategoryBlog, CategoryGit

Comments on this page

2017-11-12 My email workflow – an addendum

Some time ago I wrote about my email capturing workflow. After I’ve written that text, I noticed that my command does not work well when launched in message mode (as opposed to the headers view mode). I tried to debug this problem, but to no avail.

Until yesterday. It just occurred to me that the problem may be the asynchronicity again. I applied the method from my previous post and voilà! It Just Works™ now.

I’m not really sure about the exact reason for that behavior (I would guess that it might be buried deeply in the mu4e~proc-move function), but here’s the updated code.

(defun pause (count)
  "Pause for COUNT seconds (default one)"
  (interactive "p")
  (sleep-for count))

(defun mu4e-flag-message-at-point-now ()
  "Flag the message at point immediately."
  (interactive)
  (let ((msg (mu4e-message-at-point)))
	(funcall (plist-get (cdr (assq 'flag mu4e-marks)) :action)
		 (plist-get msg :docid) msg nil)))

(defun make-this-message-into-an-org-todo-item ()
  "Flag this message as important and capture it in Org-mode."
  (interactive)
  (mu4e-flag-message-at-point-now)
  (when (eq major-mode 'mu4e-view-mode)
	(pause 0.2))
  (org-capture nil "t"))

Notice that sleep-for (and hence pause) accepts non-integer values (which is handy in this case – a second is a long time!).

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2017-11-06 Keyboard macros and asynchronicity

Some time ago I wanted to send a bunch of emails from mu4e. I had all the data in a csv file, so I figured out that I’ll just record a keyboard macro of myself sending the email and press f4 a few times.

Well, it didn’t work. I’m not 100% sure what the reason was, but it seems to me that after sending an email, mu4e goes back to the main menu in an asynchronous way. This means that if I change the buffer fast enough after sending the message, mu4e will kick in after that and display its main menu. I’m not that fast, of course, but keyboard macros are.

Well, Elisp to the rescue:

(defun pause (count)
  "Pause for a second (or COUNT seconds)."
  (interactive "p")
  (sleep-for count))

I just bound the above command to some convenient key (f7 in my case), pressed it after sending the email while recording the macro, and everything worked! My macro properly returned to the next line of my csv with email addresses, subjects and bodies of my messages.

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

More...

CategoryEnglish, CategoryBlog