Class: Gitmirror::Backend::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gitmirror/backend/base.rb

Direct Known Subclasses

Fork, PosixSpawn

Instance Method Summary collapse

Instance Method Details

#checkout(repo_path, workdir, rev) ⇒ Object



43
44
45
46
47
48
# File 'lib/gitmirror/backend/base.rb', line 43

def checkout repo_path, workdir, rev
  #checkout workdir
  FileUtils.mkdir_p workdir
  git({}, "--git-dir", repo_path, "--work-tree", workdir, "checkout", "-f", rev)
  git({}, "--git-dir", repo_path, "rev-parse", rev).rstrip
end

#mirror(url, local_repo_path, credential = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitmirror/backend/base.rb', line 8

def mirror url, local_repo_path, credential=nil

  #init bare repository
  unless File.exists? local_repo_path 
    FileUtils.mkdir_p local_repo_path
    git({}, "init", "--bare", local_repo_path) #idempotent, does not harm existing repos
    git({}, "--git-dir", local_repo_path, "remote", "add", "origin", "--mirror=fetch", url) #raises an error
  end
  #fetch updates
  if credential
    case url
    when /^https:\/\//
      u = URI.parse(url)
      if u.user
        u.password = credential
      else
        u.user = credential
      end
      git({}, "--git-dir", local_repo_path, "remote", "set-url", "origin", u.to_s)
      git({"GIT_ASKPASS"=> "/bin/echo"}, "--git-dir" ,local_repo_path, "fetch", "origin")
    else
      #assuming ssh protocol
      Tempfile.open("ssh-key") do |file|
        file.write(credential)
        file.close
        git({"GIT_SSH_COMMAND"=> "ssh -o BatchMode=yes -i #{file.path}"}, "--git-dir" ,local_repo_path, "fetch", "origin")
      end
    end
  else
    # no auth, no problems
    git({}, "--git-dir" ,local_repo_path, "fetch", "origin")
  end
  local_repo_path
end