Class: VagrantPlugins::Orchestrate::RepoStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-orchestrate/repo_status.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ RepoStatus

Returns a new instance of RepoStatus.



9
10
11
12
13
14
# File 'lib/vagrant-orchestrate/repo_status.rb', line 9

def initialize(root_path)
  @last_sync = Time.now.utc    # Managed servers could be in different timezones
  @local_path = nil
  # The fully qualified path to the root of the repo
  @root_path = root_path
end

Instance Attribute Details

#last_syncObject (readonly)

Returns the value of attribute last_sync.



6
7
8
# File 'lib/vagrant-orchestrate/repo_status.rb', line 6

def last_sync
  @last_sync
end

#local_pathObject

Returns the value of attribute local_path.



7
8
9
# File 'lib/vagrant-orchestrate/repo_status.rb', line 7

def local_path
  @local_path
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



6
7
8
# File 'lib/vagrant-orchestrate/repo_status.rb', line 6

def root_path
  @root_path
end

Class Method Details

.clean?Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/vagrant-orchestrate/repo_status.rb', line 78

def self.clean?
  `git diff --exit-code 2>&1`
  $CHILD_STATUS == 0
end

.committed?Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/vagrant-orchestrate/repo_status.rb', line 83

def self.committed?
  `git diff-index --quiet --cached HEAD 2>&1`
  $CHILD_STATUS == 0
end

.untracked?Boolean

Return whether there are any untracked files in the git repo

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/vagrant-orchestrate/repo_status.rb', line 89

def self.untracked?
  output = `git ls-files --other --exclude-standard --directory --no-empty-directory 2>&1`
  # This command lists untracked files. There are untracked files if the ouput is not empty.
  !output.empty?
end

Instance Method Details

#branchObject



38
39
40
41
42
# File 'lib/vagrant-orchestrate/repo_status.rb', line 38

def branch
  @branch ||= ENV["VAGRANT_ORCHESTRATE_STATUS_TEST_BRANCH"]
  @branch ||= `git rev-parse --abbrev-ref HEAD`.chomp
  @branch
end

#refObject



16
17
18
19
20
21
22
# File 'lib/vagrant-orchestrate/repo_status.rb', line 16

def ref
  # Env vars are here only for testing, since vagrant-spec is executed from
  # a temp directory and can't use git to get repository information
  @ref ||= ENV["VAGRANT_ORCHESTRATE_STATUS_TEST_REF"]
  @ref ||= `git log --pretty=format:'%H' --abbrev-commit -1`
  @ref
end

#remote_origin_urlObject



24
25
26
27
28
# File 'lib/vagrant-orchestrate/repo_status.rb', line 24

def remote_origin_url
  @remote_origin_url ||= ENV["VAGRANT_ORCHESTRATE_STATUS_TEST_REMOTE_ORIGIN_URL"]
  @remote_origin_url ||= `git config --get remote.origin.url 2>#{File::NULL}`.chomp
  @remote_origin_url
end

#remote_path(communicator) ⇒ Object

The path to where this should be stored on a remote machine, inclusive of the file name.



70
71
72
73
74
75
76
# File 'lib/vagrant-orchestrate/repo_status.rb', line 70

def remote_path(communicator)
  if communicator == :winrm
    File.join("c:", "programdata", "vagrant_orchestrate", repo)
  else
    File.join("/var", "state", "vagrant_orchestrate", repo)
  end
end

#repoObject



30
31
32
33
34
35
36
# File 'lib/vagrant-orchestrate/repo_status.rb', line 30

def repo
  @repo ||= ENV["VAGRANT_ORCHESTRATE_STATUS_TEST_REPO"]
  @repo ||= File.basename(`git rev-parse --show-toplevel 2>#{File::NULL}`.chomp)
  # This might not be a git repo, and that should still be supported.
  @repo = File.basename(root_path) if @repo.nil? || @repo.empty?
  @repo
end

#to_jsonObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-orchestrate/repo_status.rb', line 52

def to_json
  contents = {
    repo: repo,
    remote_url: remote_origin_url,
    ref: ref,
    user: user,
    last_sync: last_sync
  }
  JSON.pretty_generate(contents)
end

#userObject



44
45
46
47
48
49
50
# File 'lib/vagrant-orchestrate/repo_status.rb', line 44

def user
  user = ENV["USER"] || ENV["USERNAME"] || "unknown"
  user = ENV["USERDOMAIN"] + "\\" + user if ENV["USERDOMAIN"]

  @user ||= user
  @user
end

#write(tmp_path) ⇒ Object



63
64
65
66
# File 'lib/vagrant-orchestrate/repo_status.rb', line 63

def write(tmp_path)
  @local_path = File.join(tmp_path, "vagrant_orchestrate_status")
  File.write(@local_path, to_json)
end