diff --git a/docs/examples.rst b/docs/examples.rst new file mode 100644 index 0000000..aeb6a5a --- /dev/null +++ b/docs/examples.rst @@ -0,0 +1,41 @@ +********************************************************************** +Quick examples +********************************************************************** + +A list of some common command-line operations and their pygit2 equivalents. + +Creating a new repository with ``git init`` + + >>> pygit2.init_repository('repo_name', False) + + +Viewing a commit with ``git show d370f56`` + + >>> repo = pygit2.Repository('/path/to/repository') + >>> commit = repo['d370f56'] + +Viewing the last commit message + + >>> repo[repo.head.oid].message + 'commit message' + +Traversing the commit history with ``git log`` + + >>> last = repo[repo.head.oid] + >>> for commit in repo.walk(last.oid, pygit2.GIT_SORT_TIME): + >>> print(commit.message) # or some other operation + +Listing all branches with ``git branch`` + + >>> regex = re.compile('^refs/heads/') + >>> filter(lambda r: regex.match(r), repo.listall_references()) + +Similarly, listing all tags with ``git tag`` + + >>> regex = re.compile('^refs/tags') + >>> filter(lambda r: regex.match(r), repo.listall_references()) + +Listing all files in the last commit + + >>> for e in repo[repo.head.oid].tree: + >>> print(e.name) diff --git a/docs/index.rst b/docs/index.rst index dcd3ed6..59a8854 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -25,6 +25,7 @@ Start: :maxdepth: 1 install + examples Usage guide: