Uncategorized

Git: Fixing “error: There was a problem with the editor ‘vi’.”

I just installing vim on my machine, but it affected to my installed git. Every I merge my working branch, I always got this error.

error: There was a problem with the editor 'vi'.
Not committing merge; use 'git commit' to complete the merge.

After that, I have to commit manually to the merged changes.

$ git commit "merge from ... to ..."

The error because git using “vi” as the editor and I installed “vim” in my machine.
Here is how to fix this.

$ which vim
/usr/local/bin/vim
$ git config --global core.editor /usr/local/bin/vim

Reference: http://tooky.co.uk/there-was-a-problem-with-the-editor-vi-git-on-mac-os-x/

Grep: Find File by Content Keyword

Here is how to use grep to find file by content keywords recursively.

# Find in current directory.
$ grep -nr 'keyword' .

# Find in particular directory.
$ grep -nr 'keyword' target/directory

# Find in particular directory and file extension.
$ grep -nr 'keyword' target/directory --include=*.html

Reference: http://stackoverflow.com/a/13908492/1936697

Save Media Files to Local Disk on Django-Storages

I’m using django-storages for storing my media files such us images, or document files to amazon S3. But if we using django-storages, by default all media files will uploaded to remote storage, in my case I’m using amazon S3.

I’m writing simple script to save both on remote and also local disk. To use this class, you just need to subclass this mixin class. Keep in mind to pay attention with mixin class order.

Currently only support save to local disk, but not supported yet to remove local file if the instance removed. Don’t hesitate to fork or improve my code. 😀

Generate Excel File in Django and Write it on Memory

I’m creating report to excel format using XlsxWriter, and here is some trick to generate excel file on memory instead of write the file to disk before we throw it to http response attachment.

import xlsxwriter

try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

from django.http import HttpResponse

def download_view(request):
    # Create file on memory
    output = StringIO.StringIO()

    # Generate excel.
    workbook = xlsxwriter.Workbook(output, {'in_memory': True})
    worksheet = workbook.add_worksheet()
    worksheet.write('A1', 'Hello export.')
    workbook.close()

    # Construct response.
    output.seek(0)
    response = HttpResponse(output.read(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = 'attachment; filename=your_file_name.xlsx'
    return response

Reference: http://stackoverflow.com/questions/16393242/xlsxwriter-object-save-as-http-response-to-create-download-in-django

Homebrew: Fixing error “/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory”

My homebrew accidentally error when I’m running update command “brew update” and below are the error stackstrace.

$ brew update
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
Error: update-report should not be called directly!

I’m fixing my issue by run this single command.

$ cd "$(brew --repository)" && git fetch && git reset --hard origin/master

Reference: https://discuss.circleci.com/t/brew-update-command-fails/5211/3