|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "[官网文档](https://gitpython.readthedocs.io/en/stable/tutorial.html)" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 30, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import logging\n", |
| 17 | + "import git\n", |
| 18 | + "import os\n", |
| 19 | + "logging.basicConfig(level=logging.DEBUG)" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "创建一个仓库目录" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 19, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [ |
| 34 | + { |
| 35 | + "name": "stdout", |
| 36 | + "output_type": "stream", |
| 37 | + "text": [ |
| 38 | + "/root/learn-python/demo/git_demo/my_repo\n" |
| 39 | + ] |
| 40 | + } |
| 41 | + ], |
| 42 | + "source": [ |
| 43 | + "# root_dir = os.path.abspath(os.path.dirname(__file__))\n", |
| 44 | + "root_dir = os.getcwd()\n", |
| 45 | + "my_repo_dir = os.path.join(root_dir,\"my_repo\")\n", |
| 46 | + "print(my_repo_dir)" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 20, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "name": "stderr", |
| 56 | + "output_type": "stream", |
| 57 | + "text": [ |
| 58 | + "DEBUG:git.cmd:Popen(['git', 'init'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "name": "stdout", |
| 63 | + "output_type": "stream", |
| 64 | + "text": [ |
| 65 | + "False\n" |
| 66 | + ] |
| 67 | + } |
| 68 | + ], |
| 69 | + "source": [ |
| 70 | + "# 创建一个仓库,返回 Repo 对象\n", |
| 71 | + "my_repo = git.Repo.init(my_repo_dir)\n", |
| 72 | + "print(my_repo.bare)" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "[What is a bare git repository?](https://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/)" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 21, |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [ |
| 87 | + { |
| 88 | + "name": "stderr", |
| 89 | + "output_type": "stream", |
| 90 | + "text": [ |
| 91 | + "DEBUG:git.cmd:Popen(['git', 'init', '--bare'], cwd=/root/learn-python/demo/git_demo/my_bare_repo, universal_newlines=False, shell=None, istream=None)\n" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "name": "stdout", |
| 96 | + "output_type": "stream", |
| 97 | + "text": [ |
| 98 | + "True\n" |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "# 创建一个 bare 仓库,返回 Repo 对象\n", |
| 104 | + "bare_repo_dir = os.path.join(root_dir,\"my_bare_repo\")\n", |
| 105 | + "bare_repo = git.Repo.init(repo_dir, bare=True)\n", |
| 106 | + "print(bare_repo.bare)" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": 39, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "name": "stderr", |
| 116 | + "output_type": "stream", |
| 117 | + "text": [ |
| 118 | + "DEBUG:git.cmd:Popen(['git', 'diff', '--cached', '--abbrev=40', '--full-index', '--raw'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "data": { |
| 123 | + "text/plain": [ |
| 124 | + "True" |
| 125 | + ] |
| 126 | + }, |
| 127 | + "execution_count": 39, |
| 128 | + "metadata": {}, |
| 129 | + "output_type": "execute_result" |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "# 检查仓库是否是 dirty,即存在没有提交的文件\n", |
| 134 | + "my_repo.is_dirty()\n" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": 40, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [ |
| 142 | + { |
| 143 | + "name": "stderr", |
| 144 | + "output_type": "stream", |
| 145 | + "text": [ |
| 146 | + "DEBUG:git.cmd:Popen(['git', 'diff', '--cached', '--abbrev=40', '--full-index', '--raw'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "data": { |
| 151 | + "text/plain": [ |
| 152 | + "True" |
| 153 | + ] |
| 154 | + }, |
| 155 | + "execution_count": 40, |
| 156 | + "metadata": {}, |
| 157 | + "output_type": "execute_result" |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "# 新建一个文件,再次查看 is_dirty() 属性\n", |
| 162 | + "file_name = os.path.join(my_repo_dir,\"readme.md\")\n", |
| 163 | + "open(file_name,'wb').close()\n", |
| 164 | + "# my_repo.index.add([file_name])\n", |
| 165 | + "my_repo.is_dirty()" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": 41, |
| 171 | + "metadata": {}, |
| 172 | + "outputs": [ |
| 173 | + { |
| 174 | + "name": "stderr", |
| 175 | + "output_type": "stream", |
| 176 | + "text": [ |
| 177 | + "DEBUG:git.cmd:Popen(['git', 'status', '--porcelain', '--untracked-files'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "[]" |
| 184 | + ] |
| 185 | + }, |
| 186 | + "execution_count": 41, |
| 187 | + "metadata": {}, |
| 188 | + "output_type": "execute_result" |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "# 查看未提交的文件\n", |
| 193 | + "my_repo.untracked_files" |
| 194 | + ] |
| 195 | + } |
| 196 | + ], |
| 197 | + "metadata": { |
| 198 | + "interpreter": { |
| 199 | + "hash": "b9ad67ebd6cda51a6170d8f3102c2ab5635d4edf4d086fb17b0c03dbb914c2a6" |
| 200 | + }, |
| 201 | + "kernelspec": { |
| 202 | + "display_name": "Python 3.8.12 64-bit", |
| 203 | + "language": "python", |
| 204 | + "name": "python3" |
| 205 | + }, |
| 206 | + "language_info": { |
| 207 | + "codemirror_mode": { |
| 208 | + "name": "ipython", |
| 209 | + "version": 3 |
| 210 | + }, |
| 211 | + "file_extension": ".py", |
| 212 | + "mimetype": "text/x-python", |
| 213 | + "name": "python", |
| 214 | + "nbconvert_exporter": "python", |
| 215 | + "pygments_lexer": "ipython3", |
| 216 | + "version": "3.8.12" |
| 217 | + }, |
| 218 | + "orig_nbformat": 4 |
| 219 | + }, |
| 220 | + "nbformat": 4, |
| 221 | + "nbformat_minor": 2 |
| 222 | +} |
0 commit comments