1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 230 231 232 233 234 235 236 237 238 239 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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/*
** Copyright (c) 2007 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
** drh@hwaci.com
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file implements the undo/redo functionality.
*/
#include "config.h"
#include "undo.h"
/*
** Undo the change to the file zPathname. zPathname is the pathname
** of the file relative to the root of the repository. If redoFlag is
** true the redo a change. If there is nothing to undo (or redo) then
** this routine is a noop.
*/
static void undo_one(const char *zPathname, int redoFlag){
Stmt q;
char *zFullname;
db_prepare(&q,
"SELECT content, existsflag, isExe, isLink FROM undo"
" WHERE pathname=%Q AND redoflag=%d",
zPathname, redoFlag
);
if( db_step(&q)==SQLITE_ROW ){
int old_exists;
int new_exists;
int old_exe;
int new_exe;
int new_link;
int old_link;
Blob current;
Blob new;
zFullname = mprintf("%s/%s", g.zLocalRoot, zPathname);
old_link = db_column_int(&q, 3);
new_link = file_wd_islink(zFullname);
new_exists = file_wd_size(zFullname)>=0;
if( new_exists ){
if( new_link ){
blob_read_link(¤t, zFullname);
}else{
blob_read_from_file(¤t, zFullname);
}
new_exe = file_wd_isexe(zFullname);
}else{
blob_zero(¤t);
new_exe = 0;
}
blob_zero(&new);
old_exists = db_column_int(&q, 1);
old_exe = db_column_int(&q, 2);
if( old_exists ){
db_ephemeral_blob(&q, 0, &new);
}
if( old_exists ){
if( new_exists ){
fossil_print("%s %s\n", redoFlag ? "REDO" : "UNDO", zPathname);
}else{
fossil_print("NEW %s\n", zPathname);
}
if( new_exists && (new_link || old_link) ){
file_delete(zFullname);
}
if( old_link ){
symlink_create(blob_str(&new), zFullname);
}else{
blob_write_to_file(&new, zFullname);
}
file_wd_setexe(zFullname, old_exe);
}else{
fossil_print("DELETE %s\n", zPathname);
file_delete(zFullname);
}
blob_reset(&new);
free(zFullname);
db_finalize(&q);
db_prepare(&q,
"UPDATE undo SET content=:c, existsflag=%d, isExe=%d, isLink=%d,"
" redoflag=NOT redoflag"
" WHERE pathname=%Q",
new_exists, new_exe, new_link, zPathname
);
if( new_exists ){
db_bind_blob(&q, ":c", ¤t);
}
db_step(&q);
blob_reset(¤t);
}
db_finalize(&q);
}
/*
** Undo or redo changes to the filesystem. Undo the changes in the
** same order that they were originally carried out - undo the oldest
** change first and undo the most recent change last.
*/
static void undo_all_filesystem(int redoFlag){
Stmt q;
db_prepare(&q,
"SELECT pathname FROM undo"
" WHERE redoflag=%d"
" ORDER BY rowid",
redoFlag
);
while( db_step(&q)==SQLITE_ROW ){
const char *zPathname = db_column_text(&q, 0);
undo_one(zPathname, redoFlag);
}
db_finalize(&q);
}
/*
** Undo or redo all undoable or redoable changes.
*/
static void undo_all(int redoFlag){
int ucid;
int ncid;
const char *zDb = db_name("localdb");
undo_all_filesystem(redoFlag);
db_multi_exec(
"CREATE TEMP TABLE undo_vfile_2 AS SELECT * FROM vfile;"
"DELETE FROM vfile;"
"INSERT INTO vfile SELECT * FROM undo_vfile;"
"DELETE FROM undo_vfile;"
"INSERT INTO undo_vfile SELECT * FROM undo_vfile_2;"
"DROP TABLE undo_vfile_2;"
"CREATE TEMP TABLE undo_vmerge_2 AS SELECT * FROM vmerge;"
"DELETE FROM vmerge;"
"INSERT INTO vmerge SELECT * FROM undo_vmerge;"
"DELETE FROM undo_vmerge;"
"INSERT INTO undo_vmerge SELECT * FROM undo_vmerge_2;"
"DROP TABLE undo_vmerge_2;"
);
if(db_exists("SELECT 1 FROM %s.sqlite_master WHERE name='undo_stash'", zDb) ){
if( redoFlag ){
db_multi_exec(
"DELETE FROM stash WHERE stashid IN (SELECT stashid FROM undo_stash);"
"DELETE FROM stashfile"
" WHERE stashid NOT IN (SELECT stashid FROM stash);"
);
}else{
db_multi_exec(
"INSERT OR IGNORE INTO stash SELECT * FROM undo_stash;"
"INSERT OR IGNORE INTO stashfile SELECT * FROM undo_stashfile;"
);
}
}
ncid = db_lget_int("undo_checkout", 0);
ucid = db_lget_int("checkout", 0);
db_lset_int("undo_checkout", ucid);
db_lset_int("checkout", ncid);
}
/*
** Reset the undo memory.
*/
void undo_reset(void){
static const char zSql[] =
@ DROP TABLE IF EXISTS undo;
@ DROP TABLE IF EXISTS undo_vfile;
@ DROP TABLE IF EXISTS undo_vmerge;
@ DROP TABLE IF EXISTS undo_stash;
@ DROP TABLE IF EXISTS undo_stashfile;
;
db_multi_exec(zSql);
db_lset_int("undo_available", 0);
db_lset_int("undo_checkout", 0);
}
/*
** The following variable stores the original command-line of the
** command that is a candidate to be undone.
*/
static char *undoCmd = 0;
/*
** This flag is true if we are in the process of collecting file changes
** for undo. When this flag is false, undo_save() is a no-op.
**
** The undoDisable flag, if set, prevents undo from being activated.
*/
static int undoActive = 0;
static int undoDisable = 0;
/*
** Capture the current command-line and store it as part of the undo
** state. This routine is called before options are extracted from the
** command-line so that we can record the complete command-line.
*/
void undo_capture_command_line(void){
Blob cmdline;
int i;
if( undoCmd!=0 || undoDisable ) return;
blob_zero(&cmdline);
for(i=1; i<g.argc; i++){
if( i>1 ) blob_append(&cmdline, " ", 1);
blob_append(&cmdline, g.argv[i], -1);
}
undoCmd = blob_str(&cmdline);
}
/*
** Begin capturing a snapshot that can be undone.
*/
void undo_begin(void){
int cid;
const char *zDb = db_name("localdb");
static const char zSql[] =
@ CREATE TABLE %s.undo(
@ pathname TEXT UNIQUE, -- Name of the file
@ redoflag BOOLEAN, -- 0 for undoable. 1 for redoable
@ existsflag BOOLEAN, -- True if the file exists
@ isExe BOOLEAN, -- True if the file is executable
@ isLink BOOLEAN, -- True if the file is symlink
@ content BLOB -- Saved content
@ );
@ CREATE TABLE %s.undo_vfile AS SELECT * FROM vfile;
@ CREATE TABLE %s.undo_vmerge AS SELECT * FROM vmerge;
;
if( undoDisable ) return;
undo_reset();
db_multi_exec(zSql, zDb, zDb, zDb);
cid = db_lget_int("checkout", 0);
db_lset_int("undo_checkout", cid);
db_lset_int("undo_available", 1);
db_lset("undo_cmdline", undoCmd);
undoActive = 1;
}
/*
** Permanently disable undo
*/
void undo_disable(void){
undoDisable = 1;
}
/*
** This flag is true if one or more files have changed and have been
** recorded in the undo log but the undo log has not yet been committed.
**
** If a fatal error occurs and this flag is set, that means we should
** rollback all the filesystem changes.
*/
static int undoNeedRollback = 0;
/*
** Save the current content of the file zPathname so that it
** will be undoable. The name is relative to the root of the
** tree.
*/
void undo_save(const char *zPathname){
char *zFullname;
Blob content;
int existsFlag;
int isLink;
Stmt q;
if( !undoActive ) return;
zFullname = mprintf("%s%s", g.zLocalRoot, zPathname);
existsFlag = file_wd_size(zFullname)>=0;
isLink = file_wd_islink(zFullname);
db_prepare(&q,
"INSERT OR IGNORE INTO"
" undo(pathname,redoflag,existsflag,isExe,isLink,content)"
" VALUES(%Q,0,%d,%d,%d,:c)",
zPathname, existsFlag, file_wd_isexe(zFullname), isLink
);
if( existsFlag ){
if( isLink ){
blob_read_link(&content, zFullname);
}else{
blob_read_from_file(&content, zFullname);
}
db_bind_blob(&q, ":c", &content);
}
free(zFullname);
db_step(&q);
db_finalize(&q);
if( existsFlag ){
blob_reset(&content);
}
undoNeedRollback = 1;
}
/*
** Make the current state of stashid undoable.
*/
void undo_save_stash(int stashid){
const char *zDb = db_name("localdb");
db_multi_exec(
"DROP TABLE IF EXISTS undo_stash;"
"CREATE TABLE %s.undo_stash AS"
" SELECT * FROM stash WHERE stashid=%d;",
zDb, stashid
);
db_multi_exec(
"DROP TABLE IF EXISTS undo_stashfile;"
"CREATE TABLE %s.undo_stashfile AS"
" SELECT * FROM stashfile WHERE stashid=%d;",
zDb, stashid
);
}
/*
** Complete the undo process is one is currently in process.
*/
void undo_finish(void){
if( undoActive ){
if( undoNeedRollback ){
fossil_print("\"fossil undo\" is available to undo changes"
" to the working checkout.\n");
}
undoActive = 0;
undoNeedRollback = 0;
}
}
/*
** This routine is called when the process aborts due to an error.
** If an undo was being accumulated but was not finished, attempt
** to rollback all of the filesystem changes.
**
** This rollback occurs, for example, if an "update" or "merge" operation
** could not run to completion because a file that needed to be written
** was locked or had permissions turned off.
*/
void undo_rollback(void){
if( !undoNeedRollback ) return;
assert( undoActive );
undoNeedRollback = 0;
undoActive = 0;
fossil_print("Rolling back prior filesystem changes...\n");
undo_all_filesystem(0);
}
/*
** COMMAND: undo
** COMMAND: redo*
**
** Usage: %fossil undo ?OPTIONS? ?FILENAME...?
** or: %fossil redo ?OPTIONS? ?FILENAME...?
**
** Undo the changes to the working checkout caused by the most recent
** of the following operations:
**
** (1) fossil update (5) fossil stash apply
** (2) fossil merge (6) fossil stash drop
** (3) fossil revert (7) fossil stash goto
** (4) fossil stash pop
**
** If FILENAME is specified then restore the content of the named
** file(s) but otherwise leave the update or merge or revert in effect.
** The redo command undoes the effect of the most recent undo.
**
** If the --explain option is present, no changes are made and instead
** the undo or redo command explains what actions the undo or redo would
** have done had the --explain been omitted.
**
** A single level of undo/redo is supported. The undo/redo stack
** is cleared by the commit and checkout commands.
**
** Options:
** --explain do not make changes but show what would be done
**
** See also: commit, status
*/
void undo_cmd(void){
int isRedo = g.argv[1][0]=='r';
int undo_available;
int explainFlag = find_option("explain", 0, 0)!=0;
const char *zCmd = isRedo ? "redo" : "undo";
db_must_be_within_tree();
verify_all_options();
db_begin_transaction();
undo_available = db_lget_int("undo_available", 0);
if( explainFlag ){
if( undo_available==0 ){
fossil_print("No undo or redo is available\n");
}else{
Stmt q;
int nChng = 0;
zCmd = undo_available==1 ? "undo" : "redo";
fossil_print("A %s is available for the following command:\n\n"
" %s %s\n\n",
zCmd, g.argv[0], db_lget("undo_cmdline", "???"));
db_prepare(&q,
"SELECT existsflag, pathname FROM undo ORDER BY pathname"
);
while( db_step(&q)==SQLITE_ROW ){
if( nChng==0 ){
fossil_print("The following file changes would occur if the "
"command above is %sne:\n\n", zCmd);
}
nChng++;
fossil_print("%s %s\n",
db_column_int(&q,0) ? "UPDATE" : "DELETE",
db_column_text(&q, 1)
);
}
db_finalize(&q);
if( nChng==0 ){
fossil_print("No file changes would occur with this undo/redo.\n");
}
}
}else{
int vid1 = db_lget_int("checkout", 0);
int vid2;
if( g.argc==2 ){
if( undo_available!=(1+isRedo) ){
fossil_fatal("nothing to %s", zCmd);
}
undo_all(isRedo);
db_lset_int("undo_available", 2-isRedo);
}else if( g.argc>=3 ){
int i;
if( undo_available==0 ){
fossil_fatal("nothing to %s", zCmd);
}
for(i=2; i<g.argc; i++){
const char *zFile = g.argv[i];
Blob path;
file_tree_name(zFile, &path, 1);
undo_one(blob_str(&path), isRedo);
blob_reset(&path);
}
}
vid2 = db_lget_int("checkout", 0);
if( vid1!=vid2 ){
fossil_print("--------------------\n");
show_common_info(vid2, "updated-to:", 1, 0);
}
}
db_end_transaction(0);
}
|