{"id":1967,"date":"2023-11-29T16:15:08","date_gmt":"2023-11-29T08:15:08","guid":{"rendered":"http:\/\/zhang.mba\/?p=1967"},"modified":"2023-11-29T16:18:02","modified_gmt":"2023-11-29T08:18:02","slug":"python-wu-xu-an-zhuanggitke-long","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/11\/29\/16\/15\/08\/1967\/python-wu-xu-an-zhuanggitke-long\/python\/zhangzhiqi\/","title":{"rendered":"Python \u65e0\u987b\u5b89\u88c5git_\u514b\u9686"},"content":{"rendered":"<p>pygit2\u5e93\uff0c\u53ef\u4ee5\u5feb\u901f\u6ca1\u6709\u5b89\u88c5git\u60c5\u51b5\u4e0b\u53ef\u4ee5\u8fdb\u884c\u514b\u9686\u64cd\u4f5c\uff0c\u5e76\u4e14\u652f\u6301\u5927\u90e8\u5206git\u514b\u9686.<\/p>\n<p><strong>\u5b89\u88c5\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">pip install pygit2\n<\/code><\/pre>\n<p><strong>\u5b8c\u6574\u5b9e\u4f8b\uff1a<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import os\nimport pygit2\n# \u4ed3\u5e93 URL\nrepo_url = 'https:\/\/gitee.com\/sofu456\/chat_robot.git'\n# \u83b7\u53d6\u4ed3\u5e93\u540d\u79f0\nrepo_name = repo_url.rstrip('.git').split('\/')[-1]\n# \u521b\u5efa\u672c\u5730\u76ee\u5f55\nlocal_path = os.path.join(os.getcwd(), repo_name)\nos.makedirs(local_path, exist_ok=True)\n# \u514b\u9686\u4ed3\u5e93\npygit2.clone_repository(repo_url, local_path)\n<\/code><\/pre>\n<p><strong>pygit2\u5176\u4ed6\u529f\u80fd\uff1a<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">e_repository(url, path)\uff0c\u514b\u9686\u8fdc\u7a0b\u4ed3\u5e93\u5230\u672c\u5730\u6307\u5b9a\u8def\u5f84\u3002\n\u6253\u5f00\u4ed3\u5e93\uff1apygit2.Repository(path)\uff0c\u6253\u5f00\u672c\u5730\u4ed3\u5e93\u3002\n\u521b\u5efa\u4ed3\u5e93\uff1apygit2.init_repository(path, bare=False)\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u672c\u5730\u4ed3\u5e93\u3002\n\u68c0\u67e5\u5206\u652f\uff1arepo.listall_branches()\uff0c\u5217\u51fa\u6240\u6709\u5206\u652f\u3002\n\u521b\u5efa\u5206\u652f\uff1arepo.create_branch(name, commit, force=False)\uff0c\u5728\u5f53\u524d\u4ed3\u5e93\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5206\u652f\u3002\n\u5207\u6362\u5206\u652f\uff1arepo.checkout(branch_or_commit, force=False)\uff0c\u5207\u6362\u5230\u6307\u5b9a\u7684\u5206\u652f\u6216\u63d0\u4ea4\u3002\n\u6dfb\u52a0\u6587\u4ef6\uff1arepo.index.add(path)\uff0c\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u6682\u5b58\u533a\u3002\n\u63d0\u4ea4\u66f4\u6539\uff1arepo.index.write_tree()\uff0c\u5c06\u6682\u5b58\u533a\u7684\u66f4\u6539\u63d0\u4ea4\u4e3a\u65b0\u7684\u6811\u5bf9\u8c61\u3002\n\u521b\u5efa\u63d0\u4ea4\uff1arepo.create_commit(refname, author, committer, message, tree, parents)\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63d0\u4ea4\u3002\n\u63a8\u9001\u5230\u8fdc\u7a0b\uff1arepo.remotes['origin'].push(refspecs)\uff0c\u5c06\u66f4\u6539\u63a8\u9001\u5230\u8fdc\u7a0b\u4ed3\u5e93\u3002\n\u62c9\u53d6\u8fdc\u7a0b\u66f4\u6539\uff1arepo.remotes['origin'].fetch()\uff0c\u4ece\u8fdc\u7a0b\u4ed3\u5e93\u62c9\u53d6\u6700\u65b0\u66f4\u6539\u3002\n\u5408\u5e76\u5206\u652f\uff1arepo.merge(branch_or_commit)\uff0c\u5c06\u6307\u5b9a\u5206\u652f\u6216\u63d0\u4ea4\u5408\u5e76\u5230\u5f53\u524d\u5206\u652f\u3002\n\u67e5\u770b\u63d0\u4ea4\u5386\u53f2\uff1arepo.walk(commit_id, pygit2.GIT_SORT_TIME)\uff0c\u6309\u65f6\u95f4\u987a\u5e8f\u904d\u5386\u63d0\u4ea4\u5386\u53f2\u3002\n<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/sHJnM03rkibqtqxzGYYDfR13V850Jltw95CJpicMvcyKVtWQMtnILp4ADkJ9SmVbn0woLQMBMetcxiaVh7miaohicMA\/0?wx_fmt=jpeg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/sHJnM03rkibqtqxzGYYDfR13V850Jltw95CJpicMvcyKVtWQMtnILp4ADkJ9SmVbn0woLQMBMetcxiaVh7miaohicMA\/0?wx_fmt=jpeg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u6765\u6e90&#8212;&#8211;python\u5b66\u9738<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>pygit2\u5e93\uff0c\u53ef\u4ee5\u5feb\u901f\u6ca1\u6709\u5b89\u88c5git\u60c5\u51b5\u4e0b\u53ef\u4ee5\u8fdb\u884c\u514b\u9686\u64cd\u4f5c\uff0c\u5e76\u4e14\u652f\u6301\u5927\u90e8\u5206git\u514b\u9686. \u5b89\u88c5\u65b9\u6cd5\uff1a pip install pygit2 \u5b8c\u6574\u5b9e\u4f8b\uff1a import os import pygit2 # \u4ed3\u5e93 URL repo_url = &#8216;https:\/\/gitee.com\/sofu456\/chat_robot.git&#8217; # \u83b7\u53d6\u4ed3\u5e93\u540d\u79f0 repo_name = re &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[67,12],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1967"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1967"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1967\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}