Fossil

Check-in [2ea79975]
Login

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

Overview
Comment:More work. Still not usable
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | svn-import
Files: files | file ages | folders
SHA1:2ea79975b60e7af92ccb2262edce51fb66efd6a5
User & Date: baruch 2014-12-31 23:33:47
Context
2015-01-01
15:02
Another unfinished revision check-in: 65f9525b user: baruch tags: svn-import
2014-12-31
23:33
More work. Still not usable check-in: 2ea79975 user: baruch tags: svn-import
15:46
Working on fixing/rewriting svn-import function. This is not in a stable state yet. DO NOT TEST check-in: 9604c282 user: baruch tags: svn-import
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/import.c.

716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
...
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
....
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

1046



1047
1048


1049
1050
1051
1052
1053
1054
1055
....
1214
1215
1216
1217
1218
1219
1220
1221





1222
1223

1224






1225
1226
1227
1228
1229
1230
1231
....
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
....
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
....
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
....
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
....
1395
1396
1397
1398
1399
1400
1401
1402

1403

1404
1405
1406
1407
1408
1409
1410
1411
....
1412
1413
1414
1415
1416
1417
1418
1419
1420

1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437


1438
1439
1440
1441
1442
1443
1444
1445

1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468

1469
1470
1471
1472
1473
1474
1475
1476
....
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
....
1572
1573
1574
1575
1576
1577
1578
1579

1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597

1598
1599
1600
1601
1602
1603
1604
malformed_line:
  trim_newline(zLine);
  fossil_fatal("bad fast-import line: [%s]", zLine);
  return;
}

static struct{
  char *zBranch;              /* Name of a branch for a commit */
  char *zDate;                /* Date/time stamp */
  char *zUser;                /* User name */
  char *zComment;             /* Comment of a commit */
  char *zFrom;                /* from value as a UUID */
  int nMerge;                 /* Number of merge values */
  int nMergeAlloc;            /* Number of slots in azMerge[] */
  char **azMerge;             /* Merge values */
  int nFile;                  /* Number of aFile values */
  int nFileAlloc;             /* Number of slots in aFile[] */
  ImportFile *aFile;          /* Information about files in a commit */
  int fromLoaded;             /* True zFrom content loaded into aFile[] */
} gsvn;

/*
** Create a new entry in the gsvn.aFile[] array
*/
static ImportFile *svn_import_add_file(){
  ImportFile *pFile;
  if( gsvn.nFile>=gsvn.nFileAlloc ){
    gsvn.nFileAlloc = gsvn.nFileAlloc*2 + 100;
    gsvn.aFile = fossil_realloc(gsvn.aFile, gsvn.nFileAlloc*sizeof(gsvn.aFile[0]));
  }
  pFile = &gsvn.aFile[gsvn.nFile++];
  memset(pFile, 0, sizeof(*pFile));
  return pFile;
}

/*
** Load all file information out of the gsvn.zFrom check-in
*/
static void svn_import_prior_files(void){
  Manifest *p;
  int rid;
  ManifestFile *pOld;
  ImportFile *pNew;
  if( gsvn.fromLoaded ) return;
  gsvn.fromLoaded = 1;
  if( gsvn.zFrom==0 && gsvn.zPrevCheckin!=0
   && fossil_strcmp(gsvn.zBranch, gsvn.zPrevBranch)==0
  ){
     gsvn.zFrom = gsvn.zPrevCheckin;
     gsvn.zPrevCheckin = 0;
  }
  if( gsvn.zFrom==0 ) return;
  rid = fast_uuid_to_rid(gsvn.zFrom);
  if( rid==0 ) return;
  p = manifest_get(rid, CFTYPE_MANIFEST, 0);
  if( p==0 ) return;
  manifest_file_rewind(p);
  while( (pOld = manifest_file_next(p, 0))!=0 ){
    pNew = import_add_file();
    pNew->zName = fossil_strdup(pOld->zName);
    pNew->isExe = pOld->zPerm && strstr(pOld->zPerm, "x")!=0;
    pNew->isLink = pOld->zPerm && strstr(pOld->zPerm, "l")!=0;
    pNew->zUuid = fossil_strdup(pOld->zUuid);
    pNew->isFrom = 1;
  }
  manifest_destroy(p);
}

/*
** Deallocate the state information.
**
** The azMerge[] and aFile[] arrays are zeroed but allocated space is
** retained unless the freeAll flag is set.
*/
static void svn_import_reset(int freeAll){
  int i;
//  gsvn.xFinish = 0;
//  fossil_free(gsvn.zTag); gsvn.zTag = 0;
  fossil_free(gsvn.zBranch); gsvn.zBranch = 0;
//  fossil_free(gsvn.aData); gsvn.aData = 0;
//  fossil_free(gsvn.zMark); gsvn.zMark = 0;
  fossil_free(gsvn.zDate); gsvn.zDate = 0;
  fossil_free(gsvn.zUser); gsvn.zUser = 0;
  fossil_free(gsvn.zComment); gsvn.zComment = 0;
  fossil_free(gsvn.zFrom); gsvn.zFrom = 0;
//  fossil_free(gsvn.zFromMark); gsvn.zFromMark = 0;
  for(i=0; i<gsvn.nMerge; i++){
    fossil_free(gsvn.azMerge[i]); gsvn.azMerge[i] = 0;
  }
  gsvn.nMerge = 0;
  for(i=0; i<gsvn.nFile; i++){
    fossil_free(gsvn.aFile[i].zName);
    fossil_free(gsvn.aFile[i].zUuid);
    fossil_free(gsvn.aFile[i].zPrior);
  }
  memset(gsvn.aFile, 0, gsvn.nFile*sizeof(gsvn.aFile[0]));
  gsvn.nFile = 0;
  if( freeAll ){
//    fossil_free(gsvn.zPrevBranch);
//    fossil_free(gsvn.zPrevCheckin);
    fossil_free(gsvn.azMerge);
    fossil_free(gsvn.aFile);
    memset(&gsvn, 0, sizeof(gsvn));
  }
//  gsvn.xFinish = finish_noop;
}

static struct{
  int rev;                    /* SVN revision number */
//  int parentRev;              /* SVN revision number of parent check-in */
//  char *zParentBranch;        /* Name of branch 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 */
  const char *zTrunk;         /* Name of trunk folder in repo root */
  int lenTrunk;               /* String length of zTrunk */
  const char *zBranches;      /* Name of branches folder in repo root */
  int lenBranches;            /* String length of zBranches */
  const char *zTags;          /* Name of tags folder in repo root */
  int lenTags;                /* String length of zTags */
} gsvn;
typedef struct {
  char *zKey;
  const char *zVal;
} KeyVal;
typedef struct {
  KeyVal *aHeaders;
  int nHeaders;
  char *pRawProps;
  KeyVal *aProps;
  int nProps;
................................................................................
  int contentFlag;
} SvnRecord;

#define svn_find_header(rec, zHeader) \
  svn_find_keyval((rec).aHeaders, (rec).nHeaders, (zHeader))
#define svn_find_prop(rec, zProp) \
  svn_find_keyval((rec).aProps, (rec).nProps, (zProp))
static const char *svn_find_keyval(
  KeyVal *aKeyVal,
  int nKeyVal,
  const char *zKey
){
  int i;
  for(i=0; i<nKeyVal; i++){
    if( fossil_strcmp(aKeyVal[i].zKey, zKey)==0 ){
................................................................................
    }
  }else{
    rec->contentFlag = 0;
  }
  return 1;
}

static void svn_finish_revision(
){
  Blob manifest;
  static Stmt insRev;
  static Stmt qParent;
  static Stmt qParent2;
  static Stmt qFiles;
  static Stmt qTags;
  int nBaseFilter;
  int nFilter;
  int rid;
  char *zParentBranch = 0;
  Blob mcksum;

  blob_zero(&manifest);
  nBaseFilter = blob_size(&gsvn.filter);
  if( !gsvn.flatFlag ){
    if( gsvn.zBranch==0 ){ return; }
    if( strncmp(gsvn.zBranch, gsvn.zTrunk, gsvn.lenTrunk-1)==0 ){
      blob_appendf(&gsvn.filter, "%s*", gsvn.zTrunk);
    }else{
      blob_appendf(&gsvn.filter, "%s%s/*", gsvn.zBranches, gsvn.zBranch);
    }
  }else{
    blob_append(&gsvn.filter, "*", 1);
  }
  if( db_int(0, "SELECT 1 FROM xhist WHERE trev=%d AND tpath GLOB %Q LIMIT 1",
             gsvn.rev, blob_str(&gsvn.filter))==0
  ){
    goto skip_revision;
  }
  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, max(trev) FROM xrevisions "
                              "WHERE trev<=:rev AND tbranch=:branch");
  db_static_prepare(&qParent2, "SELECT tuuid, min(trev) "
                               "FROM xrevisions WHERE tbranch=:branch");

  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( !gsvn.flatFlag ){
    if( gsvn.parentRev<0 ){
      gsvn.parentRev = db_int(-1, "SELECT ifnull(max(trev),-1) FROM xrevisions "
                                  "WHERE tbranch=%Q", gsvn.zBranch);
      if( gsvn.parentRev<0 ){
        gsvn.parentRev =
            db_int(-1, "SELECT ifnull(max(trev),-1) FROM xrevisions");
................................................................................
/*
** Extract the name of the branch or tag that the given path is on.
** Returns: 1 - It is on the trunk
**          2 - It is on a branch
**          3 - It is a tag
**          0 - It is none of the above
 */
static int *svn_parse_path(char *zPath, char **zBranch, char **zFile){





  if( strncmp(zPath, gsvn.zTrunk, gsvn.lenTrunk)==0 ){
    *zBranch = "trunk";

    *zFile = zPath+gsvn.lenTrunk;






    return 1;
  }else
  if( strncmp(zPath, gsvn.zBranches, gsvn.lenBranches)==0 ){
    *zFile = *zBranch = zPath+gsvn.lenBranches;
    while( **zFile && **zFile!='/' ){ (*zFile)++; }
    if( *zFile ){
      **zFile = '\0';
................................................................................
    fossil_fatal("Missing UUID!");
  }
  svn_free_rec(&rec);

  /* content */
  db_prepare(&addFile,
    "INSERT INTO xfiles (tpath, tbranch, tuuid, tperm)"
    " VALUES(:path, :branch, :uuid, :perm)"
  );
  db_prepare(&delPath,
    "DELETE FROM xfiles"
    " WHERE (tpath=:path OR (tpath>:path||'/' AND tpath<:path||'0'))"
    "     AND tbranch=:branch"
  );
  db_prepare(&addSrc,
................................................................................
    " FROM xfoci"
    " WHERE chekinID=:rid AND filename>:srcpath||'/' AND filename<:srcpath||'0'"
  );
  gsvn.rev = -1;
  while( svn_read_rec(pIn, &rec) ){
    if( (zTemp = svn_find_header(rec, "Revision-number")) ){ /* revision node */
      /* finish previous revision */
      const char *zDate = NULL;
      if( gsvn.rev>=0 ){
        svn_finish_revision();
        fossil_free(gsvn.zUser);
        fossil_free(gsvn.zComment);
        fossil_free(gsvn.zDate);
      }
      /* start new revision */
................................................................................
      if( zDate ){
        zDate = date_in_standard_format(zDate);
      }
      gsvn.zDate = zDate;
      fossil_print("\rImporting SVN revision: %d", gsvn.rev);
    }else
    if( (zTemp = svn_find_header(rec, "Node-path")) ){ /* file/dir node */
      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;
      char *zBranch;
      char *zFile;
      char *zSrcBranch;
      char *zSrcFile;
      int deltaFlag = 0;
      int srcRev = 0;
      int branchType = svn_parse_path(zTemp, &zBranch, &zFile);
................................................................................
      if( zSrcPath ){
        zTemp = svn_find_header(rec, "Node-copyfrom-rev");
        if( zTemp ){
          srcRev = atoi(zTemp);
        }else{
          fossil_fatal("Missing copyfrom-rev");
        }
        if( svn_extract_branch(zSrcPath, &zSrcBranch, &zSrcFile)==0 ){
          fossil_fatal("Copy from path outside the import paths");
        }
        db_bind_text(&addSrc, ":path", zFile);
        db_bind_text(&addSrc, ":branch", zBranch);
        db_bind_text(&addSrc, ":srcpath", zSrcFile);
        db_bind_text(&addSrc, ":srcbranch", zSrcBranch);
        db_bind_int(&addSrc, ":srcrev", srcRev);
................................................................................
          }
        }else{
          int rid = 0;
          if( zSrcPath ){
            int srcRid = db_int(0, "SELECT trid, max(trev) FROM xrevisions"
                            " WHERE trev<=%d AND tbranch=%Q",
                         srcRev, zSrcBranch);
            rid = db_int(0, "SELECT rid FROM xfoci"

                            " WHERE chekinID=%d AND filename=%Q",

                         srcRid, zSrcFile)
          }
          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);
          bHasFiles = 1;
        }
      }else
      if( strncmp(zAction, "change", 6)==0 ){
        int rid = 0;
        if( zKind==0 ){
          fossil_fatal("Missing Node-kind");
        }
        if( strncmp(zKind, "dir", 3)!=0 ){
          if( deltaFlag ){
            Blob deltaSrc;
            Blob target;
            rid = db_int(0, "SELECT trid, max(trev) FROM xhist"
                            " WHERE trev<=%d AND tpath=%Q", gsvn.rev, 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);
          bHasFiles = 1;
        }
      }else
      if( strncmp(zAction, "delete", 6)!=0 ){ /* already did this one above */
        fossil_fatal("Unknown Node-action");
      }
    }else{
      fossil_fatal("Unknown record type");
    }
    svn_free_rec(&rec);
  }
  if( bHasFiles ){
    svn_create_manifest();
  }
  fossil_free(gsvn.zUser);
  fossil_free(gsvn.zComment);
  fossil_free(gsvn.zDate);
  db_finalize(&addHist);
  db_finalize(&insTag);
  db_finalize(&cpyPath);

  db_finalize(&delPath);
  fossil_print(" Done!\n");
}

/*
** COMMAND: import
**
** Usage: %fossil import FORMAT ?OPTIONS? NEW-REPOSITORY ?INPUT-FILE?
................................................................................
** See also: export
*/
void import_cmd(void){
  char *zPassword;
  FILE *pIn;
  Stmt q;
  const char *zBase = find_option("base", 0, 1);
  int lenFilter;
  int forceFlag = find_option("force", "f", 0)!=0;
  int incrFlag = find_option("incremental", "i", 0)!=0;
  gsvn.zTrunk = find_option("trunk", 0, 1);
  gsvn.zBranches = find_option("branches", 0, 1);
  gsvn.zTags = find_option("tags", 0, 1);
  int flatFlag = find_option("flat", 0, 0)!=0;

................................................................................
      import_reset(0);
    }
    db_finalize(&q);
  }else
  if( strncmp(g.argv[2], "svn", 3)==0 ){
    db_multi_exec(
       "CREATE TEMP TABLE xrevisions("
       " trev INTEGER, tbranch TEXT, trid INT, PRIMARY KEY(tbranch, trev)"

       ");"
       "CREATE TEMP TABLE xfiles("
       " tpath TEXT, tbranch TEXT, tuuid TEXT, tperm TEXT,"
       " UNIQUE (tbranch, tpath) ON CONFLICT REPLACE"
       ");"
       "CREATE TEMP TABLE xsrc("
       " tpath TEXT, tbranch TEXT, tsrc TEXT, tsrcbranch TEXT, tsrcrev INT"
       ");"
       "CREATE TEMP TABLE xchanged("
       " tbranch TEXT, ttype INT,"
       " UNIQUE (tbranch) ON CONFLICT REPLACE"
       ");"
       "CREATE VIRTUAL TABLE temp.xfoci USING files_of_checkin;"
    );
    if( zBase==0 ){ zBase = ""; }
    if( strlen(zBase)>0 ){
      if( zBase[strlen(zBase)-1]!='/' ){
      zBase = mprintf("%s/", zBase);

    }
    if( flatFlag ){
      gsvn.zTrunk = zBase;
      gsvn.zBranches = 0;
      gsvn.zTags = 0;
      gsvn.lenTrunk = strlen(zBase);
      gsvn.lenBranches = 0;







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<



<









|







 







|







 







|
<


<
<
<
<
<
<
<
<



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







 







|
>
>
>
>
>
|

>
|
>
>
>
>
>
>







 







|







 







|







 







|
|
|
|







 







|







 







|
>
|
>
|







 







|
|
>
|
|
|
<











|
|
>
>






|
|
>
|
|
|
<










|
<
<



|
|
|
>
|







 







<







 







|
>








<
<
<
<





|
>







716
717
718
719
720
721
722





































































































723



724
725
726

727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
...
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
...
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
....
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
....
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
....
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
....
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
....
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
....
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
1302
1303
1304
1305
1306

1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333

1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344


1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
....
1388
1389
1390
1391
1392
1393
1394

1395
1396
1397
1398
1399
1400
1401
....
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470




1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
malformed_line:
  trim_newline(zLine);
  fossil_fatal("bad fast-import line: [%s]", zLine);
  return;
}

static struct{





































































































  int rev;                    /* SVN revision number */



  char *zDate;                /* Date/time stamp */
  char *zUser;                /* User name */
  char *zComment;             /* Comment of a commit */

  const char *zTrunk;         /* Name of trunk folder in repo root */
  int lenTrunk;               /* String length of zTrunk */
  const char *zBranches;      /* Name of branches folder in repo root */
  int lenBranches;            /* String length of zBranches */
  const char *zTags;          /* Name of tags folder in repo root */
  int lenTags;                /* String length of zTags */
} gsvn;
typedef struct {
  char *zKey;
  char *zVal;
} KeyVal;
typedef struct {
  KeyVal *aHeaders;
  int nHeaders;
  char *pRawProps;
  KeyVal *aProps;
  int nProps;
................................................................................
  int contentFlag;
} SvnRecord;

#define svn_find_header(rec, zHeader) \
  svn_find_keyval((rec).aHeaders, (rec).nHeaders, (zHeader))
#define svn_find_prop(rec, zProp) \
  svn_find_keyval((rec).aProps, (rec).nProps, (zProp))
static char *svn_find_keyval(
  KeyVal *aKeyVal,
  int nKeyVal,
  const char *zKey
){
  int i;
  for(i=0; i<nKeyVal; i++){
    if( fossil_strcmp(aKeyVal[i].zKey, zKey)==0 ){
................................................................................
    }
  }else{
    rec->contentFlag = 0;
  }
  return 1;
}

static void svn_finish_revision(){

  Blob manifest;
  static Stmt insRev;








  Blob mcksum;

  blob_zero(&manifest);























  db_static_prepare(&getChanges, "SELECT tbranch, ttype FROM xchanged");
  db_static_prepare(&getFiles, "SELECT tpath, tuuid, tperm,"
                               " tsrc, tsrcbranch, trev tsrcrev"
                               " FROM xfiles, xsrc"
                               " ON tpath, tbranch)"
                               " WHERE tbranch=:branch ORDER BY tpath");

  db_static_prepare(&setRid, "REPLACE INTO xrevisions(trev, tbranch, trid)"
                             " VALUES (%d, :branch, :rid)", gsvn.rev);
  if( !gsvn.flatFlag ){
    if( gsvn.parentRev<0 ){
      gsvn.parentRev = db_int(-1, "SELECT ifnull(max(trev),-1) FROM xrevisions "
                                  "WHERE tbranch=%Q", gsvn.zBranch);
      if( gsvn.parentRev<0 ){
        gsvn.parentRev =
            db_int(-1, "SELECT ifnull(max(trev),-1) FROM xrevisions");
................................................................................
/*
** Extract the name of the branch or tag that the given path is on.
** Returns: 1 - It is on the trunk
**          2 - It is on a branch
**          3 - It is a tag
**          0 - It is none of the above
 */
static int svn_parse_path(char *zPath, char **zBranch, char **zFile){
  if( gsvn.lenTrunk==0 ){
    *zBranch = "trunk";
    *zFile = zPath;
    return 1;
  }else
  if( strncmp(zPath, gsvn.zTrunk, gsvn.lenTrunk-1)==0 ){
    *zBranch = "trunk";
    if( zPath[gsvn.lenTrunk-1]=='/' ){
      *zFile = zPath+gsvn.lenTrunk;;
    }else if( zPath[gsvn.lenTrunk-1]==0 ){
      *zFile = 0;
    }else{
      *zFile = *zBranch = 0;
      return 0;
    }
    return 1;
  }else
  if( strncmp(zPath, gsvn.zBranches, gsvn.lenBranches)==0 ){
    *zFile = *zBranch = zPath+gsvn.lenBranches;
    while( **zFile && **zFile!='/' ){ (*zFile)++; }
    if( *zFile ){
      **zFile = '\0';
................................................................................
    fossil_fatal("Missing UUID!");
  }
  svn_free_rec(&rec);

  /* content */
  db_prepare(&addFile,
    "INSERT INTO xfiles (tpath, tbranch, tuuid, tperm)"
    " VALUES(:path, :branch, (SELECT uuid FROM blob WHERE rid=:rid), :perm)"
  );
  db_prepare(&delPath,
    "DELETE FROM xfiles"
    " WHERE (tpath=:path OR (tpath>:path||'/' AND tpath<:path||'0'))"
    "     AND tbranch=:branch"
  );
  db_prepare(&addSrc,
................................................................................
    " FROM xfoci"
    " WHERE chekinID=:rid AND filename>:srcpath||'/' AND filename<:srcpath||'0'"
  );
  gsvn.rev = -1;
  while( svn_read_rec(pIn, &rec) ){
    if( (zTemp = svn_find_header(rec, "Revision-number")) ){ /* revision node */
      /* finish previous revision */
      char *zDate = NULL;
      if( gsvn.rev>=0 ){
        svn_finish_revision();
        fossil_free(gsvn.zUser);
        fossil_free(gsvn.zComment);
        fossil_free(gsvn.zDate);
      }
      /* start new revision */
................................................................................
      if( zDate ){
        zDate = date_in_standard_format(zDate);
      }
      gsvn.zDate = zDate;
      fossil_print("\rImporting SVN revision: %d", gsvn.rev);
    }else
    if( (zTemp = svn_find_header(rec, "Node-path")) ){ /* file/dir node */
      char *zAction = svn_find_header(rec, "Node-action");
      char *zKind = svn_find_header(rec, "Node-kind");
      char *zSrcPath = svn_find_header(rec, "Node-copyfrom-path");
      char *zPerm = svn_find_prop(rec, "svn:executable") ? "x" : 0;
      char *zBranch;
      char *zFile;
      char *zSrcBranch;
      char *zSrcFile;
      int deltaFlag = 0;
      int srcRev = 0;
      int branchType = svn_parse_path(zTemp, &zBranch, &zFile);
................................................................................
      if( zSrcPath ){
        zTemp = svn_find_header(rec, "Node-copyfrom-rev");
        if( zTemp ){
          srcRev = atoi(zTemp);
        }else{
          fossil_fatal("Missing copyfrom-rev");
        }
        if( svn_parse_path(zSrcPath, &zSrcBranch, &zSrcFile)==0 ){
          fossil_fatal("Copy from path outside the import paths");
        }
        db_bind_text(&addSrc, ":path", zFile);
        db_bind_text(&addSrc, ":branch", zBranch);
        db_bind_text(&addSrc, ":srcpath", zSrcFile);
        db_bind_text(&addSrc, ":srcbranch", zSrcBranch);
        db_bind_int(&addSrc, ":srcrev", srcRev);
................................................................................
          }
        }else{
          int rid = 0;
          if( zSrcPath ){
            int srcRid = db_int(0, "SELECT trid, max(trev) FROM xrevisions"
                            " WHERE trev<=%d AND tbranch=%Q",
                         srcRev, zSrcBranch);
            rid = db_int(0, "SELECT rid FROM blob WHERE uuid=("
                            " SELECT uuid FROM xfoci"
                            "  WHERE chekinID=%d AND filename=%Q"
                            ")",
                         srcRid, zSrcFile);
          }
          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_text(&addFile, ":path", zFile);
          db_bind_text(&addFile, ":branch", zBranch);
          db_bind_int(&addFile, ":rid", rid);
          db_bind_text(&addFile, ":perm", zPerm);
          db_step(&addFile);
          db_reset(&addFile);

        }
      }else
      if( strncmp(zAction, "change", 6)==0 ){
        int rid = 0;
        if( zKind==0 ){
          fossil_fatal("Missing Node-kind");
        }
        if( strncmp(zKind, "dir", 3)!=0 ){
          if( deltaFlag ){
            Blob deltaSrc;
            Blob target;
            rid = db_int(0, "SELECT rid FROM blob WHERE uuid=("
                            " SELECT uuid FROM xfiles"
                            "  WHERE tpath=%Q AND tbranch=%Q"
                            ")", zFile, zBranch);
            content_get(rid, &deltaSrc);
            svn_apply_svndiff(&rec.content, &deltaSrc, &target);
            rid = content_put(&target);
          }else{
            rid = content_put(&rec.content);
          }
          db_bind_text(&addFile, ":path", zFile);
          db_bind_text(&addFile, ":branch", zBranch);
          db_bind_int(&addFile, ":rid", rid);
          db_bind_text(&addFile, ":perm", zPerm);
          db_step(&addFile);
          db_reset(&addFile);

        }
      }else
      if( strncmp(zAction, "delete", 6)!=0 ){ /* already did this one above */
        fossil_fatal("Unknown Node-action");
      }
    }else{
      fossil_fatal("Unknown record type");
    }
    svn_free_rec(&rec);
  }
  svn_finish_revision();


  fossil_free(gsvn.zUser);
  fossil_free(gsvn.zComment);
  fossil_free(gsvn.zDate);
  db_finalize(&addFile);
  db_finalize(&delPath);
  db_finalize(&addSrc);
  db_finalize(&addBranch);
  db_finalize(&cpyPath);
  fossil_print(" Done!\n");
}

/*
** COMMAND: import
**
** Usage: %fossil import FORMAT ?OPTIONS? NEW-REPOSITORY ?INPUT-FILE?
................................................................................
** See also: export
*/
void import_cmd(void){
  char *zPassword;
  FILE *pIn;
  Stmt q;
  const char *zBase = find_option("base", 0, 1);

  int forceFlag = find_option("force", "f", 0)!=0;
  int incrFlag = find_option("incremental", "i", 0)!=0;
  gsvn.zTrunk = find_option("trunk", 0, 1);
  gsvn.zBranches = find_option("branches", 0, 1);
  gsvn.zTags = find_option("tags", 0, 1);
  int flatFlag = find_option("flat", 0, 0)!=0;

................................................................................
      import_reset(0);
    }
    db_finalize(&q);
  }else
  if( strncmp(g.argv[2], "svn", 3)==0 ){
    db_multi_exec(
       "CREATE TEMP TABLE xrevisions("
       " tid INTEGER PRIMARY KEY, trev INTEGER, tbranch TEXT, ttype INT, trid INT,"
       " UNIQUE(tbranch, ttype, trev)"
       ");"
       "CREATE TEMP TABLE xfiles("
       " tpath TEXT, tbranch TEXT, tuuid TEXT, tperm TEXT,"
       " UNIQUE (tbranch, tpath) ON CONFLICT REPLACE"
       ");"
       "CREATE TEMP TABLE xsrc("
       " tpath TEXT, tbranch TEXT, tsrc TEXT, tsrcbranch TEXT, tsrcrev INT"
       ");"




       "CREATE VIRTUAL TABLE temp.xfoci USING files_of_checkin;"
    );
    if( zBase==0 ){ zBase = ""; }
    if( strlen(zBase)>0 ){
      if( zBase[strlen(zBase)-1]!='/' ){
        zBase = mprintf("%s/", zBase);
      }
    }
    if( flatFlag ){
      gsvn.zTrunk = zBase;
      gsvn.zBranches = 0;
      gsvn.zTags = 0;
      gsvn.lenTrunk = strlen(zBase);
      gsvn.lenBranches = 0;