Class: Pixab::GitRepoInfo

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGitRepoInfo

Returns a new instance of GitRepoInfo.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/GitRepoInfo.rb', line 10

def initialize
  if !GitUtils.is_git_repo
    puts "当前目录不是 Git 仓库!, path: #{Dir.pwd}".red
    return
  end

  `git fetch origin`

  # 获取当前分支
  @branch = GitUtils.current_branch
  if @branch.nil?
    puts "无法获取当前分支".red
  end
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



8
9
10
# File 'lib/gitlab/GitRepoInfo.rb', line 8

def branch
  @branch
end

#remote_branchObject (readonly)

获取远程分支



37
38
39
# File 'lib/gitlab/GitRepoInfo.rb', line 37

def remote_branch
  @remote_branch
end

#remote_target_branchObject (readonly)

获取远程目标分支



45
46
47
# File 'lib/gitlab/GitRepoInfo.rb', line 45

def remote_target_branch
  @remote_target_branch
end

#target_branchObject (readonly)

获取目标分支



26
27
28
# File 'lib/gitlab/GitRepoInfo.rb', line 26

def target_branch
  @target_branch
end

Class Method Details

.get_target_branch(branch) ⇒ Object

获取目标分支



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/GitRepoInfo.rb', line 62

def get_target_branch(branch)
  splited_branchs = branch.split('/')
  feature_name = splited_branchs[-1]
  version = get_version(feature_name)

  if version.nil?
    puts "无法获取目标分支,分支:#{branch}".red
    return
  end
   
  if splited_branchs.length > 1 
    return "#{splited_branchs[0]}/#{version}"
  else
    return version
  end
end

.get_version(feature_name) ⇒ Object

通过分支名获取对应版本号



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gitlab/GitRepoInfo.rb', line 80

def get_version(feature_name)
  # 用 '_' 分割字符串
  parts = feature_name.split('_')

  # 遍历分割的部分
  parts.each do |part|
    # 判断是否为纯数字
    if part.match?(/^\d+$/)
      return part
    # 判断是否为类似 7.5.0 的格式
    elsif part.match?(/^\d+(\.\d+)*$/)
      return part
    end
  end

  # 如果没有满足条件的字符串,返回 nil
  nil
end

Instance Method Details

#is_remote_branch_syncedObject

判断远程分支是否代码是否同步到远程目标分支



53
54
55
# File 'lib/gitlab/GitRepoInfo.rb', line 53

def is_remote_branch_synced
  GitUtils.is_branch_synced(@remote_branch, @remote_target_branch)
end