Fossil

Check-in [31b1e5a8]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Significant speed improvment
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | svn-import
Files: files | file ages | folders
SHA1:31b1e5a8260eecf7057e40163f6c8d259b1823ee
User & Date: baruch 2014-10-31 08:05:29
Context
2014-10-31
08:06
merge trunk check-in: 7427b40d user: baruch tags: svn-import
08:05
Significant speed improvment check-in: 31b1e5a8 user: baruch tags: svn-import
2014-10-28
12:19
fix bug with multi-window svndiff check-in: ea54a298 user: baruch tags: svn-import
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/db.c.

284
285
286
287
288
289
290


291
292
293
294
295
296
297
    va_start(ap, zFormat);
    rc = db_vprepare(pStmt, 0, zFormat, ap);
    pStmt->pNext = db.pAllStmt;
    pStmt->pPrev = 0;
    if( db.pAllStmt ) db.pAllStmt->pPrev = pStmt;
    db.pAllStmt = pStmt;
    va_end(ap);


  }
  return rc;
}

/*
** Return the index of a bind parameter
*/







>
>







284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    va_start(ap, zFormat);
    rc = db_vprepare(pStmt, 0, zFormat, ap);
    pStmt->pNext = db.pAllStmt;
    pStmt->pPrev = 0;
    if( db.pAllStmt ) db.pAllStmt->pPrev = pStmt;
    db.pAllStmt = pStmt;
    va_end(ap);
  }else{
    db_reset(pStmt);
  }
  return rc;
}

/*
** Return the index of a bind parameter
*/

Changes to src/import.c.

715
716
717
718
719
720
721









722
723
724
725
726
727
728
...
878
879
880
881
882
883
884
885

886
887

888
889
890
891





892
893
894
895
896
897
898
899
900
901
902
903
904



905
906
907
908
909
910
911
912

913




914
915



916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940

941
942
943
944
945
946








947
948
949

950


951

952
953
954

955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997



998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
....
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
....
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175



1176
1177
1178







1179
1180
1181
1182
1183
1184
1185

1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222

1223
1224
1225
1226



1227
1228
1229
1230
1231
1232
1233
....
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249

1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
....
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
....
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393




1394
1395
1396
1397






1398
1399
1400
1401
1402
1403
1404

1405
1406
1407
1408
1409
1410
1411

malformed_line:
  trim_newline(zLine);
  fossil_fatal("bad fast-import line: [%s]", zLine);
  return;
}










typedef struct {
  const char *zKey;
  const char *zVal;
} KeyVal;
typedef struct {
  KeyVal *aHeaders;
  int nHeaders;
................................................................................
    }
  }else{
    rec->contentFlag = 0;
  }
  return 1;
}

static void svn_create_manifests(int flatFlag){

  Blob manifest;
  Stmt qRev;

  Stmt qParent;
  Stmt qFiles;
  Stmt qTags;
  Stmt setUuid;






  if( !flatFlag ){
    db_multi_exec("DELETE FROM xrevisions WHERE tbranch ISNULL;"
                  "UPDATE xrevisions SET tparent=("
                  "  SELECT ifnull(max(trev),-1) FROM xrevisions t"
                  "  WHERE t.trev<xrevisions.trev"
                  "    AND t.tbranch=xrevisions.tbranch"
                  " )"
                  " WHERE tparent ISNULL;"
                  "WITH xprefix AS ( "
                  "  SELECT trev, CASE tbranch WHEN 'trunk' THEN 'trunk/'"
                  "  ELSE 'branches/'||tbranch||'/' END xpref"
                  "  FROM xrevisions"



                  " ), "
                  " x AS (SELECT trev xrev, xpref, length(xpref) xlen "
                  " FROM xprefix) "
                  " UPDATE xfiles SET tpath= "
                  " CASE substr(tpath,1,(SELECT xlen FROM x WHERE xrev=trev))"
                  " WHEN (SELECT xpref FROM x WHERE xrev=trev)"
                  "  THEN substr(tpath,(SELECT xlen FROM x WHERE xrev=trev)+1)"
                  " END;"

                  "DELETE FROM xfiles WHERE tpath ISNULL");




  }else{
    db_multi_exec("UPDATE xrevisions SET tparent=trev-1");



  }
  db_prepare(&qRev, "SELECT trev, tuser, tmsg, ttime, tparent, tbranch"
                    " FROM xrevisions "
                    " ORDER BY trev");
  db_prepare(&qParent, "SELECT tuuid, tbranch FROM xrevisions WHERE trev=:rev");
  db_prepare(&qFiles, "SELECT tpath, uuid, tperm"
                      " FROM xfiles JOIN blob ON xfiles.trid=blob.rid"
                      " WHERE trev=:rev ORDER BY tpath");
  db_prepare(&qTags, "SELECT ttag FROM xtags WHERE trev=:rev");
  db_prepare(&setUuid, "UPDATE xrevisions"
                       " SET tuuid=(SELECT uuid FROM blob WHERE rid=:rid)"
                       " WHERE trev=:rev");
  blob_zero(&manifest);
  while( db_step(&qRev)==SQLITE_ROW ){
    int rev = db_column_int(&qRev, 0);
    const char *zUser = db_column_text(&qRev, 1);
    const char *zMsg = db_column_text(&qRev, 2);
    const char *zTime = db_column_text(&qRev, 3);
    int parentRev = db_column_int(&qRev, 4);
    const char *zBranch = db_column_text(&qRev, 5);
    int rid;
    const char *zParentBranch = 0;
    Blob mcksum;
    blob_reset(&manifest);
    if( zMsg ){

      blob_appendf(&manifest, "C %F\n", zMsg);
    }else{
      blob_append(&manifest, "C (no\\scomment)\n", 16);
    }
    blob_appendf(&manifest, "D %s\n", zTime);
    db_bind_int(&qFiles, ":rev", rev);








    while( db_step(&qFiles)==SQLITE_ROW ){
      const char *zFile = db_column_text(&qFiles, 0);
      const char *zUuid = db_column_text(&qFiles, 1);

      const char *zPerm = db_column_text(&qFiles, 2);


      blob_appendf(&manifest, "F %F %s %s\n", zFile, zUuid, zPerm);

    }
    db_reset(&qFiles);
    if( parentRev>=0 ){

      const char *zParentUuid;
      db_bind_int(&qParent, ":rev", parentRev);
      db_step(&qParent);
      zParentUuid = db_column_text(&qParent, 0);
      blob_appendf(&manifest, "P %s\n", zParentUuid);
      if( !flatFlag ){
        zParentBranch = db_column_text(&qParent, 1);
        if( strcmp(zBranch, zParentBranch)!=0 ){
          blob_appendf(&manifest, "T *branch * %s\n", zBranch);
          blob_appendf(&manifest, "T *sym-%s *\n", zBranch);
          zParentBranch = mprintf("%s", zParentBranch);
        }else{
          zParentBranch = 0;
        }
      }
      db_reset(&qParent);
    }else{
      blob_appendf(&manifest, "T *branch * trunk\n");
      blob_appendf(&manifest, "T *sym-trunk *\n");
    }
    db_bind_int(&qTags, ":rev", rev);
    while( db_step(&qTags)==SQLITE_ROW ){
      const char *zTag = db_column_text(&qTags, 0);
      blob_appendf(&manifest, "T +sym-%s *\n", zTag);
    }
    db_reset(&qTags);
    blob_appendf(&manifest, "T +sym-svn-rev-%d *\n", rev);
    if( zParentBranch ) {
      blob_appendf(&manifest, "T -sym-%s *\n", zParentBranch);
      zParentBranch = 0;
    }
    if( zUser ){
      blob_appendf(&manifest, "U %F\n", zUser);
    }else{
      const char *zUserOvrd = find_option("user-override",0,1);
      blob_appendf(&manifest, "U %F\n",
        zUserOvrd ? zUserOvrd : login_name());
    }
    md5sum_blob(&manifest, &mcksum);
    blob_appendf(&manifest, "Z %b\n", &mcksum);
    blob_reset(&mcksum);

    rid = content_put(&manifest);



    db_bind_int(&setUuid, ":rid", rid);
    db_bind_int(&setUuid, ":rev", rev);
    db_step(&setUuid);
    db_reset(&setUuid);
  }
  db_finalize(&qRev);
  db_finalize(&qParent);
  db_finalize(&qFiles);
  db_finalize(&qTags);
  db_finalize(&setUuid);
}

static u64 svn_get_varint(const char **pz){
  unsigned int v = 0;
  do{
    v = (v<<7) | ((*pz)[0]&0x7f);
  }while( (*pz)++[0]&0x80 );
................................................................................
  }
}

/*
** Read the svn-dump format from pIn and insert the corresponding
** content into the database.
*/
static void svn_dump_import(FILE *pIn, int flatFlag){
  SvnRecord rec;
  int ver;
  const char *zTemp;
  const char *zUuid;
  char zBranch[200] = {0};
  Stmt insRev;
  Stmt insFile;
  Stmt insTag;
  Stmt delFile;
  Stmt setBranch;
  Stmt setParent;
  int rev = 0;

  /* version */
  if( svn_read_rec(pIn, &rec)
   && (zTemp = svn_find_header(rec, "SVN-fs-dump-format-version")) ){
    ver = atoi(zTemp);
    if( ver!=2 && ver!=3 ){
      fossil_fatal("Unknown svn-dump format version: %d", ver);
................................................................................
  svn_free_rec(&rec);
  /* UUID */
  if( !svn_read_rec(pIn, &rec) || !(zUuid = svn_find_header(rec, "UUID")) ){
    fossil_fatal("Missing UUID!");
  }
  svn_free_rec(&rec);
  /* content */
  db_prepare(&insRev,
      "INSERT INTO xrevisions (trev, tuser, tmsg, ttime) "
      "VALUES(:rev, :user, :msg, :time)"
  );
  db_prepare(&insFile,
      "INSERT INTO xfiles (trev, tpath, trid, tperm) "
      "VALUES(:rev, :path, :rid, :perm)"
  );
  db_prepare(&insTag, "INSERT INTO xtags (trev, ttag) VALUES(:rev, :tag)");
  db_prepare(&delFile,
      "DELETE FROM xfiles "
      "WHERE trev=:rev "
      "  AND (tpath=:path OR (tpath>:path||'/' AND tpath<:path||'0'))"
  );
  db_prepare(&setBranch,
      "UPDATE xrevisions SET tbranch=:branch "
      "WHERE trev=:rev"
  );
  db_prepare(&setParent,
      "UPDATE xrevisions SET tparent=:parent "
      "WHERE trev=:rev"
  );
  while( svn_read_rec(pIn, &rec) ){
    if( zTemp = svn_find_header(rec, "Revision-number") ){
      const char *zUser = svn_find_prop(rec, "svn:author");
      const char *zLog = svn_find_prop(rec, "svn:log");
      const char *zDate = svn_find_prop(rec, "svn:date");
      if( !flatFlag ){
        if( *zBranch ){
          db_bind_text(&setBranch, ":branch", zBranch);
          db_bind_int(&setBranch, ":rev", rev);
          db_step(&setBranch);
          db_reset(&setBranch);
          zBranch[0] = 0;
        }
      }
      zDate = date_in_standard_format(zDate);
      rev = atoi(zTemp);
      db_bind_int(&insRev, ":rev", rev);
      db_bind_text(&insRev, ":user", zUser);
      db_bind_text(&insRev, ":msg", zLog);
      db_bind_text(&insRev, ":time", zDate);
      db_step(&insRev);
      db_reset(&insRev);
      fossil_free(zDate);
      if( rev>0 ){
        db_multi_exec("INSERT INTO xfiles (trev, tpath, trid, tperm) "
                      "SELECT %d, tpath, trid, tperm FROM xfiles "
                      "WHERE trev=%d", rev, rev-1);
      }
    }else
    if( zTemp = svn_find_header(rec, "Node-path") ){
      const char *zPath = zTemp;
      const char *zAction = svn_find_header(rec, "Node-action");
      const char *zKind = svn_find_header(rec, "Node-kind");
      const char *zSrcPath = svn_find_header(rec, "Node-copyfrom-path");
      const char *zPerm = svn_find_prop(rec, "svn:executable") ? "x" : 0;
      int deltaFlag = 0;
      int srcRev = -1;
      int rid = 0;
      if( zTemp = svn_find_header(rec, "Text-delta") ){
        deltaFlag = strncmp(zTemp, "true", 4)==0;
      }
      if( zSrcPath ){
        zTemp = svn_find_header(rec, "Node-copyfrom-rev");
        if( zTemp ){
          srcRev = atoi(zTemp);
        }else{
          fossil_fatal("Missing copyfrom-rev");
        }
      }
      if( !flatFlag ){
        if( strncmp(zPath, "branches/", 9)==0 ){
          int i;
          sqlite3_snprintf(sizeof(zBranch), zBranch, "%s", zPath+9);
          for( i=0; zBranch[i]; i++ ){
            if( zBranch[i]=='/' ) zBranch[i]=0;
          }



        }else
        if( strncmp(zPath, "trunk/", 6)==0 ){
          memcpy(zBranch, "trunk", 6);







        }
      }
      if( strncmp(zAction, "delete", 6)==0
       || strncmp(zAction, "replace", 7)==0 )
      {
        db_bind_int(&delFile, ":rev", rev);
        db_bind_text(&delFile, ":path", zPath);

        db_step(&delFile);
        db_reset(&delFile);
      } /* no 'else' here since 'replace' does both a 'delete' and an 'add' */
      if( strncmp(zAction, "add", 3)==0
       || strncmp(zAction, "replace", 7)==0 )
      {
        if( zKind==0 ){
          fossil_fatal("Missing Node-kind");
        }else if( strncmp(zKind, "dir", 3)==0 ){
          if( zSrcPath ){
            db_multi_exec(
              "INSERT INTO xfiles (trev, tpath, trid, tperm) "
              " SELECT %d, %Q||substr(tpath, length(%Q)+1), trid, tperm "
              " FROM xfiles "
              " WHERE trev=%d AND tpath GLOB '%q/*'",
              rev, zPath, zSrcPath, srcRev, zSrcPath
            );
            if( !flatFlag ){
              if( strncmp(zPath, "branches/", 9)==0 ){
                zTemp = zPath+9;
                while( *zTemp && *zTemp!='/' ){ zTemp++; }
                if( *zTemp==0 ){
                  db_bind_int(&setParent, ":parent", srcRev);
                  db_bind_int(&setParent, ":rev", rev);
                  db_step(&setParent);
                  db_reset(&setParent);

                }
              }else if( strncmp(zPath, "tags/", 5)==0 ){
                zTemp = zPath+5;
                db_bind_int(&insTag, ":rev", srcRev);
                db_bind_text(&insTag, ":tag", zTemp);
                db_step(&insTag);
                db_reset(&insTag);
              }
            }
          }
        }else{

          if( zSrcPath ){
            rid = db_int(0,
                         "SELECT trid FROM xfiles WHERE trev=%d AND tpath=%Q",
                         srcRev, zSrcPath);



          }
          if( deltaFlag ){
            Blob deltaSrc;
            Blob target;
            if( rid!=0 ){
              content_get(rid, &deltaSrc);
            }else{
................................................................................
              blob_zero(&deltaSrc);
            }
            svn_apply_svndiff(&rec.content, &deltaSrc, &target);
            rid = content_put(&target);
          }else if( rec.contentFlag ){
            rid = content_put(&rec.content);
          }
          db_bind_int(&insFile, ":rev", rev);
          db_bind_int(&insFile, ":rid", rid);
          db_bind_text(&insFile, ":path", zPath);
          db_bind_text(&insFile, ":perm", zPerm);
          db_step(&insFile);
          db_reset(&insFile);
        }
      }else
      if( strncmp(zAction, "change", 6)==0 ){

        if( zKind==0 ){
          fossil_fatal("Missing Node-kind");
        }
        if( strncmp(zKind, "dir", 3)==0 ) continue;
        if( deltaFlag ){
          Blob deltaSrc;
          Blob target;
          rid = db_int(0, "SELECT trid FROM xfiles WHERE tpath=%Q AND trev=%d",
                       zPath, rev-1);
          content_get(rid, &deltaSrc);
          svn_apply_svndiff(&rec.content, &deltaSrc, &target);
          rid = content_put(&target);
        }else{
          rid = content_put(&rec.content);
        }
        db_bind_int(&insFile, ":rev", rev);
        db_bind_int(&insFile, ":rid", rid);
        db_bind_text(&insFile, ":path", zPath);
        db_bind_text(&insFile, ":perm", zPerm);
        db_step(&insFile);
        db_reset(&insFile);
      }else
      if( strncmp(zAction, "delete", 6)==0){ /* already did this above */
      }else{
        fossil_fatal("Unknown Node-action");
      }
    }else{
      fossil_fatal("Unknown record type");
    }
    svn_free_rec(&rec);
  }
  if( !flatFlag ){
    if( *zBranch ){
      db_bind_text(&setBranch, ":branch", zBranch);
      db_bind_int(&setBranch, ":rev", rev);
      db_step(&setBranch);
    }
  }
  db_finalize(&insRev);
  db_finalize(&insFile);
  db_finalize(&insTag);
  db_finalize(&delFile);
  db_finalize(&setBranch);
  db_finalize(&setParent);
  svn_create_manifests(flatFlag);
}

/*
** COMMAND: import
**
** Usage: %fossil import FORMAT ?OPTIONS? NEW-REPOSITORY ?INPUT-FILE?
**
................................................................................
*/
void import_cmd(void){
  char *zPassword;
  FILE *pIn;
  Stmt q;
  int forceFlag = find_option("force", "f", 0)!=0;
  int incrFlag = find_option("incremental", "i", 0)!=0;
  int flatFlag = find_option("flat", 0, 0)!=0;

  verify_all_options();
  if( g.argc!=4  && g.argc!=5 ){
    usage("FORMAT REPOSITORY-NAME");
  }
  if( g.argc==5 ){
    pIn = fossil_fopen(g.argv[4], "rb");
................................................................................
      import_reset(0);
    }
    db_finalize(&q);
  }else
  if( strncmp(g.argv[2], "svn", 3)==0 ){
    db_multi_exec(
       "CREATE TEMP TABLE xrevisions("
       " trev INTEGER PRIMARY KEY, tuser TEXT, tmsg TEXT, ttime DATETIME,"
       " tparent INT, tbranch TEXT, tuuid TEXT"
       ");"




       "CREATE TEMP TABLE xfiles("
       " trev INT, tpath TEXT, trid TEXT, tperm TEXT,"
       " UNIQUE (trev, tpath) ON CONFLICT REPLACE"
       ");"






       "CREATE TEMP TABLE xtags("
       " trev INT, ttag TEXT"
       ");"
    );
    svn_dump_import(pIn, flatFlag);
  }


  db_end_transaction(0);
  db_begin_transaction();
  fossil_print("Rebuilding repository meta-data...\n");
  rebuild_db(0, 1, !incrFlag);
  verify_cancel();
  db_end_transaction(0);
  fossil_print("Vacuuming..."); fflush(stdout);







>
>
>
>
>
>
>
>
>







 







|
>

<
>
|
|
|
<
>
>
>
>
>

<
<
<
<
<
|
|
|
<
<
<
|
>
>
>
|
|
|
|
|
|
|
<
>
|
>
>
>
>

<
>
>
>

<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
>
|
|
|
|
|
<
>
>
>
>
>
>
>
>
|
|
<
>
|
>
>
|
>
|
<
<
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
<
|
|
|
<
|
|
|
|
|
<
|
|
|
|
|

|
>
>
>
|
<
|
<
<
<
<
<
<
<







 







|




<
|
<

|
<
<
<







 







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






|
<
|










|

|
|
|
|
<
>
>
>


<
>
>
>
>
>
>
>





|
|
>
|
|








|
|
|
|
|
<
<
|

|
<

<
<
<
<
>











>

|
<
|
>
>
>







 







<
|
|
|
|
|



>







|
|






<
|
|
|
|
|

|
<







|
|
|
|
|
|
|
|
|
|
<
<
<
<







 







|







 







<
|

>
>
>
>

|
|

>
>
>
>
>
>




|


>







715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
...
887
888
889
890
891
892
893
894
895
896

897
898
899
900

901
902
903
904
905
906





907
908
909



910
911
912
913
914
915
916
917
918
919
920

921
922
923
924
925
926
927

928
929
930
931











932












933
934
935
936
937
938

939
940
941
942
943
944
945
946
947
948

949
950
951
952
953
954
955


956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971

972
973
974
975
976
977
978
979
980

981
982
983

984
985
986
987
988

989
990
991
992
993
994
995
996
997
998
999

1000







1001
1002
1003
1004
1005
1006
1007
....
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064

1065

1066
1067



1068
1069
1070
1071
1072
1073
1074
....
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122















1123
1124
1125
1126
1127
1128
1129

1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146

1147
1148
1149
1150
1151

1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181


1182
1183
1184

1185




1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200

1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
....
1212
1213
1214
1215
1216
1217
1218

1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242

1243
1244
1245
1246
1247
1248
1249

1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266




1267
1268
1269
1270
1271
1272
1273
....
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
....
1356
1357
1358
1359
1360
1361
1362

1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393

malformed_line:
  trim_newline(zLine);
  fossil_fatal("bad fast-import line: [%s]", zLine);
  return;
}

static struct{
  int rev;                    /* SVN revision number */
  int parent;                 /* SVN revision number of parent check-in */
  char *zBranch;              /* Name of a branch for a commit */
  char *zDate;                /* Date/time stamp */
  char *zUser;                /* User name */
  char *zComment;             /* Comment of a commit */
  int flatFlag;               /* True if whole repo is a single file tree */
} gsvn;
typedef struct {
  const char *zKey;
  const char *zVal;
} KeyVal;
typedef struct {
  KeyVal *aHeaders;
  int nHeaders;
................................................................................
    }
  }else{
    rec->contentFlag = 0;
  }
  return 1;
}

static void svn_create_manifest(
){
  Blob manifest;

  static Stmt insRev;
  static Stmt qParent;
  static Stmt qFiles;
  static Stmt qTags;

  char zFilter[200];
  int nFilter;
  int rid;
  const char *zParentBranch = 0;
  Blob mcksum;






  db_static_prepare(&insRev, "REPLACE INTO xrevisions (trev, tbranch, tuuid) "
                             "VALUES(:rev, :branch, "
                             " (SELECT uuid FROM blob WHERE rid=:rid))");



  db_static_prepare(&qParent, "SELECT tuuid, tbranch FROM xrevisions "
                              "WHERE trev=:rev");
  db_static_prepare(&qFiles, "SELECT tpath, trid, tperm FROM xfiles "
                             "WHERE tpath GLOB :filter ORDER BY tpath");
  db_static_prepare(&qTags, "SELECT ttag FROM xtags WHERE trev=:rev");
  if( db_int(0, "SELECT 1 FROM xfiles LIMIT 1")==0 ){ return; }
  if( !gsvn.flatFlag ){
    if( gsvn.zBranch==0 || gsvn.zBranch[0]=='\0' ){ return; }
    if( gsvn.parent<0 ){
      gsvn.parent = db_int(-1, "SELECT ifnull(max(trev),-1) FROM xrevisions "
                               "WHERE tbranch=%Q", gsvn.zBranch);

    }
    db_bind_int(&insRev, ":rev", gsvn.rev);
    db_bind_text(&insRev, ":branch", gsvn.zBranch);
    db_bind_int(&insRev, ":rid", 0);
    db_step(&insRev);
    db_reset(&insRev);
  }else{

    static int prevRev = -1;
    gsvn.parent = prevRev;
    prevRev = gsvn.rev;
  }











  blob_zero(&manifest);












  if( gsvn.zComment ){
    blob_appendf(&manifest, "C %F\n", gsvn.zComment);
  }else{
    blob_append(&manifest, "C (no\\scomment)\n", 16);
  }
  blob_appendf(&manifest, "D %s\n", gsvn.zDate);

  if( strncmp(gsvn.zBranch, "trunk", 5)==0 ){
    memcpy(zFilter, "trunk/*", 8);
    nFilter = 6;
  }else{
    sqlite3_snprintf(sizeof(zFilter), zFilter, "branches/%s/*", gsvn.zBranch);
    nFilter = strlen(zFilter)-1;
  }
  db_bind_text(&qFiles, ":filter", zFilter);
  while( db_step(&qFiles)==SQLITE_ROW ){
    const char *zFile = db_column_text(&qFiles, 0);

    int rid = db_column_int(&qFiles, 1);
    const char *zPerm = db_column_text(&qFiles, 2);
    const char *zUuid;
    zUuid = db_text("", "SELECT uuid FROM blob WHERE rid=%d", rid);
    blob_appendf(&manifest, "F %F %s %s\n", zFile+nFilter, zUuid, zPerm);
    fossil_free(zUuid);
  }


  if( gsvn.parent>=0 ){
    const char *zParentUuid;
    db_bind_int(&qParent, ":rev", gsvn.parent);
    db_step(&qParent);
    zParentUuid = db_column_text(&qParent, 0);
    blob_appendf(&manifest, "P %s\n", zParentUuid);
    if( !gsvn.flatFlag ){
      zParentBranch = db_column_text(&qParent, 1);
      if( strcmp(gsvn.zBranch, zParentBranch)!=0 ){
        blob_appendf(&manifest, "T *branch * %s\n", gsvn.zBranch);
        blob_appendf(&manifest, "T *sym-%s *\n", gsvn.zBranch);
        zParentBranch = mprintf("%s", zParentBranch);
      }else{
        zParentBranch = 0;
      }
    }

  }else{
    blob_appendf(&manifest, "T *branch * trunk\n");
    blob_appendf(&manifest, "T *sym-trunk *\n");
  }
  db_bind_int(&qTags, ":rev", gsvn.rev);
  while( db_step(&qTags)==SQLITE_ROW ){
    const char *zTag = db_column_text(&qTags, 0);
    blob_appendf(&manifest, "T +sym-%s *\n", zTag);
  }

  blob_appendf(&manifest, "T +sym-svn-rev-%d *\n", gsvn.rev);
  if( zParentBranch ) {
    blob_appendf(&manifest, "T -sym-%s *\n", zParentBranch);

  }
  if( gsvn.zUser ){
    blob_appendf(&manifest, "U %F\n", gsvn.zUser);
  }else{
    const char *zUserOvrd = find_option("user-override",0,1);

    blob_appendf(&manifest, "U %F\n", zUserOvrd ? zUserOvrd : login_name());
  }
  md5sum_blob(&manifest, &mcksum);
  blob_appendf(&manifest, "Z %b\n", &mcksum);
  blob_reset(&mcksum);

  rid = content_put(&manifest);
  blob_reset(&manifest);
  db_bind_int(&insRev, ":rev", gsvn.rev);
  db_bind_text(&insRev, ":branch", gsvn.zBranch);
  db_bind_int(&insRev, ":rid", rid);

  db_step(&insRev);







}

static u64 svn_get_varint(const char **pz){
  unsigned int v = 0;
  do{
    v = (v<<7) | ((*pz)[0]&0x7f);
  }while( (*pz)++[0]&0x80 );
................................................................................
  }
}

/*
** Read the svn-dump format from pIn and insert the corresponding
** content into the database.
*/
static void svn_dump_import(FILE *pIn){
  SvnRecord rec;
  int ver;
  const char *zTemp;
  const char *zUuid;

  Stmt addHist;

  Stmt insTag;
  Stmt cpyPath;




  /* version */
  if( svn_read_rec(pIn, &rec)
   && (zTemp = svn_find_header(rec, "SVN-fs-dump-format-version")) ){
    ver = atoi(zTemp);
    if( ver!=2 && ver!=3 ){
      fossil_fatal("Unknown svn-dump format version: %d", ver);
................................................................................
  svn_free_rec(&rec);
  /* UUID */
  if( !svn_read_rec(pIn, &rec) || !(zUuid = svn_find_header(rec, "UUID")) ){
    fossil_fatal("Missing UUID!");
  }
  svn_free_rec(&rec);
  /* content */
  db_prepare(&addHist,
    "INSERT INTO xhist (trev, tpath, trid, tperm) "
    "VALUES(:rev, :path, :rid, :perm)"
  );
  db_prepare(&insTag, "INSERT INTO xtags (trev, ttag) VALUES(:rev, :tag)");
  db_prepare(&cpyPath,
    "WITH xsrc AS (SELECT * FROM ("
    "  SELECT tpath, trid, tperm, max(trev) trev FROM xhist"
    "  WHERE trev<=:srcrev GROUP BY tpath"
    " ) WHERE trid NOTNULL)"
    "INSERT INTO xhist (trev, tpath, trid, tperm)"
    " SELECT :rev, :path||substr(tpath, length(:srcpath)+1), trid, tperm"
    " FROM xsrc WHERE tpath>:srcpath||'/' AND tpath<:srcpath||0"
  );
  gsvn.rev = -1;
  while( svn_read_rec(pIn, &rec) ){
    if( (zTemp = svn_find_header(rec, "Revision-number")) ){ /* revision node */
      /* finish previous revision */
      if( gsvn.rev>=0 ){
        svn_create_manifest();
        fossil_free(gsvn.zUser);
        fossil_free(gsvn.zComment);
        fossil_free(gsvn.zDate);
        fossil_free(gsvn.zBranch);
      }
      /* start new revision */
      gsvn.rev = atoi(zTemp);
      gsvn.zUser = mprintf("%s", svn_find_prop(rec, "svn:author"));
      gsvn.zComment = mprintf("%s", svn_find_prop(rec, "svn:log"));
      gsvn.zDate = date_in_standard_format(svn_find_prop(rec, "svn:date"));
      gsvn.parent = -1;
      gsvn.zBranch = 0;
      fossil_print("\rImporting SVN revision: %d", gsvn.rev);
      db_bind_int(&addHist, ":rev", gsvn.rev);
      db_bind_int(&cpyPath, ":rev", gsvn.rev);
    }else
    if( (zTemp = svn_find_header(rec, "Node-path")) ){ /* file/dir node */















      const char *zPath = zTemp;
      const char *zAction = svn_find_header(rec, "Node-action");
      const char *zKind = svn_find_header(rec, "Node-kind");
      const char *zSrcPath = svn_find_header(rec, "Node-copyfrom-path");
      const char *zPerm = svn_find_prop(rec, "svn:executable") ? "x" : 0;
      int deltaFlag = 0;
      int srcRev = 0;

      if( (zTemp = svn_find_header(rec, "Text-delta")) ){
        deltaFlag = strncmp(zTemp, "true", 4)==0;
      }
      if( zSrcPath ){
        zTemp = svn_find_header(rec, "Node-copyfrom-rev");
        if( zTemp ){
          srcRev = atoi(zTemp);
        }else{
          fossil_fatal("Missing copyfrom-rev");
        }
      }
      if( !gsvn.flatFlag ){
        if( strncmp(zPath, "branches/", 9)==0 ){
          int nBranch;
          zTemp = zPath+9;
          while( *zTemp && *zTemp!='/' ){ zTemp++; }
          nBranch = zTemp-zPath+9;

          gsvn.zBranch = fossil_malloc(nBranch+1);
          memcpy(gsvn.zBranch, zPath+9, nBranch);
          gsvn.zBranch[nBranch] = '\0';
        }else
        if( strncmp(zPath, "trunk/", 6)==0 ){

          gsvn.zBranch = mprintf("trunk");
        }else
        if( strncmp(zPath, "tags/", 5)!=0
         && strcmp(zPath, "branches")!=0
         && strcmp(zPath, "trunk")!=0
         && strcmp(zPath, "tags")!=0){
          fossil_fatal("Write outside repository layout: %s", zPath);
        }
      }
      if( strncmp(zAction, "delete", 6)==0
       || strncmp(zAction, "replace", 7)==0 )
      {
        db_bind_null(&addHist, ":rid");
        db_bind_text(&addHist, ":path", zPath);
        db_bind_null(&addHist, ":perm");
        db_step(&addHist);
        db_reset(&addHist);
      } /* no 'else' here since 'replace' does both a 'delete' and an 'add' */
      if( strncmp(zAction, "add", 3)==0
       || strncmp(zAction, "replace", 7)==0 )
      {
        if( zKind==0 ){
          fossil_fatal("Missing Node-kind");
        }else if( strncmp(zKind, "dir", 3)==0 ){
          if( zSrcPath ){
            db_bind_int(&cpyPath, ":srcrev", srcRev);
            db_bind_text(&cpyPath, ":path", zPath);
            db_bind_text(&cpyPath, ":srcpath", zSrcPath);
            db_step(&cpyPath);
            db_reset(&cpyPath);


            if( !gsvn.flatFlag ){
              if( strncmp(zPath, "branches/", 9)==0 ){
                zTemp = zPath+9+strlen(gsvn.zBranch);

                if( *zTemp==0 ){




                  gsvn.parent = srcRev;
                }
              }else if( strncmp(zPath, "tags/", 5)==0 ){
                zTemp = zPath+5;
                db_bind_int(&insTag, ":rev", srcRev);
                db_bind_text(&insTag, ":tag", zTemp);
                db_step(&insTag);
                db_reset(&insTag);
              }
            }
          }
        }else{
          int rid = 0;
          if( zSrcPath ){
            rid = db_int(0, "SELECT trid, max(trev) FROM xhist"

                            " WHERE trev<=%d AND tpath=%Q", srcRev, zSrcPath);
            if( rid==0 ){
              fossil_fatal("Reference to non-existent path/revision");
            }
          }
          if( deltaFlag ){
            Blob deltaSrc;
            Blob target;
            if( rid!=0 ){
              content_get(rid, &deltaSrc);
            }else{
................................................................................
              blob_zero(&deltaSrc);
            }
            svn_apply_svndiff(&rec.content, &deltaSrc, &target);
            rid = content_put(&target);
          }else if( rec.contentFlag ){
            rid = content_put(&rec.content);
          }

          db_bind_int(&addHist, ":rid", rid);
          db_bind_text(&addHist, ":path", zPath);
          db_bind_text(&addHist, ":perm", zPerm);
          db_step(&addHist);
          db_reset(&addHist);
        }
      }else
      if( strncmp(zAction, "change", 6)==0 ){
        int rid = 0;
        if( zKind==0 ){
          fossil_fatal("Missing Node-kind");
        }
        if( strncmp(zKind, "dir", 3)==0 ) continue;
        if( deltaFlag ){
          Blob deltaSrc;
          Blob target;
          rid = db_int(0, "SELECT trid, max(trev) FROM xhist"
                          " WHERE trev<=%d AND tpath=%Q", gsvn.rev-1, zPath);
          content_get(rid, &deltaSrc);
          svn_apply_svndiff(&rec.content, &deltaSrc, &target);
          rid = content_put(&target);
        }else{
          rid = content_put(&rec.content);
        }

        db_bind_int(&addHist, ":rid", rid);
        db_bind_text(&addHist, ":path", zPath);
        db_bind_text(&addHist, ":perm", zPerm);
        db_step(&addHist);
        db_reset(&addHist);
      }else
      if( strncmp(zAction, "delete", 6)!=0 ){ /* already did this above */

        fossil_fatal("Unknown Node-action");
      }
    }else{
      fossil_fatal("Unknown record type");
    }
    svn_free_rec(&rec);
  }
  if( gsvn.rev>0 ){
    svn_create_manifest();
  }
  fossil_free(gsvn.zUser);
  fossil_free(gsvn.zComment);
  fossil_free(gsvn.zDate);
  db_finalize(&addHist);
  db_finalize(&insTag);
  db_finalize(&cpyPath);
  fossil_print(" Done!\n");




}

/*
** COMMAND: import
**
** Usage: %fossil import FORMAT ?OPTIONS? NEW-REPOSITORY ?INPUT-FILE?
**
................................................................................
*/
void import_cmd(void){
  char *zPassword;
  FILE *pIn;
  Stmt q;
  int forceFlag = find_option("force", "f", 0)!=0;
  int incrFlag = find_option("incremental", "i", 0)!=0;
  gsvn.flatFlag = find_option("flat", 0, 0)!=0;

  verify_all_options();
  if( g.argc!=4  && g.argc!=5 ){
    usage("FORMAT REPOSITORY-NAME");
  }
  if( g.argc==5 ){
    pIn = fossil_fopen(g.argv[4], "rb");
................................................................................
      import_reset(0);
    }
    db_finalize(&q);
  }else
  if( strncmp(g.argv[2], "svn", 3)==0 ){
    db_multi_exec(
       "CREATE TEMP TABLE xrevisions("

       " trev INTEGER PRIMARY KEY, tbranch TEXT, tuuid TEXT"
       ");"
       "CREATE TEMP TABLE xhist("
       " trev INT, tpath TEXT NOT NULL, trid TEXT, tperm TEXT,"
       " UNIQUE (trev, tpath) ON CONFLICT REPLACE"
       ");"
       "CREATE TEMP TABLE xfiles("
       " tpath TEXT NOT NULL, trid TEXT, tperm TEXT,"
       " UNIQUE (tpath) ON CONFLICT REPLACE"
       ");"
       "CREATE TEMP TRIGGER xfilesdeltrig AFTER INSERT ON xhist FOR EACH ROW"
       " WHEN new.trid ISNULL"
       " BEGIN DELETE FROM xfiles WHERE xfiles.tpath=new.tpath; END;"
       "CREATE TEMP TRIGGER xfilesaddtrig AFTER INSERT ON xhist FOR EACH ROW"
       " WHEN new.trid NOTNULL BEGIN INSERT INTO xfiles(tpath,trid,tperm)"
       " VALUES(new.tpath, new.trid, new.tperm); END;"
       "CREATE TEMP TABLE xtags("
       " trev INT, ttag TEXT"
       ");"
    );
    svn_dump_import(pIn);
  }

  verify_cancel();
  db_end_transaction(0);
  db_begin_transaction();
  fossil_print("Rebuilding repository meta-data...\n");
  rebuild_db(0, 1, !incrFlag);
  verify_cancel();
  db_end_transaction(0);
  fossil_print("Vacuuming..."); fflush(stdout);