Gitweb源码解析
最近在搭Git Server,Git提供了一个简单但强大的网页端Gitweb供用户浏览项目 因为需要添加一项小功能,所以把Gitweb的代码研究了一下,下面和大家一起分享一下... 这里,我们有一点前置知识:
- Perl : gitweb是用perl写的,所以,在看gitweb源代码前,请熟悉一下perl的语法...
对于这一点,请一定得做,因为,凭着以往的编程经验可以使你能看懂,但是会非常痛苦。Perl追求的是“道路不止一条”,而且,语法非常奇怪,以及有非常多的默认变量,不懂语法就看gitweb,会让你发狂。 但是,了解了perl以后,再看,会让你豁然开朗...
- 正则表达式
正则表达式在很多地方都有用,所以,先预习一下非常有好处。还有就是,perl在处理字符串尤为强大,就是其正则表达式匹配使用得非常多,所以,必须熟悉正则表达式...在perl的书籍里面会专门有正则表达式一章,所以直接看perl即可
- Git
gitweb为Git服务,所以,对Git的命令熟悉会使我们做任何我们想要Git做的事
Gitweb里面最多的就是对Git输出的字符串的解析,所以看懂正则表达式是必需的,当然,如果我们只想添加功能其实还是很简单的。 Gitweb里面提供了一个全局的字符串函数映射表,这样我们就可以通过前端传入需要操作的函数进行相应操作
1 # we will also need to know the possible actions, for validation 2 our %actions = ( 3 "blame" => \&git_blame, 4 "blame_incremental" => \&git_blame_incremental, 5 "blame_data" => \&git_blame_data, 6 "blobdiff" => \&git_blobdiff, 7 "blobdiff_plain" => \&git_blobdiff_plain, 8 "blob" => \&git_blob, 9 "blob_plain" => \&git_blob_plain, 10 "commitdiff" => \&git_commitdiff, 11 "commitdiff_plain" => \&git_commitdiff_plain, 12 "commit" => \&git_commit, 13 "forks" => \&git_forks, 14 "heads" => \&git_heads, 15 "history" => \&git_history, 16 "log" => \&git_log, 17 #fei add 18 "dl_patch" => \&git_dl_patch, 19 "show_patch_xml" => \&git_write_xml_file, 20 #end fei add 21 "patch" => \&git_patch, 22 "patches" => \&git_patches, 23 "remotes" => \&git_remotes, 24 "rss" => \&git_rss, 25 "atom" => \&git_atom, 26 "search" => \&git_search, 27 "search_help" => \&git_search_help, 28 "shortlog" => \&git_shortlog, 29 "summary" => \&git_summary, 30 "tag" => \&git_tag, 31 "tags" => \&git_tags, 32 "tree" => \&git_tree, 33 "snapshot" => \&git_snapshot, 34 "object" => \&git_object, 35 # those below don't need $project 36 "opml" => \&git_opml, 37 "project_list" => \&git_project_list, 38 "project_index" => \&git_project_index, 39 );
这里的函数映射在Perl里面的实现比较简单,可以参考下这篇文章《perl函数映射》 接下来我们只需要实现我们添加的函数即可。