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.
Commit
be3eb3b85dec234e55e7ce8107a6f1301a5dcc033234bab4b4cb4be4684b0be5
Parent
30559adbaf2ccc0…
1 file changed
+34
-50
+34
-50
| --- src/diffcmd.c | ||
| +++ src/diffcmd.c | ||
| @@ -472,10 +472,11 @@ | ||
| 472 | 472 | /* Release memory resources */ |
| 473 | 473 | blob_reset(&file2); |
| 474 | 474 | }else{ |
| 475 | 475 | Blob nameFile1; /* Name of temporary file to old pFile1 content */ |
| 476 | 476 | Blob cmd; /* Text of command to run */ |
| 477 | + int useTempfile = 1; | |
| 477 | 478 | |
| 478 | 479 | if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){ |
| 479 | 480 | Blob file2; |
| 480 | 481 | if( looks_like_binary(pFile1) ){ |
| 481 | 482 | fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY); |
| @@ -500,34 +501,23 @@ | ||
| 500 | 501 | return; |
| 501 | 502 | } |
| 502 | 503 | blob_reset(&file2); |
| 503 | 504 | } |
| 504 | 505 | |
| 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)); | |
| 529 | 519 | |
| 530 | 520 | /* Construct the external diff command */ |
| 531 | 521 | blob_zero(&cmd); |
| 532 | 522 | blob_append(&cmd, pCfg->zDiffCmd, -1); |
| 533 | 523 | if( pCfg->diffFlags & DIFF_INVERT ){ |
| @@ -540,11 +530,11 @@ | ||
| 540 | 530 | |
| 541 | 531 | /* Run the external diff command */ |
| 542 | 532 | fossil_system(blob_str(&cmd)); |
| 543 | 533 | |
| 544 | 534 | /* Delete the temporary file and clean up memory used */ |
| 545 | - file_delete(blob_str(&nameFile1)); | |
| 535 | + if( useTempfile ) file_delete(blob_str(&nameFile1)); | |
| 546 | 536 | blob_reset(&nameFile1); |
| 547 | 537 | blob_reset(&cmd); |
| 548 | 538 | } |
| 549 | 539 | } |
| 550 | 540 | |
| @@ -584,10 +574,12 @@ | ||
| 584 | 574 | blob_reset(&out); |
| 585 | 575 | }else{ |
| 586 | 576 | Blob cmd; |
| 587 | 577 | Blob temp1; |
| 588 | 578 | Blob temp2; |
| 579 | + int useTempfile1 = 1; | |
| 580 | + int useTempfile2 = 1; | |
| 589 | 581 | |
| 590 | 582 | if( (pCfg->diffFlags & DIFF_INCBINARY)==0 ){ |
| 591 | 583 | if( looks_like_binary(pFile1) || looks_like_binary(pFile2) ){ |
| 592 | 584 | fossil_print("%s",DIFF_CANNOT_COMPUTE_BINARY); |
| 593 | 585 | return; |
| @@ -601,33 +593,25 @@ | ||
| 601 | 593 | } |
| 602 | 594 | glob_free(pBinary); |
| 603 | 595 | } |
| 604 | 596 | } |
| 605 | 597 | |
| 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)); | |
| 629 | 613 | |
| 630 | 614 | /* Construct the external diff command */ |
| 631 | 615 | blob_zero(&cmd); |
| 632 | 616 | blob_append(&cmd, pCfg->zDiffCmd, -1); |
| 633 | 617 | blob_append_escaped_arg(&cmd, blob_str(&temp1), 1); |
| @@ -635,12 +619,12 @@ | ||
| 635 | 619 | |
| 636 | 620 | /* Run the external diff command */ |
| 637 | 621 | fossil_system(blob_str(&cmd)); |
| 638 | 622 | |
| 639 | 623 | /* 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)); | |
| 642 | 626 | |
| 643 | 627 | blob_reset(&temp1); |
| 644 | 628 | blob_reset(&temp2); |
| 645 | 629 | blob_reset(&cmd); |
| 646 | 630 | } |
| 647 | 631 |
| --- 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 |