18c2ecf20Sopenharmony_ci.. include:: ../disclaimer-zh_CN.rst 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci:Original: :ref:`Documentation/process/7.AdvancedTopics.rst <development_advancedtopics>` 48c2ecf20Sopenharmony_ci:Translator: Alex Shi <alex.shi@linux.alibaba.com> 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci.. _cn_development_advancedtopics: 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci高级主题 98c2ecf20Sopenharmony_ci======== 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci现在,希望您能够掌握开发流程的工作方式。然而,还有更多的东西要学!本节将介绍 128c2ecf20Sopenharmony_ci一些主题,这些主题对希望成为Linux内核开发过程常规部分的开发人员有帮助。 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci使用Git管理补丁 158c2ecf20Sopenharmony_ci--------------- 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci内核使用分布式版本控制始于2002年初,当时Linus首次开始使用专有的Bitkeeper应用 188c2ecf20Sopenharmony_ci程序。虽然bitkeeper存在争议,但它所体现的软件版本管理方法却肯定不是。分布式 198c2ecf20Sopenharmony_ci版本控制可以立即加速内核开发项目。在当前的时代,有几种免费的比特保持器替代品。 208c2ecf20Sopenharmony_ci无论好坏,内核项目都将Git作为其选择的工具。 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci使用Git管理补丁可以使开发人员的生活更加轻松,尤其是随着补丁数量的增加。Git 238c2ecf20Sopenharmony_ci也有其粗糙的边缘和一定的危险,它是一个年轻和强大的工具,仍然在其开发人员完善 248c2ecf20Sopenharmony_ci中。本文档不会试图教会读者如何使用git;这会是个巨长的文档。相反,这里的重点 258c2ecf20Sopenharmony_ci将是Git如何特别适合内核开发过程。想要加快Git的开发人员可以在以下网站上找到 268c2ecf20Sopenharmony_ci更多信息: 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci https://git-scm.com/ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci https://www.kernel.org/pub/software/scm/git/docs/user-manual.html 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci在尝试使用它使补丁可供其他人使用之前,第一要务是阅读上述站点,对Git的工作 338c2ecf20Sopenharmony_ci方式有一个扎实的了解。使用Git的开发人员应该能够获得主线存储库的副本,探索 348c2ecf20Sopenharmony_ci修订历史,提交对树的更改,使用分支等。了解Git用于重写历史的工具(如Rebase) 358c2ecf20Sopenharmony_ci也很有用。Git有自己的术语和概念;Git的新用户应该了解refs、远程分支、索引、 368c2ecf20Sopenharmony_ci快进合并、推拉、分离头等。一开始可能有点吓人,但这些概念不难通过一点学习来 378c2ecf20Sopenharmony_ci理解。 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci使用git生成通过电子邮件提交的补丁是提高速度的一个很好的练习。 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci当您准备好开始安装Git树供其他人查看时,您当然需要一个可以从中提取的服务器。 428c2ecf20Sopenharmony_ci如果您有一个可以访问Internet的系统,那么使用git守护进程设置这样的服务器相 438c2ecf20Sopenharmony_ci对简单。否则,免费的公共托管网站(例如github)开始出现在网络上。成熟的开发 448c2ecf20Sopenharmony_ci人员可以在kernel.org上获得一个帐户,但这些帐户并不容易找到;有关更多信息, 458c2ecf20Sopenharmony_ci请参阅 https://kernel.org/faq/ 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci正常的Git工作流程涉及到许多分支的使用。每一条开发线都可以分为单独的“主题 488c2ecf20Sopenharmony_ci分支”,并独立维护。Git的分支机构很便宜,没有理由不免费使用它们。而且,在 498c2ecf20Sopenharmony_ci任何情况下,您都不应该在任何您打算让其他人从中受益的分支中进行开发。应该 508c2ecf20Sopenharmony_ci小心地创建公开可用的分支;当它们处于完整的形式并准备好运行时(而不是之前), 518c2ecf20Sopenharmony_ci合并开发分支的补丁。 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ciGit提供了一些强大的工具,可以让您重写开发历史。一个不方便的补丁(比如说, 548c2ecf20Sopenharmony_ci一个打破二分法的补丁,或者有其他一些明显的缺陷)可以在适当的位置修复,或者 558c2ecf20Sopenharmony_ci完全从历史中消失。一个补丁系列可以被重写,就好像它是在今天的主线之上写的 568c2ecf20Sopenharmony_ci一样,即使你已经花了几个月的时间在写它。可以透明地将更改从一个分支转移到另 578c2ecf20Sopenharmony_ci一个分支。等等。明智地使用git修改历史的能力可以帮助创建问题更少的干净补丁集。 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci然而,过度使用这种能力可能会导致其他问题,而不仅仅是对创建完美项目历史的 608c2ecf20Sopenharmony_ci简单痴迷。重写历史将重写该历史中包含的更改,将经过测试(希望)的内核树变 618c2ecf20Sopenharmony_ci为未经测试的内核树。但是,除此之外,如果开发人员没有对项目历史的共享视图, 628c2ecf20Sopenharmony_ci他们就无法轻松地协作;如果您重写了其他开发人员拉入他们存储库的历史,您将 638c2ecf20Sopenharmony_ci使这些开发人员的生活更加困难。因此,这里有一个简单的经验法则:被导出到其他 648c2ecf20Sopenharmony_ci人的历史在此后通常被认为是不可变的。 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci因此,一旦将一组更改推送到公开可用的服务器上,就不应该重写这些更改。如果您 678c2ecf20Sopenharmony_ci尝试强制进行不会导致快进合并(即不共享同一历史记录的更改)的更改,Git将尝 688c2ecf20Sopenharmony_ci试强制执行此规则。可以重写此检查,有时可能需要重写导出的树。在树之间移动变 698c2ecf20Sopenharmony_ci更集以避免Linux-next中的冲突就是一个例子。但这种行为应该是罕见的。这就是为 708c2ecf20Sopenharmony_ci什么开发应该在私有分支中进行(必要时可以重写)并且只有在公共分支处于合理的 718c2ecf20Sopenharmony_ci高级状态时才转移到公共分支中的原因之一。 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci当主线(或其他一组变更所基于的树)前进时,很容易与该树合并以保持领先地位。 748c2ecf20Sopenharmony_ci对于一个私有的分支,rebasing 可能是一个很容易跟上另一棵树的方法,但是一旦 758c2ecf20Sopenharmony_ci一棵树被导出到全世界,rebasing就不是一个选项。一旦发生这种情况,就必须进行 768c2ecf20Sopenharmony_ci完全合并(merge)。合并有时是很有意义的,但是过于频繁的合并会不必要地扰乱 778c2ecf20Sopenharmony_ci历史。在这种情况下,建议的技术是不经常合并,通常只在特定的发布点(如主线-rc 788c2ecf20Sopenharmony_ci发布)合并。如果您对特定的更改感到紧张,则可以始终在私有分支中执行测试合并。 798c2ecf20Sopenharmony_ci在这种情况下,git rerere 工具很有用;它记住合并冲突是如何解决的,这样您就 808c2ecf20Sopenharmony_ci不必重复相同的工作。 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci关于Git这样的工具的一个最大的反复抱怨是:补丁从一个存储库到另一个存储库的 838c2ecf20Sopenharmony_ci大量移动使得很容易陷入错误建议的变更中,这些变更避开审查雷达进入主线。当内 848c2ecf20Sopenharmony_ci核开发人员看到这种情况发生时,他们往往会感到不高兴;在Git树上放置未查看或 858c2ecf20Sopenharmony_ci主题外的补丁可能会影响您将来获取树的能力。引用Linus: 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci:: 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci 你可以给我发补丁,但要我从你哪里取一个Git补丁,我需要知道你知道 908c2ecf20Sopenharmony_ci 你在做什么,我需要能够相信事情而不去检查每个个人改变。 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci(http://lwn.net/articles/224135/)。 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci为了避免这种情况,请确保给定分支中的所有补丁都与相关主题紧密相关;“驱动程序 958c2ecf20Sopenharmony_ci修复”分支不应更改核心内存管理代码。而且,最重要的是,不要使用Git树来绕过 968c2ecf20Sopenharmony_ci审查过程。不时的将树的摘要发布到相关的列表中,当时间合适时,请求 978c2ecf20Sopenharmony_ciLinux-next 中包含该树。 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci如果其他人开始发送补丁以包含到您的树中,不要忘记查看它们。还要确保您维护正确 1008c2ecf20Sopenharmony_ci的作者信息; ``git am`` 工具在这方面做得最好,但是如果它通过第三方转发给您, 1018c2ecf20Sopenharmony_ci您可能需要在补丁中添加“From:”行。 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci请求pull操作时,请务必提供所有相关信息:树的位置、要拉的分支以及拉操作将导致 1048c2ecf20Sopenharmony_ci的更改。在这方面,git request pull 命令非常有用;它将按照其他开发人员的预期 1058c2ecf20Sopenharmony_ci格式化请求,并检查以确保您记住了将这些更改推送到公共服务器。 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci审查补丁 1088c2ecf20Sopenharmony_ci-------- 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci一些读者当然会反对将本节与“高级主题”放在一起,因为即使是刚开始的内核开发人员 1118c2ecf20Sopenharmony_ci也应该检查补丁。当然,学习如何在内核环境中编程没有比查看其他人发布的代码更好 1128c2ecf20Sopenharmony_ci的方法了。此外,审阅者永远供不应求;通过查看代码,您可以对整个流程做出重大贡献。 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci审查代码可能是一个令人生畏的前景,特别是对于一个新的内核开发人员来说,他们 1158c2ecf20Sopenharmony_ci可能会对公开询问代码感到紧张,而这些代码是由那些有更多经验的人发布的。不过, 1168c2ecf20Sopenharmony_ci即使是最有经验的开发人员编写的代码也可以得到改进。也许对评审员(所有评审员) 1178c2ecf20Sopenharmony_ci最好的建议是:把评审评论当成问题而不是批评。询问“在这条路径中如何释放锁?” 1188c2ecf20Sopenharmony_ci总是比说“这里的锁是错误的”更好。 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci不同的开发人员将从不同的角度审查代码。一些主要关注的是编码样式以及代码行是 1218c2ecf20Sopenharmony_ci否有尾随空格。其他人将主要关注补丁作为一个整体实现的变更是否对内核有好处。 1228c2ecf20Sopenharmony_ci然而,其他人会检查是否存在锁定问题、堆栈使用过度、可能的安全问题、在其他 1238c2ecf20Sopenharmony_ci地方发现的代码重复、足够的文档、对性能的不利影响、用户空间ABI更改等。所有 1248c2ecf20Sopenharmony_ci类型的检查,如果它们导致更好的代码进入内核,都是受欢迎和值得的。 125