Пример #1
0
    def _commit(self, original_commits, blobs):
        for filename, blob_id in blobs.items():
            file_mode = self._file_modes[filename]
            index_entry = IndexEntry(filename, blob_id, file_mode)
            self.repo.index.add(index_entry)

        commits = [self.repo.get(h) for h in original_commits]

        main_commit = commits[0]
        if len(commits) > 1:
            # most recent commit
            main_commit = sorted(commits, key=commit_datetime)[-1]

        commit_message = main_commit.message
        commit_message += "\n\nautomatic commit by git-black, original commits:\n"
        commit_message += "\n".join(
            ["  {}".format(c) for c in original_commits])

        committer = Signature(
            name=self.repo.config["user.name"],
            email=self.repo.config["user.email"],
        )

        self.repo.index.write()
        tree = self.repo.index.write_tree()
        head = self.repo.head.peel()
        self.repo.create_commit("HEAD", main_commit.author, committer,
                                commit_message, tree, [head.id])
        self.progress += 1
        now = time.monotonic()
        if now - self.last_log > 0.04:
            sys.stdout.write("Making commit {}/{} \r".format(
                self.progress, self.total))
            sys.stdout.flush()
            self.last_log = now
    def addFileToIndex(
        self,
        fileAttributes,
        fileBlob=None,
        externalPath=None,
    ):
        destRepo = self._repository
        index = destRepo.index

        filePath, fileMode = fileAttributes

        newBlobId = None

        if (fileBlob):
            if fileBlob.id not in destRepo:
                destRepo.write(fileBlob.type, fileBlob.read_raw())
            newBlobId = fileBlob.id
        elif (externalPath):
            newBlobId = destRepo.create_blob_fromdisk(externalPath)

        if not newBlobId:
            raise Exception(
                "Could not add file: attributes: {0}, blob = {1}, externalPath = {2}"
                .format(fileAttributes, fileBlob, externalPath))

        indexEntry = IndexEntry(filePath, newBlobId, fileMode)
        index.add(indexEntry)

        return newBlobId
Пример #3
0
    def add_spo(self, subject, predicate, data):
        """creates a staged entry of subject, predicate and object

        :param subject:
        :param predicate:
        :param data:
        :return: ``bytes`` - the blob id
        """
        blob_id = self.repository.create_blob(data)
        entry = IndexEntry(os.path.join(subject, predicate), blob_id,
                           GIT_FILEMODE_BLOB)
        self.repository.index.add(entry)
        return blob_id
Пример #4
0
    def add_spo(self, edge, predicate, data):
        """creates a staged entry of edge, predicate and object

        :param edge:
        :param predicate:
        :param data:
        :returns: ``bytes`` - the blob id
        """
        edge = resolve_edge_name(edge)
        blob_id = self.repository.create_blob(data)
        entry = IndexEntry(os.path.join(edge, predicate), blob_id,
                           GIT_FILEMODE_BLOB)
        self.repository.index.add(entry)
        return blob_id
Пример #5
0
    def reset(self, hard: bool = False, paths: Iterable[str] = None):
        from pygit2 import GIT_RESET_HARD, GIT_RESET_MIXED, IndexEntry

        self.repo.index.read(False)
        if paths is not None:
            tree = self.repo.revparse_single("HEAD").tree
            for path in paths:
                rel = relpath(path, self.root_dir)
                obj = tree[relpath(rel, self.root_dir)]
                self.repo.index.add(IndexEntry(rel, obj.oid, obj.filemode))
            self.repo.index.write()
        elif hard:
            self.repo.reset(self.repo.head.target, GIT_RESET_HARD)
        else:
            self.repo.reset(self.repo.head.target, GIT_RESET_MIXED)
Пример #6
0
 def do_transaction(self, commit_message, data, added, changed, removed,
                    handlers):
     self.nb_transactions += 1
     # Get informations
     git_author, git_date, git_msg, docs_to_index, docs_to_unindex = data
     git_msg = commit_message or git_msg or 'no comment'
     # List of Changed
     added_and_changed = list(added) + list(changed)
     # Build the tree from index
     index = self.repo.index
     for key in added_and_changed:
         handler = handlers.get(key)
         blob_id = self.repo.create_blob(handler.to_str())
         entry = IndexEntry(key, blob_id, GIT_FILEMODE_BLOB_EXECUTABLE)
         index.add(entry)
     for key in removed:
         index.remove(key)
     git_tree = index.write_tree()
     # Commit
     self.git_commit(git_msg, git_author, git_date, tree=git_tree)
def rewriteCommit(srcRepo, destRepo, currentCommit):
    if not currentCommit.parents:
        print("No parents, copying commit...")
        copyCommit(srcRepo, destRepo, currentCommit)
        print("Copied commit {0}\n".format(currentCommit.id))
        return currentCommit.id
    else:
        index = destRepo.index

        # Look up the original parent commit
        parentCommit = currentCommit.parents[0]

        allParentIds = [p.id for p in currentCommit.parents]
        print("Parent IDs: {0}".format(allParentIds))

        # Get the ID of the rewritten parent commit that was generated by the last iteration
        rewrittenParentHash = rewrittenCommits.get(parentCommit.id, parentCommit.id)
        # Look up that commit object too
        rewrittenParentCommit = destRepo[rewrittenParentHash]

        # Calculate the original diff for this commit
        diff = srcRepo.diff(currentCommit, parentCommit)

        #print("Resetting index to rewritten...")
        # Reset the index to the previously-rewritten commit as our starting point
        index.read_tree(rewrittenParentCommit.tree)



        changedJSFiles = []
        changedPythonFiles = []
        allOtherFiles = []

        print("Checking deltas...")
        for delta in diff.deltas:
            statusChar = delta.status_char()
            # Check for any modified or added files
            if statusChar in ('M', 'A'):
                if(isFormattableJSSourceFile(delta.new_file.path)):
                    changedJSFiles.append(delta)
                elif(isFormattablePythonSourceFile(delta.new_file.path)):
                    changedPythonFiles.append(delta)
                else:
                    allOtherFiles.append(delta)
            elif statusChar == 'D':
                try:
                    index.remove(delta.old_file.path)
                except:
                    pass
            else:
                q = 42
                raise Exception("Unexpected delta status type!")


        transformedFiles = []

        if changedJSFiles or changedPythonFiles:
            print("Transforming JS+PY files...")

            resetTempFolder()

            # TODO Transform these files appropriately
            # TODO Write each file to a temp file in the temp folder, then run transformation
            for delta in changedJSFiles + changedPythonFiles:
                new_file = delta.new_file
                # Remove the existing entry, if any:
                try:
                    index.remove(new_file.path)
                except:
                    pass

                fileBlob = srcRepo[delta.new_file.id]
                tempFilePath = writeTempBlob(fileBlob, new_file.path)
                transformedFiles.append( (new_file.path, new_file.mode, tempFilePath))

            for delta in changedJSFiles:
                print("JS: " + delta.new_file.path)

            for delta in changedPythonFiles:
                print("PY: " + delta.new_file.path)


            # TODO For each transformed file, add it to the repo's object database
            for originalFilePath, originalFileMode, transformedFilePath in transformedFiles:
                transformedFileHash = destRepo.create_blob_fromdisk(transformedFilePath)
                indexEntry = IndexEntry(originalFilePath, transformedFileHash, originalFileMode)
                index.add(indexEntry)

        for delta in allOtherFiles:
            # Replicate all other file operations
            nf = delta.new_file
            fileBlob = srcRepo[nf.id]
            destRepo.write(fileBlob.type, fileBlob.read_raw())

            indexEntry = IndexEntry(nf.path, nf.id, nf.mode)

            index.add(indexEntry)

        newTreeId = index.write_tree()

        newParents = [getRewrittenId(parentCommit) for parent in currentCommit.parents]

        rewrittenCommitId = destRepo.create_commit(
            None,
            currentCommit.author,
            currentCommit.committer,
            currentCommit.message,
            newTreeId,
            newParents
        )

        rewrittenCommits[currentCommit.id] = rewrittenCommitId
        rewrittenCommit = destRepo[rewrittenCommitId]

        print("Rewrote {0} to {1}\n".format(currentCommit.id, rewrittenCommitId))

        return rewrittenCommitId