知易行难

Archive for June, 2008

Tags on Git

Jun 30, 2008

Tags


Tags of SVN are just copies of trunk. Tags of Git, on the other hand, are much more powerful. Here I’ll show you how to add/delete git tags locally and remotely.

I’ll take google_map_example for example. The google_map_example is an open project on github that shows sample codes from the book Beginning Google Maps Applications with Rails and Ajax: From Novice to Professional. The codes will be changed slightly to use Rails 2.1.

Say I’ve keyed in the codes from chapter 1, now I’d like to tag the last commit. This is can be done in this way:

phantom:~/codex/maps (master) james$ git tag -a -m "tagged chap_1" chap_1
phantom:~/codex/maps (master) james$ git tag -l
chap_1

A local tag named “chap_1″ has been created. Keep in mind it’s merely a LOCAL tag, which means you have to push it to the remote repository to let other guys see it.

Here’s the tricky thing, git push won’t push the tag to the remote, git push –all won’t neither! Use this instead:

phantom:~/codex/maps (master) james$ git push --tags
updating 'refs/tags/chap_1'
  from 0000000000000000000000000000000000000000
  to   63db0052a27914db5f1a9fe6e1321309e8111f82
Generating pack...
Done counting 1 objects.
Deltifying 1 objects...
 100% (1/1) done
Writing 1 objects...
 100% (1/1) done
Total 1 (delta 0), reused 0 (delta 0)

Let’s go to the github project page to check if the tag has been pushed. Visit http://github.com/ashchan/google_maps_example/tree/master, hover mouse to “all tags”. Cheers, it’s there!

Git tag

Later I decide to remove this tag. Piece of cake, just use the git tag -d command:

phantom:~/codex/maps (master) james$ git tag -d chap_1
Deleted tag 'chap_1'
phantom:~/codex/maps (master) james$ git tag -l

Note when I use git tag -l it shows no local tags.

It’s a little bit complicated to use the push command to remove the remote tag:

phantom:~/codex/maps (master) james$ git push origin :refs/tags/chap_1
deleting 'refs/tags/chap_1'
Everything up-to-date

I googled a lot and couldn’t find any documents showing this example. Almost all people know that git push remotename :refs/heads/branchname would delete a remote branch, so it’s not very hard for me to figure out how to delete a remote tag :p

I’m new to git but I’ve been planning to migrate most projects to git. Hope this helps.

专业与否仅一步之差

Jun 25, 2008

Tags

这是一本讲述 rails 和 google maps 开发的书。

Google Maps Book

很遗憾,我买了一本中文版的。暂且不说翻译质量如何,让我们来看看封面吧:

Google Maps Book

专业不专业,往往仅一步之差。

BTW,这个译本是机械出的。

Meta on Rails plugin for meta tags

Jun 25, 2008

Tags

Meta on Rails is a very simple rails plugin to help you customize your pages’ meta tags a little bit easier.

How to Install

Meta on Rails is hosted on github. For Rails 2.1 or later, install in this way:

./script/plugin install git://github.com/ashchan/meta_on_rails.git

For Rails 2.0.2 or older, need to clone the public git url:

git clone git://github.com/ashchan/meta_on_rails.git vendor/plugins/meta_on_rails

Usage

Add the following code to the layout (e.g. app/views/layout/application.html.erb), and be sure to put it in the head tag:

<head>
<%= display_meta(:keywords => "default,keywords", :description => "default description") %>
</head>

Values passed to the display_meta method becomes the site-wide default values. In the above example, these two meta tags will be generated on all pages if they’re not overridden on the views:

<meta name="description" content="default description" />
<meta name="keywords" content="default,keywords" />

Then add this code to the view to set meta tags on that page:

<% set_meta(:keywords => 'my,keyword', :generator => 'a bad <script /> generator') %>

The output html will be like this (note that html tags are tripped):

<meta name="generator" content="a bad  generator" />
<meta name="description" content="default description" />
<meta name="keywords" content="my,keyword" />

Dead simple, right?

Feedback

Feel free to leave a message if you have any suggestion or find bugs.

Freelancing 并不等于 Free

Jun 24, 2008

Tags


告别全职工作已有数月了,四月份正式开始 freelancing 至今也已经有两个月 。切身感受到 Freelancing 并不意味着完全 free,因为:

  • 不够牛,所以没有高回报的项目;
  • 什么活都有进度压力,有时(确切得说是大部分时间)会比全职上班更辛苦;
  • 活多了会忙不过来,项目青黄不接时会没有饭吃;

放弃上班却正是因为 freelance 的 free 这一点。不用看老板脸色,不用朝九晚五,可以去看周二半价电影,可以自己选择要做的东西,可以在工作时间看书看碟。当然,享受这一切的前提是,要有够用的奶粉钱可以赚。

计划对 ashchan.com 再进行一次修改,使用 haml 作为 view template engine,并把外观简单化。

本人主要接 Rails 和 Ruby 相关的活。如果你有项目需要 outsourcing ,或者你有项目要寻求技术合作并且希望合作地在杭州,可以与我联系

Mechanize的内存问题

Jun 5, 2008

Tags

Mechanize来写爬虫是非常轻松的一件事。简单的事情不一定会完美,比方说:

agent = WWW::Mechanize.new
agent.user_agent_alias = 'Windows IE 6'
10000.times do
    page = agent.get("http://some.site")
    puts (page/"div#title").text
end

暂且假设http://some.site不会封禁你的访问,同时每一次agent.get都返回了正确的页面(网络无问题)。这一万次页面访问有问题吗?

有!问题在于Mechanize实在很强大,它模拟自然的浏览器行为,所以默认它会去保留访问历史纪录,并且这个纪录貌似没有加以大小限制!运行这段ruby代码,很快内存使用会上升至 1G 以上。

把历史纪录的大小设为一个合理的值,在这种情况下设为1比较合适:

agent.max_history = 1

再运行这段代码,内存使用在20~30M间波动,非常稳定。

Archives: Monthly or