Skip to content

Instantly share code, notes, and snippets.

@schuster-rainer
Created February 13, 2018 08:45
Show Gist options
  • Save schuster-rainer/6b6dee0b73747fa9df8dc8fbaa2338d6 to your computer and use it in GitHub Desktop.
Save schuster-rainer/6b6dee0b73747fa9df8dc8fbaa2338d6 to your computer and use it in GitHub Desktop.
using git as database from python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"http://www.pygit2.org/objects.html#tree-entries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Automatic calling is: Full\n",
"\n",
"Automagic is ON, % prefix IS NOT needed for line magics.\n"
]
}
],
"source": [
"%autocall 2\n",
"%automagic 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pygit2 import init_repository, Repository, IndexEntry\n",
"from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT, GIT_OBJ_TAG, GIT_OBJ_TREE, GIT_FILEMODE_BLOB\n",
"from pygit2 import Oid, Reference, hashfile, Commit"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"repo = Repository('mygitrepo')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#repo = init_repository(\"mygitrepo\", bare=False)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30d74d258442c7c65512eafab474568dd706c430"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"repo.create_blob(\"test\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<_pygit2.Blob at 0x10bd16ab0>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"repo[\"30d74d258442c7c65512eafab474568dd706c430\"]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"blob_id = repo.create_blob(\"hallo welt\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"cell_style": "center"
},
"outputs": [],
"source": [
"entry = IndexEntry('README.md', blob_id, GIT_FILEMODE_BLOB)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"repo.index.add(entry)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"repo.index.write()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[34mmygitrepo/\u001b[00m\r\n",
"└── \u001b[34m.git\u001b[00m\r\n",
" ├── HEAD\r\n",
" ├── config\r\n",
" ├── description\r\n",
" ├── \u001b[34mhooks\u001b[00m\r\n",
" │   └── \u001b[31mREADME.sample\u001b[00m\r\n",
" ├── index\r\n",
" ├── \u001b[34minfo\u001b[00m\r\n",
" │   └── exclude\r\n",
" ├── \u001b[34mobjects\u001b[00m\r\n",
" │   ├── \u001b[34m30\u001b[00m\r\n",
" │   │   └── d74d258442c7c65512eafab474568dd706c430\r\n",
" │   ├── \u001b[34m45\u001b[00m\r\n",
" │   │   └── 1136b76ba422397e3bdc94680e2916ec1713e4\r\n",
" │   ├── \u001b[34minfo\u001b[00m\r\n",
" │   └── \u001b[34mpack\u001b[00m\r\n",
" └── \u001b[34mrefs\u001b[00m\r\n",
" ├── \u001b[34mheads\u001b[00m\r\n",
" └── \u001b[34mtags\u001b[00m\r\n",
"\r\n",
"11 directories, 8 files\r\n"
]
}
],
"source": [
"!tree -la mygitrepo/"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"README.md\n"
]
}
],
"source": [
"for entry in repo.index:\n",
" print(entry.path)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"!rm -rf mygitrepo/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment