bug-vc-dwim
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-vc-dwim] bug#38334: adding --dry-run option to vc-dwim [patch]


From: Karl Berry
Subject: [Bug-vc-dwim] bug#38334: adding --dry-run option to vc-dwim [patch]
Date: Fri, 22 Nov 2019 15:51:17 -0700

Possible NEWS entry, under "New features":

  vc-dwim accepts a new option: --dry-run (-n)

Several of the changes are just replacing
  <code>
with
  if ($dry_run) {
    print "msg";
 } else {
   <code>
 }
hence they look longer than they actually are.

2019-11-14  Karl Berry  <address@hidden>

        vc-dwim: New option vc-dwim --dry-run (aka -n).

        * vc-dwim.pl ($dry_run): new variable.
        (GetOptions): set it.
        (run_command): conditionalize system call and INHIBIT_STDOUT.
        (main): conditionalize initialize operations, rename, symlink.
        * doc/vc-dwim.texi (vc-dwim Invocation): document it.

--- doc/vc-dwim.texi    2019-11-13 17:36:14.920575492 -0800
+++ n.texi      2019-11-17 18:23:06.972699737 -0800
@@ -252,4 +252,8 @@
 Perform the commit, too.
 
+@item -n
+@itemx --dry-run
+Print the commands that would be run instead of running them.
+
 @item --diff
 Determine which version control system manages the first
--- vc-dwim.pl  2019-11-17 18:43:44.673256552 -0800
+++ n.pl        2019-11-21 18:14:13.250781876 -0800
@@ -61,4 +61,5 @@
 my $verbose = 0;
 my $debug = 0;
+my $dry_run = 0;
 
 sub usage ($)
@@ -248,5 +249,5 @@
      DIE_UPON_FAILURE => 1,
      INHIBIT_STDERR => 0,
-     INHIBIT_STDOUT => 1,
+     INHIBIT_STDOUT => ! $dry_run, # keep stdout if just printing
     );
 
@@ -292,5 +293,11 @@
 
   my $fail = 1;
-  my $rc = 0xffff & system @cmd;
+  my $rc;
+  if ($dry_run) {
+    print "$ME: would run: @cmd\n";
+    $rc = 0;
+  } else {
+    $rc = 0xffff & system @cmd;
+  }
 
   # Restore stdout.
@@ -734,4 +741,6 @@
      'print-vc-list' =>
        sub { print join (' ', VC::supported_vc_names()), "\n"; exit },
+     n => \$dry_run,
+     'dry-run' => \$dry_run,
      debug => \$debug,
      verbose => \$verbose,
@@ -757,13 +766,21 @@
       do_at ($adm, sub
       {
-        ! (mkdir ('c') || $! == EEXIST)
-          and die "$ME: failed to create $adm/c: $!\n";
-        chdir 'c' or die "$ME: failed to chdir to $adm/c: $!\n";
+        if ($dry_run) {
+          print "$ME: would mkdir 'c' in $adm\n";
+        } else {
+          ! (mkdir ('c') || $! == EEXIST)
+            and die "$ME: failed to create $adm/c: $!\n";
+          chdir 'c' or die "$ME: failed to chdir to $adm/c: $!\n";
+        }
 
         # touch ChangeLog || die
-        open FH, '>>', $cl
-          or die "$ME: failed to open '$cl' for writing: $!\n";
-        close FH
-          or die "$ME: failed to write '$cl': $!\n";
+        if ($dry_run) {
+          print "$ME: would touch $cl in $adm\n";
+        } else {
+          open FH, '>>', $cl
+            or die "$ME: failed to open '$cl' for writing: $!\n";
+          close FH
+            or die "$ME: failed to write '$cl': $!\n";
+        }
 
         # Initialize the git repo, add ChangeLog and commit it.
@@ -775,11 +792,21 @@
 
       # If a ChangeLog file exists in the current directory, rename it
-      # to ChangeLog~, deliberately ignoring any rename failure.
-      rename $cl, "$cl~";
+      # deliberately ignoring any rename failure. (But only report the
+      # rename for dry runs if it does exist.)
+      my $cl_top = "../../$cl";
+      if ($dry_run && -e $cl_top) {
+        print "$ME: would rename($cl_top, $cl_top~)\n";
+      } else {
+        rename $cl_top, "$cl_top~";
+      }
 
       # Create the top-level ChangeLog symlink into $adm/c:
       my $cl_sub = "$adm/c/$cl";
-      symlink $cl_sub, $cl
-        or die "$ME: failed to create symlink, $cl, to $cl_sub: $!\n";
+      if ($dry_run) {
+        print "$ME: would symlink($cl_sub, $cl_top)\n";
+      } else {
+        symlink $cl_sub, $cl_top
+          or die "$ME: failed to create symlink, $cl, to $cl_sub: $!\n";
+      }
 
       exit 0;
@@ -799,5 +826,10 @@
         and verbose_cmd \@cmd;
 
-      exec @cmd;
+      if ($dry_run) {
+        print "$ME: would run: @cmd\n";
+        exit 0;
+      } else {
+        exec @cmd;
+      }
       exit 1;
     }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]