예제 #1
0
def update_index(files_update, mode):
    '''
    Task: Update information of tracked file or untracked file in index file
        + With add command:
            - Update hash add and timestamp of file
            - Or add new line infomation file added in index file
        + With status command:
            - Update hash current and timestamp of file in index file
        + With commit command:
            - Update hash commit of file in index file
    '''
    data_index = read_file('.lgit/index')
    location = lgit_g.get_pos_track(files_update)
    for file in files_update:
        # commit command then ignore about file doesn't exist
        # or haven't permission to read else skip this file
        # if there is valid file then get location file
        if mode != 'commit' and (not os.path.exists(file) or
                                 not os.access(file, os.R_OK)):
            continue
        line = location.get(file, -1)

        # add command need hash sha1 file
        # hash add equal hash file
        # hash commit get from index file or empty
        if mode == 'add':
            h_current = hash_sha1(file)
            h_add = h_current
            if line != -1:
                _, _, _, h_commit, _ = lgit_g.get_info_index(data_index[line])

        # commit command only read inside index file
        # read hash current, hash add
        # then now hash commit equal hash add
        elif mode == 'commit':
            _, h_current, h_add, _, _ = lgit_g.get_info_index(data_index[line])
            h_commit = h_add

        # status command update timestamp and hash file now
        # get hash sha1 file then read index file get hash add and commit
        elif mode == 'status':
            h_current = hash_sha1(file)
            _, _, h_add, h_commit, _ = lgit_g.get_info_index(data_index[line])

        if line != -1:
            data_index[line] = format_index(format_time(
                os.path.getmtime(file)), h_current, h_add, h_commit, file)

        # when add command if file not in index file then append it into list
        else:
            data_index.append(format_index(format_time(
                os.path.getmtime(file)), h_current, h_add, '', file))

    # hash information changes is diffent with origin index file
    # if different then update information changes into index file
    if hash_sha1('.lgit/index') != hash_sha1(data_index, mode='list'):
        write_file(data_index, file='.lgit/index')
예제 #2
0
def create_stash(modified_file, time_ns):
    if _is_valid_stash(modified_file):
        create_object(modified_file)
        data_stash = []
        for file in modified_file:
            data_stash.append("%s %s\n" % (hash_sha1(file), file))
        write_file(data_stash, '.lgit/refs/stash/%s' % (time_ns))
예제 #3
0
def get_modified_branch():
    branch_commit = get_commit_branch()
    files_hash = get_files_hash(branch_commit)
    modified_file = []
    for file in files_hash.keys():
        if hash_sha1(file) != files_hash[file]:
            modified_file.append(file)
    return modified_file
예제 #4
0
def create_object(files_add):
    '''
    Task:
        + Store a copy of the file content in the lgit database
        + Each file will be stocked in the following way:
            - first two characters of the SHA1 will be the directory name
            - last 38 characters will be the file name
    '''
    for path in files_add:
        hash_f = hash_sha1(path)
        direc_obj, file_obj = split_dir_file(hash_f)
        direc_obj = '.lgit/objects/%s' % (direc_obj)
        if not os.path.exists(direc_obj):
            os.makedirs(direc_obj)
        file_obj = os.path.join(direc_obj, file_obj)
        if not os.path.exists(file_obj):
            write_file(read_file(path, mode='rb'), file_obj, mode='wb')
예제 #5
0
def update_files_commit(files_hash):
    '''
    Task:
        + Get dictionary of commit passed: file is key, hash of file is value
        + Loop all file in commit if different hash then
                    get content of file in database object
        + If file exists but not have permission write then remove it
        + Create file or overwrite content of file
    :param files_hash: a dictionary key is file, value is hash of file
    :return: list files is changed content from commit passed
    '''
    files_update = []
    for file in files_hash.keys():
        if os.path.exists(file) and not os.access(file, os.W_OK):
            os.remove(file)
        if not os.path.exists(file) or files_hash[file] != hash_sha1(file):
            update_content_file(file, files_hash[file])
        files_update.append(file)
    return files_update
예제 #6
0
from utils import hash_sha1
from db import db_handler

USER = '******'
PASSWD = 'passwd'
PASSWD_HASH = hash_sha1(PASSWD)
PASS2 = hash_sha1('coucou')

db = db_handler()

db.edit('INSERT INTO user (username, passwd_hash) VALUES (?, ?)', (
    USER,
    PASSWD_HASH,
))

db.edit('INSERT INTO user (username, passwd_hash) VALUES (?,?)',
        ('Pouet', PASS2))

res = db.query("SELECT * FROM user")
print(res[0]['username'])

res = db.get_user_password(USER)
print(res)

res = db.get_user_password('test2')
print(res)

db.close()