Improved tasks `workflow:out-staging` and `workflow:out-upstream`
authorJan Vrany <jan.vrany@fit.cvut.cz>
Thu, 25 May 2017 22:33:44 +0100
changeset 127 664296ccdb4a
parent 126 9cd66b79ca54
child 128 1433658a3853
Improved tasks `workflow:out-staging` and `workflow:out-upstream` * Only print repositories with some outgoing revisions. This reduces "noice" in output and make it easier for humans to review. * Reposr exit status 0 if there are changes to push, 1 if there are no changes. Thus may be used for further scripting.
rakelib/hglib.rb
rakelib/workflow.rake
--- a/rakelib/hglib.rb	Tue Mar 21 13:00:49 2017 +0000
+++ b/rakelib/hglib.rb	Thu May 25 22:33:44 2017 +0100
@@ -101,7 +101,7 @@
                 gsub(/username\\=\S+/, "username\\=***").
                 gsub(/password\\=\S+/, "password\\=***")
     $LOGGER.debug("executing: #{cmd_info}")
-    if defined? RakeFileUtils then
+    if defined? RakeFileUtils and RakeFileUtils.verbose then
       puts cmd_info
     end
     if block_given? then
@@ -444,16 +444,20 @@
       end
     end
 
-    def outgoing(remote = 'default', user: nil, pass: nil, rev: nil)
-      hg("outgoing", remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev) do | status, stdout|                
-        case status.exitstatus
-        when 0          
-          STDOUT.print stdout
-        when 1
-          puts "No new changes going to #{remote}"          
-        else        
-          raise Exception.new("Failed to do 'outgoing' from #{remote} (exit code #{status.exitstatus})")
+    def outgoing(remote = 'default', user: nil, pass: nil, rev: nil, &block)      
+      if not block_given? then
+        hg("outgoing", remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev) do | status, stdout|                
+          case status.exitstatus
+          when 0          
+            STDOUT.print stdout            
+          when 1
+            puts "No new changes going to #{remote}"          
+          else        
+            raise Exception.new("Failed to do 'outgoing' from #{remote} (exit code #{status.exitstatus})")
+          end
         end
+      else
+        hg("outgoing", remote, ssh: sshconf(remote), config: authconf(remote, user, pass), rev: rev, &block) 
       end
     end
 
--- a/rakelib/workflow.rake	Tue Mar 21 13:00:49 2017 +0000
+++ b/rakelib/workflow.rake	Thu May 25 22:33:44 2017 +0100
@@ -17,7 +17,8 @@
     end    
   end
   
-  def push(remote, user, pass, review_only, push_bookmark)  
+  def push(remote, user, pass, review_only, push_bookmark)
+    has_changes = false;
     hg_repositories do | pkg, hg |
       opts = {
         :user => user,
@@ -43,24 +44,33 @@
       # mainly beacuse there are on separate protected repositories 
       # not accessible without a special setup. Sigh...
       if ((pkg.name == 'stx:stc') or (pkg.name == 'stx:librun')) and ((remote_used == 'upstream') or (remote_used == 'canonical')) then        
-        puts ""
-        puts "== Skipping push of #{pkg.name} - you must push manually =="
-        puts ""                  
+        if not review_only then
+          puts ""
+          puts "== Skipping push of #{pkg.name} - you must push manually =="
+          puts ""                  
+        end
       else      
         if review_only then
           opts[:rev] = '.'
           remote_url = hg.paths[remote_used] || remote_used
           bookmark = hg.bookmark()
-          puts ""
-          puts "== changes going to #{remote_url} =="
-          puts ""
-          hg.outgoing(remote_used, **opts)
-          puts ""
-          if push_bookmark and bookmark then
-            puts "Will update bookmark '#{bookmark}"
+          hg.outgoing(remote_used, **opts) do | status, stdout |
+            case status.exitstatus
+            when 0          
+              puts ""
+              puts "== changes going to #{remote_url} =="              
+              STDOUT.print stdout
+              if push_bookmark and bookmark then              
+                puts "Will update bookmark '#{bookmark}"
+              end
+              puts "===================================="
+              has_changes = true
+            when 1
+              # nothing
+            else        
+              raise Exception.new("Failed to do 'outgoing' from #{remote} (exit code #{status.exitstatus})")
+            end
           end
-          puts "===================================="
-          puts ""
         else
           if push_bookmark && bookmark then
             opts[:bookmarks] = ['.']
@@ -68,14 +78,22 @@
             opts[:rev] = '.'
           end        
           hg.push(remote_used, **opts)
+          has_changes = true
         end
       end
     end
+    return has_changes
   end
 
   desc "Display changes to be pushed to upstream repositores (use it to review what workflow:push-upstream would do)"
   task :'out-upstream', :user, :pass do | t, args |
-    push('upstream', args[:user], args[:pass], true, false)
+    RakeFileUtils.verbose(false) do
+      has_changes = push('upstream', args[:user], args[:pass], true, false)
+      if not has_changes then
+        puts "No changes to be pushed to upstream repositories"
+      end      
+      exit(has_changes ? 0 : 1 )
+    end
   end
   task :'out-upstream' => :'setup'
 
@@ -88,7 +106,13 @@
 
   desc "Display changes to be pushed to staging repositores (use it to review what workflow:push-staging would do)"
   task :'out-staging', :user, :pass do | t, args |
-    push('staging', args[:user], args[:pass], true, true)
+    RakeFileUtils.verbose(false) do
+      has_changes = push('staging', args[:user], args[:pass], true, true)
+      if not has_changes then
+        puts "No changes to be pushed to staging repositories"
+      end
+      exit(has_changes ? 0 : 1 )
+    end
   end
   task :'out-staging' => :'setup'