|
1 | 1 | #Git基本知识与Github使用
|
2 | 2 |
|
3 |
| -##Git |
| 3 | +##Github |
| 4 | + |
| 5 | +Wiki百科上是这么说的 |
| 6 | + |
| 7 | +> GitHub 是一个共享虚拟主机服务,用于存放使用Git版本控制的软件代码和内容项目。它由GitHub公司(曾称Logical Awesome)的开发者Chris Wanstrath、PJ Hyett和Tom Preston-Werner |
| 8 | +使用Ruby on Rails编写而成。 |
| 9 | + |
| 10 | +当然让我们看看官方的介绍: |
| 11 | + |
| 12 | +> GitHub is the best place to share code with friends, co-workers, classmates, and complete strangers. Over eight million people use GitHub to build amazing things together. |
| 13 | +
|
| 14 | + |
| 15 | +它还是什么? |
| 16 | + |
| 17 | +- 网站 |
| 18 | +- 免费博客 |
| 19 | +- 管理配置文件 |
| 20 | +- 收集资料 |
| 21 | +- 简历 |
| 22 | +- 管理代码片段 |
| 23 | +- 托管编程环境 |
| 24 | +- 写作 |
| 25 | + |
| 26 | +等等。看上去像是大餐,但是你还需要了解点什么? |
| 27 | + |
| 28 | +###版本管理与软件部署 |
| 29 | + |
| 30 | +jQuery[^jQuery]在发布版本``2.1.3``,一共有152个commit。我们可以看到如下的提交信息: |
| 31 | + |
| 32 | + - Ajax: Always use script injection in globalEval … bbdfbb4 |
| 33 | + - Effects: Reintroduce use of requestAnimationFrame … 72119e0 |
| 34 | + - Effects: Improve raf logic … 708764f |
| 35 | + - Build: Move test to appropriate module fbdbb6f |
| 36 | + - Build: Update commitplease dev dependency |
| 37 | + - ... |
| 38 | + |
| 39 | +###Github与Git |
4 | 40 |
|
| 41 | +> Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理。在推出后,Git在其它项目中也取得了很大成功,尤其是在Ruby社区中。目前,包括Rubinius、Merb和Bitcoin在内的很多知名项目都使用了Git。Git同样可以被诸如Capistrano和Vlad the Deployer这样的部署工具所使用。 |
| 42 | +
|
| 43 | +> GitHub可以托管各种git库,并提供一个web界面,但与其它像 SourceForge或Google Code这样的服务不同,GitHub的独特卖点在于从另外一个项目进行分支的简易性。为一个项目贡献代码非常简单:首先点击项目站点的“fork”的按钮,然后将代码检出并将修改加入到刚才分出的代码库中,最后通过内建的“pull request”机制向项目负责人申请代码合并。已经有人将GitHub称为代码玩家的MySpace。 |
| 44 | +
|
| 45 | +[^jQuery]: jQuery是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作。 |
| 46 | + |
| 47 | +##Git |
5 | 48 |
|
6 | 49 | 从一般开发者的角度来看,git有以下功能:
|
7 | 50 |
|
@@ -97,167 +140,81 @@ git push -u origin master
|
97 | 140 |
|
98 | 141 | 如果你完成了上面的步骤之后,那么我想你想知道你需要怎样的项目。
|
99 | 142 |
|
100 |
| -<hr /> |
101 |
| -
|
102 |
| -##用好Github |
103 |
| -
|
104 |
| -如何用好Github,并实践一些敏捷软件开发是一个很有意思的事情.我们可以在上面做很多事情,从测试到CI,再到自动部署. |
105 |
| -
|
106 |
| -###敏捷软件开发 |
107 |
| -
|
108 |
| -显然我是在扯淡,这和敏捷软件开发没有什么关系。不过我也不知道瀑布流是怎样的。说说我所知道的一个项目的组成吧: |
109 |
| -
|
110 |
| - - 看板式管理应用程序(如trello,简单地说就是管理软件功能) |
111 |
| - - CI(持续集成) |
112 |
| - - 测试覆盖率 |
113 |
| - - 代码质量(code smell) |
114 |
| - |
115 |
| -对于一个不是远程的团队(如只有一个人的项目) 来说,Trello、Jenkin、Jira不是必需的: |
116 |
| -
|
117 |
| -> 你存在,我深深的脑海里 |
118 |
| -
|
119 |
| -当只有一个人的时候,你只需要明确知道自己想要什么就够了。我们还需要的是CI、测试,以来提升代码的质量。 |
120 |
| -
|
121 |
| -###测试 |
122 |
| -
|
123 |
| -通常我们都会找Document,如果没有的话,你会找什么?看源代码,还是看测试? |
124 |
| -
|
125 |
| -```javascript |
126 |
| -it("specifying response when you need it", function (done) { |
127 |
| - var doneFn = jasmine.createSpy("success"); |
128 |
| -
|
129 |
| - lettuce.get('/some/cool/url', function (result) { |
130 |
| - expect(result).toEqual("awesome response"); |
131 |
| - done(); |
132 |
| - }); |
133 |
| -
|
134 |
| - expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url'); |
135 |
| - expect(doneFn).not.toHaveBeenCalled(); |
136 |
| -
|
137 |
| - jasmine.Ajax.requests.mostRecent().respondWith({ |
138 |
| - "status": 200, |
139 |
| - "contentType": 'text/plain', |
140 |
| - "responseText": 'awesome response' |
141 |
| - }); |
142 |
| -}); |
143 |
| -``` |
144 |
| - |
145 |
| -代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce) |
146 |
| - |
147 |
| -上面的测试用例,清清楚楚地写明了用法,虽然写得有点扯。 |
148 |
| - |
149 |
| -等等,测试是用来干什么的。那么,先说说我为什么会想去写测试吧: |
150 |
| - |
151 |
| - - 我不希望每次做完一个个新功能的时候,再手动地去测试一个个功能。(自动化测试) |
152 |
| - - 我不希望在重构的时候发现破坏了原来的功能,而我还一无所知。 |
153 |
| - - 我不敢push代码,因为我没有把握。 |
| 143 | +##Github流行项目分析 |
| 144 | +
|
| 145 | +之前曾经分析过一些Github的用户行为,现在我们先来说说Github上的Star吧。(截止: 2015年3月9日23时。) |
| 146 | +
|
| 147 | +用户 | 项目名 | Language | Star | Url |
| 148 | +-----|---------- |----------|------|---- |
| 149 | +twbs | Bootstrap | CSS | 78490 | [https://github.com/twbs/bootstrap](https://github.com/twbs/bootstrap) |
| 150 | +vhf |free-programming books | - | 37240 | [https://github.com/vhf/free-programming-books](https://github.com/vhf/free-programming-books) |
| 151 | +angular | angular.js | JavaScript | 36,061 | [https://github.com/angular/angular.js](https://github.com/angular/angular.js) |
| 152 | +mbostock | d3 | JavaScript | 35,257 | [https://github.com/mbostock/d3](https://github.com/mbostock/d3) |
| 153 | +joyent | node | JavaScript | 35,077 | [https://github.com/joyent/node](https://github.com/joyent/node) |
| 154 | +
|
| 155 | +上面列出来的是前5的,看看大于1万个stars的项目的分布,一共有82个: |
| 156 | +
|
| 157 | +语言 | 项目数 |
| 158 | +-----|----- |
| 159 | +JavaScript | 37 |
| 160 | +Ruby | 6 |
| 161 | +CSS | 6 |
| 162 | +Python | 4 |
| 163 | +HTML | 3 |
| 164 | +C++ | 3 |
| 165 | +VimL | 2 |
| 166 | +Shell | 2 |
| 167 | +Go | 2 |
| 168 | +C | 2 |
| 169 | +
|
| 170 | +类型分布: |
| 171 | +
|
| 172 | +
|
| 173 | + - 库和框架: 和``jQuery`` |
| 174 | + - 系统: 如``Linux``、``hhvm``、``docker`` |
| 175 | + - 配置集: 如``dotfiles`` |
| 176 | + - 辅助工具: 如``oh-my-zsh`` |
| 177 | + - 工具: 如``Homewbrew``和``Bower`` |
| 178 | + - 资料收集: 如``free programming books``,``You-Dont-Know-JS``,``Font-Awesome`` |
| 179 | + - 其他:简历如``Resume`` |
154 | 180 |
|
155 |
| -虽然,我不是TDD的死忠,测试的目的是保证功能正常,TDD没法让我们写出质量更高的代码。但是有时TDD是不错的,可以让我们写出逻辑更简单地代码。 |
156 |
| - |
157 |
| -也许你已经知道了``Selenium``、``Jasmine``、``Cucumber``等等的框架,看到过类似于下面的测试 |
158 |
| - |
159 |
| -``` |
160 |
| - Ajax |
161 |
| - ✓ specifying response when you need it |
162 |
| - ✓ specifying html when you need it |
163 |
| - ✓ should be post to some where |
164 |
| - Class |
165 |
| - ✓ respects instanceof |
166 |
| - ✓ inherits methods (also super) |
167 |
| - ✓ extend methods |
168 |
| - Effect |
169 |
| - ✓ should be able fadein elements |
170 |
| - ✓ should be able fadeout elements |
171 |
| -``` |
172 |
| - |
173 |
| -代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce) |
174 |
| - |
175 |
| -看上去似乎每个测试都很小,不过补完每一个测试之后我们就得到了测试覆盖率 |
| 181 | +##创建Pull Request |
176 | 182 |
|
177 |
| -File | Statements | Branches | Functions | Lines |
178 |
| ------|------------|----------|-----------|------ |
179 |
| -lettuce.js | 98.58% (209 / 212)| 82.98%(78 / 94) | 100.00% (54 / 54) | 98.58% (209 / 212) |
| 183 | +除了创建项目之外,我们也可以创建Pull Request来做贡献。 |
180 | 184 |
|
181 |
| -本地测试都通过了,于是我们添加了``Travis-CI``来跑我们的测试 |
| 185 | +###我的第一个PR |
182 | 186 |
|
183 |
| -###CI |
| 187 | +我的第一个PR是给一个小的Node的CoAP相关的库的Pull Request。原因比较简单,是因为它的README.md写错了,导致我无法办法进行下一步。 |
184 | 188 |
|
185 |
| -虽然node.js不算是一门语言,但是因为我们用的node,下面的是一个简单的``.travis.yml``示例: |
| 189 | + const dgram = require('dgram') |
| 190 | + - , coapPacket = require('coap-packet') |
| 191 | + + , package = require('coap-packet') |
186 | 192 |
|
187 |
| -```yml |
188 |
| -language: node_js |
189 |
| -node_js: |
190 |
| - - "0.10" |
| 193 | +很简单,却又很有用的步骤,另外一个也是: |
191 | 194 |
|
192 |
| -notifications: |
193 |
| - email: false |
194 |
| - |
195 |
| -before_install: npm install -g grunt-cli |
196 |
| -install: npm install |
197 |
| -after_success: CODECLIMATE_REPO_TOKEN=321480822fc37deb0de70a11931b4cb6a2a3cc411680e8f4569936ac8ffbb0ab codeclimate < coverage/lcov.info |
198 | 195 | ```
|
| 196 | + else |
| 197 | + cat << END |
| 198 | + $0: error: module ngx_pagespeed requires the pagespeed optimization library. |
| 199 | +-Look in obj/autoconf.err for more details. |
| 200 | ++Look in objs/autoconf.err for more details. |
| 201 | + END |
| 202 | + exit 1 |
| 203 | + fi |
| 204 | +``` |
199 | 205 |
|
200 |
| -代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce) |
201 |
| -
|
202 |
| -我们把这些集成到``README.md``之后,就有了之前那张图。 |
203 |
| -
|
204 |
| -CI对于一个开发者在不同城市开发同一项目上来说是很重要的,这意味着当你添加的部分功能有测试覆盖的时候,项目代码会更加强壮。 |
205 |
| -
|
206 |
| -###代码质量 |
207 |
| -
|
208 |
| -像``jslint``这类的工具,只能保证代码在语法上是正确的,但是不能保证你写了一堆bad smell的代码。 |
209 |
| -
|
210 |
| - - 重复代码 |
211 |
| - - 过长的函数 |
212 |
| - - 等等 |
213 |
| - |
214 |
| -``Code Climate``是一个与github集成的工具,我们不仅仅可以看到测试覆盖率,还有代码质量。 |
215 |
| -
|
216 |
| -先看看上面的ajax类: |
217 |
| -
|
218 |
| -```javascript |
219 |
| -Lettuce.get = function (url, callback) { |
220 |
| - Lettuce.send(url, 'GET', callback); |
221 |
| -}; |
222 |
| - |
223 |
| -Lettuce.send = function (url, method, callback, data) { |
224 |
| - data = data || null; |
225 |
| - var request = new XMLHttpRequest(); |
226 |
| - if (callback instanceof Function) { |
227 |
| - request.onreadystatechange = function () { |
228 |
| - if (request.readyState === 4 && (request.status === 200 || request.status === 0)) { |
229 |
| - callback(request.responseText); |
230 |
| - } |
231 |
| - }; |
232 |
| - } |
233 |
| - request.open(method, url, true); |
234 |
| - if (data instanceof Object) { |
235 |
| - data = JSON.stringify(data); |
236 |
| - request.setRequestHeader('Content-Type', 'application/json'); |
237 |
| - } |
238 |
| - request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
239 |
| - request.send(data); |
240 |
| -}; |
241 |
| -``` |
242 |
| - |
243 |
| -代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce) |
244 |
| - |
245 |
| -在[Code Climate](https://codeclimate.com/github/phodal/lettuce/src/ajax.js)在出现了一堆问题 |
246 |
| - |
247 |
| - - Missing "use strict" statement. (Line 2) |
248 |
| - - Missing "use strict" statement. (Line 14) |
249 |
| - - 'Lettuce' is not defined. (Line 5) |
| 206 | +###CLA |
250 | 207 |
|
251 |
| -而这些都是小问题啦,有时可能会有 |
| 208 | +CLA即Contributor License Agreement,在为一些大的组织、机构提交Pull Request的时候,可能需要签署这个协议。他们会在你的Pull Request里问你,只有你到他们的网站去注册并同意协议才会接受你的PR。 |
252 | 209 |
|
253 |
| - - Similar code found in two :expression_statement nodes (mass = 86) |
| 210 | +以下是我为Google提交的一个PR |
254 | 211 |
|
255 |
| -这就意味着我们可以对上面的代码进行重构,他们是重复的代码。 |
| 212 | + |
256 | 213 |
|
257 |
| -###重构 |
| 214 | +以及Eclipse的一个PR |
258 | 215 |
|
259 |
| -不想在这里说太多关于``重构``的东西,可以参考Martin Flower的《重构》一书去多了解一些重构的细节。 |
| 216 | + |
260 | 217 |
|
261 |
| -这时想说的是,只有代码被测试覆盖住了,那么才能保证重构的过程没有出错。 |
| 218 | +他们都要求我签署CLA。 |
262 | 219 |
|
263 | 220 | <hr>
|
0 commit comments