def commit(doc: Doc) -> Doc:
    """Add new index for incremental update."""
    chg = changes(doc)
    if len(chg) == 0:
        return doc
    current_index = doc.index[-1]
    nb_rev = len(doc.index)
    new_doc = copy_doc(doc, revision='NEXT')
    new_index0 = {'o_num': 0, 'o_gen': 0, 'o_ver': nb_rev, 'doc_ver': nb_rev}
    if type(doc.index[0][0]) == dict:
        x_num = doc.index[0][0].get('xref_stream_num')
    else: #TODO
        x_num = doc.index[0][0][0].get('xref_stream_num')
    if x_num:
        new_index0['xref_stream_num'] = -1
        if x_num == -1:
            x_num = len(current_index)
            new_index_xs = {'o_num': x_num, 'o_gen': 0, 'o_ver': 0, 'doc_ver': nb_rev-1}
            new_doc.index[-1].append(new_index_xs)
            new_doc.index[-1][0]['xref_stream_num'] = x_num
        if new_doc.cache[0].get('/DecodeParms'):
            del new_doc.cache[0]['/DecodeParms']
    new_doc.cache[0]['/Size'] = len(new_doc.index[-1])
    if 'eof_cut' not in new_doc.data[-1]:
        if nb_rev == 1:
            v = version(doc)
            header = f"%PDF-{v}".encode('ascii')
            idx = len(header)
            new_bdata, new_i = build_revision_byte_stream(chg, new_doc.index[-1], new_doc.cache, idx, x_num)
            new_bdata = header + new_bdata
            new_prov = bdata_dummy(new_bdata)
        else:
            header = b''
            idx = revision_index(doc)
            new_bdata, new_i = build_revision_byte_stream(chg, new_doc.index[-1], new_doc.cache, idx, x_num)
            new_prov = merge_fdata(new_doc.data[-1]['fdata'], idx, new_bdata)
        if new_bdata:
            new_doc.data[-1]['bdata'] = new_bdata
            new_doc.data[-1]['fdata'] = new_prov
        new_doc.index[-1] = new_i
    new_doc.data.append({'fdata': new_doc.data[-1]['fdata']})
    new_v = [new_index0] + [new_doc.index[-1][i] for i in range(1,len(new_doc.index[-1]))] 
    new_doc.index.append(new_v)
    new_trailer = new_doc.cache[0]
    if type(new_doc.index[-2][0]) == list: #Linearized
        new_trailer['/Prev'] = new_doc.index[-2][1].get('xref_table_pos') or new_doc.index[-2][1].get('xref_stream_pos')
    else:
        new_trailer['/Prev'] = new_doc.index[-2][0].get('xref_table_pos') or new_doc.index[-2][0].get('xref_stream_pos')
    if '/XRefStm' in new_trailer:
        del new_trailer['/XRefStm']
    return new_doc
Add new index for incremental update.
pdfsyntax/docstruct.py