Module: DNGG::Submodule

Includes:
Hooks
Included in:
Delete, Finish, Foreach, MR, Merge, Start, Switch
Defined in:
lib/dngg/util/submodule.rb

Instance Method Summary collapse

Methods included from Hooks

#check_hooks, #cp_files, #cp_hooks, #update_hooks

Instance Method Details

#check_submodule(check = true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/dngg/util/submodule.rb', line 7

def check_submodule(check=true)
  sub_str = `git submodule`
  if check && sub_str.empty?
    puts '所在目录工程下不存在Submodule,请检查所在目录!'.red
    exit 1
  end

  correct_dir
  check_hooks
end

#check_submodule_status(is_sync) ⇒ Object



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
# File 'lib/dngg/util/submodule.rb', line 43

def check_submodule_status(is_sync)
  modified_sub = get_modified_submodule
  if !is_sync && modified_sub.size > 0
    subs = ''
    modified_sub.each do |sub|
      subs = "#{sub} , #{subs}"
    end
    puts "\n存在未跟踪的子模块提交: #{subs[0...subs.size-2]} \n\n请选择操作:\n1 丢弃子模块的改动
2 追踪子模块(仅追踪子模块)\n3 追踪所有改变\n4 取消".red

    input = STDIN.gets.chomp.upcase
    if input == '1'
    elsif input == '2'
      add = 'git add'
      modified_sub.each do |sub|
        add = "#{add} #{sub}"
      end
      system "#{add};git commit"
    elsif input == '3'
      system 'git add .;git commit'
    else
      puts '==> 取消操作'.blue
      exit 0
    end
  end
end

#check_un_commit_codeObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dngg/util/submodule.rb', line 104

def check_un_commit_code
  subs = get_submodule
  project_path = Dir.pwd
  subs.each do |sub|
    Dir.chdir sub
    status = `git status --ignore-submodules | grep 'nothing to commit'`
    if status.strip == ''
      puts "#{sub} 有未提交的代码".red
      exit 1
    end
    Dir.chdir project_path
  end

  status = `git status --ignore-submodules | grep 'nothing to commit'`
  if status.strip == ''
    puts '主工程 有未提交的代码'.red
    exit 1
  end
end

#correct_dirObject



174
175
176
177
178
179
180
181
# File 'lib/dngg/util/submodule.rb', line 174

def correct_dir
  if File.exist?('.git')
    return
  end

  Dir.chdir '..'
  correct_dir
end

#foreach_moduleObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/dngg/util/submodule.rb', line 124

def foreach_module
  subs = get_submodule
  project_path = Dir.pwd
  subs.each do |sub|
    Dir.chdir sub
    puts "==> 进入#{sub}".yellow
    yield sub, subs.index(sub)
    Dir.chdir project_path
  end
end

#get_current_branchObject



164
165
166
# File 'lib/dngg/util/submodule.rb', line 164

def get_current_branch
  `git branch | grep "*"`.split('* ')[1].split("\n")[0]
end

#get_head_commit(branch) ⇒ Object

获取#branch上HEAD的commit id



184
185
186
# File 'lib/dngg/util/submodule.rb', line 184

def get_head_commit(branch)
  `git rev-parse #{branch}`
end

#get_lastest_msg(depth) ⇒ Object



159
160
161
162
# File 'lib/dngg/util/submodule.rb', line 159

def get_lastest_msg(depth) 
  msg = `git log --oneline --format=%B -n #{depth}`.split(/\n\n/)
  msg[depth - 1].strip
end

#get_lastest_msg_not_mergeObject

寻找上次merge之后的commit消息



151
152
153
154
155
156
157
# File 'lib/dngg/util/submodule.rb', line 151

def get_lastest_msg_not_merge
  depth = 1
  while (msg = get_lastest_msg(depth)).start_with?('Merge ') do
    depth += 1
  end
  msg
end

#get_lastest_msg_of_module(module_name) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/dngg/util/submodule.rb', line 135

def get_lastest_msg_of_module(module_name)
  msg = ''
  project_path = Dir.pwd
  if is_submodule(module_name)
    Dir.chdir module_name
    msg = get_lastest_msg_not_merge
    Dir.chdir project_path
  end
  msg
end

#get_modified_submoduleObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dngg/util/submodule.rb', line 70

def get_modified_submodule
  result = []

  status = `git status -s`.split(/\n/)
  subs = get_submodule

  status.each do |sub|
    array_name = sub.split(' ')
    name = array_name[array_name.size-1]
    if subs.include? name
      result.push(name)
    end
  end
  result
end

#get_submoduleObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dngg/util/submodule.rb', line 18

def get_submodule
  pattern = /(?<=\s)[\/0-9a-zA-Z\-]*(?=\s)/

  sub_status = `git submodule`
  sub_status = sub_status.split(/\n/)
  match = pattern.match(sub_status[0])
  if match==nil
    puts '==> 初始化子模块'.yellow
    `git submodule update --init --recursive`
    sub_status = `git submodule`
    sub_status = sub_status.split(/\n/)
  end

  result = []
  sub_status.each do |sub|
    match = pattern.match(sub.strip)
    unless match
      puts `pwd`,"====>>>子模块id #{sub}拉取异常!!! 执行 git submodule 检查".red
      next
    end
    result.push(match[0])
  end
  result
end

#get_submodule_commitObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dngg/util/submodule.rb', line 86

def get_submodule_commit
  sub_tree = 'git ls-tree HEAD | grep "160000"'
  sub_commits = `#{sub_tree}`
  # if sub_commits.strip == '' && (File.directory? 'submodules')
  #   Dir.chdir 'submodules'
  #   sub_commits = `#{sub_tree}`
  #   Dir.chdir '..'
  # end
  pattern = /(?<=\s)[0-9a-zA-Z]{40}(?=\s)/
  sub_commits = sub_commits.split(/\n/)
  result = []
  sub_commits.each do |sub|
    match = pattern.match(sub.strip)
    result.push(match[0][0...7])
  end
  result
end

#is_submodule(module_name) ⇒ Object



146
147
148
# File 'lib/dngg/util/submodule.rb', line 146

def is_submodule(module_name)
  get_submodule.include?(module_name)
end

#tip_contact_authorObject



168
169
170
171
172
# File 'lib/dngg/util/submodule.rb', line 168

def tip_contact_author
  latest_commit = `git log test -1 --oneline | grep ''`.split(' ')[0]
  author = `git show #{latest_commit}|grep "Author:"`.chomp
  puts "请联系#{author} 推送远程缺失的commit".red
end