from flask import Flask
from flask import Response
from git_repository import GitRepository
app = Flask(__name__)
app.debug=True
@app.route('/file/<sha>')
def retrieve_file_blob(sha):
repo = GitRepository('/home/j/src/git-sandbox')
return Response(repo.retrieve_file_blob(sha),
mimetype='application/octet-stream')
@app.route('/exists/<sha>')
def file_exists(sha):
repo = GitRepository('/home/j/src/git-sandbox')
return Response(repo.file_exists(sha),
mimetype='application/octet-stream')
if __name__ == '__main__':
app.run()
main.py
from repository import Repository
from subprocess import Popen
import subprocess
class GitRepository(Repository):
def retrieve_file_blob(self, sha):
"""
Retrieves the Blob for the specified file sha :param sha: The Git sha of the file. :return: The blob given by the Git file sha. >>> repo = GitRepository('/home/j/src/git-sandbox') >>> repo.retrieve_file_blob('ce013625030ba8dba906f756967f9e9ca394464a') 'hello' """ # git show <sha> outputs the file, alternatively so can # git cat-file -p <sha>p = Popen(['git', 'show', sha],
stdout=subprocess.PIPE, cwd=self.location)
return p.stdout.read()[:-1]
def file_exists(self, sha):
""" Checks whether the file exists at the specified file sha. :param sha: The Git sha of the file. :return: Whether the file exists. >>> repo = GitRepository('/home/j/src/git-sandbox') git_repository.py
| repository.py | |
|---|---|
| 1 | from abc import ABCMeta, abstractmethod |
| 2 | |
| 3 | |
| 4 | class Repository(object): |
| 5 | __metaclass__ = ABCMeta |
| 6 | |
| 7 | def __init__(self, location, repo_name=None): |
| 8 | self.location = location |
| 9 | self.repo_name = repo_name |
| 10 | |
| 11 | @abstractmethod |
| 12 | def retrieve_file_blob(self, sha): |
| 13 | pass |
| 14 | |
| 15 | @abstractmethod |
| 16 | def file_exists(self, sha): |
| 17 | pass |