Module: GGSM::Submodule

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

Instance Method Summary collapse

Methods included from Hooks

#check_hooks, #cp_files, #cp_hooks, #install_billow, #update_hooks

Instance Method Details

#check_submodule(check = true) ⇒ Object

[View source]

7
8
9
10
11
12
13
14
15
16
# File 'lib/ggsm/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

[View source]

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

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

[View source]

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ggsm/util/submodule.rb', line 101

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

[View source]

170
171
172
173
174
175
176
177
# File 'lib/ggsm/util/submodule.rb', line 170

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

  Dir.chdir '..'
  correct_dir
end

#foreach_moduleObject

[View source]

121
122
123
124
125
126
127
128
129
130
# File 'lib/ggsm/util/submodule.rb', line 121

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

[View source]

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

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

#get_head_commit(branch) ⇒ Object

获取#branch上HEAD的commit id

[View source]

180
181
182
# File 'lib/ggsm/util/submodule.rb', line 180

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

#get_lastest_msg(depth) ⇒ Object

[View source]

155
156
157
158
# File 'lib/ggsm/util/submodule.rb', line 155

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

#get_lastest_msg_not_mergeObject

[View source]

147
148
149
150
151
152
153
# File 'lib/ggsm/util/submodule.rb', line 147

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

#get_lastest_msg_of_module(module_name) ⇒ Object

[View source]

132
133
134
135
136
137
138
139
140
141
# File 'lib/ggsm/util/submodule.rb', line 132

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

[View source]

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ggsm/util/submodule.rb', line 67

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

[View source]

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ggsm/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)
    result.push(match[0])
  end
  result
end

#get_submodule_commitObject

[View source]

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ggsm/util/submodule.rb', line 83

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

[View source]

143
144
145
# File 'lib/ggsm/util/submodule.rb', line 143

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

#tip_contact_authorObject

[View source]

164
165
166
167
168
# File 'lib/ggsm/util/submodule.rb', line 164

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