Class: GitMaintain::Branch

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

Direct Known Subclasses

RDMACoreBranch

Constant Summary collapse

ACTION_LIST =
[
    :cp, :steal, :list, :list_stable,
    :merge, :push, :monitor,
    :push_stable, :monitor_stable,
    :release, :reset, :create, :delete
]
NO_FETCH_ACTIONS =
[
    :cp, :merge, :monitor, :release, :delete
]
NO_CHECKOUT_ACTIONS =
[
    :create, :delete, :list, :list_stable, :push, :monitor, :monitor_stable
]
ALL_BRANCHES_ACTIONS =
[
    :create
]
ACTION_HELP =
[
    "* cp: Backport commits and eventually push them to github",
    "* create: Create missing local branches from all the stable branches",
    "* delete: Delete all local branches using the suffix",
    "* steal: Steal commit from upstream that fixes commit in the branch or were tagged as stable",
    "* list: List commit present in the branch but not in the stable branch",
    "* list_stable: List commit present in the stable branch but not in the latest associated relase",
    "* merge: Merge branch with suffix specified in -m <suff> into the main branch",
    "* push: Push branches to github for validation",
    "* monitor: Check the travis state of all branches",
    "* push_stable: Push to stable repo",
    "* monitor_stable: Check the travis state of all stable branches",
    "* release: Create new release on all concerned branches",
    "* reset: Reset branch against upstream",
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, version, travis, branch_suff) ⇒ Branch

Returns a new instance of Branch.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/branch.rb', line 170

def initialize(repo, version, travis, branch_suff)
    GitMaintain::checkDirectConstructor(self.class)

    @repo          = repo
    @travis        = travis
    @version       = version
    @branch_suff   = branch_suff

    if version =~ /^[0-9]+$/
        @local_branch  = @repo.versionToLocalBranch(@version, @branch_suff)
        @remote_branch = @repo.versionToStableBranch(@version)
        @branch_type = :std
        @verbose_name = "v"+version
    else
        @remote_branch = @local_branch = version
        @branch_type = :user_specified
        @verbose_name = version
    end

    @head          = @repo.runGit("rev-parse --verify --quiet #{@local_branch}")
    @remote_ref    = "#{@repo.stable_repo}/#{@remote_branch}"
    @stable_head   = @repo.runGit("rev-parse --verify --quiet #{@remote_ref}")
    @stable_base   = @repo.findStableBase(@local_branch)
end

Instance Attribute Details

#existsObject (readonly)

Returns the value of attribute exists.



194
195
196
# File 'lib/branch.rb', line 194

def exists
  @exists
end

#headObject (readonly)

Returns the value of attribute head.



194
195
196
# File 'lib/branch.rb', line 194

def head
  @head
end

#local_branchObject (readonly)

Returns the value of attribute local_branch.



194
195
196
# File 'lib/branch.rb', line 194

def local_branch
  @local_branch
end

#remote_branchObject (readonly)

Returns the value of attribute remote_branch.



194
195
196
# File 'lib/branch.rb', line 194

def remote_branch
  @remote_branch
end

#remote_refObject (readonly)

Returns the value of attribute remote_ref.



194
195
196
# File 'lib/branch.rb', line 194

def remote_ref
  @remote_ref
end

#stable_headObject (readonly)

Returns the value of attribute stable_head.



194
195
196
# File 'lib/branch.rb', line 194

def stable_head
  @stable_head
end

#verbose_nameObject (readonly)

Returns the value of attribute verbose_name.



194
195
196
# File 'lib/branch.rb', line 194

def verbose_name
  @verbose_name
end

#versionObject (readonly)

Returns the value of attribute version.



194
195
196
# File 'lib/branch.rb', line 194

def version
  @version
end

Class Method Details

.check_opts(opts) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/branch.rb', line 108

def self.check_opts(opts)
    if opts[:action] == :push_stable ||
       opts[:action] == :release then
        if opts[:br_suff] != "master" then
            raise "Action #{opts[:action]} can only be done on 'master' suffixed branches"
        end
    end
    if opts[:action] == :delete then
        if opts[:br_suff] == "master" then
            raise "Action #{opts[:action]} can NOT be done on 'master' suffixed branches"
        end
    end
end

.execAction(opts, action) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
# File 'lib/branch.rb', line 122

def self.execAction(opts, action)
    repo   = Repo::load()
    travis = TravisChecker::load(repo)

    if NO_FETCH_ACTIONS.index(action) == nil && opts[:no_fetch] == false then
        repo.stableUpdate()
    end

    branchList=[]
    if opts[:manual_branch] == nil then
        unfilteredList = nil
        if ALL_BRANCHES_ACTIONS.index(action) != nil then
            unfilteredList = repo.getStableBranchList()
        else
            unfilteredList = repo.getBranchList(opts[:br_suff])
        end
        branchList = unfilteredList.map(){|br|
            branch = Branch::load(repo, br, travis, opts[:br_suff])
            case branch.is_targetted?(opts)
            when :too_old
                GitMaintain::log(:VERBOSE, "Skipping older v#{branch.version}")
                next
            when :no_match
                GitMaintain::log(:VERBOSE, "Skipping v#{branch.version} not matching" +
                                           opts[:version].to_s())
                next
            end
            branch
        }.compact()
    else
        branchList = [ Branch::load(repo, opts[:manual_branch], travis, opts[:br_suff]) ]
    end

    loop do
        system("clear; date") if opts[:watch] != false
        branchList.each(){|branch|
            if NO_CHECKOUT_ACTIONS.index(action) == nil  then
                GitMaintain::log(:INFO, "Working on #{branch.verbose_name}")
                branch.checkout()
            end
            branch.send(action, opts)
        }
        break if opts[:watch] == false
        sleep(opts[:watch])
        travis.emptyCache()
    end
end

.load(repo, version, travis, branch_suff) ⇒ Object



43
44
45
46
# File 'lib/branch.rb', line 43

def self.load(repo, version, travis, branch_suff)
    repo_name = File.basename(repo.path)
    return GitMaintain::loadClass(Branch, repo_name, repo, version, travis, branch_suff)
end

.set_opts(action, optsParser, opts) ⇒ Object



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
# File 'lib/branch.rb', line 48

def self.set_opts(action, optsParser, opts)
    opts[:base_ver] = 0
    opts[:version] = /.*/
    opts[:commits] = []
    opts[:do_merge] = false
    opts[:push_force] = false
    opts[:no_travis] = false
    opts[:all] = false
    opts[:check_only] = false
    opts[:no_fetch] = false
    opts[:watch] = false

    optsParser.on("-v", "--base-version [MIN_VER]", Integer, "Older release to consider.") {
        |val| opts[:base_ver] = val}
    optsParser.on("-V", "--version [regexp]", Regexp, "Regexp to filter versions.") {
        |val| opts[:version] = val}

    if  ALL_BRANCHES_ACTIONS.index(action) == nil &&
        action != :merge &&
        action != :delete then
        optsParser.on("-B", "--manual-branch <branch name>", "Work on a specific (non-stable) branch.") {
            |val| opts[:manual_branch] = val}
    end

    if NO_FETCH_ACTIONS.index(action) == nil
        optsParser.on("--no-fetch", "Skip fetch of stable repo.") {
            |val| opts[:no_fetch] = true}
    end

    case action
    when :cp
        optsParser.banner += "-c <sha1> [-c <sha1> ...]"
        optsParser.on("-c", "--sha1 [SHA1]", String, "Commit to cherry-pick. Can be used multiple time.") {
            |val| opts[:commits] << val}
    when :merge
        optsParser.banner += "-m <suffix>"
        optsParser.on("-m", "--merge [SUFFIX]", "Merge branch with suffix.") {
            |val| opts[:do_merge] = val}
    when :monitor, :monitor_stable
        optsParser.on("-w", "--watch <PERIOD>", Integer,
                      "Watch and refresh travis status every <PERIOD>.") {
            |val| opts[:watch] = val}
    when :push
        optsParser.banner += "[-f]"
        optsParser.on("-f", "--force", "Add --force to git push (for 'push' action).") {
            |val| opts[:push_force] = val}
    when :push_stable
        optsParser.banner += "[-T]"
        optsParser.on("-T", "--no-travis", "Ignore Travis build status and push anyway.") {
            |val| opts[:no_travis] = true}
        optsParser.on("-c", "--check", "Check if there is something to be pushed.") {
            |val| opts[:check_only] = true}
    when :steal
        optsParser.banner += "[-a]"
        optsParser.on("-a", "--all", "Check all commits from master. "+
                                       "By default only new commits (since last successful run) are considered.") {
            |val| opts[:all] = true}
    end
end

Instance Method Details

#checkoutObject

Checkout the repo to the given branch



213
214
215
216
217
218
# File 'lib/branch.rb', line 213

def checkout()
    print @repo.runGit("checkout -q #{@local_branch}")
    if $? != 0 then
        raise "Error: Failed to checkout the branch"
    end
end

#cp(opts) ⇒ Object

Cherry pick an array of commits



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/branch.rb', line 221

def cp(opts)
    opts[:commits].each(){|commit|
        prev_head=@repo.runGit("rev-parse HEAD")
        log(:INFO, "Applying #{@repo.getCommitHeadline(commit)}")
        @repo.runGit("cherry-pick #{commit}")
        if $? != 0 then
            log(:WARNING, "Cherry pick failure. Starting bash for manual fixes. Exit shell to continue")
   @repo.runBash()
  end
        new_head=@repo.runGit("rev-parse HEAD")
        # Do not make commit pretty if it was not applied
        if new_head != prev_head
      make_pretty(commit)
        end
    }
end

#create(opts) ⇒ Object



405
406
407
408
409
# File 'lib/branch.rb', line 405

def create(opts)
    return if @head != ""
    log(:INFO, "Creating missing #{@local_branch} from #{@remote_ref}")
    @repo.runGit("branch #{@local_branch} #{@remote_ref}")
end

#delete(opts) ⇒ Object



411
412
413
414
415
416
417
418
419
# File 'lib/branch.rb', line 411

def delete(opts)
    rep = GitMaintain::confirm(opts, "delete branch #{@local_branch}")
    if rep == "y" then
        @repo.runGit("branch -D #{@local_branch}")
    else
        log(:INFO, "Skipping deletion")
        return
    end
end

#is_targetted?(opts) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
209
210
# File 'lib/branch.rb', line 201

def is_targetted?(opts)
    return true if @branch_type == :user_specified
    if @version.to_i < opts[:base_ver] then
        return :too_old
    end
    if @version !~ opts[:version] then
        return :no_match
    end
    return true
end

#list(opts) ⇒ Object

List commits in the branch that are no in the stable branch



267
268
269
270
# File 'lib/branch.rb', line 267

def list(opts)
    GitMaintain::log(:INFO, "Working on #{@verbose_name}")
    GitMaintain::showLog(opts, @local_branch, @remote_ref)
end

#list_stable(opts) ⇒ Object

List commits in the stable_branch that are no in the latest release



273
274
275
276
# File 'lib/branch.rb', line 273

def list_stable(opts)
    GitMaintain::log(:INFO, "Working on #{@verbose_name}")
    GitMaintain::showLog(opts, @remote_ref, @repo.runGit("describe --abbrev=0 #{@local_branch}"))
end

#log(lvl, str) ⇒ Object



197
198
199
# File 'lib/branch.rb', line 197

def log(lvl, str)
    GitMaintain::log(lvl, str)
end

#merge(opts) ⇒ Object

Merge merge_branch into this one



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/branch.rb', line 279

def merge(opts)
    merge_branch = @repo.versionToLocalBranch(@version, opts[:do_merge])

    # Make sure branch exists
    hash_to_merge = @repo.runGit("rev-parse --verify --quiet #{merge_branch}")
    if $? != 0 then
        log(:INFO, "Branch #{merge_branch} does not exists. Skipping...")
        return
    end

    # See if there is anything worth merging
    merge_base_hash = @repo.runGit("merge-base #{merge_branch} #{@local_branch}")
    if merge_base_hash == hash_to_merge then
        log(:INFO, "Branch #{merge_branch} has no commit that needs to be merged")
        return
    end

    rep = GitMaintain::checkLog(opts, merge_branch, @local_branch, "merge")
    if rep == "y" then
        @repo.runGit("merge #{merge_branch}")
        if $? != 0 then
            log(:WARNING, "Merge failure. Starting bash for manual fixes. Exit shell to continue")
   @repo.runBash()
  end
    else
        log(:INFO, "Skipping merge")
        return
    end 
end

#monitor(opts) ⇒ Object

Monitor the build status on Travis



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/branch.rb', line 320

def monitor(opts)
    st = @travis.getValidState(head)
    suff=""
    case st
    when "started"
        suff= " started at #{@travis.getValidTS(head)}"
    end
    log(:INFO, "Status for v#{@version}: " + st + suff)
    if st == "failed" && opts[:watch] == false
        rep = "y"
        suff=""
        while rep == "y"
            rep = GitMaintain::confirm(opts, "see the build log#{suff}")
            if rep == "y" then
                log = @travis.getValidLog(head)
                tmp = `mktemp`.chomp()
                tmpfile = File.open(tmp, "w+")
                tmpfile.puts(log)
                tmpfile.close()
                system("less -r #{tmp}")
                `rm -f #{tmp}`
            end
            suff=" again"
        end
    end
end

#monitor_stable(opts) ⇒ Object

Monitor the build status of the stable branch on Travis



375
376
377
378
379
380
381
382
383
# File 'lib/branch.rb', line 375

def monitor_stable(opts)
    st = @travis.getStableState(@stable_head)
    suff=""
    case st
    when "started"
        suff= " started at #{@travis.getStableTS(@stable_head)}"
    end
    log(:INFO, "Status for v#{@version}: " + st + suff)
end

#push(opts) ⇒ Object

Push the branch to the validation repo



310
311
312
313
314
315
316
317
# File 'lib/branch.rb', line 310

def push(opts)
    if same_sha?(@local_branch, @repo.valid_repo + "/" + @local_branch) then
        log(:INFO, "Nothing to push")
        return
    end

   @repo.runGit("push #{opts[:push_force] == true ? "-f" : ""} #{@repo.valid_repo} #{@local_branch}")
end

#push_stable(opts) ⇒ Object

Push branch to the stable repo



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/branch.rb', line 348

def push_stable(opts)
    if (opts[:no_travis] != true && @NO_TRAVIS != true) &&
       @travis.checkValidState(@head) != true then
        log(:WARNING, "Build is not passed on travis. Skipping push to stable")
        return
    end

    if same_sha?(@local_branch, @remote_ref) then
        log(:INFO, "Stable is already up-to-date")
        return
    end

    if opts[:check_only] == true then
        GitMaintain::checkLog(opts, @local_branch, @remote_ref, "")
        return
    end

    rep = GitMaintain::checkLog(opts, @local_branch, @remote_ref, "submit")
    if rep == "y" then
        @repo.runGit("push #{@repo.stable_repo} #{@local_branch}:#{@remote_branch}")
    else
        log(:INFO, "Skipping push to stable")
        return
    end
end

#release(opts) ⇒ Object



401
402
403
# File 'lib/branch.rb', line 401

def release(opts)
    log(:ERROR,"#No release command available for this repo")
end

#reset(opts) ⇒ Object

Reset the branch to the upstream stable one



386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/branch.rb', line 386

def reset(opts)
    if same_sha?(@local_branch, @remote_ref) then
        log(:INFO, "Nothing to reset")
        return
    end

    rep = GitMaintain::checkLog(opts, @local_branch, @remote_ref, "reset")
    if rep == "y" then
        @repo.runGit("reset --hard #{@remote_ref}")
    else
        log(:INFO, "Skipping reset")
        return
    end
end

#steal(opts) ⇒ Object

Steal upstream commits that are not in the branch



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/branch.rb', line 239

def steal(opts)
    base_ref=@stable_base

    # If we are not force checking everything,
    # try to start from the last tag we steal upto
    if opts[:all] != true then
        sha = @repo.runGit("rev-parse 'git-maintain/steal/last/#{@stable_base}' 2>&1")
        if $? == 0 then
            base_ref=sha
            log(:VERBOSE, "Starting from last successfull run:")
            log(:VERBOSE, @repo.getCommitHeadline(base_ref))
        end
    end

    master_sha=@repo.runGit("rev-parse origin/master")
    res = steal_all(opts, "#{base_ref}..#{master_sha}")

    # If we picked all the commits (or nothing happened)
    # Mark the current master as the last checked point so we
    # can just steal from this point on the next run
    if res == true then
        @repo.runGit("tag -f 'git-maintain/steal/last/#{@stable_base}' origin/master")
        log(:VERBOSE, "Marking new last successfull run at:")
        log(:VERBOSE, @repo.getCommitHeadline(master_sha))
    end
end