Fossil SCM

Simplify handling of /dev/null with external diff commands in [fd359387ca], reveting to using existing temp file naming convention. Also avoids attempting to remove non-temporary files.

preben 2023-10-06 10:41 diff-deleted-files
Commit be3eb3b85dec234e55e7ce8107a6f1301a5dcc033234bab4b4cb4be4684b0be5
1 file changed +34 -50
+34 -50
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -472,10 +472,11 @@
472472
/* Release memory resources */
473473
blob_reset(&file2);
474474
}else{
475475
Blob nameFile1; /* Name of temporary file to old pFile1 content */
476476
Blob cmd; /* Text of command to run */
477
+ int useTempfile = 1;
477478
478479
if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){
479480
Blob file2;
480481
if( looks_like_binary(pFile1) ){
481482
fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY);
@@ -500,34 +501,23 @@
500501
return;
501502
}
502503
blob_reset(&file2);
503504
}
504505
505
-#if defined(_WIN32)
506
- /* TODO: How would a (common?) diff program on Windows react to NUL? */
507
- /* Construct a temporary file to hold pFile1 based on the name of
508
- ** zFile2 */
509
- file_tempname(&nameFile1, zFile2, "orig");
510
- blob_write_to_file(pFile1, blob_str(&nameFile1));
511
-#else
512
- if( pCfg->diffFlags & DIFF_FILE_ADDED ){
513
- blob_init(&nameFile1, NULL_DEVICE, -1);
514
- }else{
515
- if( pCfg->diffFlags & DIFF_FILE_DELETED ){
516
- /* Indicate a deleted file as /dev/null on Unix.
517
- ** For Windows, depends on how the diff tool handles NUL.
518
- */
519
- blob_init(&nameFile1, zFile2, -1);
520
- zFile2 = NULL_DEVICE;
521
- }else{
522
- /* Construct a temporary file to hold pFile1 based on the name of
523
- ** zFile2 */
524
- file_tempname(&nameFile1, zFile2, "orig");
525
- }
526
- blob_write_to_file(pFile1, blob_str(&nameFile1));
527
- }
528
-#endif
506
+ /* Construct a temporary file to hold pFile1 based on the name of
507
+ ** zFile2 */
508
+ file_tempname(&nameFile1, zFile2, "orig");
509
+#if !defined(_WIN32)
510
+ /* On Unix, use /dev/null for added or deleted files. */
511
+ if( pCfg->diffFlags & DIFF_FILE_ADDED ){
512
+ blob_init(&nameFile1, NULL_DEVICE, -1);
513
+ useTempfile = 0;
514
+ }else if( pCfg->diffFlags & DIFF_FILE_DELETED ){
515
+ zFile2 = NULL_DEVICE;
516
+ }
517
+#endif
518
+ if( useTempfile ) blob_write_to_file(pFile1, blob_str(&nameFile1));
529519
530520
/* Construct the external diff command */
531521
blob_zero(&cmd);
532522
blob_append(&cmd, pCfg->zDiffCmd, -1);
533523
if( pCfg->diffFlags & DIFF_INVERT ){
@@ -540,11 +530,11 @@
540530
541531
/* Run the external diff command */
542532
fossil_system(blob_str(&cmd));
543533
544534
/* Delete the temporary file and clean up memory used */
545
- file_delete(blob_str(&nameFile1));
535
+ if( useTempfile ) file_delete(blob_str(&nameFile1));
546536
blob_reset(&nameFile1);
547537
blob_reset(&cmd);
548538
}
549539
}
550540
@@ -584,10 +574,12 @@
584574
blob_reset(&out);
585575
}else{
586576
Blob cmd;
587577
Blob temp1;
588578
Blob temp2;
579
+ int useTempfile1 = 1;
580
+ int useTempfile2 = 1;
589581
590582
if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){
591583
if( looks_like_binary(pFile1) || looks_like_binary(pFile2) ){
592584
fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY);
593585
return;
@@ -601,33 +593,25 @@
601593
}
602594
glob_free(pBinary);
603595
}
604596
}
605597
606
-#if defined(_WIN32)
607
- /* TODO: How would a (common?) diff program on Windows react to NUL? */
608
- /* Construct a temporary file names */
609
- file_tempname(&temp1, zName, "before");
610
- file_tempname(&temp2, zName, "after");
611
- blob_write_to_file(pFile1, blob_str(&temp1));
612
- blob_write_to_file(pFile2, blob_str(&temp2));
613
-#else
614
- if( pCfg->diffFlags & DIFF_FILE_ADDED ){
615
- blob_init(&temp1, NULL_DEVICE, -1);
616
- blob_init(&temp2, zName, -1);
617
- blob_write_to_file(pFile2, blob_str(&temp2));
618
- }else if( pCfg->diffFlags & DIFF_FILE_DELETED ){
619
- blob_init(&temp1, zName, -1);
620
- blob_init(&temp2, NULL_DEVICE, -1);
621
- blob_write_to_file(pFile1, blob_str(&temp1));
622
- }else{
623
- file_tempname(&temp1, zName, "before");
624
- file_tempname(&temp2, zName, "after");
625
- blob_write_to_file(pFile1, blob_str(&temp1));
626
- blob_write_to_file(pFile2, blob_str(&temp2));
627
- }
628
-#endif
598
+ /* Construct temporary file names */
599
+ file_tempname(&temp1, zName, "before");
600
+ file_tempname(&temp2, zName, "after");
601
+#if !defined(_WIN32)
602
+ /* On Unix, use /dev/null for added or deleted files. */
603
+ if( pCfg->diffFlags & DIFF_FILE_ADDED ){
604
+ useTempfile1 = 0;
605
+ blob_init(&temp1, NULL_DEVICE, -1);
606
+ }else if( pCfg->diffFlags & DIFF_FILE_DELETED ){
607
+ useTempfile2 = 0;
608
+ blob_init(&temp2, NULL_DEVICE, -1);
609
+ }
610
+#endif
611
+ if( useTempfile1 ) blob_write_to_file(pFile1, blob_str(&temp1));
612
+ if( useTempfile2 ) blob_write_to_file(pFile2, blob_str(&temp2));
629613
630614
/* Construct the external diff command */
631615
blob_zero(&cmd);
632616
blob_append(&cmd, pCfg->zDiffCmd, -1);
633617
blob_append_escaped_arg(&cmd, blob_str(&temp1), 1);
@@ -635,12 +619,12 @@
635619
636620
/* Run the external diff command */
637621
fossil_system(blob_str(&cmd));
638622
639623
/* Delete the temporary file and clean up memory used */
640
- file_delete(blob_str(&temp1));
641
- file_delete(blob_str(&temp2));
624
+ if( useTempfile1 ) file_delete(blob_str(&temp1));
625
+ if( useTempfile2 ) file_delete(blob_str(&temp2));
642626
643627
blob_reset(&temp1);
644628
blob_reset(&temp2);
645629
blob_reset(&cmd);
646630
}
647631
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -472,10 +472,11 @@
472 /* Release memory resources */
473 blob_reset(&file2);
474 }else{
475 Blob nameFile1; /* Name of temporary file to old pFile1 content */
476 Blob cmd; /* Text of command to run */
 
477
478 if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){
479 Blob file2;
480 if( looks_like_binary(pFile1) ){
481 fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY);
@@ -500,34 +501,23 @@
500 return;
501 }
502 blob_reset(&file2);
503 }
504
505 #if defined(_WIN32)
506 /* TODO: How would a (common?) diff program on Windows react to NUL? */
507 /* Construct a temporary file to hold pFile1 based on the name of
508 ** zFile2 */
509 file_tempname(&nameFile1, zFile2, "orig");
510 blob_write_to_file(pFile1, blob_str(&nameFile1));
511 #else
512 if( pCfg->diffFlags & DIFF_FILE_ADDED ){
513 blob_init(&nameFile1, NULL_DEVICE, -1);
514 }else{
515 if( pCfg->diffFlags & DIFF_FILE_DELETED ){
516 /* Indicate a deleted file as /dev/null on Unix.
517 ** For Windows, depends on how the diff tool handles NUL.
518 */
519 blob_init(&nameFile1, zFile2, -1);
520 zFile2 = NULL_DEVICE;
521 }else{
522 /* Construct a temporary file to hold pFile1 based on the name of
523 ** zFile2 */
524 file_tempname(&nameFile1, zFile2, "orig");
525 }
526 blob_write_to_file(pFile1, blob_str(&nameFile1));
527 }
528 #endif
529
530 /* Construct the external diff command */
531 blob_zero(&cmd);
532 blob_append(&cmd, pCfg->zDiffCmd, -1);
533 if( pCfg->diffFlags & DIFF_INVERT ){
@@ -540,11 +530,11 @@
540
541 /* Run the external diff command */
542 fossil_system(blob_str(&cmd));
543
544 /* Delete the temporary file and clean up memory used */
545 file_delete(blob_str(&nameFile1));
546 blob_reset(&nameFile1);
547 blob_reset(&cmd);
548 }
549 }
550
@@ -584,10 +574,12 @@
584 blob_reset(&out);
585 }else{
586 Blob cmd;
587 Blob temp1;
588 Blob temp2;
 
 
589
590 if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){
591 if( looks_like_binary(pFile1) || looks_like_binary(pFile2) ){
592 fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY);
593 return;
@@ -601,33 +593,25 @@
601 }
602 glob_free(pBinary);
603 }
604 }
605
606 #if defined(_WIN32)
607 /* TODO: How would a (common?) diff program on Windows react to NUL? */
608 /* Construct a temporary file names */
609 file_tempname(&temp1, zName, "before");
610 file_tempname(&temp2, zName, "after");
611 blob_write_to_file(pFile1, blob_str(&temp1));
612 blob_write_to_file(pFile2, blob_str(&temp2));
613 #else
614 if( pCfg->diffFlags & DIFF_FILE_ADDED ){
615 blob_init(&temp1, NULL_DEVICE, -1);
616 blob_init(&temp2, zName, -1);
617 blob_write_to_file(pFile2, blob_str(&temp2));
618 }else if( pCfg->diffFlags & DIFF_FILE_DELETED ){
619 blob_init(&temp1, zName, -1);
620 blob_init(&temp2, NULL_DEVICE, -1);
621 blob_write_to_file(pFile1, blob_str(&temp1));
622 }else{
623 file_tempname(&temp1, zName, "before");
624 file_tempname(&temp2, zName, "after");
625 blob_write_to_file(pFile1, blob_str(&temp1));
626 blob_write_to_file(pFile2, blob_str(&temp2));
627 }
628 #endif
629
630 /* Construct the external diff command */
631 blob_zero(&cmd);
632 blob_append(&cmd, pCfg->zDiffCmd, -1);
633 blob_append_escaped_arg(&cmd, blob_str(&temp1), 1);
@@ -635,12 +619,12 @@
635
636 /* Run the external diff command */
637 fossil_system(blob_str(&cmd));
638
639 /* Delete the temporary file and clean up memory used */
640 file_delete(blob_str(&temp1));
641 file_delete(blob_str(&temp2));
642
643 blob_reset(&temp1);
644 blob_reset(&temp2);
645 blob_reset(&cmd);
646 }
647
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -472,10 +472,11 @@
472 /* Release memory resources */
473 blob_reset(&file2);
474 }else{
475 Blob nameFile1; /* Name of temporary file to old pFile1 content */
476 Blob cmd; /* Text of command to run */
477 int useTempfile = 1;
478
479 if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){
480 Blob file2;
481 if( looks_like_binary(pFile1) ){
482 fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY);
@@ -500,34 +501,23 @@
501 return;
502 }
503 blob_reset(&file2);
504 }
505
506 /* Construct a temporary file to hold pFile1 based on the name of
507 ** zFile2 */
508 file_tempname(&nameFile1, zFile2, "orig");
509 #if !defined(_WIN32)
510 /* On Unix, use /dev/null for added or deleted files. */
511 if( pCfg->diffFlags & DIFF_FILE_ADDED ){
512 blob_init(&nameFile1, NULL_DEVICE, -1);
513 useTempfile = 0;
514 }else if( pCfg->diffFlags & DIFF_FILE_DELETED ){
515 zFile2 = NULL_DEVICE;
516 }
517 #endif
518 if( useTempfile ) blob_write_to_file(pFile1, blob_str(&nameFile1));
 
 
 
 
 
 
 
 
 
 
 
519
520 /* Construct the external diff command */
521 blob_zero(&cmd);
522 blob_append(&cmd, pCfg->zDiffCmd, -1);
523 if( pCfg->diffFlags & DIFF_INVERT ){
@@ -540,11 +530,11 @@
530
531 /* Run the external diff command */
532 fossil_system(blob_str(&cmd));
533
534 /* Delete the temporary file and clean up memory used */
535 if( useTempfile ) file_delete(blob_str(&nameFile1));
536 blob_reset(&nameFile1);
537 blob_reset(&cmd);
538 }
539 }
540
@@ -584,10 +574,12 @@
574 blob_reset(&out);
575 }else{
576 Blob cmd;
577 Blob temp1;
578 Blob temp2;
579 int useTempfile1 = 1;
580 int useTempfile2 = 1;
581
582 if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){
583 if( looks_like_binary(pFile1) || looks_like_binary(pFile2) ){
584 fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY);
585 return;
@@ -601,33 +593,25 @@
593 }
594 glob_free(pBinary);
595 }
596 }
597
598 /* Construct temporary file names */
599 file_tempname(&temp1, zName, "before");
600 file_tempname(&temp2, zName, "after");
601 #if !defined(_WIN32)
602 /* On Unix, use /dev/null for added or deleted files. */
603 if( pCfg->diffFlags & DIFF_FILE_ADDED ){
604 useTempfile1 = 0;
605 blob_init(&temp1, NULL_DEVICE, -1);
606 }else if( pCfg->diffFlags & DIFF_FILE_DELETED ){
607 useTempfile2 = 0;
608 blob_init(&temp2, NULL_DEVICE, -1);
609 }
610 #endif
611 if( useTempfile1 ) blob_write_to_file(pFile1, blob_str(&temp1));
612 if( useTempfile2 ) blob_write_to_file(pFile2, blob_str(&temp2));
 
 
 
 
 
 
 
 
613
614 /* Construct the external diff command */
615 blob_zero(&cmd);
616 blob_append(&cmd, pCfg->zDiffCmd, -1);
617 blob_append_escaped_arg(&cmd, blob_str(&temp1), 1);
@@ -635,12 +619,12 @@
619
620 /* Run the external diff command */
621 fossil_system(blob_str(&cmd));
622
623 /* Delete the temporary file and clean up memory used */
624 if( useTempfile1 ) file_delete(blob_str(&temp1));
625 if( useTempfile2 ) file_delete(blob_str(&temp2));
626
627 blob_reset(&temp1);
628 blob_reset(&temp2);
629 blob_reset(&cmd);
630 }
631

Keyboard Shortcuts

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