<commit_before>from os.path import join, isdir, realpath, basename
from os import listdir, environ
from urllib import quote, unquote
from mimetypes import guess_type
from functools import wraps

from git import Repo
from flask import request, session, current_app, redirect

from .repo_functions import start_branch
from .href import needs_redirect, get_redirect

def dos2unix(string):
    ''' Returns a copy of the strings with line-endings corrected.
    '''
    return string.replace('\r\n', '\n').replace('\r', '\n')

def get_repo(flask_app):
    ''' Gets repository for the current user, cloned from the origin.
    '''
    dir_name = 'repo-' + session.get('email', 'nobody')
    user_dir = realpath(join(flask_app.config['WORK_PATH'], quote(dir_name)))
    
    if isdir(user_dir):
        user_repo = Repo(user_dir)
        user_repo.git.reset(hard=True)
        user_repo.remotes.origin.fetch()
        return user_repo
    
    source_repo = Repo(flask_app.config['REPO_PATH'])
    user_repo = source_repo.clone(user_dir, bare=False)
    
    return user_repo

def get_google_token():
    '''
    '''
    return environ.get('TOKEN_ROOT_DIR', '.').rstrip('/') + '/access_token'

def name_branch(description):
    ''' Generate a name for a branch from a description.
    
        Prepends with session.email, and replaces spaces with dashes.

        TODO: follow rules in http://git-scm.com/docs/git-check-ref-format.html
    '''
    safe_description = description.replace('.', '-').replace(' ', '-')
    return quote(session['email'], '@.-_') + '/' + quote(safe_description, '-_!')

def branch_name2path(branch_name):
    ''' Quote the branch name for safe use in URLs.<commit_msg>Added start of first commit SHA to repo dirname<commit_after>from os.path import join, isdir, realpath, basename
from os import listdir, environ
from urllib import quote, unquote
from mimetypes import guess_type
from functools import wraps

from git import Repo
from flask import request, session, current_app, redirect

from .repo_functions import start_branch
from .href import needs_redirect, get_redirect

def dos2unix(string):
    ''' Returns a copy of the strings with line-endings corrected.
    '''
    return string.replace('\r\n', '\n').replace('\r', '\n')

def get_repo(flask_app):
    ''' Gets repository for the current user, cloned from the origin.
    '''
    source_repo = Repo(flask_app.config['REPO_PATH'])
    first_commit = list(source_repo.iter_commits())[-1].hexsha
    dir_name = 'repo-{}-{}'.format(first_commit[:8], session.get('email', 'nobody'))
    user_dir = realpath(join(flask_app.config['WORK_PATH'], quote(dir_name)))
    
    if isdir(user_dir):
        user_repo = Repo(user_dir)
        user_repo.git.reset(hard=True)
        user_repo.remotes.origin.fetch()
    else:
        user_repo = source_repo.clone(user_dir, bare=False)
    
    return user_repo

def get_google_token():
    '''
    '''
    return environ.get('TOKEN_ROOT_DIR', '.').rstrip('/') + '/access_token'

def name_branch(description):
    ''' Generate a name for a branch from a description.
    
        Prepends with session.email, and replaces spaces with dashes.

        TODO: follow rules in http://git-scm.com/docs/git-check-ref-format.html
    '''
    safe_description = description.replace('.', '-').replace(' ', '-')
    return quote(session['email'], '@.-_') + '/' + quote(safe_description, '-_!')

def branch_name2path(branch_name):
    ''' Quote the branch name for safe use in URLs.
{'fraction_non_alphanumeric': 0.13123209169054442, 'fraction_numerical': 0.0017191977077363897, 'mean_word_length': 3.6671122994652405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2817934', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1088, 'n_words': 290}