pátek 12. prosince 2014

Git error: Short SHA1 is Ambiguous

Git allows using shortened SHA1 hashes. Default is to use 7 characters (see core.abbrev config option). But sooner or later, you may hit problem, that short hashes are ambiguous. E.g.
$ git log 0ed98e5
...
error: short SHA1 0ed98e5 is ambiguous

To find, what Git objects are referenced by the same short has, use
$git rev-parse --disambiguate=0ed98e | git cat-file --batch-check
0ed98e56dd43b172d438dba0aa6ea9ebed0554c7 commit 307
0ed98e50771521f9ec27314d49286fd54e989c87 blob 1478
0ed98e50771521f9ec27314d49286fd54e989c87 blob 1478

In this case, only the first hash (0ed98e56) refers to a commit. The second hash (0ed98e50) refers to a blob - file. So, Git does check SHA1 for all it's objects, not only for commits, although the git log command uses just commits SHA1 for it's arguments.

How to fix the error? Use longer prefix of the SHA1 hash. The bulletproof solution is to use all 40 characters. But if you still like abbreviates, then you can find the safe minimum length of SHA1 prefix for your project by Josh Stone's solution:
$git rev-list --all --abbrev=0 --abbrev-commit | wc -L

čtvrtek 15. května 2014

Simple console.log wrapper hangs IE10 and Chrome

Many programmers write a wrapper around console.log to use logging only when it's available. I have reassigned log to my own variable this.log = console.log; and it has worked fine in Firefox with Firebug plugin. However, it has caused Javascript to stop working in Internet Explorer 10 (IE10) and Chrome. Strange thing is, IE10 hangs only when it has closed the developer tools F12. When I have opened the developer tools to see what's going on, the IE10 has proceeded well.

var mylog = {
    log : this._logNull,
    init: function() {
        if (typeof console == 'object' && typeof console.log == 'function') {
            console.log(this);
            this.log = console.log; // IE10 hangs bug
            //this.log = this._log; // IE10 ok
        }
    },
    _logNull : function() {},
    _log : function() { console.log.apply(console, arguments); }
    
};
Try the code at JSFiddle.

After writing a proper wrapper function _log, the code goes well even in IE10 with closed developer tools.

Note, this is just proof of concept code for IE10. The real console.log wrapper may become more complicated when considering various browser like Chrome, Safari, etc.