User:Chot/OPW/fetchrepo.py
Appearance
import git
import os
import sys
import argparse
from datetime import datetime
def fetch_git_file(git_url, requested_file, branch, start_line, end_line):
dirname = '/tmp/gittest-' + datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
repo = git.Repo() # inializes an empty repo, awkward
repo = repo.clone_from(git_url, dirname)
our_repo = repo.git
our_repo.checkout(branch)
with open(dirname + "/" + requested_file, 'r') as f:
f = f.readlines()
if start_line == -1 and end_line:
lines = "".join(f)
elif start_line == -1:
lines = "".join(f[:end_line])
elif end_line == -1:
lines = "".join(f[(start_line - 1):])
else:
lines = "".join(f[(start_line - 1): end_line])
lines = lines.strip()
print lines
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('git_url', help='git URL')
parser.add_argument('file_name', help='file name')
parser.add_argument('--branch', nargs='?', default='master', help='branch name')
parser.add_argument('--start', nargs='?', default=-1, type=int, help='start line')
parser.add_argument('--end', nargs='?', default=-1, type=int, help='end line')
args = parser.parse_args()
fetch_git_file(args.git_url, args.file_name, args.branch, args.start, args.end)