Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add the "fossil git export" command, designed to simplify making a Git mirror of a working Fossil repository. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
702959420fa6a5878fd0dc2db0b5b88d |
User & Date: | drh 2019-03-16 00:58:05.201 |
Context
2019-03-16
| ||
01:07 | Add a README.md file for the sake of the GitHub test mirror. ... (check-in: ced33d1a user: drh tags: trunk) | |
00:58 | Add the "fossil git export" command, designed to simplify making a Git mirror of a working Fossil repository. ... (check-in: 70295942 user: drh tags: trunk) | |
00:26 | Enhance the "fossil git export" command so that it also exports tags as lightweight Git tags. ... (Closed-Leaf check-in: 4f19d67b user: drh tags: mirror-cmd) | |
2019-03-14
| ||
13:52 | When doing a "fossil stash" make sure that the stash has committed before deleting changes from disk, so that the changes can be recovered if something goes wrong. ... (check-in: 60af514d user: drh tags: trunk) | |
Changes
Changes to src/export.c.
︙ | ︙ | |||
26 27 28 29 30 31 32 | */ static struct { const char *zTrunkName; /* Name of trunk branch */ } gexport; #if INTERFACE /* | | < | < < < < < | | | | | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | */ static struct { const char *zTrunkName; /* Name of trunk branch */ } gexport; #if INTERFACE /* ** Each line in a git-fast-export "marK" file is an instance of ** this object. */ struct mark_t { char *name; /* Name of the mark. Also starts with ":" */ int rid; /* Corresponding object in the BLOB table */ char uuid[65]; /* The GIT hash name for this object */ }; #endif /* ** Output a "committer" record for the given user. ** NOTE: the given user name may be an email itself. */ |
︙ | ︙ | |||
295 296 297 298 299 300 301 | return NULL; } } return zMark; } /* | > | < < | | | | > | | 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | return NULL; } } return zMark; } /* ** Parse a single line of the mark file. Store the result in the mark object. ** ** "line" is a single line of input. ** This function returns -1 in the case that the line is blank, malformed, or ** the rid/uuid named in 'line' does not match what is in the repository ** database. Otherwise, 0 is returned. ** ** mark->name is dynamically allocated, and owned by the caller. */ int parse_mark(char *line, struct mark_t *mark){ char *cur_tok; char type_; cur_tok = strtok(line, " \t"); if( !cur_tok || strlen(cur_tok)<2 ){ return -1; |
︙ | ︙ | |||
359 360 361 362 363 364 365 | /* insert a cross-ref into the 'xmark' table */ insert_commit_xref(mark->rid, mark->name, mark->uuid); return 0; } /* | < | | | | | | > | | | > | | | | 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | /* insert a cross-ref into the 'xmark' table */ insert_commit_xref(mark->rid, mark->name, mark->uuid); return 0; } /* ** Import the marks specified in file 'f'; ** If 'blobs' is non-null, insert all blob marks into it. ** If 'vers' is non-null, insert all commit marks into it. ** If 'unused_marks' is non-null, upon return of this function, all values ** x >= *unused_marks are free to use as marks, i.e. they do not clash with ** any marks appearing in the marks file. ** ** Each line in the file must be at most 100 characters in length. This ** seems like a reasonable maximum for a 40-character uuid, and 1-13 ** character rid. ** ** The function returns -1 if any of the lines in file 'f' are malformed, ** or the rid/uuid information doesn't match what is in the repository ** database. Otherwise, 0 is returned. */ int import_marks(FILE* f, Bag *blobs, Bag *vers, unsigned int *unused_mark){ char line[101]; while(fgets(line, sizeof(line), f)){ struct mark_t mark; if( strlen(line)==100 && line[99]!='\n' ){ /* line too long */ |
︙ | ︙ | |||
450 451 452 453 454 455 456 | do{ export_mark(f, rid, 'c'); }while( (rid = bag_next(vers, rid))!=0 ); } } } | | | | | 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | do{ export_mark(f, rid, 'c'); }while( (rid = bag_next(vers, rid))!=0 ); } } } /* This is the original header command (and hence documentation) for ** the "fossil export" command: ** ** Usage: %fossil export --git ?OPTIONS? ?REPOSITORY? ** ** Write an export of all check-ins to standard output. The export is ** written in the git-fast-export file format assuming the --git option is ** provided. The git-fast-export format is currently the only VCS ** interchange format supported, though other formats may be added in ** the future. |
︙ | ︙ | |||
481 482 483 484 485 486 487 488 489 490 491 492 493 494 | ** --export-marks FILE export rids of exported data to FILE ** --import-marks FILE read rids of data to ignore from FILE ** --rename-trunk NAME use NAME as name of exported trunk branch ** --repository|-R REPOSITORY export the given REPOSITORY ** ** See also: import */ void export_cmd(void){ Stmt q, q2, q3; Bag blobs, vers; unsigned int unused_mark = 1; const char *markfile_in; const char *markfile_out; | > > > > > | 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | ** --export-marks FILE export rids of exported data to FILE ** --import-marks FILE read rids of data to ignore from FILE ** --rename-trunk NAME use NAME as name of exported trunk branch ** --repository|-R REPOSITORY export the given REPOSITORY ** ** See also: import */ /* ** COMMAND: export* ** ** This command is deprecated. Use "fossil git export" instead. */ void export_cmd(void){ Stmt q, q2, q3; Bag blobs, vers; unsigned int unused_mark = 1; const char *markfile_in; const char *markfile_out; |
︙ | ︙ | |||
505 506 507 508 509 510 511 | db_find_and_open_repository(0, 2); verify_all_options(); if( g.argc!=2 && g.argc!=3 ){ usage("--git ?REPOSITORY?"); } db_multi_exec("CREATE TEMPORARY TABLE oldblob(rid INTEGER PRIMARY KEY)"); db_multi_exec("CREATE TEMPORARY TABLE oldcommit(rid INTEGER PRIMARY KEY)"); | | > | 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | db_find_and_open_repository(0, 2); verify_all_options(); if( g.argc!=2 && g.argc!=3 ){ usage("--git ?REPOSITORY?"); } db_multi_exec("CREATE TEMPORARY TABLE oldblob(rid INTEGER PRIMARY KEY)"); db_multi_exec("CREATE TEMPORARY TABLE oldcommit(rid INTEGER PRIMARY KEY)"); db_multi_exec("CREATE TEMP TABLE xmark(tname TEXT UNIQUE, trid INT," " tuuid TEXT)"); db_multi_exec("CREATE INDEX xmark_trid ON xmark(trid)"); if( markfile_in!=0 ){ Stmt qb,qc; FILE *f; int rid; f = fossil_fopen(markfile_in, "r"); |
︙ | ︙ | |||
752 753 754 755 756 757 758 | ** tid INTEGER PRIMARY KEY, -- Check-in id ** tseq INT -- integer total order on check-ins. ** ); ** ** This table contains all check-ins of the repository in topological ** order. "Topological order" means that every parent check-in comes ** before all of its children. Topological order is *almost* the same | | | 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 | ** tid INTEGER PRIMARY KEY, -- Check-in id ** tseq INT -- integer total order on check-ins. ** ); ** ** This table contains all check-ins of the repository in topological ** order. "Topological order" means that every parent check-in comes ** before all of its children. Topological order is *almost* the same ** thing as "ORDER BY event.mtime". Differences only arise when there ** are timewarps. In as much as Git hates timewarps, we have to compute ** a correct topological order when doing an export. ** ** Since mtime is a usually already nearly in topological order, the ** algorithm is to start with mtime, then make adjustments as necessary ** for timewarps. This is not a great algorithm for the general case, ** but it is very fast for the overwhelmingly common case where there |
︙ | ︙ | |||
833 834 835 836 837 838 839 | */ void test_topological_sort(void){ int n; db_find_and_open_repository(0, 0); n = topological_sort_checkins(1); fossil_print("%d reorderings required\n", n); } | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 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 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 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 1056 1057 1058 1059 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 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 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 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 1412 1413 1414 1415 1416 1417 1418 1419 | */ void test_topological_sort(void){ int n; db_find_and_open_repository(0, 0); n = topological_sort_checkins(1); fossil_print("%d reorderings required\n", n); } /*************************************************************************** ** Implementation of the "fossil git" command follows. We hope that the ** new code that follows will largely replace the legacy "fossil export" ** and "fossil import" code above. */ /* ** Convert characters of z[] that are not allowed to be in branch or ** tag names into "_". */ static void gitmirror_sanitize_name(char *z){ static unsigned char aSafe[] = { /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 2x */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* 3x */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, /* 7x */ }; unsigned char *zu = (unsigned char*)z; int i; for(i=0; zu[i]; i++){ if( zu[i]>0x7f || !aSafe[zu[i]] ){ zu[i] = '_'; }else if( zu[i]=='/' && (i==0 || zu[i+1]==0 || zu[i+1]=='/') ){ zu[i] = '_'; }else if( zu[i]=='.' && (zu[i+1]==0 || zu[i+1]=='.' || (i>0 && zu[i-1]=='.')) ){ zu[i] = '_'; } } } /* ** Quote a filename as a C-style string using \\ and \" if necessary. ** If quoting is not necessary, just return a copy of the input string. ** ** The return value is a held in memory obtained from fossil_malloc() ** and must be freed by the caller. */ static char *gitmirror_quote_filename_if_needed(const char *zIn){ int i, j; char c; int nSpecial = 0; char *zOut; for(i=0; (c = zIn[i])!=0; i++){ if( c=='\\' || c=='"' || c=='\n' ){ nSpecial++; } } if( nSpecial==0 ){ return fossil_strdup(zIn); } zOut = fossil_malloc( i+nSpecial+3 ); zOut[0] = '"'; for(i=0, j=1; (c = zIn[i])!=0; i++){ if( c=='\\' || c=='"' || c=='\n' ){ zOut[j++] = '\\'; if( c=='\n' ){ zOut[j++] = 'n'; }else{ zOut[j++] = c; } }else{ zOut[j++] = c; } } zOut[j++] = '"'; zOut[j] = 0; return zOut; } /* ** Locate the mark for a UUID. ** ** If the mark does not exist and if the bCreate flag is false, then ** return 0. If the mark does not exist and the bCreate flag is true, ** then create the mark. */ static int gitmirror_find_mark(const char *zUuid, int bCreate){ int iMark; static Stmt sFind, sIns; db_static_prepare(&sFind, "SELECT id FROM mirror.mmark WHERE uuid=:uuid" ); db_bind_text(&sFind, ":uuid", zUuid); if( db_step(&sFind)==SQLITE_ROW ){ iMark = db_column_int(&sFind, 0); db_reset(&sFind); return iMark; } db_reset(&sFind); if( !bCreate ) return 0; db_static_prepare(&sIns, "INSERT INTO mirror.mmark(uuid) VALUES(:uuid)" ); db_bind_text(&sIns, ":uuid", zUuid); db_step(&sIns); db_reset(&sIns); return db_last_insert_rowid(); } /* This is the SHA3-256 hash of an empty file */ static const char zEmptySha3[] = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"; /* ** Export a single file named by zUuid. */ static void gitmirror_send_file(FILE *xCmd, const char *zUuid){ int iMark; int rid; int rc; Blob data; rid = fast_uuid_to_rid(zUuid); if( rid<0 ){ zUuid = zEmptySha3; }else{ rc = content_get(rid, &data); if( rc==0 ){ blob_init(&data, 0, 0); zUuid = zEmptySha3; } } iMark = gitmirror_find_mark(zUuid, 1); fprintf(xCmd, "blob\nmark :%d\ndata %d\n", iMark, blob_size(&data)); fwrite(blob_buffer(&data), 1, blob_size(&data), xCmd); fprintf(xCmd, "\n"); blob_reset(&data); } /* ** Transfer a check-in over to the mirror. "rid" is the BLOB.RID for ** the check-in to export. ** ** If any ancestor of the check-in has not yet been exported, then ** invoke this routine recursively to export the ancestor first. ** This can only happen on a timewarp, so deep nesting is unlikely. ** ** Before sending the check-in, first make sure all associated files ** have already been exported, and send "blob" records for any that ** have not been. Update the MIRROR.MMARK table so that it holds the ** marks for the exported files. */ static void gitmirror_send_checkin( FILE *xCmd, /* Write fast-import text on this pipe */ int rid, /* BLOB.RID for the check-in to export */ const char *zUuid, /* BLOB.UUID for the check-in to export */ int *pnLimit, /* Stop when the counter reaches zero */ int fManifest /* MFESTFLG_* values */ ){ Manifest *pMan; /* The check-in to be output */ int i; /* Loop counter */ int iParent; /* Which immediate ancestor is primary. -1 for none */ Stmt q; /* An SQL query */ char *zBranch; /* The branch of the check-in */ int iMark; /* The mark for the check-in */ Blob sql; /* String of SQL for part of the query */ Blob comment; /* The comment text for the check-in */ pMan = manifest_get(rid, CFTYPE_MANIFEST, 0); if( pMan==0 ){ /* Must be a phantom. Return without doing anything, and in particular ** without creating a mark for this check-in. */ return; } /* Check to see if any parent logins have not yet been processed, and ** if so, create them */ for(i=0; i<pMan->nParent; i++){ int iMark = gitmirror_find_mark(pMan->azParent[i], 0); if( iMark<=0 ){ int prid = db_int(0, "SELECT rid FROM blob WHERE uuid=%Q", pMan->azParent[i]); gitmirror_send_checkin(xCmd, prid, pMan->azParent[i], pnLimit, fManifest); if( *pnLimit<=0 ){ manifest_destroy(pMan); return; } } } /* Make sure all necessary files have been exported */ db_prepare(&q, "SELECT uuid FROM files_of_checkin(%Q)" " WHERE uuid NOT IN (SELECT uuid FROM mirror.mmark)", zUuid ); while( db_step(&q)==SQLITE_ROW ){ const char *zFUuid = db_column_text(&q, 0); gitmirror_send_file(xCmd, zFUuid); } db_finalize(&q); /* Figure out which branch this check-in is a member of */ zBranch = db_text(0, "SELECT value FROM tagxref WHERE tagid=%d AND tagtype>0 AND rid=%d", TAG_BRANCH, rid ); if( fossil_strcmp(zBranch,"trunk")==0 ){ fossil_free(zBranch); zBranch = mprintf("master"); }else if( zBranch==0 ){ zBranch = mprintf("unknown"); }else{ gitmirror_sanitize_name(zBranch); } /* Export the check-in */ fprintf(xCmd, "commit refs/heads/%s\n", zBranch); fossil_free(zBranch); iMark = gitmirror_find_mark(zUuid, 1); fprintf(xCmd, "mark :%d\n", iMark); fprintf(xCmd, "committer %s <%s@noemail.net> %lld +0000\n", pMan->zUser, pMan->zUser, (sqlite3_int64)((pMan->rDate-2440587.5)*86400.0) ); blob_init(&comment, pMan->zComment, -1); if( blob_size(&comment)==0 ){ blob_append(&comment, "(no comment)", -1); } blob_appendf(&comment, "\n\nFossilOrigin-Name: %s", zUuid); fprintf(xCmd, "data %d\n%s\n", blob_size(&comment), blob_str(&comment)); blob_reset(&comment); iParent = -1; /* Which ancestor is the primary parent */ for(i=0; i<pMan->nParent; i++){ int iOther = gitmirror_find_mark(pMan->azParent[i], 0); if( iOther==0 ) continue; if( iParent<0 ){ iParent = i; fprintf(xCmd, "from :%d\n", iOther); }else{ fprintf(xCmd, "merge :%d\n", iOther); } } if( iParent>=0 ){ db_prepare(&q, "SELECT filename FROM files_of_checkin(%Q)" " EXCEPT SELECT filename FROM files_of_checkin(%Q)", pMan->azParent[iParent], zUuid ); while( db_step(&q)==SQLITE_ROW ){ fprintf(xCmd, "D %s\n", db_column_text(&q,0)); } db_finalize(&q); } blob_init(&sql, 0, 0); blob_append_sql(&sql, "SELECT filename, uuid, perm FROM files_of_checkin(%Q)", zUuid ); if( pMan->nParent ){ blob_append_sql(&sql, " EXCEPT SELECT filename, uuid, perm FROM files_of_checkin(%Q)", pMan->azParent[0]); } db_prepare(&q, "SELECT x.filename, x.perm, mmark.id FROM (%s) AS x, mirror.mmark" " WHERE mmark.uuid=x.uuid", blob_sql_text(&sql) ); blob_reset(&sql); while( db_step(&q)==SQLITE_ROW ){ const char *zFilename = db_column_text(&q,0); const char *zMode = db_column_text(&q,1); int iMark = db_column_int(&q,2); const char *zGitMode = "100644"; char *zFNQuoted = 0; if( zMode ){ if( strchr(zMode,'x') ) zGitMode = "100755"; if( strchr(zMode,'l') ) zGitMode = "120000"; } zFNQuoted = gitmirror_quote_filename_if_needed(zFilename); fprintf(xCmd,"M %s :%d %s\n", zGitMode, iMark, zFNQuoted); fossil_free(zFNQuoted); } db_finalize(&q); /* Include Fossil-generated auxiliary files in the check-in */ if( fManifest & MFESTFLG_RAW ){ Blob manifest; content_get(rid, &manifest); fprintf(xCmd,"M 100644 inline manifest\ndata %d\n%s\n", blob_size(&manifest), blob_str(&manifest)); blob_reset(&manifest); } if( fManifest & MFESTFLG_UUID ){ int n = (int)strlen(zUuid); fprintf(xCmd,"M 100644 inline manifest.uuid\ndata %d\n%s\n", n, zUuid); } if( fManifest & MFESTFLG_TAGS ){ Blob tagslist; blob_init(&tagslist, 0, 0); get_checkin_taglist(rid, &tagslist); fprintf(xCmd,"M 100644 inline manifest.tags\ndata %d\n%s\n", blob_size(&tagslist), blob_str(&tagslist)); blob_reset(&tagslist); } /* The check-in is finished, so decrement the counter */ (*pnLimit)--; } /* ** Implementation of the "fossil git export" command. */ void gitmirror_export_command(void){ const char *zLimit; /* Text of the --limit flag */ int nLimit = 0x7fffffff; /* Numeric value of the --limit flag */ int nTotal = 0; /* Total number of check-ins to export */ char *zMirror; /* Name of the mirror */ char *z; /* Generic string */ char *zCmd; /* git command to run as a subprocess */ const char *zDebug = 0; /* Value of the --debug flag */ double rEnd; /* time of most recent export */ int rc; /* Result code */ int fManifest; /* Current "manifest" setting */ FILE *xCmd; /* Pipe to the "git fast-import" command */ FILE *pIn, *pOut; /* Git mark files */ Stmt q; /* Queries */ char zLine[200]; /* One line of a mark file */ zDebug = find_option("debug",0,1); db_find_and_open_repository(0, 0); zLimit = find_option("limit", 0, 1); if( zLimit ){ nLimit = (unsigned int)atoi(zLimit); if( nLimit<=0 ) fossil_fatal("--limit must be positive"); } verify_all_options(); if( g.argc!=4 ){ usage("export MIRROR"); } zMirror = g.argv[3]; /* Make sure the GIT repository directory exists */ rc = file_mkdir(zMirror, ExtFILE, 0); if( rc ) fossil_fatal("cannot create directory \"%s\"", zMirror); /* Make sure GIT has been initialized */ z = mprintf("%s/.git", zMirror); if( !file_isdir(z, ExtFILE) ){ zCmd = mprintf("git init '%s'",zMirror); fossil_print("%s\n", zCmd); rc = fossil_system(zCmd); if( rc ){ fossil_fatal("command failed: \"%s\"", zCmd); } fossil_free(zCmd); } fossil_free(z); /* Make sure the .mirror_state subdirectory exists */ z = mprintf("%s/.mirror_state", zMirror); rc = file_mkdir(z, ExtFILE, 0); if( rc ) fossil_fatal("cannot create directory \"%s\"", z); fossil_free(z); /* Attach the .mirror_state/db database */ db_multi_exec("ATTACH '%q/.mirror_state/db' AS mirror;", zMirror); db_begin_write(); db_multi_exec( "CREATE TABLE IF NOT EXISTS mirror.mconfig(\n" " key TEXT PRIMARY KEY,\n" " Value ANY\n" ") WITHOUT ROWID;\n" "CREATE TABLE IF NOT EXISTS mirror.mmark(\n" " id INTEGER PRIMARY KEY,\n" " uuid TEXT UNIQUE,\n" " githash TEXT\n" ");" ); /* See if there is any work to be done. Exit early if not, before starting ** the "git fast-import" command. */ if( !db_exists("SELECT 1 FROM event WHERE type IN ('ci','t')" " AND mtime>coalesce((SELECT value FROM mconfig" " WHERE key='start'),0.0)") ){ fossil_print("no changes\n"); return; } /* Do we need to include manifest files in the clone? */ fManifest = db_get_manifest_setting(); /* Change to the MIRROR directory so that the Git commands will work */ rc = file_chdir(zMirror, 0); if( rc ) fossil_fatal("cannot change the working directory to \"%s\"", zMirror); /* Start up the git fast-import command */ if( zDebug ){ if( fossil_strcmp(zDebug,"stdout")==0 ){ xCmd = stdout; }else{ xCmd = fopen(zDebug, "wb"); if( xCmd==0 ) fossil_fatal("cannot open file \"%s\" for writing", zDebug); } }else{ zCmd = mprintf("git fast-import" " --import-marks-if-exists=.mirror_state/in" " --export-marks=.mirror_state/out" " --quiet --done"); fossil_print("%s\n", zCmd); xCmd = popen(zCmd, "w"); if( zCmd==0 ){ fossil_fatal("cannot start the \"git fast-import\" command"); } fossil_free(zCmd); } /* Run the export */ rEnd = 0.0; db_multi_exec( "CREATE TEMP TABLE tomirror(objid,mtime,uuid);\n" "INSERT INTO tomirror " "SELECT objid, mtime, blob.uuid FROM event, blob\n" " WHERE type='ci'" " AND mtime>coalesce((SELECT value FROM mconfig WHERE key='start'),0.0)" " AND blob.rid=event.objid" " AND blob.uuid NOT IN (SELECT uuid FROM mirror.mmark);" ); nTotal = db_int(0, "SELECT count(*) FROM tomirror"); if( nLimit<nTotal ){ nTotal = nLimit; }else if( nLimit>nTotal ){ nLimit = nTotal; } db_prepare(&q, "SELECT objid, mtime, uuid FROM tomirror ORDER BY mtime" ); while( nLimit && db_step(&q)==SQLITE_ROW ){ int rid = db_column_int(&q, 0); double rMTime = db_column_double(&q, 1); const char *zUuid = db_column_text(&q, 2); if( rMTime>rEnd ) rEnd = rMTime; gitmirror_send_checkin(xCmd, rid, zUuid, &nLimit, fManifest); printf("\r%d/%d ", nTotal-nLimit, nTotal); fflush(stdout); } db_finalize(&q); fprintf(xCmd, "done\n"); if( zDebug ){ if( xCmd!=stdout ) fclose(xCmd); }else{ pclose(xCmd); } fossil_print("%d check-ins added to the mirror\n", nTotal-nLimit); /* Read the export-marks file. Transfer the new marks over into ** the import-marks file. */ pOut = fopen(".mirror_state/out", "rb"); if( pOut ){ pIn = fopen(".mirror_state/in", "ab"); if( pIn==0 ){ fossil_fatal("cannot open %s/.mirror_state/in for appending", zMirror); } db_prepare(&q, "UPDATE mirror.mmark SET githash=:githash WHERE id=:id"); while( fgets(zLine, sizeof(zLine), pOut) ){ int j, k; if( zLine[0]!=':' ) continue; db_bind_int(&q, ":id", atoi(zLine+1)); for(j=1; zLine[j] && zLine[j]!=' '; j++){} if( zLine[j]!=' ' ) continue; j++; if( zLine[j]==0 ) continue; for(k=j; fossil_isalnum(zLine[k]); k++){} zLine[k] = 0; db_bind_text(&q, ":githash", &zLine[j]); db_step(&q); db_reset(&q); zLine[k] = '\n'; fputs(zLine, pIn); } db_finalize(&q); fclose(pOut); fclose(pIn); file_delete(".mirror_state/out"); }else{ fossil_fatal("git fast-import didn't generate a marks file!"); } db_multi_exec( "CREATE INDEX IF NOT EXISTS mirror.mmarkx1 ON mmark(githash);" ); /* Do any tags that have been created since the start time */ db_prepare(&q, "SELECT substr(tagname,5), githash" " FROM (SELECT tagxref.tagid AS xtagid, tagname, rid, max(mtime) AS mtime" " FROM tagxref JOIN tag ON tag.tagid=tagxref.tagid" " WHERE tag.tagname GLOB 'sym-*'" " AND tagxref.tagtype=1" " AND tagxref.mtime > coalesce((SELECT value FROM mconfig" " WHERE key='start'),0.0)" " GROUP BY tagxref.tagid) AS tx" " JOIN blob ON tx.rid=blob.rid" " JOIN mmark ON mmark.uuid=blob.uuid;" ); while( db_step(&q)==SQLITE_ROW ){ char *zTagname = fossil_strdup(db_column_text(&q,0)); const char *zObj = db_column_text(&q,1); char *zTagCmd; gitmirror_sanitize_name(zTagname); zTagCmd = mprintf("git tag -f \"%s\" %s", zTagname, zObj); fossil_free(zTagname); fossil_print("%s\n", zTagCmd); fossil_system(zTagCmd); fossil_free(zTagCmd); } db_finalize(&q); /* Update the start time */ db_prepare(&q, "REPLACE INTO mirror.mconfig(key,value) VALUES('start',:x)"); db_bind_double(&q, ":x", rEnd); db_step(&q); db_finalize(&q); db_commit_transaction(); /* Optionally do a "git push" */ } /* ** COMMAND: git ** ** Usage: %fossil git SUBCOMMAND ** ** Do incremental import or export operations between Fossil and Git. ** Subcommands: ** ** fossil git export MIRROR [OPTIONS] ** ** Write content from the Fossil repository into the Git repository ** in directory MIRROR. The Git repository is created if it does not ** already exist. If the Git repository does already exist, then ** new content added to fossil since the previous export is appended. ** ** Repeat this command whenever new checkins are added to the Fossil ** repository in order to reflect those changes into the mirror. ** ** The MIRROR directory will contain a subdirectory named ** ".mirror_state" that contains information that Fossil needs to ** do incremental exports. Do not attempt to manage or edit the files ** in that directory since doing so can disrupt future incremental ** exports. ** ** Options: ** --debug FILE Write fast-export text to FILE rather than ** piping it into "git fast-import". ** --limit N Add no more than N new check-ins to MIRROR. ** Useful for debugging ** ** fossil git import MIRROR ** ** TBD... */ void gitmirror_command(void){ char *zCmd; int nCmd; if( g.argc<3 ){ usage("export ARGS..."); } zCmd = g.argv[2]; nCmd = (int)strlen(zCmd); if( nCmd>2 && strncmp(zCmd,"export",nCmd)==0 ){ gitmirror_export_command(); }else if( nCmd>2 && strncmp(zCmd,"import",nCmd)==0 ){ fossil_fatal("not yet implemented - check back later"); }else { fossil_fatal("unknown subcommand \"%s\": should be one of " "\"export\", \"import\"", zCmd); } } |