예제 #1
0
    def get_commit_diffs(self, src: Commit, dst: Commit,
                         storage: StorageController):
        src_files = set(src.file_to_storage_name.keys())
        dst_files = set(dst.file_to_storage_name.keys())

        fdiffs = []

        for f in src_files.intersection(dst_files):
            diffs = self.get_diff(
                storage.read_lines_of(src.get_file_storage_name(f)),
                storage.read_lines_of(dst.get_file_storage_name(f)))

            fdiffs.append(
                FileDiff(
                    f, diffs,
                    State.NOT_CHANGED if len(diffs) == 0 else State.MODIFIED))

        fdiffs.extend([
            FileDiff(f, [], State.DELETED)
            for f in src_files.difference(dst_files)
        ])
        fdiffs.extend([
            FileDiff(
                f,
                self.get_diff([],
                              storage.read_lines_of(
                                  dst.get_file_storage_name(f))), State.NEW)
            for f in dst_files.difference(src_files)
        ])

        return fdiffs
예제 #2
0
파일: checkout.py 프로젝트: rkhapov/lgit
    def execute(self, args, path: Path):
        name = _parse_args(args)

        if not contains_repository_at(path):
            print(f'{path.root} doesnt have repository')

        provider = Provider(path)

        if not provider.is_branch(name):
            print(f'Repository doesnt have branch \'{name}\'')
            return

        if isinstance(provider.get_branch(name), Tag):
            print('Cant checkout to tag')
            return

        if provider.get_current_branch().name == name:
            print(f'Already at {name}')
            return

        provider.set_current_branch(name)
        print(f'Current branch for repository was switched to \'{name}\'')

        performer = Performer(path, StorageController(path))
        performer.perform_commit(provider.get_current_commit())

        stage = StageController(path)
        stage.clear()
        stage.write()
예제 #3
0
파일: commit.py 프로젝트: rkhapov/lgit
    def execute(self, args, path: Path):
        if not contains_repository_at(path):
            print(f'There is no repository at {path.root}')
            return

        about = _parse_about(args)

        provider = Provider(path)
        storage = StorageController(path)
        stage = StageController(path)
        differ = Differ()
        branch = provider.get_current_branch()
        commit = provider.get_commit(branch.commit_id)

        if stage.is_empty():
            print('Nothing to commit: stage is empty')
            return

        changes = differ.get_changes_from_commit(commit, storage, path)
        file_to_id = {}

        for f, s in changes.items():
            if not stage.contains(f):
                continue

            if s == State.NOT_CHANGED:
                file_to_id[f] = commit.get_file_storage_name(f)
            elif s == State.NEW or s == State.MODIFIED:
                print(f'Commit {f}')
                file_to_id[f] = storage.save_file(f)
            # deleted files are not saved in commit

        id_ = provider.get_next_commit_id()

        new_commit = cm(about, id_, commit.id, file_to_id)
        branch.set_commit_id(id_)

        provider.save_new(new_commit)
        provider.save_new(branch)

        print(new_commit.description_string)

        stage.clear()
        stage.write()
예제 #4
0
    def execute(self, args, path: Path):
        if not contains_repository_at(path):
            print(f'There is no repository at {path.root}')
            return

        src, dst, file = _parse_args(args)

        provider = Provider(path)
        storage = StorageController(path)

        if not provider.is_commit(src) or not provider.is_commit(dst):
            print('Invalid commit id')
            return

        differ = Differ()
        diffs = differ.get_commit_diffs(provider.get_commit(src),
                                        provider.get_commit(dst), storage)

        if file is not None:
            print(f'Diffs at file {file}:')

            for d in diffs:
                if path.relpath(d.name) != normpath(file):
                    continue

                if d.state == State.DELETED:
                    print('File was deleted')
                elif d.state == State.NOT_CHANGED:
                    print('File didnt changed')
                else:
                    d.print()
                break
        else:

            def print_(t, p, m):
                l = list(filter(lambda x: x.state == t, p))

                if len(l) == 0:
                    return

                print(m)
                for d in l:
                    print(d.name)

            print_(State.DELETED, diffs, 'Deleted files')
            print_(State.NEW, diffs, 'New files')
            print_(State.MODIFIED, diffs, 'Modified files')
예제 #5
0
    def get_changes_from_commit(self, commit: Commit,
                                storage: StorageController, path: Path):
        file_to_status = {}
        current_files = path.get_all_files(ignore_config_dir=True)

        for file in current_files:
            if not commit.contains_file(file):
                file_to_status[file] = State.NEW
                continue

            id_ = commit.get_file_storage_name(file)

            if self.is_file_differs(file, storage.get_path_of(id_)):
                file_to_status[file] = State.MODIFIED
            else:
                file_to_status[file] = State.NOT_CHANGED

        for cf in commit.file_to_storage_name.keys():
            if cf not in current_files:
                file_to_status[cf] = State.DELETED

        return file_to_status
예제 #6
0
파일: branch.py 프로젝트: rkhapov/lgit
    def execute(self, args, path: Path):
        if not contains_repository_at(path):
            print(f'There is not repository at {path.root}')
            return

        name, id_, all_ = _parse_args(args)
        provider = Provider(path)

        if all_:
            _print_all_branches(provider)
            return

        if id_ is None:
            _create_branch(name, provider)
        else:
            if not provider.is_commit(id_):
                print(f'No that commit: {id_}')
                return

            _move_branch(name, id_, provider)

            performer = Performer(path, StorageController(path))
            performer.perform_commit(provider.get_commit(id_))
예제 #7
0
 def __init__(self, path: Path):
     self.__path = path
     self.__provider = Provider(path)
     self.__storage = StorageController(path)