Merge branch 'master' into 3198-writable-fuse
[arvados.git] / services / api / app / models / commit.rb
index 9e1176b9b37e8640184be2eb62178e9520744cc7..a6b085722e90fb043a2277f7781727218e8e2559 100644 (file)
@@ -1,4 +1,6 @@
 class Commit < ActiveRecord::Base
+  extend CurrentApiClient
+
   class GitError < StandardError
     def http_status
       422
@@ -29,7 +31,7 @@ class Commit < ActiveRecord::Base
   # repository can be the name of a locally hosted repository or a git
   # URL (see git-fetch(1)). Currently http, https, and git schemes are
   # supported.
-  def self.find_commit_range(current_user, repository, minimum, maximum, exclude)
+  def self.find_commit_range repository, minimum, maximum, exclude
     if minimum and minimum.empty?
       minimum = nil
     end
@@ -109,14 +111,17 @@ class Commit < ActiveRecord::Base
   # The repo can be a remote url, but in this case sha1 must already
   # be present in our local cache for that repo: e.g., sha1 was just
   # returned by find_commit_range.
-  def self.tag_in_internal_repository repo, sha1, tag
+  def self.tag_in_internal_repository repo_name, sha1, tag
     unless git_check_ref_format tag
       raise ArgumentError.new "invalid tag #{tag}"
     end
     unless /^[0-9a-f]{40}$/ =~ sha1
       raise ArgumentError.new "invalid sha1 #{sha1}"
     end
-    src_gitdir, _ = git_dir_for repo
+    src_gitdir, _ = git_dir_for repo_name
+    unless src_gitdir
+      raise ArgumentError.new "no local repository for #{repo_name}"
+    end
     dst_gitdir = Rails.configuration.git_internal_dir
     must_pipe("echo #{sha1.shellescape}",
               "git --git-dir #{src_gitdir.shellescape} pack-objects -q --revs --stdout",
@@ -127,8 +132,8 @@ class Commit < ActiveRecord::Base
 
   protected
 
-  def self.remote_url? repository
-    /^(https?|git):\/\// =~ repository
+  def self.remote_url? repo_name
+    /^(https?|git):\/\// =~ repo_name
   end
 
   # Return [local_git_dir, is_remote]. If is_remote, caller must use
@@ -152,8 +157,11 @@ class Commit < ActiveRecord::Base
   end
 
   def self.cache_dir_for git_url
-    Rails.root.join('tmp', 'git', Digest::SHA1.hexdigest(git_url) + ".git").
-      to_s
+    File.join(cache_dir_base, Digest::SHA1.hexdigest(git_url) + ".git").to_s
+  end
+
+  def self.cache_dir_base
+    Rails.root.join 'tmp', 'git'
   end
 
   def self.fetch_remote_repository gitdir, git_url
@@ -164,9 +172,16 @@ class Commit < ActiveRecord::Base
     unless /^[a-z]+:\/\// =~ git_url
       raise ArgumentError.new "invalid git url #{git_url}"
     end
-    FileUtils.mkdir_p gitdir
+    begin
+      must_git gitdir, "branch"
+    rescue GitError => e
+      raise unless /Not a git repository/ =~ e.to_s
+      # OK, this just means we need to create a blank cache repository
+      # before fetching.
+      FileUtils.mkdir_p gitdir
+      must_git gitdir, "init"
+    end
     must_git(gitdir,
-             "init",
              "fetch --no-progress --tags --prune --force --update-head-ok #{git_url.shellescape} 'refs/heads/*:refs/heads/*'")
   end