{"id":10006594,"date":"2022-09-26T16:33:13","date_gmt":"2022-09-26T08:33:13","guid":{"rendered":"https:\/\/ityet.com\/?p=10006594"},"modified":"2022-09-26T16:33:13","modified_gmt":"2022-09-26T08:33:13","slug":"%e5%bd%93%e4%bd%a0-git-push-%e6%97%b6%ef%bc%8c%e6%9e%81%e7%8b%90gitlab%e4%b8%8a%e5%8f%91%e7%94%9f%e4%ba%86%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006594","title":{"rendered":"\u5f53\u4f60 git push \u65f6\uff0c\u6781\u72d0GitLab\u4e0a\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f"},"content":{"rendered":"<blockquote><p>\u672c\u6587\u6765\u81ea\uff1a<br \/>\u674e\u632f\u6960 \u6781\u72d0(GitLab) \u7814\u53d1\u5de5\u7a0b\u5e08<\/p><\/blockquote>\n<p>\u52c7\u58eb\uff0c\u4f60\u53ef\u66fe\u597d\u5947\u8fc7 Git \u548c\u6781\u72d0GitLab \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u73b0\u5728\uff0c\u62ff\u8d77\u4f60\u5fc3\u7231\u7684 IDE\uff0c\u548c\u6211\u4eec\u4e00\u8d77\u8e0f\u4e0a\u63a2\u7d22\u4e4b\u65c5\u5427\uff01<\/p>\n<h2>\u57fa\u7840\u77e5\u8bc6<\/h2>\n<p>\u5728\u5f00\u59cb\u65c5\u7a0b\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u505a\u4e09\u5206\u949f\u7684\u77e5\u8bc6\u50a8\u5907\uff0c\u8ba1\u65f6\u5f00\u59cb\uff01<\/p>\n<h4>Git\u4ed3\u5e93\u5185\u5e55<\/h4>\n<p>\u4f7f\u7528\u4e86 Git \u7684\u9879\u76ee\u90fd\u4f1a\u5728\u5176\u6839\u76ee\u5f55\u6709\u4e2a .git \u6587\u4ef6\u5939\uff08\u9690\u85cf\uff09\uff0c\u5b83\u627f\u8f7d\u4e86 Git \u4fdd\u5b58\u7684\u6240\u6709\u4fe1\u606f\uff0c\u4e0b\u9762\u662f\u6211\u4eec\u8fd9\u6b21\u5173\u6ce8\u7684\u90e8\u5206\uff1a<\/p>\n<pre><code class=\"bash\">.git\n\u251c\u2500\u2500 HEAD # \u5f53\u524d\u5de5\u4f5c\u7a7a\u95f4\u5904\u4e8e\u7684\u5206\u652f\uff08ref\uff09\n\u251c\u2500\u2500 objects # git\u5bf9\u8c61\uff0cgit\u6839\u636e\u8fd9\u4e9b\u5bf9\u8c61\u53ef\u4ee5\u91cd\u5efa\u51fa\u4ed3\u5e93\u7684\u5168\u90e8commit\u53ca\u5f53\u65f6\u7684\u5168\u90e8\u6587\u4ef6\n\u2502   \u251c\u2500\u2500 20 # \u7a00\u758f\u5bf9\u8c61\uff0c\u57fa\u4e8e\u5bf9\u8c61hash\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u6309\u6587\u4ef6\u5939\u5206\u7247\uff0c\u907f\u514d\u67d0\u4e2a\u76ee\u5f55\u6709\u592a\u591a\u7684\u6587\u4ef6\n\u2502   \u2502   \u2514\u2500\u2500 7151a78fb5e2d99f1185db7ebbd7d883ebde6c\n\u2502   \u251c\u2500\u2500 43 # \u53e6\u4e00\u7ec4\u7a00\u758f\u5bf9\u8c61\n\u2502   \u2502   \u2514\u2500\u2500 49b682aeaf8dc281c7a7c8d8460f443835c0c2\n\u2502   \u2514\u2500\u2500 pack # \u538b\u7f29\u8fc7\u7684\u5bf9\u8c61\n\u2514\u2500\u2500 refs # \u5206\u652f\uff0c\u6587\u4ef6\u5185\u5bb9\u662fcommit\u7684hash\n    \u251c\u2500\u2500 heads\n    \u2502   \u251c\u2500\u2500 feat\n    \u2502   \u2502   \u2514\u2500\u2500 hello-world # \u67d0\u4e2afeature\u5206\u652f\n    \u2502   \u2514\u2500\u2500 main # \u4e3b\u5206\u652f\n    \u251c\u2500\u2500 remotes\n    \u2502   \u2514\u2500\u2500 origin\n    \u2502       \u2514\u2500\u2500 HEAD # \u672c\u5730\u8bb0\u5f55\u7684\u8fdc\u7aef\u5206\u652f\n    \u2514\u2500\u2500 tags # \u6807\u7b7e\uff0c\u6587\u4ef6\u5185\u5bb9\u662fcommit\u7684hash\n<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2D6K\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<blockquote><p><strong>\u56fe\uff1a<\/strong>Pro Git on git-scm.com<br \/><strong>\u56fe\u6ce8\uff1a<\/strong>\u7ea2\u8272\u90e8\u5206\u7531 refs \u63d0\u4f9b\uff0c\u5176\u4f59\u90e8\u5206\u5168\u90e8\u7531 objects \u63d0\u4f9b\uff0ccommit \u5bf9\u8c61\uff08\u9ec4\u8272\uff09\u6307\u5411\u4fdd\u5b58\u6587\u4ef6\u7ed3\u6784\u7684 tree \u5bf9\u8c61\uff08\u84dd\u8272\uff09\uff0c\u540e\u8005\u518d\u6307\u5411\u5404\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff08\u7070\u8272\uff09<\/p><\/blockquote>\n<p>Git \u670d\u52a1\u7aef\u53ea\u4f1a\u5b58\u50a8 .git \u6587\u4ef6\u5939\u5185\u7684\u4fe1\u606f\uff08\u79f0\u4e3a bare repository\uff0c\u88f8\u4ed3\u5e93\uff09\uff0cgit clone \u662f\u4ece\u8fdc\u7aef\u62c9\u53d6\u8fd9\u4e9b\u4fe1\u606f\u5230\u672c\u5730\u518d\u91cd\u5efa\u4ed3\u5e93\u4f4d\u4e8e HEAD \u7684\u72b6\u6001\u7684\u64cd\u4f5c\uff0c\u800c git push \u662f\u628a\u672c\u5730\u7684 ref \u53ca\u5176\u76f8\u5173 commit \u5bf9\u8c61\u3001tree \u5bf9\u8c61\u548c\u6587\u4ef6\u5bf9\u8c61\u53d1\u9001\u5230\u8fdc\u7aef\u7684\u64cd\u4f5c\u3002<\/p>\n<p>Git \u5728\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u5bf9\u8c61\u65f6\u4f1a\u5c06\u5176\u538b\u7f29\uff0c\u538b\u7f29\u540e\u7684\u5bf9\u8c61\u79f0\u4e3a packfile\u3002<\/p>\n<h4>Git \u4f20\u8f93\u534f\u8bae<\/h4>\n<p>\u8ba9\u6211\u4eec\u6309\u65f6\u95f4\u5148\u540e\u987a\u5e8f\u7406\u7406 git push \u65f6\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u5728\u5ba2\u6237\u7aef\u4e0a\u8fd0\u884c git push<\/li>\n<li>\u5ba2\u6237\u7aef\u7684 Git \u7684 git-send-pack \u670d\u52a1\u5e26\u4e0a\u4ed3\u5e93\u6807\u8bc6\u7b26\uff0c\u8c03\u7528\u670d\u52a1\u7aef\u7684 git-receive-pack \u670d\u52a1<\/li>\n<li>\u670d\u52a1\u7aef\u8fd4\u56de\u76ee\u524d\u670d\u52a1\u7aef\u4ed3\u5e93\u5404\u4e2a ref \u6240\u5904\u7684 commit hash\uff0c\u6bcf\u4e2a hash \u8bb0\u4e3a 40 \u4f4d hex \u7f16\u7801\u7684\u6587\u672c\uff0c\u5b83\u4eec\u957f\u8fd9\u6837\uff1a<\/li>\n<\/ol>\n<pre><code class=\"bash\">001f# service=git-receive-pack\n000000c229859bcc73cdab4db2b70ed681077a5885f80134 refs\/heads\/main\/x00report-status report-status-v2 delete-refs side-band-64k quiet atomic ofs-delta push-options object-format=sha1 agent=git\/2.37.1.gl1\n0000<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u670d\u52a1\u7aef\u7684 main \u5206\u652f\u4f4d\u4e8e 229859bcc73cdab4db2b70ed681077a5885f80134\uff08\u5ffd\u7565\u524d\u9762\u7684\u534f\u8bae\u5185\u5bb9\uff09\u3002<\/p>\n<ol start=\"4\">\n<li>\u5ba2\u6237\u7aef\u6839\u636e\u8fd4\u56de\u7684 ref \u60c5\u51b5\uff0c\u627e\u51fa\u90a3\u4e9b\u81ea\u5df1\u6709\u4f46\u662f\u670d\u52a1\u7aef\u6ca1\u6709\u7684 commit\uff0c\u628a\u5373\u5c06\u53d8\u66f4\u7684 ref \u544a\u77e5\u670d\u52a1\u7aef\uff1a<\/li>\n<\/ol>\n<pre><code class=\"bash\">009f0000000000000000000000000000000000000000 8fa91ae7af0341e6524d1bc2ea067c99dff65f1c refs\/heads\/feat\/hello-world<\/code><\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6b63\u5728\u63a8\u9001\u4e00\u4e2a\u65b0\u5206\u652f feat\/hello-world\uff0c\u5b83\u73b0\u5728\u6307\u5411 8fa91ae7af0341e6524d1bc2ea067c99dff65f1c\uff0c\u7531\u4e8e\u5b83\u662f\u4e2a\u65b0\u5206\u652f\uff0c\u4ee5\u524d\u7684\u6307\u5411\u8bb0\u4e3a0000000000000000000000000000000000000000\u3002<\/p>\n<ol start=\"5\">\n<li>\u5ba2\u6237\u7aef\u5c06\u76f8\u5173 commit \u53ca\u5176 tree \u5bf9\u8c61\u3001\u6587\u4ef6\u5bf9\u8c61\u6253\u5305\u538b\u7f29\u4e3a packfile\uff0c\u53d1\u9001\u5230\u670d\u52a1\u7aef\uff0cpackfile \u662f\u4e8c\u8fdb\u5236\uff1a<\/li>\n<\/ol>\n<pre><code class=\"bash\">report-status side-band-64k agent=git\/2.20.10000PACK\/x00\/x00\/x00\/x02\/x00\/x00\/x00\/x03\/x98\/x0cx\/x9c\/x8d\/x8bI\n\/xc30\/x0c\/x00\/xef~\/x85\/xee\/x85&quot;[^$(\/xa5_\/x91m\/x85\/xe6\/xe0\/xa4\/x04\/xe7\/xff]^\/xd0\/xcb0\/x87\/x99y\/x98A\/x11\/xa5\/xd8\/xab,\/xbdSA]Z\/x15\/xcb(\/x94|4\/xdf\/x88\/x02&amp;\/x94\/xa0\/xec^z\/xd86!\/x08'\/xa9\/xad\/x15j]\/xeb\/xe7\/x0c\/xb5\/xa0\/xf5\/xcc\/x1eK\/xd1\/xc4\/x9c\/x16FO\/xd1\/xe99\/x9f\/xfb\/x01\/x9bn\/xe3\/x8c\/x01n\/xeb\/xe3\/xa7\/xd7aw\/xf09\/x07\/xf4\/\/\/x88\/xe1\/x82\/x8c\/xe8\/xda&gt;\/xc6:\/xa7\/xfd\/xdb\/xbb\/xf3\/xd5u\/x1a|\/xe1\/xde\/xac\/xe29o\/xa9\/x04x\/x9c340031Q\/x08rut\/xf1u\/xd5\/xcbMap\/xf6\/xdc\/xd6\/xb4n}\/xef\/xa1\/xc6\/xe3\/xcbO\/xdcp\/xe3w\/xb10=p\/xc8\/x10\/xa2(%\/xb1$U\/xaf\/xa4\/xa2\/x84\/xa1T\/xe5\/x8eO\/xe9\/xcf\/xd3\/x0c\/\/R\/x7f\/xcf\/xed\/xdb\/xb9]n\/xd1\/xea3\/xa2\/x00\/xd3\/x86\/x1db\/xbb\/x02x\/x9c\/x01+\/x00\/xd4\/xff2022\/xe5\/xb9\/xb4 09\/xe6\/x9c\/x88 01\/xe6\/x97\/xa5 \/xe6\/x98\/x9f\/xe6\/x9c\/x9f\/xe5\/x9b\/x9b 15:52:13 CST\n\/xa4d\/x11\/xa1\/xe8\/x86\/xdeQ\/x90\/xb1\/xe0Z\/xfd\/x7f\/x91\/x90\/xc3\/xd6\/x17\/xe8\/x02&amp;K\/xd0<\/code><\/pre>\n<ol start=\"6\">\n<li>\u670d\u52a1\u7aef\u89e3\u5305 packfile\uff0c\u66f4\u65b0 ref\uff0c\u8fd4\u56de\u5904\u7406\u7ed3\u679c\uff1a<\/li>\n<\/ol>\n<pre><code class=\"bash\">003a\/x01000eunpack ok\n0023ok refs\/heads\/feat\/hello-world\n<\/code><\/pre>\n<p>Git \u4f20\u8f93\u534f\u8bae\u53ef\u4ee5\u7531 SSH \u6216\u8005 HTTP(S) \u627f\u8f7d\u3002<\/p>\n<p>\u8fd8\u662f\u633a\u76f4\u63a5\u7684\uff0c\u5bf9\u5427\uff1f<\/p>\n<h4>\u6781\u72d0GitLab \u7684\u7ec4\u6210\u90e8\u5206<\/h4>\n<p>\u6781\u72d0GitLab \u662f\u4e00\u4e2a\u5e38\u7528\u7684 Git \u4ee3\u7801\u6258\u7ba1\u670d\u52a1\uff0c\u540c\u65f6\u652f\u6301\u534f\u4f5c\u5f00\u53d1\u3001\u4efb\u52a1\u8ddf\u8e2a\u3001CI\/CD \u7b49\u529f\u80fd\u3002<\/p>\n<p>\u6781\u72d0GitLab \u7684\u670d\u52a1\u5e76\u4e0d\u662f\u4e00\u4e2a\u5355\u4f53\uff0c\u6211\u4eec\u4ee5\u5927\u7248\u672c 15 \u4e3a\u4f8b\uff0c\u548c git push \u6709\u5173\u7684\u7ec4\u4ef6\u6709\u4e0b\u9762\u8fd9\u4e9b\uff1a<\/p>\n<ul>\n<li><strong>\u6781\u72d0GitLab\uff1a<\/strong>\u4f7f\u7528 Ruby \u5f00\u53d1\uff0c\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff0c \u6781\u72d0GitLab \u7684 Web \u670d\u52a1 \/API \u670d\u52a1\uff08\u4e0b\u6587\u8bb0\u4e3a Rails\uff09\u4ee5\u53ca\u4efb\u52a1\u961f\u5217\/\u80cc\u666f\u4efb\u52a1\uff08\u4e0b\u6587\u8bb0\u4e3a Sidekiq\uff09\u3002<\/li>\n<li><strong>Gitaly\uff1a<\/strong>\u4f7f\u7528 Go \u5f00\u53d1\uff0c\u6781\u72d0GitLab \u7684 Git \u670d\u52a1\u540e\u7aef\uff0c\u8d1f\u8d23 Git \u4ed3\u5e93\u7684\u5b58\u50a8\u548c\u8bfb\u5199\uff0c\u5c06\u5404\u79cd Git \u64cd\u4f5c\u66b4\u9732\u4e3a GRPC \u8c03\u7528\u3002\u65e9\u671f Rails \u76f4\u63a5\u901a\u8fc7 Git \u547d\u4ee4\u884c\u64cd\u4f5c NFS \u4e0a\u7684 Git \u4ed3\u5e93\uff0c\u89c4\u6a21\u5927\u4e86\u4e4b\u540e\u7f51\u7edc IO \u5ef6\u8fdf\u611f\u4eba\uff0c\u9042\u5206\u89e3\u51fa\u4e86 Gitaly.<\/li>\n<li><strong>Workhorse\uff1a<\/strong>\u4f7f\u7528 Go \u5f00\u53d1\uff0c\u4f5c\u4e3a Rails \u7684\u524d\u7f6e\u4ee3\u7406\uff0c\u5904\u7406 Git push\/pull\u3001\u6587\u4ef6\u4e0b\u8f7d\/\u4e0a\u4f20\u8fd9\u7c7b\u201c\u7f13\u6162\u201d\u7684 HTTP \u8bf7\u6c42\u3002\u65e9\u671f\u8fd9\u4e9b\u8bf7\u6c42\u7531 Rails \u5904\u7406\uff0c\u5b83\u4eec\u4f1a\u957f\u65f6\u95f4\u5360\u7528\u53ef\u89c2\u7684 CPU \u548c\u5185\u5b58\uff0c\u4e3a\u4e86\u670d\u52a1\u7a33\u5b9a\uff0c\u6781\u72d0GitLab \u4e0d\u5f97\u4e0d\u5c06 git clone \u7684\u8d85\u65f6\u65f6\u95f4\u8bbe\u4e3a 1 \u5206\u949f\uff0c\u4f46\u662f\u8fd9\u53c8\u5e26\u6765\u4e86\u5927\u4ed3\u5e93\u65e0\u6cd5\u5b8c\u6574\u514b\u9686\u7684\u53ef\u7528\u6027\u95ee\u9898\u3002\u800c goroutine \u7684\u6210\u672c\u4f4e\u5f88\u591a\uff0c\u5c31\u88ab\u7528\u6765\u4e13\u95e8\u5904\u7406\u8fd9\u7c7b\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u6781\u72d0GitLab Shell\uff1a<\/strong>\u4f7f\u7528 Go \u5f00\u53d1\uff0c\u7528\u6765\u54cd\u5e94\u548c\u9274\u6743 Git SSH \u8fde\u63a5\uff0c\u5728\u7528\u6237 Git \u5ba2\u6237\u7aef\u548c Gitaly \u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/li>\n<li><strong>\u6781\u72d0GitLab Runner\uff1a<\/strong>\u4f7f\u7528 Go \u5f00\u53d1\uff0c\u8d1f\u8d23 CI\/CD \u5de5\u4f5c\u7684\u6267\u884c\u3002<\/li>\n<li>\u6781\u72d0GitLab \u7684\u6570\u636e\u5b58\u50a8\u5728 Postgres \u4e2d\uff0c\u4f7f\u7528 Redis \u505a\u7f13\u5b58\u3002Rails \u548c Sidekiq \u76f4\u63a5\u8fde\u63a5\u6570\u636e\u5e93\u548c\u7f13\u5b58\uff0c\u5176\u4ed6\u7ec4\u4ef6\u7ecf\u7531 Rails \u66b4\u9732\u7684 API \u8fdb\u884c\u6570\u636e\u8bfb\u5199\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2D7Q\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<blockquote><p>\u7b80\u660e\u7684\u6781\u72d0GitLab \u7ec4\u4ef6\u5173\u7cfb<br \/>\u56fe\/\u6781\u72d0GitLab architecture overview on docs.gitlab.cn<\/p><\/blockquote>\n<h2>\u5f00\u59cb git push!<\/h2>\n<p>\u4e09\u5206\u949f\u8fc7\u5f97\u771f\u5feb\uff01\u73b0\u5728\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u57fa\u7840\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5f81\u9014\u5427\uff01<\/p>\n<h4>\u4f60\u559c\u6b22SSH?<\/h4>\n<p>\u5982\u679c\u4f60\u7684\u8fdc\u7aef\u5730\u5740\u662f <a href=\"https:\/\/segmentfault.com\/cdn-cgi\/l\/email-protection#03646a7743696a6b766f62612d667b626e736f662d606c6e\"><span class=\"__cf_email__\" data-cfemail=\"8fe8e6fbcfe5e6e7fae3eeeda1eaf7eee2ffe3eaa1ece0e2\">[email&#160;protected]<\/span><\/a>:user\/repo.git \u8fd9\u6837\u7684\uff0c\u90a3\u4e48\u4f60\u5728\u7528 SSH \u4e0e \u6781\u72d0GitLab \u8fdb\u884c\u901a\u8baf\u3002\u5728\u4f60\u6267\u884c git push \u65f6\uff0c\u672c\u8d28\u4e0a\uff0c\u4f60\u7684 Git \u5ba2\u6237\u7aef\u7684 upload-pack \u670d\u52a1\u5728\u6267\u884c\u4e0b\u5217\u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"bash\">ssh -x <a href=\"\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"22454b5662484b4a574e43400c475a434f524e470c414d4f\">[email&#160;protected]<\/a> &quot;git-receive-pack 'user\/repo.git'&quot;<\/code><\/pre>\n<p>\u8fd9\u91cc\u9762\u6709\u633a\u591a\u95ee\u9898\u503c\u5f97\u8bf4\u9053\u7684\uff1a<\/p>\n<p>\u5927\u5bb6\u7684\u7528\u6237\u540d\u90fd\u53eb git\uff0c\u670d\u52a1\u7aef\u600e\u4e48\u5206\u6e05\u8c01\u662f\u8c01\uff1f\uff08\u5b89\u80fd\u8fa8\u6211\u662f\u96c4\u96cc\uff1f\uff09<br \/>ssh? \u6211\u53ef\u4ee5\u5728\u670d\u52a1\u7aef\u4e0a\u8fd0\u884c\u4efb\u610f\u547d\u4ee4\u5417\uff1f<\/p>\n<p>\u8fd9\u4e24\u4e2a\u95ee\u9898\u7531\u6781\u72d0GitLab Shell \u7684 gitlab-sshd \u6765\u89e3\u51b3\u3002\u5b83\u662f\u4e2a\u5b9a\u5236\u5316\u7684 SSH Daemon\uff0c\u548c\u4e00\u822c\u7684 sshd \u8bb2\u540c\u6837\u7684 SSH \u534f\u8bae\uff0c\u5ba2\u6237\u7aef\u6ca1\u6cd5\u5206\u6e05\u5b83\u4eec\u3002\u5ba2\u6237\u7aef\u5728\u505a SSH \u63e1\u624b\u65f6\u4f1a\u63d0\u4f9b\u81ea\u5df1\u7684\u516c\u94a5\uff0cgitlab-sshd \u4f1a\u8c03\u7528 Rails \u7684\u5185\u90e8 API GET \/api\/v4\/internal\/authorized_keys \u67e5\u8be2\u516c\u94a5\u662f\u5426\u5728\u6781\u72d0GitLab \u6ce8\u518c\u8fc7\u5e76\u8fd4\u56de\u5bf9\u5e94\u516c\u94a5 ID\uff08\u53ef\u5b9a\u4f4d\u5230\u7528\u6237\uff09\uff0c\u540c\u65f6\u6821\u9a8c SSH \u63e1\u624b\u7684\u7b7e\u540d\u662f\u5426\u7531\u540c\u4e00\u4efd\u516c\u94a5\u5bf9\u5e94\u7684\u79c1\u94a5\u751f\u6210\u3002<\/p>\n<p>\u53e6\u5916\uff0cgitlab-sshd \u9650\u5236\u4e86\u5ba2\u6237\u7aef\u53ef\u4ee5\u8fd0\u884c\u7684\u547d\u4ee4\uff0c\u5176\u5b9e\uff0c\u5b83\u5728\u4f7f\u7528\u7528\u6237\u8fd0\u884c\u7684\u547d\u4ee4\u6765\u5339\u914d\u81ea\u5df1\u5e94\u8be5\u8fd0\u884c\u54ea\u4e2a\u65b9\u6cd5\uff0c\u6ca1\u6709\u5bf9\u5e94\u65b9\u6cd5\u7684\u547d\u4ee4\u90fd\u4f1a\u88ab\u62d2\u7edd\u3002<\/p>\n<p>\u53ef\u60dc\uff0c\u770b\u6765\u6211\u4eec\u662f\u6ca1\u6cd5\u901a\u8fc7 SSH \u5728\u6781\u72d0GitLab \u7684\u670d\u52a1\u5668\u4e0a\u8fd0\u884c bash \u6216\u8005 rm -rf \/ \u4e86\u3002\u2511(\uffe3\u0414 \uffe3)\u250d<\/p>\n<p>\u8bf4\u70b9\u6709\u8da3\u7684\uff0c\u65e9\u671f\u6781\u72d0GitLab \u5f53\u771f\u4f7f\u7528 sshd \u6765\u54cd\u5e94 Git \u8bf7\u6c42\u3002\u4e3a\u4e86\u89e3\u51b3\u4e0a\u9762\u8fd9\u4e24\u4e2a\u95ee\u9898\uff0c\u4ed6\u4eec\u8fd9\u4e48\u5199 authorized_keys\uff1a<\/p>\n<pre><code># Managed by gitlab-rails\ncommand=&quot;\/bin\/gitlab-shell key-1&quot;,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-\nrsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt1016k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt\/aZZ9wa++Oi7\nQkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=\ncommand=&quot;\/bin\/gitlab-shell key-2&quot;,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-\nrsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt1026k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt\/aZZ9wa++Oi7\nQkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=<\/code><\/pre>\n<p>\u5bf9\uff0c\u4f60\u6ca1\u731c\u9519\uff0c\u6574\u4e2a\u6781\u72d0GitLab \u7684\u7528\u6237\u516c\u94a5\u90fd\u4f1a\u88ab\u653e\u5230\u8fd9\u4e2a\u6587\u4ef6\u91cc\uff0c\u5b83\u53ef\u80fd\u4f1a\u4e0a\u767e MB \u7684\u5927\u5c0f\uff01\u6734\u5b9e\u65e0\u534e\uff01<\/p>\n<p>Command \u53c2\u6570\u8986\u76d6\u4e86\u6bcf\u6b21 SSH \u5ba2\u6237\u7aef\u60f3\u8fd0\u884c\u7684\u547d\u4ee4\uff0c\u8ba9 sshd \u542f\u52a8 gitlab-shell\uff0c\u542f\u52a8\u53c2\u6570\u662f\u516c\u94a5 ID. gitlab-shell \u53ef\u4ee5\u5728\u7531 sshd \u8bbe\u5b9a\u7684\u73af\u5883\u53d8\u91cf SSH_ORIGINAL_COMMAND \u83b7\u53d6\u5230\u5ba2\u6237\u7aef\u539f\u672c\u60f3\u6267\u884c\u7684\u547d\u4ee4\uff0c\u8fdb\u800c\u8fd0\u884c\u76f8\u5173\u65b9\u6cd5\u3002<\/p>\n<p>\u7531\u4e8e sshd \u5728\u5339\u914d authorized_keys \u65f6\u7528\u7684\u662f\u7ebf\u6027\u68c0\u7d22\uff0c\u5728 authorized_keys \u5f88\u5927\u65f6\uff0c\u5148\u6ce8\u518c\u7684\u7528\u6237\uff08\u516c\u94a5\u5728\u6587\u4ef6\u7684\u524d\u9762\uff09\u7684\u5339\u914d\u4f18\u5148\u7ea7\u4f1a\u88ab\u540e\u6ce8\u518c\u7684\u7528\u6237\u9ad8\u5f88\u591a\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u8001\u7528\u6237\u7684 SSH \u9274\u6743\u8981\u6bd4\u65b0\u7528\u6237\u7684\u5feb\uff0c\u800c\u4e14\u662f\u53ef\u5bdf\u89c9\u7684\u5feb\u3002\uff08\u771f\u00b7\u8001\u7528\u6237\u798f\u5229\uff09<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2D8Y\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<blockquote><p>\u9ec4\u91d1\u8001\u7528\u6237\u7684\u7279\u522b\u798f\u5229\u2014\u2014\u8d85\u957fgit push\u65f6\u95f4<br \/>\u56fe\/xkcd-excuse.com<\/p><\/blockquote>\n<p>\u5982\u4eca gitlab-sshd \u4f9d\u8d56\u7684 Rails API \u80cc\u540e\u662f Postgres \u7d22\u5f15\uff0c\u8fd9\u4e2a bug\uff08feature?\uff09\u4e0d\u590d\u5b58\u5728\u3002<\/p>\n<p>\u901a\u8fc7\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u540e\uff0cgitlab-sshd \u4f1a\u68c0\u67e5\u7528\u6237\u5bf9\u76ee\u6807\u4ed3\u5e93\u662f\u5426\u6709\u5199\u6743\u9650\uff08POST \/api\/v4\/internal\/allowed\uff09\uff0c\u540c\u65f6\u83b7\u77e5\u8fd9\u4e2a\u4ed3\u5e93\u5728\u54ea\u4e00\u4e2a Gitaly \u5b9e\u4f8b\uff0c\u4ee5\u53ca\u7528\u6237 ID \u548c\u4ed3\u5e93\u4fe1\u606f\u3002<\/p>\n<p>\u6700\u540e\uff0cgitlab-sshd \u4f1a\u8c03\u7528\u5bf9\u5e94\u7684 Gitaly \u5b9e\u4f8b\u7684 SSHReceivePack \u65b9\u6cd5\uff0c\u5728 Git \u5ba2\u6237\u7aef\uff08SSH\uff09\u4e0e Gitaly\uff08GRPC\uff09\u4e4b\u95f4\u4f5c\u4e3a\u4e2d\u7ee7\u548c\u7ffb\u8bd1\u3002<\/p>\n<p>\u6700\u540e\u4e24\u6b65 gitlab-shell \u7684\u884c\u4e3a\u548c gitlab-sshd \u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u4ece\u5b8f\u89c2\u89c6\u89d2\u770b\uff0c\u7ecf\u7531 SSH \u7684 git push \u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u6267\u884c git push\uff1b<\/li>\n<li>Git \u5ba2\u6237\u7aef\u901a\u8fc7 SSH \u94fe\u63a5\u5230 gitlab-shell\uff1b<\/li>\n<li>gitlab-shell \u4f7f\u7528\u5ba2\u6237\u7aef\u516c\u94a5\u8c03\u7528 GET \/api\/v4\/internal\/authorized_keys \u83b7\u5f97\u516c\u94a5 ID\uff0c\u8fdb\u884c SSH \u63e1\u624b\uff1b<\/li>\n<li>gitlab-shell \u4f7f\u7528\u516c\u94a5 ID \u548c\u4ed3\u5e93\u5730\u5740\u8c03\u7528 POST \/api\/v4\/internal\/allowed\uff0c\u786e\u8ba4\u7528\u6237\u6709\u5230\u4ed3\u5e93\u7684\u5199\u6743\u9650\uff1b<\/li>\n<li>API \u8fd4\u56de\uff1aGitaly \u5730\u5740\u548c\u9274\u6743 token\u3001repo \u5bf9\u8c61\u3001\u94a9\u5b50\u56de\u8c03\u4fe1\u606f\uff08\u903b\u8f91\u7528\u6237\u540d GL_ID\u3001\u903b\u8f91\u9879\u76ee\u540d GL_REPOSITORY\uff09\uff1b<\/li>\n<li>gitlab-shell \u7528\u4e0a\u5217\u4fe1\u606f\u8c03\u7528 Gitaly \u7684 SSHReceivePack \u65b9\u6cd5\uff0c\u6210\u4e3a\u5ba2\u6237\u7aef\u548c Gitaly \u7684\u4e2d\u7ee7\uff1b<\/li>\n<li>Gitaly \u5728\u9002\u5f53\u7684\u5de5\u4f5c\u76ee\u5f55\u8fd0\u884c git-receive-pack\uff0c\u5e76\u4e14\u9884\u5148\u8bbe\u5b9a\u597d\u73af\u5883\u53d8\u91cf GITALY_HOOKS_PAYLOAD\uff0c\u5176\u4e2d\u5305\u542b GL_ID, GL_REPOSITORY \u7b49\uff1b<\/li>\n<li>\u670d\u52a1\u7aef Git \u5c1d\u8bd5\u66f4\u65b0 refs\uff0c\u8fd0\u884c Git hooks\uff1b<\/li>\n<li>\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p>Gitaly \u548c refs \u66f4\u65b0\u6211\u4eec\u7a0d\u540e\u4f1a\u804a\u5230\u3002<\/p>\n<h4>\u4f60\u66f4\u559c\u6b22HTTP(S)?<\/h4>\n<p>HTTP(S)\u7684\u8fdc\u7aef\u5730\u5740\u5f62\u5982 <a href=\"https:\/\/link.segmentfault.com\/?enc=pK29eMESWy6900x5QmRWSA%3D%3D.ypQMesNueKsR9wpVveJaQwTsBnG6suOIeO6Ghq98mu2oWMHvOi4VvL7dZtTeZmBo\" rel=\"nofollow\">https:\/\/jihulab.example.com\/u&#8230;<\/a> \u548c SSH \u4e0d\u4e00\u6837\uff0cHTTP \u8bf7\u6c42\u662f\u65e0\u72b6\u6001\u7684\uff0c\u800c\u4e14\u603b\u662f\u4e00\u95ee\u4e00\u7b54\u3002\u5728\u4f60\u6267\u884c git push \u65f6\uff0cGit \u5ba2\u6237\u7aef\u4f1a\u6309\u987a\u5e8f\u548c\u4e24\u4e2a\u63a5\u53e3\u6253\u4ea4\u9053\uff1a<\/p>\n<ul>\n<li>GET <a href=\"https:\/\/link.segmentfault.com\/?enc=oYPu8dl%2F8V9YSzwk265EaA%3D%3D.44hgTz1l0kBQ%2BP8BE3LUjeYPdGL0iQt1pV7vt%2BfdWyk3sdi50Hpu5LbbAi29HAj3HGGYh7%2BXL%2FMxBBVzieXMRyhf4JSwdmyBnyP8XttZ0rY%3D\" rel=\"nofollow\">https:\/\/jihulab.example.com\/u&#8230;<\/a>\uff1a\u670d\u52a1\u7aef\u4f1a\u5728body\u4e2d\u8fd4\u56de\u76ee\u524d\u670d\u52a1\u7aef\u4ed3\u5e93\u5404\u4e2a\u5206\u652f\u6240\u5904\u7684commit\u7684hash\u3002<\/li>\n<li>POST <a href=\"https:\/\/link.segmentfault.com\/?enc=hoqotHLPytLGOCLh6EalfA%3D%3D.jOpISVbLrxHTxvuEz6y5EXM3cpHABi9D%2BptMwZrv9pko8RGiZBbG8nkFO8cEO%2FOYQpES2Hsgols8fHK3hzTrYw%3D%3D\" rel=\"nofollow\">https:\/\/jihulab.example.com\/u&#8230;<\/a>\uff1a\u5ba2\u6237\u7aef\u4f1a\u5728body\u4e2d\u63d0\u4ea4\u8981\u66f4\u65b0\u7684\u5206\u652f\u53ca\u5176\u65e7commit hash\u548c\u65b0commit hash\uff0c\u540c\u65f6\u9644\u4e0a\u6240\u9700\u7684packfile. \u670d\u52a1\u7aef\u4f1a\u5728body\u4e2d\u8fd4\u56de\u5904\u7406\u7ed3\u679c\uff0c\u4ee5\u53ca\u6211\u4eec\u8001\u719f\u4eba&#8221;to create a merge request&#8221;\u63d0\u793a\uff1a<\/li>\n<\/ul>\n<pre><code>003a\/x01000eunpack ok\n0023ok refs\/heads\/feat\/hello-world\n00000085\/x02\nTo create a merge request for feat\/hello-world, visit:\n  https:\/\/jihulab.example.com\/user\/repo\/-\/merge_requests\/new?merge_request%5Bs0029\/x02ource_branch%5D=feat%2Fhello-world\n0000<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4e24\u4e2a\u8bf7\u6c42\u4f1a\u88ab Workhorse \u622a\u83b7\uff0c\u6bcf\u6b21\u5b83\u90fd\u505a\u8fd9\u4e24\u4ef6\u4e8b\uff1a<\/p>\n<ul>\n<li>\u628a\u8bf7\u6c42\u539f\u6837\u53d1\u5230 Rails\uff0c\u540e\u8005\u4f1a\u8fd4\u56de\u9274\u6743\u7ed3\u679c\u3001\u7528\u6237 ID\u3001 \u4ed3\u5e93\u5bf9\u5e94 Gitaly \u5b9e\u4f8b\u4fe1\u606f\uff08\u6709\u70b9\u602a\uff0c\u5bf9\u5427\uff1fRails \u7684 info\/refs \u548c git-receive-pack \u63a5\u53e3\u5c45\u7136\u662f\u7528\u6765\u9274\u6743\u7684\uff0c\u6211\u731c\u8fd9\u540e\u9762\u591a\u5c11\u6709\u4e9b\u5386\u53f2\u539f\u56e0\uff09\uff1b<\/li>\n<li>Workhorse \u6839\u636e\u4e0a\u4e00\u6b65 Rails \u8fd4\u56de\u7684\u4fe1\u606f\uff0c\u5efa\u7acb\u4e0e Gitaly \u7684\u8fde\u63a5\uff0c\u5728\u5ba2\u6237\u7aef\u548c Gitaly \u4e4b\u95f4\u5145\u5f53\u4e2d\u7ee7\u3002<\/li>\n<\/ul>\n<p><strong>\u603b\u7ed3\u4e00\u4e0b\uff0c\u7ecf\u7531 HTTP(S) \u7684 git push \u662f\u8fd9\u6837\u7684\uff1a<\/strong><\/p>\n<ol>\n<li>\u7528\u6237\u6267\u884c git push\uff1b<\/li>\n<li>Git\u5ba2\u6237\u7aef\u8c03\u7528GET <a href=\"https:\/\/link.segmentfault.com\/?enc=tA5x1uRwdF%2BIeg8PkMlG%2Fg%3D%3D.%2FpHMV0Hw4F4SfzY5hH9ASAdffQUVmVlwE86I8LpKPOVdUX2XCBL297GBgvdOuULaU62V5GDHn3UpAICAANREYqvOYy6mX8MdXNzrBOWcZ8E%3D\" rel=\"nofollow\">https:\/\/jihulab.example.com\/u&#8230;<\/a>\uff0c\u5e26\u4e0a\u5bf9\u5e94\u7684authorization header\uff1b<\/li>\n<li>Workhorse \u622a\u83b7\u8bf7\u6c42\uff0c\u539f\u6837\u53d1\u9001\u8bf7\u6c42\u5230 Rails\uff0c\u83b7\u5f97\u9274\u6743\u7ed3\u679c\u3001\u7528\u6237 ID\u3001\u4ed3\u5e93\u5bf9\u5e94 Gitaly \u5b9e\u4f8b\u4fe1\u606f\uff1b<\/li>\n<li>Workhorse \u6839\u636e\u4e0a\u4e00\u6b65 Rails \u7684\u8fd4\u56de\u4fe1\u606f\uff0c\u8c03\u7528 Gitaly \u7684 GRPC \u670d\u52a1 InfoRefsReceivePack\uff0c\u5728\u5ba2\u6237\u7aef\u548c Gitaly \u4e4b\u95f4\u5145\u5f53\u4e2d\u7ee7\uff1b<\/li>\n<li>Gitaly \u5728\u9002\u5f53\u7684\u5de5\u4f5c\u76ee\u5f55\u8fd0\u884c git-receive-pack\uff0c\u8fd4\u56de refs \u4fe1\u606f\uff1b<\/li>\n<li>Git\u5ba2\u6237\u7aef\u8c03\u7528POST <a href=\"https:\/\/link.segmentfault.com\/?enc=0MjQ1aklkRTmsOSxj65AyQ%3D%3D.PrgN45a0gxjuRAI9CfwVwEn9Qan0x4kT20S%2BHRFJnQ0i22n%2BFCGeNlc0YAZf28fJuDFvQCRyaEGfmqESyE38SQ%3D%3D\" rel=\"nofollow\">https:\/\/jihulab.example.com\/u&#8230;<\/a>\uff1b<\/li>\n<li>Workhorse  \u622a\u83b7\u8bf7\u6c42\uff0c\u539f\u6837\u53d1\u9001\u8bf7\u6c42\u5230 Rails\uff0c\u83b7\u5f97\u9274\u6743\u7ed3\u679c\u3001\u7528\u6237 ID\u3001\u4ed3\u5e93\u5bf9\u5e94 Gitaly \u5b9e\u4f8b\u4fe1\u606f\uff1b<\/li>\n<li>Workhorse \u6839\u636e\u4e0a\u4e00\u6b65 Rails \u7684\u8fd4\u56de\u4fe1\u606f\uff0c\u8c03\u7528 Gitaly \u7684 GRPC \u670d\u52a1 PostReceivePack\uff0c\u5728\u5ba2\u6237\u7aef\u548c Gitaly \u4e4b\u95f4\u5145\u5f53\u4e2d\u7ee7\uff1b<\/li>\n<li>Gitaly \u5728\u9002\u5f53\u7684\u5de5\u4f5c\u76ee\u5f55\u8fd0\u884c git-receive-pack\uff0c\u5e76\u4e14\u9884\u5148\u8bbe\u5b9a\u597d\u73af\u5883\u53d8\u91cf GITALY_HOOKS_PAYLOAD\uff0c\u5176\u4e2d\u5305\u542b GL_ID, GL_REPOSITORY \u7b49\uff1b<\/li>\n<li>\u670d\u52a1\u7aef Git \u5c1d\u8bd5\u66f4\u65b0 refs\uff0c\u8fd0\u884c Git hooks\uff1b<\/li>\n<li>\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<h2>Gitaly \u548c Git Hooks<\/h2>\n<p>\u547c\u2026\u8bf4\u5b8c\u4e86\u524d\u9762\u7684\u8fde\u63a5\u5c42\u548c\u6743\u9650\u63a7\u5236\uff0c\u6211\u4eec\u7ec8\u4e8e\u5f97\u4ee5\u63a5\u8fd1\u6781\u72d0GitLab \u7684 Git \u6838\u5fc3\uff0cGitaly\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2D9o\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Gitaly \u8fd9\u4e2a\u540d\u5b57\u5176\u5b9e\u662f\u5728\u73a9\u6897\uff0c\u81f4\u656c\u4e86 Git \u548c\u4fc4\u7f57\u65af\u5c0f\u9547 Aly\uff0c\u540e\u8005\u5728 2010 \u5e74\u4fc4\u7f57\u65af\u4eba\u53e3\u666e\u67e5\u4e2d\u5f97\u51fa\u7684\u5e38\u4f4f\u4eba\u53e3\u662f 0\uff0cGitaly \u7684\u5de5\u7a0b\u5e08\u5e0c\u671b Gitaly \u7684\u5927\u90e8\u5206\u64cd\u4f5c\u7684\u78c1\u76d8 IO \u4e5f\u662f 0\u3002<\/p>\n<p>\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u7684\u6897\u5b9e\u5728\u662f\u592a\u751f\u786c\u4e86\uff0c\u4e00\u822c\u4eba\u6050\u6015\u5403\u4e0d\u4e0b\u2026\u2026<\/p>\n<p>Gitaly \u8d1f\u8d23\u6781\u72d0GitLab \u4ed3\u5e93\u7684\u5b58\u50a8\u548c\u64cd\u4f5c\uff0c\u5b83\u901a\u8fc7 fork\/exec \u8fd0\u884c\u672c\u5730\u7684 Git \u4e8c\u8fdb\u5236\u7a0b\u5e8f\uff0c\u91c7\u7528 cgroups \u9632\u6b62\u5355\u4e2a Git \u5403\u6389\u592a\u591a CPU \u548c\u5185\u5b58\u3002\u4ed3\u5e93\u5b58\u50a8\u5728\u672c\u5730\uff0c\u8def\u5f84\u5f62\u5982\/var\/opt\/gitlab\/git-data\/repositories\/@hashed\/b1\/7e\/b17ef6d19c7a5b1ee83b907c595526dcb1eb06db8227d650d5dda0a9f4ce8cd9.git\uff0c\u65e9\u671f\u6781\u72d0GitLab\/Gitaly \u4e5f\u4f7f\u7528 #{namespace}\/#{project_name}.git \u7684\u5f62\u5f0f\uff0c\u4f46\u662f namespace \u548c project_name \u90fd\u53ef\u4ee5\u88ab\u7528\u6237\u4fee\u6539\uff0c\u8fd9\u5e26\u6765\u4e86\u989d\u5916\u7684\u8fd0\u884c\u5f00\u9500\u3002<\/p>\n<p>git push \u5bf9\u5e94 Gitaly \u7684 SSHReceivePack\uff08SSH\uff09\u548c PostReceivePack\uff08HTTPS\uff09\u65b9\u6cd5\uff0c\u5b83\u4eec\u7684\u5e95\u90e8\u90fd\u662f Git \u7684 git-receive-pack\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6700\u6838\u5fc3\u7684 refs \u548c object \u66f4\u65b0\u7531 Git \u4e8c\u8fdb\u5236\u6765\u5b8c\u6210\u3002git-receive-pack \u63d0\u4f9b\u4e86\u94a9\u5b50\u4f7f\u5f97\u8fd9\u4e2a\u8fc7\u7a0b\u80fd\u591f\u88ab Gitaly \u4ecb\u5165\uff0c\u8fd9\u91cc\u9762\u8fd8\u7275\u626f Rails\uff0c\u4e00\u4e2a\u5355\u8fb9\u7684\u8bf7\u6c42\uff08\u4e0d\u542b\u8fd4\u56de\uff09\u6d41\u7a0b\u5927\u6982\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2D9p\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Gitaly \u5728\u542f\u52a8 git-receive-pack \u65f6\u4f1a\u901a\u8fc7\u73af\u5883\u53d8\u91cf GITALY_HOOKS_PAYLOAD \u4f20\u5165\u4e00\u4e2a Base64 \u7f16\u7801\u7684 JSON\uff0c\u5176\u4e2d\u6709\u4ed3\u5e93\u4fe1\u606f\u3001Gitaly Unix Socket \u5730\u5740\u548c\u94fe\u63a5 token\u3001\u7528\u6237\u4fe1\u606f\u3001\u8981\u6267\u884c\u7684\u54ea\u4e9b Hook\uff08\u5bf9\u4e8e git push\uff0c\u603b\u662f\u4e0b\u9762\u8fd9\u51e0\u4e2a\uff09\uff0c\u5e76\u4e14\u8bbe\u5b9a Git \u7684 core.hooksPath \u53c2\u6570\u5230 Gitaly \u81ea\u5df1\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u51c6\u5907\u597d\u7684\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\u5939\uff0c\u90a3\u91cc\u7684\u6240\u6709 Hook \u6587\u4ef6\u90fd\u7b26\u53f7\u94fe\u63a5\u5230\u4e86 gitaly-hooks\u4e0a\u3002<\/p>\n<p>gitaly-hooks \u5728\u88ab git-receive-pack \u542f\u52a8\u540e\u4ece\u73af\u5883\u53d8\u91cf\u8bfb\u53d6 GITALY_HOOKS_PAYLOAD\uff0c\u901a\u8fc7 Unix Socket \u548c GRPC \u8fde\u63a5\u56de Gitaly\uff0c\u544a\u77e5 Gitaly \u76ee\u524d\u6267\u884c\u7684 Hook\uff0c\u4ee5\u53ca Git \u63d0\u4f9b\u7ed9 Hook \u7684\u53c2\u6570\u3002<\/p>\n<h4>pre-receive hook<\/h4>\n<p>\u8fd9\u4e2a\u94a9\u5b50\u4f1a\u5728 Git \u6536\u5230 git push \u65f6\u89e6\u53d1\u4e00\u6b21\uff0c\u5728\u8c03\u7528 gitlab-hooks \u65f6\uff0cGit \u4f1a\u5411\u5176\u6807\u51c6\u8f93\u5165\u4e2d\u5199\u5165\u53d8\u66f4\u4fe1\u606f\uff0c\u5373\u201c\u67d0\u4e2a ref \u60f3\u4ece commit hash A \u66f4\u65b0\u5230 commit hash B\u201d\uff0c\u4e00\u884c\u4e00\u4e2a\uff1a<\/p>\n<pre><code>&lt;\u65e7commit ref hash&gt; SP &lt;\u65b0commit ref hash&gt; SP &lt;ref\u540d\u5b57&gt; LF<\/code><\/pre>\n<p>\u5176\u4e2d SP \u662f\u7a7a\u683c\uff0cLF \u662f\u6362\u884c\u7b26\u3002<\/p>\n<p>\u4e0a\u8ff0\u4fe1\u606f\u56de\u5230 Gitaly \u4e4b\u540e\uff0cGitaly \u4f1a\u4f9d\u6b21\u8c03\u7528 Rails \u7684\u4e24\u4e2a\u63a5\u53e3\uff1a<\/p>\n<ul>\n<li>POST \/api\/v4\/internal\/allowed\uff1a\u8fd9\u4e2a\u63a5\u53e3\u4e4b\u524d\u5728\u8fde\u63a5\u5c42\u9274\u6743\u65f6\u5c31\u8c03\u8fc7\uff0c\u8fd9\u6b21\u989d\u5916\u9644\u4e0a\u53d8\u66f4\u4fe1\u606f\uff0cRails \u53ef\u4ee5\u4f9d\u636e\u5176\u8fdb\u884c\u66f4\u7ec6\u7c92\u5ea6\u7684\u5224\u65ad\uff0c\u4f8b\u5982\u7981\u7528 force push\uff0c\u4ee5\u53ca\u5224\u65ad\u5206\u652f\u662f\u5426\u53d7\u4fdd\u62a4\u7b49\u3002<\/li>\n<li>POST \/api\/v4\/internal\/pre_receive\uff1a\u901a\u77e5 Rails \u5f53\u524d\u4ed3\u5e93\u5373\u5c06\u6709\u5199\u66f4\u65b0\uff0cRails \u5bf9\u8fd9\u4e2a\u4ed3\u5e93\u7684\u5f15\u7528\u8ba1\u6570 +1\uff0c\u8fd9\u53ef\u4ee5\u907f\u514d\u4ed3\u5e93\u7684 Git \u5199\u64cd\u4f5c\u88ab\u5176\u4ed6\u5730\u65b9\u7684\u91cd\u5927\u53d8\u66f4\u6253\u65ad\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c POST \/api\/v4\/internal\/allowed \u8fd4\u56de\u9519\u8bef\uff0cGitaly \u4f1a\u5c06\u9519\u8bef\u8fd4\u56de\u7ed9 gitaly-hooks\uff0cgitaly-hooks \u4f1a\u5728\u6807\u51c6\u9519\u8bef\u4e2d\u5199\u5165\u9519\u8bef\u4fe1\u606f\u5e76\u4e14\u9000\u51fa\uff0c\u9000\u51fa\u7801\u975e 0. \u9519\u8bef\u4fe1\u606f\u4f1a\u88ab git-receive-pack \u6536\u96c6\u540e\u518d\u5199\u5165\u5230\u6807\u51c6\u9519\u8bef\uff0cgitaly-hooks \u975e 0 \u7684\u9000\u51fa\u7801\u4f1a\u4f7f\u5f97 git-receive-pack \u505c\u6b62\u5904\u7406\u5f53\u524d\u7684 git push \u800c\u9000\u51fa\uff0c\u9000\u51fa\u7801\u540c\u6837\u975e 0\uff0c\u63a7\u5236\u6743\u56de\u5230 Gitaly\uff0c\u540e\u8005\u6536\u96c6 git-receive-pack \u7684\u6807\u51c6\u9519\u8bef\u8f93\u51fa\uff0c\u56de\u590d GRPC \u54cd\u5e94\u5230 Workhorse\/Gitlab-Shell.<\/p>\n<p>\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u4f1a\u95ee\uff0cHooks \u5728\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u76f8\u5173\u7684 object \u80af\u5b9a\u5df2\u7ecf\u4e0a\u4f20\u5230\u670d\u52a1\u7aef\u4e86\uff0c\u8fd9\u65f6\u505c\u4e0b\u6765\u8fd9\u90e8\u5206\u60ac\u7a7a\u7684 object \u5982\u4f55\u5904\u7406\u5462\uff1f<\/p>\n<p>\u5176\u5b9e\u6ca1\u6709\u5904\u7406\u5b8c\u7684 git push \u5bf9\u5e94\u7684 object \u4f1a\u88ab\u5148\u5199\u5165\u5230\u9694\u79bb\u73af\u5883\u4e2d\uff0c\u5b83\u4eec\u72ec\u7acb\u5b58\u50a8\u5728 objects \u4e0b\u7684\u4e00\u4e2a\u5b50\u6587\u4ef6\u5939\uff0c\u5f62\u5982 incoming-8G4u9v\uff0c\u8fd9\u6837\u5982\u679c Hooks \u8ba4\u4e3a\u8fd9\u4e2a push \u6709\u95ee\u9898\uff0c\u76f8\u5173\u7684\u8d44\u6e90\u5c31\u80fd\u5bb9\u6613\u5730\u5f97\u5230\u6e05\u7406\u4e86\u3002<\/p>\n<h4>update hook<\/h4>\n<p>\u8fd9\u4e2a\u94a9\u5b50\u4f1a\u5728 Git \u5b9e\u9645\u66f4\u65b0 ref \u7684\u524d\u4e00\u523b\u89e6\u53d1\uff0c\u6bcf\u4e2a ref \u89e6\u53d1\u4e00\u6b21\uff0c\u5165\u53c2\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u5165\uff1a\u8981\u66f4\u65b0\u7684 ref\u3001\u65e7 commit hash\u3001\u65b0 commit hash\u3002\u76ee\u524d\u8fd9\u4e2a\u94a9\u5b50\u4e0d\u4f1a\u4e0e Rails \u4e92\u52a8\u3002<\/p>\n<p>\u6781\u72d0GitLab \u540c\u65f6\u652f\u6301\u81ea\u5b9a\u4e49 Git Hooks\uff0cpre-receive hook, update hook \u548c post-receive hook \u90fd\u652f\u6301\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5728 gitlab-hooks \u901a\u77e5 Gitaly \u94a9\u5b50\u8fd0\u884c\u65f6\u5728 Gitaly \u4e2d\u5b8c\u6210\u3002\u6b64\u523b\u5c31\u662f\u89e6\u53d1\u81ea\u5b9a\u4e49 update hook \u7684\u65f6\u5019\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2D95\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<blockquote><p>\u56fe\/Vishal Jadhav on Unsplash<\/p><\/blockquote>\n<p>\u56fe\u4e2d\u7684\u8fd9\u4e2a\u94a9\u5b50\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u6709\u7740\u5386\u53f2\u60a0\u4e45\u7684\u8054\u7cfb\u2026\u2026\u54b3\u54b3\uff0c\u597d\u5427\u6211\u7f16\u4e0d\u4e0b\u53bb\u4e86\uff0c\u6211\u53ea\u662f\u62c5\u5fc3\u4f60\u770b\u5230\u8fd9\u91cc\u5df2\u7ecf\u8981\u7761\u7740\u4e86\uff0c\u627e\u5f20\u56fe\u7247\u8ba9\u4f60\u653e\u677e\u4e00\u4e0b~<\/p>\n<h4>post-receive hook<\/h4>\n<p>\u5728\u6240\u6709 refs \u90fd\u5f97\u5230\u66f4\u65b0\u540e\uff0cGit \u4f1a\u6267\u884c\u4e00\u6b21 post-receive \u94a9\u5b50\uff0c\u5b83\u83b7\u5f97\u7684\u53c2\u6570\u4e0epre-receive\u94a9\u5b50\u76f8\u540c\u3002<\/p>\n<p>Gitaly \u6536\u5230 gitaly-hooks \u7684\u63d0\u9192\u540e\uff0c\u4f1a\u8c03\u7528 Rails \u7684 POST \/api\/v4\/internal\/post_receive\uff0cRails \u4f1a\u5728\u8fd9\u65f6\u5e72\u5f88\u591a\u4e8b\uff1a<\/p>\n<ul>\n<li>\u8fd4\u56de\u63d0\u9192\u7528\u6237\u521b\u5efa Merge Request \u7684\u4fe1\u606f\uff1b<\/li>\n<li>\u5c06 pre-receive \u4e2d\u63d0\u5230\u7684\u4ed3\u5e93\u5f15\u7528\u8ba1\u6570 -1\uff1b<\/li>\n<li>\u5237\u65b0\u4ed3\u5e93\u7f13\u5b58\uff1b<\/li>\n<li>\u89e6\u53d1 CI\uff1b<\/li>\n<li>\u5982\u679c\u9002\u7528\uff0c\u53d1 Email\u3002<\/li>\n<\/ul>\n<p>\u5176\u4e2d\u6709\u7684\u64cd\u4f5c\u662f\u5f02\u6b65\u7684\uff0c\u88ab\u4ea4\u7ed9 SideKiq \u8c03\u5ea6\u3002<\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u73b0\u5728\uff0c\u4f60\u5df2\u7ecf\u4ece\u5ba2\u6237\u7aef\u5230\u670d\u52a1\u7aef\u8d70\u5b8c\u4e86 git push \u5168\u7a0b\uff0c\u771f\u662f\u4e00\u6b21\u4f1f\u5927\u7684\u65c5\u7a0b\uff01<\/p>\n<p>\u52c7\u58eb\uff0c\u4e0b\u56fe\u5c31\u662f\u4f60\u7684\u901a\u5173\u5b9d\u85cf\uff01<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2Eaa\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u7ee7\u7eed\u6df1\u5165\u4e86\u89e3\u76f8\u5173\u5185\u5bb9\uff0c\u4e0b\u9762\u7684\u8d44\u6599\u4f1a\u662f\u4e0d\u9519\u7684\u8d77\u70b9\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=PSXWD7%2F2mFkqbM5MTOSRZw%3D%3D.TG7QUwOL4zeC%2BnohmDqHgESnd0ENYBxRFDT2KisHMy8%3D\" rel=\"nofollow\">Pro Git &#8211; Chapter 10: Git Internals<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=7W6Nml81HrLTWkGcQT4DkQ%3D%3D.F3lJ8hSWItgmP63UWApmS%2BhV490iVjbDZJdgAdDeElNjtYwINeBGj0i9N%2BoFi54wuIWCyJ7BSlmBhfInKGwfWvcXxA7npEGQ1RjrFdOOkjQ%3D\" rel=\"nofollow\">How Gitaly fits into GitLab<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=AI2Ld76zz5HSZ4Voc9nxoA%3D%3D.yMA1r6c7GWYyoAU6tWVtNoJj0kWw7IDLqKDVJLAlBgcVwYHS%2Bs1tjY495cRAYZ8rx7NFCrXlovJbhAEpauNqYw%3D%3D\" rel=\"nofollow\">Gitaly &#8211; Git Hooks<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=Ww9TY9A%2BDFqumauJbO4k0Q%3D%3D.xxc5uSkVo%2FQ%2B0mkv%2BFC1VDM0CnqFnLLzhIH%2FAJ3mULLM7T3ZXuSOEZjmAfkjZNoP\" rel=\"nofollow\">Git Hooks<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=tjphzn%2BSB4qUuLvQ6jtQvw%3D%3D.Cm0dm4uFlBXUmgC2pUmiPCXDo1g1P6pOds4m7Wj7vbaLysqnroanhZhcbSxBWHRp4I9hrnY6oDOGSaMq6yEEvGD7jCxbzho%2Fe2bwoje1VZw%3D\" rel=\"nofollow\">Rails internal APIs<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042537837\">https:\/\/segmentfault.com\/a\/1190000042537837<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6765\u81ea\uff1a\u674e\u632f\u6960 \u6781\u72d0(GitLab) \u7814\u53d1\u5de5\u7a0b\u5e08 \u52c7\u58eb\uff0c\u4f60\u53ef\u66fe\u597d\u5947\u8fc7 Git \u548c\u6781\u72d0GitLab \u662f\u5982\u4f55\u5de5\u4f5c [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006594","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006594"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006594\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}