Commit 226df39341c5b120fca94dbc269652ef0541bc14

Thomas de Grivel 2023-02-20T19:38:11

clone, checkout, pull

diff --git a/bin/rbpkg b/bin/rbpkg
index 7bbc485..0c38b07 100755
--- a/bin/rbpkg
+++ b/bin/rbpkg
@@ -39,27 +39,37 @@ Misc commands :
   exit 1
 end
 
-def rbpkg(args)
-  case args[0]
-  when nil
-    usage()
+$ARGS = ARGV
+
+def shift
+  usage if $ARGS == []
+  arg = $ARGS[0]
+  $ARGS = $ARGS[1..] || []
+  arg
+end
+
+def rbpkg
+  case shift
   when "-h"
     usage()
   when "--help"
     usage()
   when "clone"
-    Rbpkg.git_clone(args[1..])
+    Rbpkg.git_clone($ARGS)
   when "clean-sources"
-    Rbpkg.clean_sources(args[1..])
+    Rbpkg.clean_sources($ARGS)
   when "fetch"
-    Rbpkg.fetch(args[1..])
+    Rbpkg.fetch($ARGS)
   when "pull"
-    Rbpkg.pull(args[1..])
+    Rbpkg.pull($ARGS)
+  when "checkout"
+    branch = shift()
+    Rbpkg.checkout(branch, $ARGS)
   when "info"
-    Rbpkg.info(args[1..])
+    Rbpkg.info($ARGS)
   else
     usage
   end
 end
 
-rbpkg ARGV
+rbpkg
diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 0f467b4..ec7eec8 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -3,6 +3,12 @@ require __FILE__ + '/../rbpkg/repos'
 
 module Rbpkg
 
+  def self.checkout(branch, repos)
+    repos.each do |name|
+      Repos.repo(name).checkout(branch)
+    end
+  end
+
   def self.clean_sources(repos)
     repos.each do |name|
       Repos.repo(name).clean_sources()
diff --git a/lib/rbpkg/repo.rb b/lib/rbpkg/repo.rb
index 7678748..53ca094 100644
--- a/lib/rbpkg/repo.rb
+++ b/lib/rbpkg/repo.rb
@@ -2,6 +2,14 @@ require 'fileutils'
 
 class Rbpkg::Repo
 
+  @@git_remote_default = "origin"
+  @@git_branch_default = "master"
+
+  def checkout(branch)
+    sh_branch=sh_quote(branch)
+    cmd "cd #{sh_quote(src_dir!)} && git fetch #{sh_quote(git_remote)} #{sh_branch} && git checkout #{sh_branch} && git submodule update"
+  end
+
   def clean_sources
     if File.directory?(src_dir)
       cmd "rm -rf #{sh_quote(src_dir)}"
@@ -19,6 +27,10 @@ class Rbpkg::Repo
     cmd "cd #{sh_quote(src_dir!)} && git fetch"
   end
 
+  def git_branch
+    @@git_branch_default
+  end
+
   def git_clone
     if File.directory?(src_dir)
       false
@@ -26,10 +38,15 @@ class Rbpkg::Repo
       if ! File.directory?(src_parent_dir)
         cmd "mkdir -p #{sh_quote(src_parent_dir)}"
       end
-      cmd "cd #{sh_quote(src_parent_dir)} && git clone #{sh_quote(git_url)} #{sh_quote(name)}"
+      cmd "cd #{sh_quote(src_parent_dir)} && git clone #{sh_quote(git_url)} -b #{sh_quote(git_branch)} #{sh_quote(name)}"
+      cmd "cd #{sh_quote(src_dir)} && git submodule init && git submodule update"
     end
   end
 
+  def git_remote
+    @@git_remote_default
+  end
+
   def git_url
     raise "no git url for #{self}"
   end
@@ -49,7 +66,7 @@ class Rbpkg::Repo
   end
 
   def pull
-    cmd "cd #{sh_quote(src_dir!)} && git pull"
+    cmd "cd #{sh_quote(src_dir!)} && git pull && git submodule update"
   end
 
   def src_dir