Fossil

Check-in [c95c183e]
Login

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

Overview
Comment:import branches
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | svn-import
Files: files | file ages | folders
SHA1:c95c183e7be7dbe812b391ac32dd6552b9b4263f
User & Date: baruch 2014-10-26 14:16:58
Context
2014-10-26
14:17
Merge changes from trunk check-in: 980acc8f user: baruch tags: svn-import
14:16
import branches check-in: c95c183e user: baruch tags: svn-import
2014-10-22
11:53
Can now import full dump. Still only flat and only format 2. No tracking of file moves yet check-in: 8d2e6751 user: baruch tags: svn-import
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/import.c.

875
876
877
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
...
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
...
977
978
979
980
981
982
983








984
985
986
987
988
989









990
991
992
993
994
995
996
....
1013
1014
1015
1016
1017
1018
1019












1020
1021
1022
1023
1024
1025
1026
....
1036
1037
1038
1039
1040
1041
1042










1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
....
1071
1072
1073
1074
1075
1076
1077
1078






1079
1080
1081



1082
1083
1084
1085
1086
1087
1088
....
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116

1117
1118
1119
1120
1121
1122
1123
....
1170
1171
1172
1173
1174
1175
1176
1177

1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
    fossil_fatal("short read: got %d of %d bytes",
      blob_size(&rec->content), nLen
    );
  }
  return 1;
}

static void svn_create_manifests(){
  Blob manifest;
  Stmt qRev;

  Stmt qFiles;
  int parentRid = 0;


  blob_zero(&manifest);

























  db_prepare(&qRev, "SELECT trev, tuser, tmsg, ttime FROM xrevisions"

                    " ORDER BY trev");

  db_prepare(&qFiles, "SELECT tpath, uuid, tperm"
                      " FROM xfiles JOIN blob ON xfiles.trid=blob.rid"
                      " WHERE trev=:rev ORDER BY tpath");




  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);



    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);
................................................................................
    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( parentRid>0 ){
      const char *zParent;
      zParent = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", parentRid);

      blob_appendf(&manifest, "P %s\n", zParent);







      fossil_free(zParent);
    }else{
      blob_appendf(&manifest, "R d41d8cd98f00b204e9800998ecf8427e\n");
      blob_appendf(&manifest, "T *branch * trunk\n");
      blob_appendf(&manifest, "T *sym-trunk *\n");
    }

    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);

    parentRid = content_put(&manifest);




  }
  db_finalize(&qRev);

  db_finalize(&qFiles);

}
/*
** 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 insRev;
  Stmt insFile;
  Stmt delFile;


  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 ){
................................................................................
      "INSERT INTO xfiles (trev, tpath, trid, tperm) "
      "VALUES(:rev, :path, :rid, :perm)"
  );
  db_prepare(&delFile,
      "DELETE FROM xfiles "
      "WHERE trev=:rev "
      "  AND (tpath=:path OR (tpath>:path||'/' AND tpath<:path||'0'))"








  );
  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");









      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);
................................................................................
      if( zSrcPath ){
        zTemp = svn_find_header(rec, "Node-copyfrom-rev");
        if( zTemp ){
          srcRev = atoi(zTemp);
        }else{
          fossil_fatal("Missing copyfrom-rev");
        }












      }
      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_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
            );










          }
        }else{
          if( blob_size(&rec.content)==0 && zSrcPath ){
            rid = db_int(rid,
                         "SELECT trid FROM xfiles WHERE trev=%d AND tpath=%Q",
                         srcRev, zSrcPath);
          }else{
            rid = content_put(&rec.content);
          }
          db_bind_int(&insFile, ":rev", rev);
          db_bind_int(&insFile, ":rid", rid);
................................................................................
        fossil_fatal("Unknown Node-action");
      }
    }else{
      fossil_fatal("Unknown record type");
    }
    svn_free_rec(&rec);
  }
  svn_create_manifests();






  db_finalize(&insRev);
  db_finalize(&insFile);
  db_finalize(&delFile);



}

/*
** COMMAND: import
**
** Usage: %fossil import FORMAT ?OPTIONS? NEW-REPOSITORY ?INPUT-FILE?
**
................................................................................
** with new content.
**
** Options:
**   --incremental  allow importing into an existing repository
**
** See also: export
*/
void git_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;


  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 INT, tuser TEXT, tmsg TEXT, ttime DATETIME"

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

    svn_dump_import(pIn);
  }

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







|


>

<
>

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

>



>
>
>
>





>
>
>

>
>







 







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

<



>











|
>
>
>
>


>

>





|




>



>
>







 







>
>
>
>
>
>
>
>






>
>
>
>
>
>
>
>
>







 







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







 







>
>
>
>
>
>
>
>
>
>



|







 







|
>
>
>
>
>
>



>
>
>







 







|





>







 







|
>






<
|







875
876
877
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
...
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
....
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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
....
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
....
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
....
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
....
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
....
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286

1287
1288
1289
1290
1291
1292
1293
1294
    fossil_fatal("short read: got %d of %d bytes",
      blob_size(&rec->content), nLen
    );
  }
  return 1;
}

static void svn_create_manifests(int flatFlag){
  Blob manifest;
  Stmt qRev;
  Stmt qParent;
  Stmt qFiles;

  Stmt setUuid;


  if( !flatFlag ){
    db_multi_exec("DELETE FROM xrevisions WHERE tbranch 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;"
                  "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");
  }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(&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;
    Blob mcksum;
    const char *zParentUuid = 0;
    const char *zParentBranch = 0;
    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);
................................................................................
    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 ){
      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);
        }
      }
      db_reset(&qParent);
    }else{

      blob_appendf(&manifest, "T *branch * trunk\n");
      blob_appendf(&manifest, "T *sym-trunk *\n");
    }
    blob_appendf(&manifest, "T +sym-svn-rev-%d *\n", rev);
    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(&setUuid);
}
/*
** 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 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 ){
................................................................................
      "INSERT INTO xfiles (trev, tpath, trid, tperm) "
      "VALUES(:rev, :path, :rid, :perm)"
  );
  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);
................................................................................
      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_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 && 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( blob_size(&rec.content)==0 && zSrcPath ){
            rid = db_int(0,
                         "SELECT trid FROM xfiles WHERE trev=%d AND tpath=%Q",
                         srcRev, zSrcPath);
          }else{
            rid = content_put(&rec.content);
          }
          db_bind_int(&insFile, ":rev", rev);
          db_bind_int(&insFile, ":rid", rid);
................................................................................
        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(&delFile);
  db_finalize(&setBranch);
  db_finalize(&setParent);
  svn_create_manifests(flatFlag);
}

/*
** COMMAND: import
**
** Usage: %fossil import FORMAT ?OPTIONS? NEW-REPOSITORY ?INPUT-FILE?
**
................................................................................
** with new content.
**
** Options:
**   --incremental  allow importing into an existing repository
**
** See also: export
*/
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 INT, 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"
       ");"
    );

    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();