def view(request, repo_name, branch, commit_sha=None): """ view diffs of affeted files in the commit """ commit = diff = None repo = get_repo( repo_name ) commit_list = get_commit( repo, commit_sha) if commit_list: commit = commit_list[0] if len(commit_list) > 1: diff = get_diff( repo, commit_list[1].hexsha, commit.hexsha, ) context = dict( STRATUS_MEDIA_URL = STRATUS_MEDIA_URL, repo_name = repo_name, branch_name = branch, repo = repo, diff = diff, commit = commit, ) return mix_response( request, 'stratus/commit.html', context)
def search(request, repo_name, branch): """ serch files for string """ found_files = [] query = "" repo = get_repo(repo_name) if request.method == "POST": form = SearchForm(request.POST) query = request.POST.get("query", "") if query: git = repo.git # http://book.git-scm.com/4_finding_with_git_grep.html try: result = git.grep("--name-only", query) except GitCommandError: pass else: found_files = result.split("\n") else: form = SearchForm(request.POST) context = dict( GITTER_MEDIA_URL=GITTER_MEDIA_URL, form=form, query=query, found_files=found_files, repo_name=repo_name, branch_name=branch, ) return mix_response(request, "commitlog/found_files.html", context)
def log(request, repo_name, branch=REPO_BRANCH, path=None): page = int(request.GET.get("page", 0)) repo = get_repo( repo_name ) if path: paths = [path] template = 'stratus/file_history.html' else: paths = [] template = 'stratus/commitlog.html' commits = get_commits(repo, branch, paths, page) context = dict( REPO_ITEMS_IN_PAGE = REPO_ITEMS_IN_PAGE, STRATUS_MEDIA_URL = STRATUS_MEDIA_URL, repo_name = repo_name, branch_name = branch, commits = commits, repo = repo, next_page = page + 1, path = path, url= reverse('stratus-log', args=[repo_name, branch ]) ) if page > 0: context["previous_page"] = page-1 return mix_response( request, template, context)
def log(request, repo_name, branch=REPO_BRANCH, path=None): page = int(request.GET.get("page", 0)) repo = get_repo( repo_name ) if path: paths = [path] else: paths = [] commits = get_commits(repo, branch, paths, page) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, repo_name = repo_name, branch_name = branch, commits = commits, next_page = page + 1, path = path, url= reverse('commitlog-log', args=[repo_name, branch ]) ) if page > 0: context["previous_page"] = page-1 return mix_response( request, 'commitlog/commitlog.html', context)
def undo(request, repo_name, branch_name): """ undo last commit """ msgs = [] json_convert = None repo = get_repo( repo_name ) git = repo.git reset_result = git.reset( *GIT_RESET ) msgs.append( reset_result ) if request.is_ajax(): json_convert = message_convert context = dict( STRATUS_MEDIA_URL = STRATUS_MEDIA_URL, repo_name = repo_name, branch_name = branch_name, repo = repo, msg=msgs, ) return mix_response( request, 'stratus/undo.html', context, json_convert, )
def repos(request): context = { "STRATUS_MEDIA_URL": STRATUS_MEDIA_URL, "repos":dict([ (repo_name, get_repo( repo_name )) for repo_name in REPOS]), } return mix_response( request, 'stratus/list_repos.html', context)
def rename(request, repo_name, branch, file_path): if request.method == 'POST': repo = get_repo( repo_name ) tree = repo.tree() try: tree = tree[file_path] except KeyError: msg = MSG_NO_FILE_IN_TREE return error_view( request, msg) form = RenameForm( request.POST ) if form.is_valid(): git = repo.git new_name = form.cleaned_data["new_name"] try: os.rename(file_path, new_name) except IOError: msg = MSG_RENAME_ERROR % (file_path, new_name) return error_view( request, msg) else: git.mv( path, new_name ) msg = MSG_RENAME_SUCCESS % (path, new_name) commit_result = git.commit("-m", """%s""" % msg) messages.success(request, commit_result ) dir_path = "/".join( path.split("/")[:-1] ) return redirect('commitlog-tree-view', repo_name, branch, dir_path ) else: messages.error(request, MSG_RENAME_ERROR ) else: form = RenameForm( ) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, breadcrumbs = make_crumbs(file_path), form = form, repo_name = repo_name, branch_name = branch, path = file_path, ) return mix_response( request, 'commitlog/rename_file.html', context)
def view(request, repo_name, branch=REPO_BRANCH, path=None, commit_sha=None ): repo = get_repo( repo_name ) commit, tree = get_commit_tree(repo, commit_sha) dir_path = path.split("/") if commit_sha: template_name = 'commitlog/view_commit_tree.html' else: template_name = 'commitlog/view_tree.html' if path: if path[-1:] == "/": path = path[:-1] try: tree = tree[path] except AttributeError: tree = tree[dir_path[:-1]] if request.method == 'POST': form = FileUploadForm(request.POST, request.FILES) if form.is_valid(): file_path = os.path.join( repo.working_dir, path, request.FILES["file_source"].name) handle_uploaded_file(file_path, request.FILES['file_source']) msg = "file %s uploaded" % file_path result_msg = mk_commit(repo, msg, file_path ) messages.success(request, result_msg ) else: form = FileUploadForm( initial={ "dir_path": path } ) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, repo_name = repo_name, branch_name = branch, commit = commit, upload_form = form, tree = tree.list_traverse(depth = 1), breadcrumbs = make_crumbs(path), dir_path = dir_path, ) return mix_response( request, template_name, context)
def view(request, repo_name, branch=REPO_BRANCH, path=None, commit_sha=None ): json_convert = None repo = get_repo( repo_name ) commit, tree = get_commit_tree(repo, commit_sha) the_tree = tree dir_path = path.split("/") if commit_sha: template_name = 'stratus/view_commit_tree.html' else: template_name = 'stratus/view_tree.html' if path: the_tree = tree[path] # if its file try to get file directory if the_tree.type != "tree": path = "/".join(dir_path[:-1]) if len(dir_path) == 1: the_tree = tree else: the_tree = tree[path] if request.is_ajax(): json_convert = message_convert context = dict( STRATUS_MEDIA_URL = STRATUS_MEDIA_URL, repo_name = repo_name, branch_name = branch, commit = commit, repo = repo, tree = the_tree.list_traverse(depth = 1), breadcrumbs = make_crumbs(path), parent_dir = dir_path[:-1], dir_path = dir_path, path = path, ) return mix_response( request, template_name, context, )
def new(request, repo_name, branch=REPO_BRANCH, path=None ): result_msg = file_source = "" form_class = TextFileEditForm file_path = path #!!! FIX security #TODO check if file exist allready file_meta = dict( type = "python", ) if request.method == 'POST': form = FileUploadForm( request.POST, request.FILES ) if form.is_valid(): repo = Repo(REPOS[repo_name]) file_source = form.cleaned_data["file_source"] write_file(file_path, file_source ) msg = form.cleaned_data["message"] result_msg = mk_commit(repo, msg, file_path ) messages.success(request, result_msg ) dir_path = "/".join( path.split("/")[:-1] ) return redirect('commitlog-tree-view', repo_name, branch, dir_path ) else: result_msg = MSG_COMMIT_ERROR else: form = form_class( initial={"message":"%s added" % path} ) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, form= form, breadcrumbs = make_crumbs(path), result_msg = result_msg, file_meta = file_meta, repo_name = repo_name, branch_name = branch, path = path, ) return mix_response( request, 'commitlog/new_file.html', context)
def new(request, repo_name, branch=REPO_BRANCH, path=None): file_source = "" msgs = [] file_path = path #!!! FIX security # TODO check if file exist allready file_meta = dict(type="python") if request.method == "POST": form = TextFileEditForm(request.POST, request.FILES) if form.is_valid(): repo = get_repo(repo_name) file_source = form.cleaned_data["file_source"] file_writen = write_file(file_path, file_source) if file_writen: msg = form.cleaned_data["message"] signals.file_created.send(sender=repo, file_path=file_path, url="") result_msg = mk_commit(repo, msg, file_path) msgs.append(result_msg) else: msgs.append("Error. file has been created") else: msgs.append(form.errors) else: form = TextFileEditForm(initial={"message": "%s added" % path}) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, form=form, breadcrumbs=make_crumbs(path), msg=msgs, file_meta=file_meta, repo_name=repo_name, branch_name=branch, repo=repo, path=path, ) return mix_response(request, "stratus/new_file.html", context)
def replace(request, repo_name, branch): """ serch files for string """ found_files = [] query = "" repo = get_repo( repo_name ) if request.method == 'POST': form = SearchForm( request.POST ) query = request.POST.get("query", "") replace = request.POST.get("replace", "") if query and replace: git = repo.git #http://book.git-scm.com/4_finding_with_git_grep.html try: result = git.grep( "--name-only", query ) except GitCommandError: pass else: found_files = result.split("\n") for file_path in found_files: f = codecs.open(file_path, encoding='utf-8', mode=mode) content = f.read f.write(content.replace(query, replace)) f.close else: form = SearchForm( request.POST ) context = dict( STRATUS_MEDIA_URL = STRATUS_MEDIA_URL, form = form, query = query, found_files = found_files, repo = repo, repo_name = repo_name, branch_name = branch, ) return mix_response( request, 'stratus/found_files.html', context)
def delete(request, repo_name, branch, path ): repo = get_repo( repo_name ) tree = repo.tree() try: ftree = tree[path] #check if exixs under the tree except KeyError: pass if request.method == "POST": form = FileDeleteForm(request.POST) if form.is_valid(): if os.path.isfile(path): os.remove(path) git = repo.git del_message = git.rm(path) msg = form.cleaned_data["message"] commit_result = git.commit("-m", """%s""" % msg) messages.success(request, commit_result ) dir_path = "/".join( path.split("/")[:-1] ) return redirect('commitlog-tree-view', repo_name, branch, dir_path ) else: form = FileDeleteForm(initial={ "message": "file %s deleted" % path, "path": path, }) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, breadcrumbs = make_crumbs(path), form = form, repo_name = repo_name, branch_name = branch, path = path, ) return mix_response( request, 'commitlog/delete_file.html', context)
def view(request, repo_name, branch, commit_sha=None): """ view diffs of affeted files in the commit """ commit = diff = None repo = get_repo( repo_name ) commit_list = get_commit( repo, commit_sha) if commit_list: commit = commit_list[0] diff = get_diff( repo, commit_list[1].hexsha, commit.hexsha, ) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, repo_name = repo_name, branch_name = branch, diff = diff, commit = commit, ) return mix_response( request, 'commitlog/commit.html', context)
def new_folder(request, repo_name, branch, path): msgs = [] json_convert = None success = False repo = get_repo(repo_name) folder_name = path # FIX security dir_abs_path = os.path.join(repo.working_dir, folder_name) if not os.path.exists(dir_abs_path): try: os.makedirs(dir_abs_path) except IOError: msgs.append(MSG_FOLDER_CREATE_ERROR % folder_name) else: msgs.append(MSG_FOLDER_CREATED_SUCCESS % folder_name) success = True else: msgs.append(MSG_FOLDER_EXIST_ERROR % folder_name) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, breadcrumbs=make_crumbs(path), repo_name=repo_name, repo=repo, msg=msgs, branch_name=branch, reload_fbrowser=success, path=path, ) if request.is_ajax(): json_convert = message_convert return mix_response(request, "stratus/new_folder.html", context, json_convert)
def upload(request, repo_name, branch=REPO_BRANCH ): result_msg = file_source = path = "" if request.method == 'POST': form = FileUploadForm( request.POST, request.FILES ) if form.is_valid(): dir_path = form.cleaned_data["dir_path"] #!!! FIX security #TODO check if file exist allready repo = Repo(REPOS[repo_name]) file_source = form.cleaned_data["file_source"] path = os.path.join( dir_path, file_source["name"] ) write_file(file_path, file_source ) msg = form.cleaned_data["message"] result_msg = mk_commit(repo, msg, path ) messages.success(request, result_msg ) return redirect('commitlog-tree-view', repo_name, branch, dir_path ) else: result_msg = MSG_COMMIT_ERROR else: form = FileUploadForm( initial={"message":"%s added" % path} ) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, form= form, breadcrumbs = make_crumbs(path), result_msg = result_msg, repo_name = repo_name, branch_name = branch, path = path, ) return mix_response( request, 'commitlog/new_file.html', context)
def repos(request): context = {"repos": dict([(repo_name, get_repo(repo_name)) for repo_name in REPOS])} return mix_response(request, "commitlog/list_repos.html", context)
def upload(request, repo_name, branch=REPO_BRANCH): file_source = path = "" msgs = [] json_convert = None success = False if request.method == "POST": # from _os_helpers import ProgressBarUploadHandler # request.upload_handlers.insert(0, ProgressBarUploadHandler()) repo = get_repo(repo_name) dir_path = request.GET.get("upload_dir") #!!! FIX security orig_file_name = request.GET.get("qqfile") file_name = slugify(orig_file_name) if orig_file_name is not file_name: msgs.append(MSG_FILE_NAME_CHANGED % (orig_file_name, file_name)) file_path = os.path.join(dir_path, file_name) #!!!FIX convert correctly unicode names file_abs_path = os.path.join(repo.working_dir, file_path) if request.META["CONTENT_TYPE"] == "application/octet-stream": try: destination = open(file_abs_path, "wb+") for chunk in request.raw_post_data: destination.write(chunk) destination.close() except: file_writen = False else: file_writen = True else: file_writen = handle_uploaded_file(file_abs_path, request.FILES["file_source"]) if file_writen: signals.file_created.send(sender=repo, file_path=file_path, url="") msgs.append(MSG_UPLOAD_SUCCESS % file_path) message = MSG_COMMIT_SUCCESS % file_path msg = mk_commit(repo, message, file_path) msgs.append(msg) success = True # return HttpResponse('{ "success": true }', mimetype='application/javascript') else: msgs.append(MSG_CANT_SAVE_FILE) success = False # return HttpResponse('{ "success": false }', mimetype='application/javascript') if request.is_ajax(): json_convert = message_convert else: form = FileUploadForm(initial={"message": "%s added" % path}) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, breadcrumbs=make_crumbs(path), msg=msgs, repo_name=repo_name, branch_name=branch, path=path, repo=repo, success=success, ) return mix_response(request, "stratus/upload_file.html", context, json_convert)
def delete(request, repo_name, branch, path): msgs = [] json_convert = None success = False removed = False repo = get_repo(repo_name) tree = repo.tree() try: ftree = tree[path] # check if exixs under the tree except KeyError: msgs.append(MSG_NO_FILE_REPO % path) else: file_abs_path = os.path.join(repo.working_dir, path) if os.path.isfile(file_abs_path): try: os.remove(file_abs_path) except: removed = False else: removed = True elif os.path.isdir(file_abs_path): import shutil try: shutil.rmtree(file_abs_path) except: removed = False else: removed = True if removed: git = repo.git del_message = git.rm("-r", file_abs_path) signals.file_removed.send(sender=repo, file_path=file_path, url="") msgs.append(del_message) msg = MSG_DELETE_SUCCESS % path commit_result = git.commit("-m", u"""%s""" % msg) msgs.append(commit_result) success = True if request.is_ajax(): json_convert = message_convert else: dir_path = "/".join(path.split("/")[:-1]) return redirect("stratus-tree-view", repo_name, branch, dir_path) else: msgs.append(MSG_DELETE_ERROR % path) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, breadcrumbs=make_crumbs(path), repo_name=repo_name, repo=repo, msg=msgs, branch_name=branch, reload_fbrowser=success, path=path, ) return mix_response(request, "stratus/delete_file.html", context, json_convert)
def view(request, repo_name, branch, path, commit_sha=None): """ view file in the commit """ file_source = diff = "" if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view(request, msg) file_path = path #!!! FIX security if path[-1:] == "/": path = path[:-1] repo = get_repo(repo_name) commit, tree = get_commit_tree(repo, commit_sha) if commit.parents: diff = get_diff(repo, path, commit.parents[0].hexsha, commit.hexsha) try: tree = tree[path] except KeyError: msg = MSG_NO_FILE_IN_TREE return error_view(request, msg) if not tree.type is "blob": msg = MSG_NO_FILE_IN_TREE return error_view(request, msg) mime = tree.mime_type.split("/") file_source = tree.data_stream[3].read() # import ipdb; ipdb.set_trace() file_meta = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, abspath=tree.abspath, mime=tree.mime_type, size=tree.size, tree=tree, path=tree.abspath, mime_type=mime[0], type=file_type_from_mime(tree.mime_type), ) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, file_source=file_source, breadcrumbs=make_crumbs(path), commit=commit, diff=diff, file_meta=file_meta, repo_name=repo_name, repo=repo, branch_name=branch, path=path, name=path.split("/")[-1:][0], ) if mime[0] == "image": import base64 context["img_base"] = base64.b64encode(file_source) from getimageinfo import getImageInfo context["img_meta"] = getImageInfo(file_source) return mix_response(request, "stratus/view_file.html", context)
def edit(request, repo_name, branch=REPO_BRANCH, path=None): file_source = "" msgs = [] json_convert = None if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view(request, result_msg) if path[-1:] == "/": path = path[:-1] file_path = path #!!! FIX security repo = get_repo(repo_name) tree = repo.tree() # edit only if exist in tree try: tree = tree[path] except KeyError: msg.append(MSG_NO_FILE_IN_TREE) return error_view(request, msg) # edit only if it is file if not tree.type is "blob": msgs.append(MSG_CANT_VIEW) return error_view(request, msg) mime = tree.mime_type.split("/") file_meta = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, abspath=tree.abspath, repo=repo, mime=tree.mime_type, size=tree.size, tree=tree, mime_type=mime[0], type=file_type_from_mime(tree.mime_type), ) if file_meta["mime_type"] in EDITABLE_MIME_TYPES: form_class = TextFileEditForm else: form_class = FileEditForm signals.file_edit_start.send(sender=repo, file_path=file_path, url="") if request.method == "POST": form = form_class(request.POST, request.FILES) if form.is_valid(): file_abs_path = os.path.join(repo.working_dir, file_path) if file_meta["mime_type"] == "text" or mime[1] in ["xml"]: file_source = form.cleaned_data["file_source"] file_writen = write_file(file_abs_path, file_source) else: file_writen = handle_uploaded_file(file_abs_path, request.FILES["file_source"]) if file_writen: msgs.append("File has been saved") message = form.cleaned_data["message"] ammend = form.cleaned_data.get("ammend", None) msg = mk_commit(repo, message, file_path, ammend) msgs.append(msg) else: msgs.append(MSG_CANT_SAVE_FILE) else: msgs.append(form.errors) if request.is_ajax(): json_convert = message_convert else: if file_meta["mime_type"] in EDITABLE_MIME_TYPES: file_source = tree.data_stream[3].read else: file_source = file_meta["abspath"] form = form_class(initial={"file_source": file_source, "message": "modified %s" % path}) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, form=form, file_source=file_source, breadcrumbs=make_crumbs(path), file_meta=file_meta, msg=msgs, repo_name=repo_name, branch_name=branch, repo=repo, delete_form=FileDeleteForm(initial={"message": MSG_DELETE_SUCCESS % path}), path=path, name=path.split("/")[-1:][0], ) if mime[0] == "image": import base64 context["img_base"] = base64.b64encode(file_source) from getimageinfo import getImageInfo context["img_meta"] = getImageInfo(file_source) return mix_response(request, "stratus/edit.html", context, json_convert)
def edit(request, repo_name, branch=REPO_BRANCH, path=None ): result_msg = file_source = "" if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view( request, result_msg) if path[-1:] == "/": path = path[:-1] file_path = path #!!! FIX security repo = get_repo( repo_name ) tree = repo.tree() try: tree = tree[path] except KeyError: msg = MSG_NO_FILE_IN_TREE return error_view( request, msg) if not tree.type is "blob": msg = MSG_CANT_VIEW return error_view( request, msg) mime = tree.mime_type.split("/") file_meta = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, abspath = tree.abspath, mime = tree.mime_type, size = tree.size, tree = tree, mime_type = mime[0], type = file_type_from_mime(tree.mime_type), ) if file_meta["mime_type"] in EDITABLE_MIME_TYPES: form_class = TextFileEditForm else: form_class = FileEditForm if request.method == 'POST': form = form_class( request.POST, request.FILES ) if form.is_valid(): if file_meta["mime_type"] == "text": file_source = form.cleaned_data["file_source"] write_file(file_path, file_source ) else: handle_uploaded_file(file_path, request.FILES['file_source']) message = form.cleaned_data["message"] result_msg = mk_commit(repo, message, file_path ) else: result_msg = MSG_COMMIT_ERROR else: if file_meta["mime_type"] in EDITABLE_MIME_TYPES: file_source = tree.data_stream[3].read else: file_source = file_meta["abspath"] form = form_class( initial={ "file_source":file_source, "message":"modified %s" % path } ) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, form= form, file_source = file_source, breadcrumbs = make_crumbs(path), file_meta = file_meta, result_msg = result_msg, repo_name = repo_name, branch_name = branch, delete_form = FileDeleteForm( initial={"message":MSG_DELETE % path }), path = path, ) return mix_response( request, 'commitlog/edit.html', context)