Fossil

Check-in [9604c282]
Login

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

Overview
Comment:Working on fixing/rewriting svn-import function. This is not in a stable state yet. DO NOT TEST
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | svn-import
Files: files | file ages | folders
SHA1:9604c28207d0aea31510a2c717d94c2c5e4fcf0e
User & Date: baruch 2014-12-31 15:46:06
Context
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
2014-12-24
09:00
merge trunk check-in: 2caad83d user: baruch tags: svn-import
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/foci.c.

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "config.h"
#include "foci.h"
#include <assert.h>

/*
** The schema for the virtual table:
*/
static const char zFociSchema[] = 
@ CREATE TABLE files_of_checkin(
@  checkinID    INTEGER,    -- RID for the checkin manifest
@  filename     TEXT,       -- Name of a file
@  uuid         TEXT,       -- SHA1 hash of the file
@  previousName TEXT,       -- Name of the file in previous checkin
@  prem         TEXT        -- Permissions on the file
@ );
;

#if INTERFACE
/*
** The subclasses of sqlite3_vtab  and sqlite3_vtab_cursor tables
** that implement the files_of_checkin virtual table.
................................................................................

static int fociEof(sqlite3_vtab_cursor *pCursor){
  FociCursor *pCsr = (FociCursor *)pCursor;
  return pCsr->pFile==0;
}

static int fociFilter(
  sqlite3_vtab_cursor *pCursor, 
  int idxNum, const char *idxStr,
  int argc, sqlite3_value **argv
){
  FociCursor *pCur = (FociCursor *)pCursor;
  manifest_destroy(pCur->pMan);
  if( idxNum ){
    pCur->pMan = manifest_get(sqlite3_value_int(argv[0]), CFTYPE_MANIFEST, 0);
................................................................................
    pCur->pMan = 0;
    pCur->iFile = 0;
  }
  return SQLITE_OK;
}

static int fociColumn(
  sqlite3_vtab_cursor *pCursor, 
  sqlite3_context *ctx, 
  int i
){
  FociCursor *pCsr = (FociCursor *)pCursor;
  switch( i ){
    case 0:            /* checkinID */
      sqlite3_result_int(ctx, pCsr->pMan->rid);
      break;







|





|







 







|







 







|
|







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "config.h"
#include "foci.h"
#include <assert.h>

/*
** The schema for the virtual table:
*/
static const char zFociSchema[] =
@ CREATE TABLE files_of_checkin(
@  checkinID    INTEGER,    -- RID for the checkin manifest
@  filename     TEXT,       -- Name of a file
@  uuid         TEXT,       -- SHA1 hash of the file
@  previousName TEXT,       -- Name of the file in previous checkin
@  perm         TEXT        -- Permissions on the file
@ );
;

#if INTERFACE
/*
** The subclasses of sqlite3_vtab  and sqlite3_vtab_cursor tables
** that implement the files_of_checkin virtual table.
................................................................................

static int fociEof(sqlite3_vtab_cursor *pCursor){
  FociCursor *pCsr = (FociCursor *)pCursor;
  return pCsr->pFile==0;
}

static int fociFilter(
  sqlite3_vtab_cursor *pCursor,
  int idxNum, const char *idxStr,
  int argc, sqlite3_value **argv
){
  FociCursor *pCur = (FociCursor *)pCursor;
  manifest_destroy(pCur->pMan);
  if( idxNum ){
    pCur->pMan = manifest_get(sqlite3_value_int(argv[0]), CFTYPE_MANIFEST, 0);
................................................................................
    pCur->pMan = 0;
    pCur->iFile = 0;
  }
  return SQLITE_OK;
}

static int fociColumn(
  sqlite3_vtab_cursor *pCursor,
  sqlite3_context *ctx,
  int i
){
  FociCursor *pCsr = (FociCursor *)pCursor;
  switch( i ){
    case 0:            /* checkinID */
      sqlite3_result_int(ctx, pCsr->pMan->rid);
      break;

Changes to src/import.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
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
...
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
...
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
....
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
....
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
1302
....
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
....
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381

1382
1383
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
1412
1413
1414
1415
....
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486


1487

1488













1489
1490
1491



1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
** merchantability or fitness for a particular purpose.
**
** Author contact information:
**   drh@sqlite.org
**
*******************************************************************************
**
** This file contains code used to import the content of a Git
** repository in the git-fast-import format as a new Fossil
** repository.
*/
#include "config.h"
#include "import.h"
#include <assert.h>

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

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 */
  Blob filter;                /* Path to repo root */
} gsvn;
typedef struct {
  char *zKey;
  const char *zVal;
} KeyVal;
typedef struct {
  KeyVal *aHeaders;
................................................................................
  int i;
  for(i=0; i<rec->nHeaders; i++){
    fossil_free(rec->aHeaders[i].zKey);
  }
  fossil_free(rec->aHeaders);
  fossil_free(rec->aProps);
  fossil_free(rec->pRawProps);
  blob_reset(&rec->content);  
}

static int svn_read_headers(FILE *pIn, SvnRecord *rec){
  char zLine[1000];

  rec->aHeaders = 0;
  rec->nHeaders = 0;
................................................................................
    }
  }else{
    rec->contentFlag = 0;
  }
  return 1;
}

static void svn_create_manifest(
){
  Blob manifest;
  static Stmt insRev;
  static Stmt qParent;
  static Stmt qParent2;
  static Stmt qFiles;
  static Stmt qTags;
................................................................................
        *zOut++ = *zCpy++;
      }
    }
    zDiff += lenData;
  }
}

static char *svn_extract_branch(const char *zPath){
  int nFilter = blob_size(&gsvn.filter);
  char *zBranch = 0;
  if( strncmp(zPath, blob_str(&gsvn.filter), nFilter)==0 ){
    if( strncmp(zPath+nFilter, gsvn.zBranches, gsvn.lenBranches)==0 ){
      int lenBranch;
      const char *zTemp = zPath+nFilter+gsvn.lenBranches;
      while( *zTemp && *zTemp!='/' ){ zTemp++; }
      lenBranch = zTemp-(zPath+nFilter+gsvn.lenBranches);
      zTemp = zPath+nFilter+gsvn.lenBranches;
      zBranch = fossil_malloc(lenBranch+1);
      memcpy(zBranch, zTemp, lenBranch);
      zBranch[lenBranch] = '\0';
    }else
    if( strncmp(zPath+nFilter, gsvn.zTrunk, gsvn.lenTrunk-1)==0 ){
      zBranch = mprintf("trunk");
    }
  }
  return zBranch;


















}

/*
** 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;
  Stmt delPath;
  int bHasFiles = 0;
  int nFilter = blob_size(&gsvn.filter);

  /* 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'"
  );
  db_prepare(&delPath,
    "INSERT INTO xhist (trev, tpath, trid, tperm)"
    " SELECT :rev, tpath, NULL, NULL"

    " FROM xfiles WHERE (tpath>:path||'/' AND tpath<:path||'0') OR tpath=:path"




  );
  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( bHasFiles ){
        svn_create_manifest();
      }
      fossil_free(gsvn.zUser);
      fossil_free(gsvn.zComment);
      fossil_free(gsvn.zDate);
      fossil_free(gsvn.zBranch);
      fossil_free(gsvn.zParentBranch);

      /* 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"));
      zDate = svn_find_prop(rec, "svn:date");
      if( zDate ){
        gsvn.zDate = date_in_standard_format(zDate);
      }
      gsvn.parentRev = -1;
      gsvn.zParentBranch = 0;
      gsvn.zBranch = 0;
      bHasFiles = 0;
      fossil_print("\rImporting SVN revision: %d", gsvn.rev);
      db_bind_int(&addHist, ":rev", gsvn.rev);
      db_bind_int(&cpyPath, ":rev", gsvn.rev);
      db_bind_int(&delPath, ":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 ){
        char *zBranch;
        if( (zBranch=svn_extract_branch(zPath))!=0 ){
          if( gsvn.zBranch!=0 ){
            if( strcmp(zBranch, gsvn.zBranch)!=0
             && strncmp(zAction, "delete", 6)!=0)
            {
              fossil_fatal("Commit to multiple branches");
            }
            fossil_free(zBranch);
          }else{
            gsvn.zBranch = zBranch;
          }
        }
      }
      if( strncmp(zAction, "delete", 6)==0
       || strncmp(zAction, "replace", 7)==0 )
      {
        db_bind_text(&delPath, ":path", zPath);

        db_step(&delPath);
        db_reset(&delPath);
        bHasFiles = 1;
      } /* 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);
            bHasFiles = 1;
            if( !gsvn.flatFlag ){
              if( strncmp(zPath+nFilter, gsvn.zBranches, gsvn.lenBranches)==0 ){
                zTemp = zPath+nFilter+gsvn.lenBranches+strlen(gsvn.zBranch);
                if( *zTemp==0 ){
                  gsvn.parentRev = srcRev;
                  gsvn.zParentBranch = svn_extract_branch(zSrcPath);
                }
              }else if( strncmp(zPath+nFilter, gsvn.zTags, gsvn.lenTags)==0 ){
                zTemp = zPath+nFilter+gsvn.lenTags;
                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{
................................................................................
          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 above */
        fossil_fatal("Unknown Node-action");
      }
    }else{
      fossil_fatal("Unknown record type");
    }
    svn_free_rec(&rec);
  }
................................................................................
** data is read from standard input.
**
** The following formats are currently understood by this command
**
**   git          Import from the git-fast-export file format
**
**   svn          Import from the svnadmin-dump file format. The default
**                behaviour is to treat 3 folders in the SVN root as special,
**                following the common layout of SVN repositories. These are
**                (by default) trunk/, branches/ and tags/

**                Options:
**                  --trunk FOLDER     Name of trunk folder
**                  --branches FOLDER  Name of branches folder
**                  --tags FOLDER      Name of tags folder
**                  --filter PATH      Path to project root in repository
**                  --flat             The whole dump is a single branch
**
** The --incremental option allows an existing repository to be extended
** with new content.
**
** Options:
**   --incremental  allow importing into an existing repository
................................................................................
**
** See also: export
*/
void import_cmd(void){
  char *zPassword;
  FILE *pIn;
  Stmt q;
  const char *zFilter = find_option("filter", 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);
  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"


       ");"

    );













    if( gsvn.zTrunk==0 ){ gsvn.zTrunk = "trunk/"; }
    if( gsvn.zBranches==0 ){ gsvn.zBranches = "branches/"; }
    if( gsvn.zTags==0 ){ gsvn.zTags = "tags/"; }



    gsvn.lenTrunk = strlen(gsvn.zTrunk);
    gsvn.lenBranches = strlen(gsvn.zBranches);
    gsvn.lenTags = strlen(gsvn.zTags);
    if( gsvn.zTrunk[gsvn.lenTrunk-1]!='/' ){
      gsvn.zTrunk = mprintf("%s/", gsvn.zTrunk);
      gsvn.lenTrunk++;
    }
    if( gsvn.zBranches[gsvn.lenBranches-1]!='/' ){
      gsvn.zBranches = mprintf("%s/", gsvn.zBranches);
      gsvn.lenBranches++;
    }
    if( gsvn.zTags[gsvn.lenTags-1]!='/' ){
      gsvn.zTags = mprintf("%s/", gsvn.zTags);
      gsvn.lenTags++;
    }
    if( zFilter==0 ){ zFilter = ""; }
    lenFilter = strlen(zFilter);
    blob_zero(&gsvn.filter);
    blob_set(&gsvn.filter, zFilter);
    if( lenFilter>0 && zFilter[lenFilter-1]!='/' ){
      blob_append(&gsvn.filter, "/", 1);
    }
    svn_dump_import(pIn);
  }

  verify_cancel();
  db_end_transaction(0);
  db_begin_transaction();







|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
|



|






<







 







|







 







|







 







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









|

>
>
|
|

<
<
<







 







>

|
<
|
>

<
|
|
|
|
|
|
|
|

|
|
<
>
|
>
>
>
>






|
|
<
|
|
|
<
<
>






|

|
<
<
<

<
<
<


<




>
>
>
>


>
>
>
>
>
>
>
>
>










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




|
>


<








|
>
>
|
>
|
>


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




|
|
|
|
<
>
>







 







|







 







|
|
|
>




|







 







|






|







 







|

|
|
|

|
|
<

<
<
<
<
<
<
|
<
>
>

>

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







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
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
...
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
....
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
....
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
....
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
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
1394
















1395
1396
1397
1398
1399
1400
1401
1402

1403
1404
1405
1406
1407
1408
1409
1410
1411
....
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
....
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
....
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
....
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
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627






1628
1629
1630
1631
1632
1633
1634
** merchantability or fitness for a particular purpose.
**
** Author contact information:
**   drh@sqlite.org
**
*******************************************************************************
**
** This file contains code used to import the content of a Git/SVN
** repository in the git-fast-import/svn-dump formats as a new Fossil
** repository.
*/
#include "config.h"
#include "import.h"
#include <assert.h>

#if INTERFACE
................................................................................
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 i;
  for(i=0; i<rec->nHeaders; i++){
    fossil_free(rec->aHeaders[i].zKey);
  }
  fossil_free(rec->aHeaders);
  fossil_free(rec->aProps);
  fossil_free(rec->pRawProps);
  blob_reset(&rec->content);
}

static int svn_read_headers(FILE *pIn, SvnRecord *rec){
  char zLine[1000];

  rec->aHeaders = 0;
  rec->nHeaders = 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;
................................................................................
        *zOut++ = *zCpy++;
      }
    }
    zDiff += lenData;
  }
}

/*
** 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';
      (*zFile)++;
    }else{
      *zFile = 0;
    }
    return 2;
  }else
  if( strncmp(zPath, gsvn.zTags, gsvn.lenTags)==0 ){
    *zFile = *zBranch = zPath+gsvn.lenTags;
    while( **zFile && **zFile!='/' ){ (*zFile)++; }
    if( *zFile ){
      **zFile = '\0';
      (*zFile)++;
    }else{
      *zFile = 0;
    }
    return 3;
  }
  *zFile = *zBranch = 0;
  return 0;
}

/*
** 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;
  char *zTemp;
  const char *zUuid;
  Stmt addFile;
  Stmt delPath;
  Stmt addSrc;
  Stmt addBranch;
  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(&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,
    "INSERT INTO xsrc (tpath, tbranch, tsrc, tsrcbranch, tsrcrev)"
    " VALUES(:path, :branch, :srcpath, :srcbranch, :srcrev)"
  );
  db_prepare(&addBranch,
    "INSERT INTO xchanges (tbranch, ttype) VALUES(:branch, :type)"

  );
  db_prepare(&cpyPath,
    "INSERT INTO xfiles (tpath, tbranch, tuuid, tperm)"
    " SELECT :path||substr(filename, length(:srcpath)+1), :branch, uuid, perm"
    " 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 */
      gsvn.rev = atoi(zTemp);
      gsvn.zUser = mprintf("%s", svn_find_prop(rec, "svn:author"));
      gsvn.zComment = mprintf("%s", svn_find_prop(rec, "svn:log"));
      zDate = svn_find_prop(rec, "svn:date");
      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( branchType==0 ){
        svn_free_rec(&rec);
        continue;
      }
      db_bind_text(&addBranch, ":branch", zBranch);
      db_bind_int(&addBranch, ":type", branchType);
      db_step(&addBranch);
      db_reset(&addBranch);
      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( 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);
        db_step(&addSrc);
        db_reset(&addSrc);





      }
      if( strncmp(zAction, "delete", 6)==0
       || strncmp(zAction, "replace", 7)==0 )
      {
        db_bind_text(&delPath, ":path", zFile);
        db_bind_text(&delPath, ":branch", zBranch);
        db_step(&delPath);
        db_reset(&delPath);

      } /* 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 ){
            int srcRid = db_int(0, "SELECT trid, max(trev) FROM xrevisions"
                                   " WHERE trev<=%d AND tbranch=%Q",
                                srcRev, zSrcBranch);
            db_bind_text(&cpyPath, ":path", zFile);
            db_bind_text(&cpyPath, ":branch", zBranch);
            db_bind_text(&cpyPath, ":srcpath", zSrcFile);
            db_bind_int(&cpyPath, ":rid", srcRid);
            db_step(&cpyPath);
            db_reset(&cpyPath);
















          }
        }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{
................................................................................
          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);
  }
................................................................................
** data is read from standard input.
**
** The following formats are currently understood by this command
**
**   git          Import from the git-fast-export file format
**
**   svn          Import from the svnadmin-dump file format. The default
**                behaviour (unless overridden by --flat) is to treat 3 folders
**                in the SVN root as special, following the common layout of
**                SVN repositories. These are (by default) trunk/, branches/
**                and tags/
**                Options:
**                  --trunk FOLDER     Name of trunk folder
**                  --branches FOLDER  Name of branches folder
**                  --tags FOLDER      Name of tags folder
**                  --base PATH        Path to project root in repository
**                  --flat             The whole dump is a single branch
**
** The --incremental option allows an existing repository to be extended
** with new content.
**
** Options:
**   --incremental  allow importing into an existing repository
................................................................................
**
** 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;

  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, 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;
      gsvn.lenTags = 0;
    }else{
      if( gsvn.zTrunk==0 ){ gsvn.zTrunk = "trunk/"; }
      if( gsvn.zBranches==0 ){ gsvn.zBranches = "branches/"; }
      if( gsvn.zTags==0 ){ gsvn.zTags = "tags/"; }
      gsvn.zTrunk = mprintf("%s%s", zBase, gsvn.zTrunk);
      gsvn.zBranches = mprintf("%s%s", zBase, gsvn.zBranches);
      gsvn.zTags = mprintf("%s%s", zBase, gsvn.zTags);
      gsvn.lenTrunk = strlen(gsvn.zTrunk);
      gsvn.lenBranches = strlen(gsvn.zBranches);
      gsvn.lenTags = strlen(gsvn.zTags);
      if( gsvn.zTrunk[gsvn.lenTrunk-1]!='/' ){
        gsvn.zTrunk = mprintf("%s/", gsvn.zTrunk);
        gsvn.lenTrunk++;
      }
      if( gsvn.zBranches[gsvn.lenBranches-1]!='/' ){
        gsvn.zBranches = mprintf("%s/", gsvn.zBranches);
        gsvn.lenBranches++;
      }
      if( gsvn.zTags[gsvn.lenTags-1]!='/' ){
        gsvn.zTags = mprintf("%s/", gsvn.zTags);
        gsvn.lenTags++;
      }






    }
    svn_dump_import(pIn);
  }

  verify_cancel();
  db_end_transaction(0);
  db_begin_transaction();