dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Copyright (c) 2007 D. Richard Hipp dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This program is free software; you can redistribute it and/or dbda8d6ce9 2007-07-21 drh: ** modify it under the terms of the GNU General Public dbda8d6ce9 2007-07-21 drh: ** License version 2 as published by the Free Software Foundation. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This program is distributed in the hope that it will be useful, dbda8d6ce9 2007-07-21 drh: ** but WITHOUT ANY WARRANTY; without even the implied warranty of dbda8d6ce9 2007-07-21 drh: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU dbda8d6ce9 2007-07-21 drh: ** General Public License for more details. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** You should have received a copy of the GNU General Public dbda8d6ce9 2007-07-21 drh: ** License along with this library; if not, write to the dbda8d6ce9 2007-07-21 drh: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, dbda8d6ce9 2007-07-21 drh: ** Boston, MA 02111-1307, USA. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** Author contact information: dbda8d6ce9 2007-07-21 drh: ** drh@hwaci.com dbda8d6ce9 2007-07-21 drh: ** http://www.hwaci.com/drh/ dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ******************************************************************************* dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This file contains code used to check-out versions of the project dbda8d6ce9 2007-07-21 drh: ** from the local repository. dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: #include "config.h" dbda8d6ce9 2007-07-21 drh: #include "checkout.h" dbda8d6ce9 2007-07-21 drh: #include <assert.h> dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Check to see if there is an existing checkout that has been dbda8d6ce9 2007-07-21 drh: ** modified. Return values: dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** 0: There is an existing checkout but it is unmodified dbda8d6ce9 2007-07-21 drh: ** 1: There is a modified checkout - there are unsaved changes dbda8d6ce9 2007-07-21 drh: ** 2: There is no existing checkout dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: int unsaved_changes(void){ dbda8d6ce9 2007-07-21 drh: int vid; dbda8d6ce9 2007-07-21 drh: db_must_be_within_tree(); dbda8d6ce9 2007-07-21 drh: vid = db_lget_int("checkout",0); dbda8d6ce9 2007-07-21 drh: if( vid==0 ) return 2; dbda8d6ce9 2007-07-21 drh: vfile_check_signature(vid); dbda8d6ce9 2007-07-21 drh: return db_exists("SELECT 1 FROM vfile WHERE chnged"); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Undo the current check-out. Unlink all files from the disk. dbda8d6ce9 2007-07-21 drh: ** Clear the VFILE table. dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: void uncheckout(int vid){ dbda8d6ce9 2007-07-21 drh: if( vid==0 ) return; dbda8d6ce9 2007-07-21 drh: vfile_unlink(vid); dbda8d6ce9 2007-07-21 drh: db_multi_exec("DELETE FROM vfile WHERE vid=%d", vid); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Given the abbreviated UUID name of a version, load the content of that dbda8d6ce9 2007-07-21 drh: ** version in the VFILE table. Return the VID for the version. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** If anything goes wrong, panic. dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: int load_vfile(const char *zName){ dbda8d6ce9 2007-07-21 drh: Blob uuid; dbda8d6ce9 2007-07-21 drh: int vid; dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: blob_init(&uuid, zName, -1); dbda8d6ce9 2007-07-21 drh: if( name_to_uuid(&uuid, 1) ){ dbda8d6ce9 2007-07-21 drh: fossil_panic(g.zErrMsg); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: vid = db_int(0, "SELECT rid FROM blob WHERE uuid=%B", &uuid); dbda8d6ce9 2007-07-21 drh: if( vid==0 ){ dbda8d6ce9 2007-07-21 drh: fossil_panic("no such version: %s", g.argv[2]); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: load_vfile_from_rid(vid); dbda8d6ce9 2007-07-21 drh: return vid; dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Load a vfile from a record ID. dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: void load_vfile_from_rid(int vid){ dbda8d6ce9 2007-07-21 drh: Blob manifest; dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: if( db_exists("SELECT 1 FROM vfile WHERE vid=%d", vid) ){ dbda8d6ce9 2007-07-21 drh: return; dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: content_get(vid, &manifest); dbda8d6ce9 2007-07-21 drh: vfile_build(vid, &manifest); dbda8d6ce9 2007-07-21 drh: blob_reset(&manifest); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: /* a040ae6e17 2007-08-08 drh: ** Read the manifest file given by vid out of the repository a040ae6e17 2007-08-08 drh: ** and store it in the root of the local check-out. a040ae6e17 2007-08-08 drh: */ a040ae6e17 2007-08-08 drh: void manifest_to_disk(int vid){ a040ae6e17 2007-08-08 drh: char *zManFile; a040ae6e17 2007-08-08 drh: Blob manifest; 95e17f4e3f 2007-08-25 drh: Blob hash; a040ae6e17 2007-08-08 drh: a040ae6e17 2007-08-08 drh: blob_zero(&manifest); a040ae6e17 2007-08-08 drh: zManFile = mprintf("%smanifest", g.zLocalRoot); a040ae6e17 2007-08-08 drh: content_get(vid, &manifest); a040ae6e17 2007-08-08 drh: blob_write_to_file(&manifest, zManFile); a040ae6e17 2007-08-08 drh: free(zManFile); 95e17f4e3f 2007-08-25 drh: blob_zero(&hash); 95e17f4e3f 2007-08-25 drh: sha1sum_blob(&manifest, &hash); a040ae6e17 2007-08-08 drh: blob_reset(&manifest); 95e17f4e3f 2007-08-25 drh: zManFile = mprintf("%smanifest.uuid", g.zLocalRoot); 95e17f4e3f 2007-08-25 drh: blob_append(&hash, "\n", 1); 95e17f4e3f 2007-08-25 drh: blob_write_to_file(&hash, zManFile); 95e17f4e3f 2007-08-25 drh: free(zManFile); 95e17f4e3f 2007-08-25 drh: blob_reset(&hash); a040ae6e17 2007-08-08 drh: } a040ae6e17 2007-08-08 drh: a040ae6e17 2007-08-08 drh: /* dbda8d6ce9 2007-07-21 drh: ** COMMAND: checkout dbda8d6ce9 2007-07-21 drh: ** c9fdb846fb 2007-08-18 drh: ** Usage: %fossil checkout VERSION ?-f|--force? c9fdb846fb 2007-08-18 drh: ** Check out a version specified on the command-line. This command c9fdb846fb 2007-08-18 drh: ** will not overwrite edited files in the current checkout unless c9fdb846fb 2007-08-18 drh: ** the --force option appears on the command-line. c9fdb846fb 2007-08-18 drh: ** c9fdb846fb 2007-08-18 drh: ** See also the "update" command. dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: void checkout_cmd(void){ dbda8d6ce9 2007-07-21 drh: int forceFlag; dbda8d6ce9 2007-07-21 drh: int noWrite; dbda8d6ce9 2007-07-21 drh: int vid, prior; 3945057916 2007-08-01 drh: Blob cksum1, cksum1b, cksum2; dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: db_must_be_within_tree(); dbda8d6ce9 2007-07-21 drh: db_begin_transaction(); dbda8d6ce9 2007-07-21 drh: forceFlag = find_option("force","f",0)!=0; dbda8d6ce9 2007-07-21 drh: noWrite = find_option("dontwrite",0,0)!=0; dbda8d6ce9 2007-07-21 drh: if( g.argc!=3 ) usage("?--force? VERSION"); dbda8d6ce9 2007-07-21 drh: if( !forceFlag && unsaved_changes()==1 ){ 4452576730 2007-08-11 drh: fossil_fatal("there are unsaved changes in the current checkout"); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: if( forceFlag ){ dbda8d6ce9 2007-07-21 drh: db_multi_exec("DELETE FROM vfile"); dbda8d6ce9 2007-07-21 drh: prior = 0; dbda8d6ce9 2007-07-21 drh: }else{ dbda8d6ce9 2007-07-21 drh: prior = db_lget_int("checkout",0); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: vid = load_vfile(g.argv[2]); dbda8d6ce9 2007-07-21 drh: if( prior==vid ){ dbda8d6ce9 2007-07-21 drh: return; dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: if( !noWrite ){ dbda8d6ce9 2007-07-21 drh: uncheckout(prior); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid); dbda8d6ce9 2007-07-21 drh: if( !noWrite ){ dbda8d6ce9 2007-07-21 drh: vfile_to_disk(vid, 0, 1); a040ae6e17 2007-08-08 drh: manifest_to_disk(vid); dbda8d6ce9 2007-07-21 drh: db_lset_int("checkout", vid); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: db_multi_exec("DELETE FROM vmerge"); 3945057916 2007-08-01 drh: vfile_aggregate_checksum_manifest(vid, &cksum1, &cksum1b); 3945057916 2007-08-01 drh: vfile_aggregate_checksum_disk(vid, &cksum2); 3945057916 2007-08-01 drh: if( blob_compare(&cksum1, &cksum2) ){ 3945057916 2007-08-01 drh: printf("WARNING: manifest checksum does not agree with disk\n"); 3945057916 2007-08-01 drh: } 3945057916 2007-08-01 drh: if( blob_compare(&cksum1, &cksum1b) ){ 3945057916 2007-08-01 drh: printf("WARNING: manifest checksum does not agree with manifest\n"); 3945057916 2007-08-01 drh: } dbda8d6ce9 2007-07-21 drh: db_end_transaction(0); 4452576730 2007-08-11 drh: } 4452576730 2007-08-11 drh: 4452576730 2007-08-11 drh: /* 4452576730 2007-08-11 drh: ** COMMAND: close 4452576730 2007-08-11 drh: ** c9fdb846fb 2007-08-18 drh: ** Usage: %fossil close ?-f|--force? 4452576730 2007-08-11 drh: ** The opposite of "open". Close the current database connection. 4452576730 2007-08-11 drh: ** Require a -f or --force flag if there are unsaved changed in the 4452576730 2007-08-11 drh: ** current check-out. 4452576730 2007-08-11 drh: */ 4452576730 2007-08-11 drh: void close_cmd(void){ 4452576730 2007-08-11 drh: int forceFlag = find_option("force","f",0)!=0; 4452576730 2007-08-11 drh: db_must_be_within_tree(); 4452576730 2007-08-11 drh: if( !forceFlag && unsaved_changes()==1 ){ 4452576730 2007-08-11 drh: fossil_fatal("there are unsaved changes in the current checkout"); 4452576730 2007-08-11 drh: } 4452576730 2007-08-11 drh: db_close(); 4452576730 2007-08-11 drh: unlink(mprintf("%s_FOSSIL_", g.zLocalRoot)); 4452576730 2007-08-11 drh: unlink(mprintf("%s_FOSSIL_-journal", g.zLocalRoot)); dbda8d6ce9 2007-07-21 drh: }