一个用于远程处理github的简单工具。
simplygithub的Python项目详细描述
一个用于远程处理github的简单工具。基本接口是最小的。 使用Simply Github,您可以:
- Create and delete branches.
- Add or remove files from a branch.
- Merge branches.
配置文件
几乎每个Simply Github函数都需要一个概要文件作为其第一个 争论。配置文件为Simply Github提供两条信息。 它告诉Simply Github:
- The ^{tt5}$ it should connect to.
- The ^{tt6}$ it should connect with.
repo是标准git形式的回购的名称 :owner/:repo–例如,jtpaasch/simplygithub。
token是一个有效的个人访问令牌,您可以从中创建 您的github帐户,在“帐户设置”下。见 https://help.github.com/articles/creating-an-access-token-for-command-line-use
要生成仅在编码会话期间使用的配置文件,请导入 simplygithub.authentication.profile包并使用 ephemeral_profile()函数。例如:
from simplygithub.authentication import profile my_profile = profile.ephemeral_profile(repo="jtpaasch/simplygithub", token="a3ef21ac0f...")
然后,可以对任何Simply Github函数使用my_profile。 需要个人资料。
不过,短暂的配置文件只会持续到编码会话。它消失了 当my_profile变量消失时。如果要存储配置文件 在配置文件中,使用write_profile()函数。
该函数接受前面提到的repo和token参数,但是 您还需要给概要文件命名,以便以后检索它。
这将创建与上面相同的配置文件,但将其命名为default:
from simplygithub.authentication import profile my_profile = profile.write_profile(name="default", repo="jtpaasch/simplygithub", token="a3ef21ac0f...")
将配置文件保存在~/.profile/simplygithub/github的文件中。 (您也可以手动创建/修改此文件。)
您可以稍后使用read_profile()函数加载配置文件:
from simplygithub.authentication import profile my_profile = profile.read_profile("default")
一旦将配置文件加载到变量(如my_profile)中,就可以 用于任何需要配置文件为的Simply Github函数 他们的第一个论点。
创建分支
使用simplygithub.branches包处理分支:
from simplygithub import branches
查看所有分支的列表(使用上面的my_profile):
branches.list_branches(my_profile)
创建母版的分支:
branches.create_branch(my_profile, "feature-branch", branch_off="master")
将要素分支合并到主要素:
branches.merge(my_profile, "feature-branch", merge_into="master")
删除分支:
branches.delete_branch(my_profile, "feature-branch")
从分支添加和删除文件
使用simplygithub.files包处理文件:
from simplygithub import files
列出分支中的所有文件对象(使用上面的my_profile):
files.list_files(my_profile, "feature-branch")
要获取分支上文件的(utf-8编码)内容:
files.get_file(my_profile, branch="feature-branch", file_path="folder/path/foo.py")
将文件添加到分支:
files.add_file(my_profile, branch="feature-branch", file_path="folder/path/foo.py", file_contents="This is a silly file.")
它将在folder/path/foo.py处添加一个文件,并为其提供内容 This is a silly file.。它将把它提交到feature-branch。
如果文件是可执行文件,则添加is_executable=True作为参数。如果 要提供提交消息,可以使用commit_message 参数:
files.add_file(my_profile, branch="feature-branch", file_path="folder/path/foo.py", file_contents="This is a silly file.", is_executable=True, commit_message="Added a silly file.")
从分支删除文件:
files.remove_file(my_profile, branch="feature-branch", file_path="folder/path/foo.py")
这将从分支中删除文件并提交更改。您可以添加 一个可选的commit_message参数,如果您愿意的话:
files.remove_file(my_profile, branch="feature-branch", file_path="folder/path/foo.py", commit_message="Removed a silly file.")