Diff
Not logged in

Differences From Artifact [6dc187b147]:

To Artifact [cdb1482c24]:


82
83
84
85
86
87
88
89

90
91
92
93
94
95
96
97
98
99
100
101






102
103
104

105
106



107
108
109
110



111
112
113

































114
115
116
117
118

119



120
121
122
123



124
125
126
127
128
129
130

131
132
133
134
135
136
137
138
139

140
141
142

143
144

145
146
147
148
149
150
151
152



153
154
155
156
157
158
159
160
161
162
163
164
165
166


167
168
169
170
171
172
173
82
83
84
85
86
87
88

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

191
192
193
194
195
196

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220


221
222
223
224
225
226
227
228
229







-
+












+
+
+
+
+
+


-
+


+
+
+




+
+
+



+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+





+
-
+
+
+




+
+
+







+








-
+



+

-
+








+
+
+












-
-
+
+








#define BLOBMARK(rid)   ((rid) * 2)
#define COMMITMARK(rid) ((rid) * 2 + 1)

/*
** COMMAND: export
**
** Usage: %fossil export --git ?REPOSITORY?
** 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.
**
** Run this command within a checkout.  Or use the -R or --repository
** option to specify a Fossil repository to be exported.
**
** Only check-ins are exported using --git.  Git does not support tickets 
** or wiki or events or attachments, so none of those are exported.
**
** If the "--import-marks FILE" option is used, it contains a list of
** rids to skip.
**
** If the "--export-marks FILE" option is used, the rid of all commits and
** blobs written on exit for use with "--import-marks" on the next run.
*/
void export_cmd(void){
  Stmt q;
  Stmt q, q2;
  int i;
  Bag blobs, vers;
  const char *markfile_in;
  const char *markfile_out;

  bag_init(&blobs);
  bag_init(&vers);

  find_option("git", 0, 0);   /* Ignore the --git option for now */
  markfile_in = find_option("import-marks", 0, 1);
  markfile_out = find_option("export-marks", 0, 1);

  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)");
  db_multi_exec("CREATE TEMPORARY TABLE oldcommit(rid INTEGER)");
  if( markfile_in!=0 ){
    Stmt qb,qc;
    char line[100];
    FILE *f;

    f = fopen(markfile_in, "r");
    if( f==0 ){
      fossil_panic("cannot open %s for reading", markfile_in);
    }
    db_prepare(&qb, "INSERT INTO oldblob VALUES (:rid)");
    db_prepare(&qc, "INSERT INTO oldcommit VALUES (:rid)");
    while( fgets(line, sizeof(line), f)!=0 ){
      if( *line == 'b' ){
        db_bind_text(&qb, ":rid", line + 1);
        db_step(&qb);
        db_reset(&qb);
        bag_insert(&blobs, atoi(line + 1));
      }else if( *line == 'c' ){
        db_bind_text(&qc, ":rid", line + 1);
        db_step(&qc);
        db_reset(&qc);
        bag_insert(&vers, atoi(line + 1));
      }else{
        fossil_panic("bad input from %s: %s", markfile_in, line);
      }
    }
    db_finalize(&qb);
    db_finalize(&qc);
    fclose(f);
  }

  /* Step 1:  Generate "blob" records for every artifact that is part
  ** of a check-in 
  */
  fossil_binary_mode(stdout);
  db_prepare(&q,
  db_prepare(&q, "SELECT DISTINCT fid FROM mlink WHERE fid>0");
    "SELECT DISTINCT fid FROM mlink"
    " WHERE fid>0 AND NOT EXISTS(SELECT 1 FROM oldblob WHERE rid=fid)");
  db_prepare(&q2, "INSERT INTO oldblob VALUES (:rid)");
  while( db_step(&q)==SQLITE_ROW ){
    int rid = db_column_int(&q, 0);
    Blob content;
    content_get(rid, &content);
    db_bind_int(&q2, ":rid", rid);
    db_step(&q2);
    db_reset(&q2);
    printf("blob\nmark :%d\ndata %d\n", BLOBMARK(rid), blob_size(&content));
    bag_insert(&blobs, rid);
    fwrite(blob_buffer(&content), 1, blob_size(&content), stdout);
    printf("\n");
    blob_reset(&content);
  }
  db_finalize(&q);
  db_finalize(&q2);

  /* Output the commit records.
  */
  db_prepare(&q,
    "SELECT strftime('%%s',mtime), objid, coalesce(comment,ecomment),"
    "       coalesce(user,euser),"
    "       (SELECT value FROM tagxref WHERE rid=objid AND tagid=%d)"
    "  FROM event"
    " WHERE type='ci'"
    " WHERE type='ci' AND NOT EXISTS (SELECT 1 FROM oldcommit WHERE objid=rid)"
    " ORDER BY mtime ASC",
    TAG_BRANCH
  );
  db_prepare(&q2, "INSERT INTO oldcommit VALUES (:rid)");
  while( db_step(&q)==SQLITE_ROW ){
    Stmt q2;
    Stmt q3;
    const char *zSecondsSince1970 = db_column_text(&q, 0);
    int ckinId = db_column_int(&q, 1);
    const char *zComment = db_column_text(&q, 2);
    const char *zUser = db_column_text(&q, 3);
    const char *zBranch = db_column_text(&q, 4);
    char *zBr;

    bag_insert(&vers, ckinId);
    db_bind_int(&q2, ":rid", ckinId);
    db_step(&q2);
    db_reset(&q2);
    if( zBranch==0 ) zBranch = "trunk";
    zBr = mprintf("%s", zBranch);
    for(i=0; zBr[i]; i++){
      if( !fossil_isalnum(zBr[i]) ) zBr[i] = '_';
    }
    printf("commit refs/heads/%s\nmark :%d\n", zBr, COMMITMARK(ckinId));
    free(zBr);
    printf("committer");
    print_person(zUser);
    printf(" %s +0000\n", zSecondsSince1970);
    if( zComment==0 ) zComment = "null comment";
    printf("data %d\n%s\n", (int)strlen(zComment), zComment);
    db_prepare(&q2, "SELECT pid FROM plink WHERE cid=%d AND isprim", ckinId);
    if( db_step(&q2) != SQLITE_ROW ){
    db_prepare(&q3, "SELECT pid FROM plink WHERE cid=%d AND isprim", ckinId);
    if( db_step(&q3) != SQLITE_ROW ){
      const char *zFromType;
      Manifest *p;
      ManifestFile *pFile;

      zFromType = "from";
      p = manifest_get(ckinId, CFTYPE_ANY);
      for(i=0; i<p->nParent; i++){
184
185
186
187
188
189
190
191

192
193
194

195
196

197
198
199
200
201
202
203


204
205

206
207

208
209
210
211
212
213
214
215
216




217
218
219
220
221
222

223
224

225
226

227
228
229
230
231
232
233
240
241
242
243
244
245
246

247
248
249

250
251

252
253
254
255
256
257


258
259
260

261
262

263
264
265
266
267
268




269
270
271
272
273
274
275
276
277

278
279

280
281
282
283
284
285
286
287
288
289
290







-
+


-
+

-
+





-
-
+
+

-
+

-
+





-
-
-
-
+
+
+
+





-
+

-
+


+







        if( fid==0 ) continue;
        if( pFile->zPerm && strstr(pFile->zPerm,"x") ) zPerm = "100755";
        if( !bag_find(&blobs, fid) ) continue;
        printf("M %s :%d %s\n", zPerm, BLOBMARK(fid), pFile->zName);
      }
      manifest_cache_insert(p);
    }else{
      Stmt q3;
      Stmt q4;
      int parent;

      parent = db_column_int(&q2, 0);
      parent = db_column_int(&q3, 0);
      printf("from :%d\n", COMMITMARK(parent));
      db_prepare(&q3,
      db_prepare(&q4,
        "SELECT pid FROM plink"
        " WHERE cid=%d AND NOT isprim"
        "   AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=pid)"
        " ORDER BY pid",
        ckinId);
      while( db_step(&q3)==SQLITE_ROW ){
        printf("merge :%d\n", COMMITMARK(db_column_int(&q3,0)));
      while( db_step(&q4)==SQLITE_ROW ){
        printf("merge :%d\n", COMMITMARK(db_column_int(&q4,0)));
      }
      db_finalize(&q3);
      db_finalize(&q4);

      db_prepare(&q3,
      db_prepare(&q4,
        "SELECT filename.name, mlink.fid, mlink.mperm FROM mlink"
        " JOIN filename ON filename.fnid=mlink.fnid"
        " WHERE mlink.mid=%d",
        parent
      );
      while( db_step(&q3)==SQLITE_ROW ){
        const char *zName = db_column_text(&q3,0);
        int zNew = db_column_int(&q3,1);
        int mPerm = db_column_int(&q3,2);
      while( db_step(&q4)==SQLITE_ROW ){
        const char *zName = db_column_text(&q4,0);
        int zNew = db_column_int(&q4,1);
        int mPerm = db_column_int(&q4,2);
        if( zNew==0)
           printf("D %s\n", zName);
        else if( bag_find(&blobs, zNew) )
           printf("M %s :%d %s\n", mPerm ? "100755" : "100644", BLOBMARK(zNew), zName);
      }
      db_finalize(&q3);
      db_finalize(&q4);
    }
    db_finalize(&q2);
    db_finalize(&q3);
    printf("\n");
  }
  db_finalize(&q2);
  db_finalize(&q);
  bag_clear(&blobs);
  manifest_cache_clear();


  /* Output tags */
  db_prepare(&q,
251
252
253
254
255
256
257
258






















308
309
310
311
312
313
314

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336







-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
    printf("from :%d\n", COMMITMARK(rid));
    printf("tagger <tagger> %s +0000\n", zSecSince1970);
    printf("data 0\n");
    fossil_free(zEncoded);
  }
  db_finalize(&q);
  bag_clear(&vers);
}

  if( markfile_out!=0 ){
    FILE *f;
    f = fopen(markfile_out, "w");
    if( f == 0 ){
      fossil_panic("cannot open %s for writing", markfile_out);
    }
    db_prepare(&q, "SELECT rid FROM oldblob");
    while( db_step(&q)==SQLITE_ROW ){
      fprintf(f, "b%d\n", db_column_int(&q, 0));
    }
    db_finalize(&q);
    db_prepare(&q, "SELECT rid FROM oldcommit");
    while( db_step(&q)==SQLITE_ROW ){
      fprintf(f, "c%d\n", db_column_int(&q, 0));
    }
    db_finalize(&q);
    if( ferror(f)!=0 || fclose(f)!=0 ) {
      fossil_panic("error while writing %s", markfile_out);
    }
  }
}