Fossil SCM

Remove the --yes option from the "revert" command. In its place, make the "revert" opration undoable.

drh 2009-12-17 21:22 trunk
Commit 7b82a73bd39b7f864d69978bd1fbfbfadb6339c7
3 files changed +3 -3 +4 -4 +44 -48
+3 -3
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -190,11 +190,11 @@
190190
*/
191191
static void diff_one_against_disk(const char *zFrom, const char *zDiffCmd){
192192
Blob fname;
193193
Blob content;
194194
file_tree_name(g.argv[2], &fname, 1);
195
- historical_version_of_file(zFrom, blob_str(&fname), &content);
195
+ historical_version_of_file(zFrom, blob_str(&fname), &content, 0);
196196
diff_file(&content, g.argv[2], g.argv[2], zDiffCmd);
197197
blob_reset(&content);
198198
blob_reset(&fname);
199199
}
200200
@@ -295,12 +295,12 @@
295295
char *zName;
296296
Blob fname;
297297
Blob v1, v2;
298298
file_tree_name(g.argv[2], &fname, 1);
299299
zName = blob_str(&fname);
300
- historical_version_of_file(zFrom, zName, &v1);
301
- historical_version_of_file(zTo, zName, &v2);
300
+ historical_version_of_file(zFrom, zName, &v1, 0);
301
+ historical_version_of_file(zTo, zName, &v2, 0);
302302
diff_file_mem(&v1, &v2, zName, zDiffCmd);
303303
blob_reset(&v1);
304304
blob_reset(&v2);
305305
blob_reset(&fname);
306306
}
307307
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -190,11 +190,11 @@
190 */
191 static void diff_one_against_disk(const char *zFrom, const char *zDiffCmd){
192 Blob fname;
193 Blob content;
194 file_tree_name(g.argv[2], &fname, 1);
195 historical_version_of_file(zFrom, blob_str(&fname), &content);
196 diff_file(&content, g.argv[2], g.argv[2], zDiffCmd);
197 blob_reset(&content);
198 blob_reset(&fname);
199 }
200
@@ -295,12 +295,12 @@
295 char *zName;
296 Blob fname;
297 Blob v1, v2;
298 file_tree_name(g.argv[2], &fname, 1);
299 zName = blob_str(&fname);
300 historical_version_of_file(zFrom, zName, &v1);
301 historical_version_of_file(zTo, zName, &v2);
302 diff_file_mem(&v1, &v2, zName, zDiffCmd);
303 blob_reset(&v1);
304 blob_reset(&v2);
305 blob_reset(&fname);
306 }
307
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -190,11 +190,11 @@
190 */
191 static void diff_one_against_disk(const char *zFrom, const char *zDiffCmd){
192 Blob fname;
193 Blob content;
194 file_tree_name(g.argv[2], &fname, 1);
195 historical_version_of_file(zFrom, blob_str(&fname), &content, 0);
196 diff_file(&content, g.argv[2], g.argv[2], zDiffCmd);
197 blob_reset(&content);
198 blob_reset(&fname);
199 }
200
@@ -295,12 +295,12 @@
295 char *zName;
296 Blob fname;
297 Blob v1, v2;
298 file_tree_name(g.argv[2], &fname, 1);
299 zName = blob_str(&fname);
300 historical_version_of_file(zFrom, zName, &v1, 0);
301 historical_version_of_file(zTo, zName, &v2, 0);
302 diff_file_mem(&v1, &v2, zName, zDiffCmd);
303 blob_reset(&v1);
304 blob_reset(&v2);
305 blob_reset(&fname);
306 }
307
+4 -4
--- src/undo.c
+++ src/undo.c
@@ -189,13 +189,13 @@
189189
/*
190190
** COMMAND: undo
191191
**
192192
** Usage: %fossil undo ?FILENAME...?
193193
**
194
-** Undo the most recent update or merge operation. If FILENAME is
194
+** Undo the most recent update or merge or revert operation. If FILENAME is
195195
** specified then restore the content of the named file(s) but otherwise
196
-** leave the update or merge in effect.
196
+** leave the update or merge or revert in effect.
197197
**
198198
** A single level of undo/redo is supported. The undo/redo stack
199199
** is cleared by the commit and checkout commands.
200200
*/
201201
void undo_cmd(void){
@@ -228,12 +228,12 @@
228228
/*
229229
** COMMAND: redo
230230
**
231231
** Usage: %fossil redo ?FILENAME...?
232232
**
233
-** Redo the an update or merge operation that has been undone by the
234
-** undo command. If FILENAME is specified then restore the changes
233
+** Redo the an update or merge or revert operation that has been undone
234
+** by the undo command. If FILENAME is specified then restore the changes
235235
** associated with the named file(s) but otherwise leave the update
236236
** or merge undone.
237237
**
238238
** A single level of undo/redo is supported. The undo/redo stack
239239
** is cleared by the commit and checkout commands.
240240
--- src/undo.c
+++ src/undo.c
@@ -189,13 +189,13 @@
189 /*
190 ** COMMAND: undo
191 **
192 ** Usage: %fossil undo ?FILENAME...?
193 **
194 ** Undo the most recent update or merge operation. If FILENAME is
195 ** specified then restore the content of the named file(s) but otherwise
196 ** leave the update or merge in effect.
197 **
198 ** A single level of undo/redo is supported. The undo/redo stack
199 ** is cleared by the commit and checkout commands.
200 */
201 void undo_cmd(void){
@@ -228,12 +228,12 @@
228 /*
229 ** COMMAND: redo
230 **
231 ** Usage: %fossil redo ?FILENAME...?
232 **
233 ** Redo the an update or merge operation that has been undone by the
234 ** undo command. If FILENAME is specified then restore the changes
235 ** associated with the named file(s) but otherwise leave the update
236 ** or merge undone.
237 **
238 ** A single level of undo/redo is supported. The undo/redo stack
239 ** is cleared by the commit and checkout commands.
240
--- src/undo.c
+++ src/undo.c
@@ -189,13 +189,13 @@
189 /*
190 ** COMMAND: undo
191 **
192 ** Usage: %fossil undo ?FILENAME...?
193 **
194 ** Undo the most recent update or merge or revert operation. If FILENAME is
195 ** specified then restore the content of the named file(s) but otherwise
196 ** leave the update or merge or revert in effect.
197 **
198 ** A single level of undo/redo is supported. The undo/redo stack
199 ** is cleared by the commit and checkout commands.
200 */
201 void undo_cmd(void){
@@ -228,12 +228,12 @@
228 /*
229 ** COMMAND: redo
230 **
231 ** Usage: %fossil redo ?FILENAME...?
232 **
233 ** Redo the an update or merge or revert operation that has been undone
234 ** by the undo command. If FILENAME is specified then restore the changes
235 ** associated with the named file(s) but otherwise leave the update
236 ** or merge undone.
237 **
238 ** A single level of undo/redo is supported. The undo/redo stack
239 ** is cleared by the commit and checkout commands.
240
+44 -48
--- src/update.c
+++ src/update.c
@@ -253,11 +253,12 @@
253253
** Get the contents of a file within a given revision.
254254
*/
255255
int historical_version_of_file(
256256
const char *revision, /* The baseline name containing the file */
257257
const char *file, /* Full treename of the file */
258
- Blob *content /* Put the content here */
258
+ Blob *content, /* Put the content here */
259
+ int errCode /* Error code if file not found. Panic if 0. */
259260
){
260261
Blob mfile;
261262
Manifest m;
262263
int i, rid=0;
263264
@@ -265,10 +266,11 @@
265266
rid = name_to_rid(revision);
266267
}else{
267268
rid = db_lget_int("checkout", 0);
268269
}
269270
if( !is_a_version(rid) ){
271
+ if( errCode>0 ) return errCode;
270272
fossil_fatal("no such check-out: %s", revision);
271273
}
272274
content_get(rid, &mfile);
273275
274276
if( manifest_parse(&m, &mfile) ){
@@ -276,82 +278,76 @@
276278
if( strcmp(m.aFile[i].zName, file)==0 ){
277279
rid = uuid_to_rid(m.aFile[i].zUuid, 0);
278280
return content_get(rid, content);
279281
}
280282
}
281
- fossil_fatal("file %s does not exist in baseline: %s", file, revision);
282
- }else{
283
+ if( errCode<=0 ){
284
+ fossil_fatal("file %s does not exist in baseline: %s", file, revision);
285
+ }
286
+ }else if( errCode<=0 ){
283287
fossil_panic("could not parse manifest for baseline: %s", revision);
284288
}
285
- return 0;
289
+ return errCode;
286290
}
287291
288292
289293
/*
290294
** COMMAND: revert
291295
**
292
-** Usage: %fossil revert ?--yes? ?-r REVISION? FILE ...
296
+** Usage: %fossil revert ?-r REVISION? FILE ...
293297
**
294298
** Revert to the current repository version of FILE, or to
295299
** the version associated with baseline REVISION if the -r flag
296
-** appears. This command will confirm your operation unless the
297
-** file is missing or the --yes option is used.
298
-**/
300
+** appears.
301
+**
302
+** If a file is reverted accidently, it can be restored using
303
+** the "fossil undo" command.
304
+*/
299305
void revert_cmd(void){
300306
char *zFile;
301307
const char *zRevision;
302308
Blob fname;
303309
Blob record;
304
- Blob ans;
305310
int i;
311
+ int errCode;
306312
int rid = 0;
307
- int yesFlag;
308
- int yesRevert;
309313
310
- yesFlag = find_option("yes", "y", 0)!=0;
311314
zRevision = find_option("revision", "r", 1);
312315
verify_all_options();
313316
314317
if( g.argc<3 ){
315
- usage("?OPTIONS FILE ...");
318
+ usage("?OPTIONS? FILE ...");
316319
}
317320
db_must_be_within_tree();
321
+ db_begin_transaction();
322
+ undo_begin();
318323
319324
for(i=2; i<g.argc; i++){
320325
zFile = mprintf("%/", g.argv[i]);
321326
file_tree_name(zFile, &fname, 1);
322
- yesRevert = yesFlag;
323
- if( !yesRevert && access(zFile, 0) ) yesRevert = 1;
324
- if( yesRevert==0 ){
325
- char *prompt = mprintf("revert file %B? this will"
326
- " destroy local changes (y/N)? ",
327
- &fname);
328
- blob_zero(&ans);
329
- prompt_user(prompt, &ans);
330
- free( prompt );
331
- if( blob_str(&ans)[0]=='y' ){
332
- yesRevert = 1;
333
- }
334
- blob_reset(&ans);
335
- }
336
-
337
- if( yesRevert==1 && zRevision!=0 ){
338
- historical_version_of_file(zRevision, zFile, &record);
339
- }else if( yesRevert==1 ){
340
- rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
341
- if( rid==0 ){
342
- fossil_panic("no history for file: %b", &fname);
343
- }
344
- content_get(rid, &record);
345
- }
346
-
347
- if( yesRevert==1 ){
348
- blob_write_to_file(&record, zFile);
349
- printf("%s reverted\n", zFile);
350
- blob_reset(&record);
351
- blob_reset(&fname);
352
- }else{
353
- printf("revert canceled\n");
354
- }
355
- free(zFile);
356
- }
327
+
328
+ if( zRevision!=0 ){
329
+ errCode = historical_version_of_file(zRevision, blob_str(&fname),
330
+ &record, 2);
331
+ }else{
332
+ rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
333
+ if( rid==0 ){
334
+ errCode = 2;
335
+ }else{
336
+ content_get(rid, &record);
337
+ errCode = 0;
338
+ }
339
+ }
340
+
341
+ if( errCode==2 ){
342
+ fossil_warning("file not in repository: %s", zFile);
343
+ }else{
344
+ undo_save(blob_str(&fname));
345
+ blob_write_to_file(&record, zFile);
346
+ printf("%s reverted\n", zFile);
347
+ }
348
+ blob_reset(&record);
349
+ blob_reset(&fname);
350
+ free(zFile);
351
+ }
352
+ db_end_transaction(0);
357353
}
358354
--- src/update.c
+++ src/update.c
@@ -253,11 +253,12 @@
253 ** Get the contents of a file within a given revision.
254 */
255 int historical_version_of_file(
256 const char *revision, /* The baseline name containing the file */
257 const char *file, /* Full treename of the file */
258 Blob *content /* Put the content here */
 
259 ){
260 Blob mfile;
261 Manifest m;
262 int i, rid=0;
263
@@ -265,10 +266,11 @@
265 rid = name_to_rid(revision);
266 }else{
267 rid = db_lget_int("checkout", 0);
268 }
269 if( !is_a_version(rid) ){
 
270 fossil_fatal("no such check-out: %s", revision);
271 }
272 content_get(rid, &mfile);
273
274 if( manifest_parse(&m, &mfile) ){
@@ -276,82 +278,76 @@
276 if( strcmp(m.aFile[i].zName, file)==0 ){
277 rid = uuid_to_rid(m.aFile[i].zUuid, 0);
278 return content_get(rid, content);
279 }
280 }
281 fossil_fatal("file %s does not exist in baseline: %s", file, revision);
282 }else{
 
 
283 fossil_panic("could not parse manifest for baseline: %s", revision);
284 }
285 return 0;
286 }
287
288
289 /*
290 ** COMMAND: revert
291 **
292 ** Usage: %fossil revert ?--yes? ?-r REVISION? FILE ...
293 **
294 ** Revert to the current repository version of FILE, or to
295 ** the version associated with baseline REVISION if the -r flag
296 ** appears. This command will confirm your operation unless the
297 ** file is missing or the --yes option is used.
298 **/
 
 
299 void revert_cmd(void){
300 char *zFile;
301 const char *zRevision;
302 Blob fname;
303 Blob record;
304 Blob ans;
305 int i;
 
306 int rid = 0;
307 int yesFlag;
308 int yesRevert;
309
310 yesFlag = find_option("yes", "y", 0)!=0;
311 zRevision = find_option("revision", "r", 1);
312 verify_all_options();
313
314 if( g.argc<3 ){
315 usage("?OPTIONS FILE ...");
316 }
317 db_must_be_within_tree();
 
 
318
319 for(i=2; i<g.argc; i++){
320 zFile = mprintf("%/", g.argv[i]);
321 file_tree_name(zFile, &fname, 1);
322 yesRevert = yesFlag;
323 if( !yesRevert && access(zFile, 0) ) yesRevert = 1;
324 if( yesRevert==0 ){
325 char *prompt = mprintf("revert file %B? this will"
326 " destroy local changes (y/N)? ",
327 &fname);
328 blob_zero(&ans);
329 prompt_user(prompt, &ans);
330 free( prompt );
331 if( blob_str(&ans)[0]=='y' ){
332 yesRevert = 1;
333 }
334 blob_reset(&ans);
335 }
336
337 if( yesRevert==1 && zRevision!=0 ){
338 historical_version_of_file(zRevision, zFile, &record);
339 }else if( yesRevert==1 ){
340 rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
341 if( rid==0 ){
342 fossil_panic("no history for file: %b", &fname);
343 }
344 content_get(rid, &record);
345 }
346
347 if( yesRevert==1 ){
348 blob_write_to_file(&record, zFile);
349 printf("%s reverted\n", zFile);
350 blob_reset(&record);
351 blob_reset(&fname);
352 }else{
353 printf("revert canceled\n");
354 }
355 free(zFile);
356 }
357 }
358
--- src/update.c
+++ src/update.c
@@ -253,11 +253,12 @@
253 ** Get the contents of a file within a given revision.
254 */
255 int historical_version_of_file(
256 const char *revision, /* The baseline name containing the file */
257 const char *file, /* Full treename of the file */
258 Blob *content, /* Put the content here */
259 int errCode /* Error code if file not found. Panic if 0. */
260 ){
261 Blob mfile;
262 Manifest m;
263 int i, rid=0;
264
@@ -265,10 +266,11 @@
266 rid = name_to_rid(revision);
267 }else{
268 rid = db_lget_int("checkout", 0);
269 }
270 if( !is_a_version(rid) ){
271 if( errCode>0 ) return errCode;
272 fossil_fatal("no such check-out: %s", revision);
273 }
274 content_get(rid, &mfile);
275
276 if( manifest_parse(&m, &mfile) ){
@@ -276,82 +278,76 @@
278 if( strcmp(m.aFile[i].zName, file)==0 ){
279 rid = uuid_to_rid(m.aFile[i].zUuid, 0);
280 return content_get(rid, content);
281 }
282 }
283 if( errCode<=0 ){
284 fossil_fatal("file %s does not exist in baseline: %s", file, revision);
285 }
286 }else if( errCode<=0 ){
287 fossil_panic("could not parse manifest for baseline: %s", revision);
288 }
289 return errCode;
290 }
291
292
293 /*
294 ** COMMAND: revert
295 **
296 ** Usage: %fossil revert ?-r REVISION? FILE ...
297 **
298 ** Revert to the current repository version of FILE, or to
299 ** the version associated with baseline REVISION if the -r flag
300 ** appears.
301 **
302 ** If a file is reverted accidently, it can be restored using
303 ** the "fossil undo" command.
304 */
305 void revert_cmd(void){
306 char *zFile;
307 const char *zRevision;
308 Blob fname;
309 Blob record;
 
310 int i;
311 int errCode;
312 int rid = 0;
 
 
313
 
314 zRevision = find_option("revision", "r", 1);
315 verify_all_options();
316
317 if( g.argc<3 ){
318 usage("?OPTIONS? FILE ...");
319 }
320 db_must_be_within_tree();
321 db_begin_transaction();
322 undo_begin();
323
324 for(i=2; i<g.argc; i++){
325 zFile = mprintf("%/", g.argv[i]);
326 file_tree_name(zFile, &fname, 1);
327
328 if( zRevision!=0 ){
329 errCode = historical_version_of_file(zRevision, blob_str(&fname),
330 &record, 2);
331 }else{
332 rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
333 if( rid==0 ){
334 errCode = 2;
335 }else{
336 content_get(rid, &record);
337 errCode = 0;
338 }
339 }
340
341 if( errCode==2 ){
342 fossil_warning("file not in repository: %s", zFile);
343 }else{
344 undo_save(blob_str(&fname));
345 blob_write_to_file(&record, zFile);
346 printf("%s reverted\n", zFile);
347 }
348 blob_reset(&record);
349 blob_reset(&fname);
350 free(zFile);
351 }
352 db_end_transaction(0);
 
 
 
 
 
 
 
 
 
353 }
354

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button