Class: Nixenvironment::SCM

Inherits:
Object
  • Object
show all
Defined in:
lib/nixenvironment/scm.rb

Constant Summary collapse

SCM_SVN =
'svn'
SCM_GIT =
'git'
SCM_MERCURIAL =
'mercurial'

Class Method Summary collapse

Class Method Details

.clean_working_copy(all) ⇒ Object

TODO: implement –all for svn



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/nixenvironment/scm.rb', line 136

def self.clean_working_copy(all)
  puts '--- Clean all specified' if all.present?
  scm_type = current

  case scm_type
    when SCM_SVN
      puts 'SVN working copy detected, cleaning ...'

      # revert all changes
      system('svn revert --depth=infinity .')

      # revert all changes in external directories
      # TODO: rework this for Windows
      system("
      for i in $(svn status | grep ^Performing | cut -d\' -f 2)
      do
        svn revert -R $i
      done")

      # Wipes out unversioned files from Subversion working copy
      system('svn cleanup . --remove-unversioned')
    when SCM_GIT
      puts 'GIT working copy detected, cleaning ...'

      # revert all changes
      system('git reset --hard')

      if all
        # wipe out unversioned files (force, remove whole directories, remove ignored files)
        system('git clean -fdx')
      else
        # wipe out unversioned files (force, remove whole directories)
        system('git clean -fd')
      end
    when SCM_MERCURIAL
      puts 'Mercurial working copy detected, cleaning ...'

      # revert all changes
      system('hg revert --all')

      if all
        # wipe out unversioned files
        system('hg --config extensions.purge= clean --all')
      else
        # wipe out unversioned files (except ignored files)
        system('hg --config extensions.purge= clean')
      end
    else
      puts 'Error: script must be run from working copy!'
  end
end

.currentObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/nixenvironment/scm.rb', line 7

def self.current
  %x[ git status ]
  return SCM_GIT if $?.success?

  %x[ hg status ]
  return SCM_MERCURIAL if $?.success?

  %x[ svn info ]
  return SCM_SVN if $?.success?

  return 'undefined'
end

.last_revisionObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/nixenvironment/scm.rb', line 20

def self.last_revision
  scm_type = current
  case scm_type
    when SCM_SVN
      puts 'SVN working copy detected'
      current_revision = %x[ svnversion ]
      current_monotonic_revision = current_revision

      # simply check that the SVN version is a digit
      if Integer(current_revision)
        working_copy_is_clean = 1
      else
        puts "Working copy is not clean because of svnversion returning #{current_revision}"
        working_copy_is_clean = 0
      end
    when SCM_GIT
      puts 'GIT working copy detected'

      usedf = ENV['USE_DATEFORMAT']
      unless !usedf.nil?
        current_revision = %x[ git rev-parse HEAD ]
        last_revision_in_repo = %x[ git rev-list --all -n 1 ]
        # number of pushes before current
        current_monotonic_revision = %x[ git rev-list --all --count ]
        if current_revision == last_revision_in_repo
          current_monotonic_revision = current_monotonic_revision.succ
        end
      else
        # revision in datetime format
        git_commit_timestamp = %x[ git show -s --format=%ct #{last_revision_in_repo} ]
        git_result = Time.at(git_commit_timestamp.to_i)
        year = "%02d" % git_result.year.to_s[2,2].to_i
        month = "%02d" % git_result.month
        day =  "%02d" % git_result.day
        hour =  "%02d" % git_result.hour
        min =  "%02d" % git_result.min
        sec =  "%02d" % git_result.sec
        current_monotonic_revision = " #{year}#{month}#{day}.#{hour}#{min}#{sec} "
        current_revision = " #{year}#{month}#{day}.#{hour}#{min}#{sec} "
        #current_monotonic_revision = %x[ git log -1 --date=format:"%Y%m%d.%H%M%S" --format="%ad" ] #old one
      end

      status = %x[ git status --porcelain ]

      if status == ''
        working_copy_is_clean = 1
      else
        # TODO: check this out for Unity builds
        # ignore changes to build folder, because it's created by Xcode before the script runs as a build phase
        lines_count = status.lines.count

        if lines_count != 1
          puts "Working copy is not clean because of status:\n#{status}"
          puts
          working_copy_is_clean = 0
        else
          wc_root = %[ git rev-parse --show-toplevel ]
          modified_path = status[3..-1]
          abs_modified_path = File.join(wc_root, modified_path)

          # compare path to Xcode env var
          if abs_modified_path == ENV['BUILD_ROOT']
            working_copy_is_clean = 1
          else
            puts "Working copy is not clean because of status:\n#{status}"
            puts
            working_copy_is_clean = 0
          end
        end
      end
    when SCM_MERCURIAL
      puts 'Mercurial working copy detected'
      current_revision = %x[ hg id -i ]
      # number of pushes in current branch
      # TODO: compare with: "$(hg log --template '{rev}:{node|short} {desc|firstline} ({author})\n' | wc -l | tr -d ' ')"
      current_monotonic_revision = %x[ hg id --num --rev tip ]

      status = %x[ hg status ]

      if status == ''
        working_copy_is_clean = 1
      else
        # TODO: check this out for Unity builds
        # ignore changes to build folder, because it's created by Xcode before the script runs as a build phase
        lines_count = status.lines.count

        if lines_count != 1
          puts "Working copy is not clean because of status:\n#{status}"
          puts
          working_copy_is_clean = 0
        else
          wc_root = %[ hg root ]
          modified_path = status[3..-1]
          abs_modified_path = File.join(wc_root, modified_path)

          # compare path to Xcode env var
          if abs_modified_path == ENV['BUILD_ROOT']
            working_copy_is_clean = 1
          else
            puts "Working copy is not clean because of status:\n#{status}"
            puts
            working_copy_is_clean = 0
          end
        end
      end
    else
      puts 'warning: script must be run from working copy. Revision is undefined.'
      current_revision = 'undefined'
      current_monotonic_revision = 'undefined'
      working_copy_is_clean = 0
  end

  return current_revision.strip!, current_monotonic_revision.strip!, working_copy_is_clean
end