Fossil SCM

Enhancements to "fossil patch". The -f flag now causes a revert prior to applying the patch. The -f, -v and -n flags work with "patch push".

drh 2021-08-19 19:21 trunk
Commit 136d2004084130180da22cf6368c50fbf1b785dca6f554869fd21d0455d5f97a
1 file changed +30 -6
+30 -6
--- src/patch.c
+++ src/patch.c
@@ -372,14 +372,28 @@
372372
*/
373373
void patch_apply(unsigned mFlags){
374374
Stmt q;
375375
Blob cmd;
376376
377
- if( (mFlags & PATCH_FORCE)==0 && unsaved_changes(0) ){
378
- fossil_fatal("there are unsaved changes in the current checkout");
379
- }
380377
blob_init(&cmd, 0, 0);
378
+ if( unsaved_changes(0) ){
379
+ if( (mFlags & PATCH_FORCE)==0 ){
380
+ fossil_fatal("there are unsaved changes in the current checkout");
381
+ }else{
382
+ blob_appendf(&cmd, "%$ revert", g.nameOfExe);
383
+ if( mFlags & PATCH_DRYRUN ){
384
+ fossil_print("%s\n", blob_str(&cmd));
385
+ }else{
386
+ int rc = fossil_system(blob_str(&cmd));
387
+ if( rc ){
388
+ fossil_fatal("unable to revert preexisting changes: %s",
389
+ blob_str(&cmd));
390
+ }
391
+ }
392
+ blob_reset(&cmd);
393
+ }
394
+ }
381395
file_chdir(g.zLocalRoot, 0);
382396
db_prepare(&q,
383397
"SELECT patch.cfg.value"
384398
" FROM patch.cfg, localdb.vvar"
385399
" WHERE patch.cfg.key='baseline'"
@@ -653,11 +667,18 @@
653667
){
654668
char *zRemote;
655669
char *zDir;
656670
Blob cmd;
657671
FILE *f;
658
- const char *zForce = (mFlags & PATCH_FORCE)!=0 ? " -f" : "";
672
+ Blob flgs;
673
+ char *zForce;
674
+
675
+ blob_init(&flgs, 0, 0);
676
+ if( mFlags & PATCH_FORCE ) blob_appendf(&flgs, " -f");
677
+ if( mFlags & PATCH_VERBOSE ) blob_appendf(&flgs, " -v");
678
+ if( mFlags & PATCH_DRYRUN ) blob_appendf(&flgs, " -n");
679
+ zForce = blob_size(&flgs)>0 ? blob_str(&flgs) : "";
659680
if( g.argc!=4 ){
660681
usage(mprintf("%s [USER@]HOST:DIRECTORY", zThisCmd));
661682
}
662683
zRemote = fossil_strdup(g.argv[3]);
663684
zDir = (char*)file_skip_userhost(zRemote);
@@ -685,10 +706,11 @@
685706
f = popen(blob_str(&cmd), zRW);
686707
if( f==0 ){
687708
fossil_fatal("cannot run command: %s", blob_str(&cmd));
688709
}
689710
blob_reset(&cmd);
711
+ blob_reset(&flgs);
690712
return f;
691713
}
692714
693715
/*
694716
** Show a diff for the patch currently loaded into database "patch".
@@ -839,11 +861,12 @@
839861
**
840862
** Apply the changes in FILENAME to the check-out at DIRECTORY, or
841863
** in the current directory if DIRECTORY is omitted. Options:
842864
**
843865
** -f|--force Apply the patch even though there are unsaved
844
-** changes in the current check-out.
866
+** changes in the current check-out. Unsaved changes
867
+** are reverted and permanently lost.
845868
** -n|--dryrun Do nothing, but print what would have happened.
846869
** -v|--verbose Extra output explaining what happens.
847870
**
848871
** > fossil patch diff [DIRECTORY] FILENAME
849872
**
@@ -871,11 +894,12 @@
871894
**
872895
** Create a patch on a remote check-out, transfer that patch to the
873896
** local machine (using ssh) and apply the patch in the local checkout.
874897
**
875898
** -f|--force Apply the patch even though there are unsaved
876
-** changes in the current check-out.
899
+** changes in the current check-out. Unsaved changes
900
+** will be reverted and then the patch is applied.
877901
** -n|--dryrun Do nothing, but print what would have happened.
878902
** -v|--verbose Extra output explaining what happens.
879903
**
880904
** > fossil patch view FILENAME
881905
**
882906
--- src/patch.c
+++ src/patch.c
@@ -372,14 +372,28 @@
372 */
373 void patch_apply(unsigned mFlags){
374 Stmt q;
375 Blob cmd;
376
377 if( (mFlags & PATCH_FORCE)==0 && unsaved_changes(0) ){
378 fossil_fatal("there are unsaved changes in the current checkout");
379 }
380 blob_init(&cmd, 0, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381 file_chdir(g.zLocalRoot, 0);
382 db_prepare(&q,
383 "SELECT patch.cfg.value"
384 " FROM patch.cfg, localdb.vvar"
385 " WHERE patch.cfg.key='baseline'"
@@ -653,11 +667,18 @@
653 ){
654 char *zRemote;
655 char *zDir;
656 Blob cmd;
657 FILE *f;
658 const char *zForce = (mFlags & PATCH_FORCE)!=0 ? " -f" : "";
 
 
 
 
 
 
 
659 if( g.argc!=4 ){
660 usage(mprintf("%s [USER@]HOST:DIRECTORY", zThisCmd));
661 }
662 zRemote = fossil_strdup(g.argv[3]);
663 zDir = (char*)file_skip_userhost(zRemote);
@@ -685,10 +706,11 @@
685 f = popen(blob_str(&cmd), zRW);
686 if( f==0 ){
687 fossil_fatal("cannot run command: %s", blob_str(&cmd));
688 }
689 blob_reset(&cmd);
 
690 return f;
691 }
692
693 /*
694 ** Show a diff for the patch currently loaded into database "patch".
@@ -839,11 +861,12 @@
839 **
840 ** Apply the changes in FILENAME to the check-out at DIRECTORY, or
841 ** in the current directory if DIRECTORY is omitted. Options:
842 **
843 ** -f|--force Apply the patch even though there are unsaved
844 ** changes in the current check-out.
 
845 ** -n|--dryrun Do nothing, but print what would have happened.
846 ** -v|--verbose Extra output explaining what happens.
847 **
848 ** > fossil patch diff [DIRECTORY] FILENAME
849 **
@@ -871,11 +894,12 @@
871 **
872 ** Create a patch on a remote check-out, transfer that patch to the
873 ** local machine (using ssh) and apply the patch in the local checkout.
874 **
875 ** -f|--force Apply the patch even though there are unsaved
876 ** changes in the current check-out.
 
877 ** -n|--dryrun Do nothing, but print what would have happened.
878 ** -v|--verbose Extra output explaining what happens.
879 **
880 ** > fossil patch view FILENAME
881 **
882
--- src/patch.c
+++ src/patch.c
@@ -372,14 +372,28 @@
372 */
373 void patch_apply(unsigned mFlags){
374 Stmt q;
375 Blob cmd;
376
 
 
 
377 blob_init(&cmd, 0, 0);
378 if( unsaved_changes(0) ){
379 if( (mFlags & PATCH_FORCE)==0 ){
380 fossil_fatal("there are unsaved changes in the current checkout");
381 }else{
382 blob_appendf(&cmd, "%$ revert", g.nameOfExe);
383 if( mFlags & PATCH_DRYRUN ){
384 fossil_print("%s\n", blob_str(&cmd));
385 }else{
386 int rc = fossil_system(blob_str(&cmd));
387 if( rc ){
388 fossil_fatal("unable to revert preexisting changes: %s",
389 blob_str(&cmd));
390 }
391 }
392 blob_reset(&cmd);
393 }
394 }
395 file_chdir(g.zLocalRoot, 0);
396 db_prepare(&q,
397 "SELECT patch.cfg.value"
398 " FROM patch.cfg, localdb.vvar"
399 " WHERE patch.cfg.key='baseline'"
@@ -653,11 +667,18 @@
667 ){
668 char *zRemote;
669 char *zDir;
670 Blob cmd;
671 FILE *f;
672 Blob flgs;
673 char *zForce;
674
675 blob_init(&flgs, 0, 0);
676 if( mFlags & PATCH_FORCE ) blob_appendf(&flgs, " -f");
677 if( mFlags & PATCH_VERBOSE ) blob_appendf(&flgs, " -v");
678 if( mFlags & PATCH_DRYRUN ) blob_appendf(&flgs, " -n");
679 zForce = blob_size(&flgs)>0 ? blob_str(&flgs) : "";
680 if( g.argc!=4 ){
681 usage(mprintf("%s [USER@]HOST:DIRECTORY", zThisCmd));
682 }
683 zRemote = fossil_strdup(g.argv[3]);
684 zDir = (char*)file_skip_userhost(zRemote);
@@ -685,10 +706,11 @@
706 f = popen(blob_str(&cmd), zRW);
707 if( f==0 ){
708 fossil_fatal("cannot run command: %s", blob_str(&cmd));
709 }
710 blob_reset(&cmd);
711 blob_reset(&flgs);
712 return f;
713 }
714
715 /*
716 ** Show a diff for the patch currently loaded into database "patch".
@@ -839,11 +861,12 @@
861 **
862 ** Apply the changes in FILENAME to the check-out at DIRECTORY, or
863 ** in the current directory if DIRECTORY is omitted. Options:
864 **
865 ** -f|--force Apply the patch even though there are unsaved
866 ** changes in the current check-out. Unsaved changes
867 ** are reverted and permanently lost.
868 ** -n|--dryrun Do nothing, but print what would have happened.
869 ** -v|--verbose Extra output explaining what happens.
870 **
871 ** > fossil patch diff [DIRECTORY] FILENAME
872 **
@@ -871,11 +894,12 @@
894 **
895 ** Create a patch on a remote check-out, transfer that patch to the
896 ** local machine (using ssh) and apply the patch in the local checkout.
897 **
898 ** -f|--force Apply the patch even though there are unsaved
899 ** changes in the current check-out. Unsaved changes
900 ** will be reverted and then the patch is applied.
901 ** -n|--dryrun Do nothing, but print what would have happened.
902 ** -v|--verbose Extra output explaining what happens.
903 **
904 ** > fossil patch view FILENAME
905 **
906

Keyboard Shortcuts

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