Class: Panoramix::Plugin::Git

Inherits:
Base
  • Object
show all
Defined in:
lib/panoramix/plugin/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#shell

Constructor Details

#initialize(dst, src, tag) ⇒ Git

Returns a new instance of Git.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/panoramix/plugin/git.rb', line 12

def initialize(dst, src, tag)
  # Get git project name
  repo_name=src.split("/").last.gsub(".git", "")

  # Base path under which project will be downloaded
  base_path=dst.split(repo_name)[0..-1].join(repo_name)

  @dst=File.join(base_path, repo_name)
  @src = src
  @tag = tag
end

Instance Attribute Details

#dstObject (readonly)

Returns the value of attribute dst.



8
9
10
# File 'lib/panoramix/plugin/git.rb', line 8

def dst
  @dst
end

#srcObject (readonly)

Returns the value of attribute src.



9
10
11
# File 'lib/panoramix/plugin/git.rb', line 9

def src
  @src
end

#tagObject (readonly)

Returns the value of attribute tag.



10
11
12
# File 'lib/panoramix/plugin/git.rb', line 10

def tag
  @tag
end

Instance Method Details

#cleanObject

Action clean fot this task



60
61
62
# File 'lib/panoramix/plugin/git.rb', line 60

def clean
  shell "rm -rf #{@dst}"
end

#created?Boolean

Has this instance already been created

Returns:

  • (Boolean)


55
56
57
# File 'lib/panoramix/plugin/git.rb', line 55

def created?
  File.directory?(@dst)
end

#needed?(timestamps) ⇒ Boolean

When this instance needs to be executed

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/panoramix/plugin/git.rb', line 33

def needed? timestamps
  return true if !created?
  # Get remote branches
  remote = shell("git ls-remote #{src}", true)[:out].split("\n")

  # Is @tag a branch?
  is_branch = remote.select { |r| r.include?("refs/heads/#{tag}") }

  if is_branch.any?
    # Compare local refs with remote ref
    remote_ref = is_branch.first.gsub /\t/, ' '
    local_refs = shell("git -C #{@dst} show-ref", true)[:out].split("\n")

    return !local_refs.include?(remote_ref)
  end

  #TODO: To be implemented for tags and commits

  return true
end

#run_defaultObject

Default action for this task



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/panoramix/plugin/git.rb', line 65

def run_default
  if created?
     shell "git -C #{@dst} pull"
   # File does not exists, so clone the full repository
   else
     shell "git clone #{@src} #{@dst}"
   end

   # Switch to tag
   shell "git -C #{@dst} checkout #{@tag}"
end

#timestampObject

Return current timestamp



25
26
27
28
29
30
# File 'lib/panoramix/plugin/git.rb', line 25

def timestamp
  return Time.at 0 unless created?
   git_time = shell("git -C #{@dst} log -1 --format=\"%cd\"", true)[:out]
   #TODO check exit code
   Time.parse(git_time)
end