kk-git 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/kk/git/rake_tasks.rb +76 -1
- data/lib/kk/git/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4e2ef1f50e051f8167139fae0478f01da4a0cae6ea6401a73cdf67765ad13936
|
|
4
|
+
data.tar.gz: 71d53513506ebff53f091a7d10c9ab41e0e6b3b60e393d783061aa2f19072288
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c25c845bb90eae62b73feff52b0a0043e20004b20bec670460fa90ba6bb09e53fa468986c960305787a311622b30947eda805c1dc9395e7294a3bca663d877b5
|
|
7
|
+
data.tar.gz: 0534a91d78bef3ddbd34f85cdc5f7e9392a68d771a2c940a1d435151fe28910b925164c8277e08e2addfa5faf085af0fe27a0375e4900a1ea2f5468e8dd4f966
|
data/lib/kk/git/rake_tasks.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require 'rake'
|
|
4
4
|
require 'kk/git'
|
|
5
|
+
require 'open3'
|
|
6
|
+
require 'tempfile'
|
|
5
7
|
|
|
6
8
|
module KKGit
|
|
7
9
|
# Rake 集成:在任意项目的 Rakefile 中 `require 'kk/git/rake_tasks'` 即可注册任务。
|
|
@@ -10,6 +12,32 @@ module KKGit
|
|
|
10
12
|
# - 这些任务**不会 exit/abort**,以便在其它 task 中被 invoke。
|
|
11
13
|
# - 会把生成结果写入 `ENV['KK_GIT_COMMIT_MESSAGE']`,方便上层 task 复用。
|
|
12
14
|
module RakeTasks
|
|
15
|
+
def self.run_cmd(*cmd)
|
|
16
|
+
stdout, stderr, status = Open3.capture3(*cmd)
|
|
17
|
+
[stdout.to_s, stderr.to_s, status.success?]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.ensure_ok!(ok, title, stdout: nil, stderr: nil)
|
|
21
|
+
return if ok
|
|
22
|
+
|
|
23
|
+
msg = +"#{title} 失败"
|
|
24
|
+
msg << "\n#{stderr}" unless stderr.to_s.strip.empty?
|
|
25
|
+
msg << "\n#{stdout}" unless stdout.to_s.strip.empty?
|
|
26
|
+
raise msg
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.current_branch
|
|
30
|
+
out, err, ok = run_cmd('git', 'rev-parse', '--abbrev-ref', 'HEAD')
|
|
31
|
+
ensure_ok!(ok, '获取当前分支', stdout: out, stderr: err)
|
|
32
|
+
out.strip
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.working_tree_clean?
|
|
36
|
+
out, err, ok = run_cmd('git', 'status', '--porcelain')
|
|
37
|
+
ensure_ok!(ok, '检查 git 状态', stdout: out, stderr: err)
|
|
38
|
+
out.strip.empty?
|
|
39
|
+
end
|
|
40
|
+
|
|
13
41
|
def self.install!
|
|
14
42
|
extend Rake::DSL
|
|
15
43
|
|
|
@@ -29,11 +57,58 @@ module KKGit
|
|
|
29
57
|
end
|
|
30
58
|
|
|
31
59
|
desc '合并暂存区+工作区变更生成 commit message'
|
|
32
|
-
task :
|
|
60
|
+
task :auto_commit do
|
|
33
61
|
msg = KKGit::CommitMessage.generate(mode: :all)
|
|
34
62
|
ENV['KK_GIT_COMMIT_MESSAGE'] = msg.to_s
|
|
35
63
|
puts msg if msg
|
|
36
64
|
end
|
|
65
|
+
|
|
66
|
+
desc '自动 add/commit/pull/push(基于 git:auto_commit)'
|
|
67
|
+
task :auto_commit_push do
|
|
68
|
+
if working_tree_clean?
|
|
69
|
+
puts '没有变更需要提交'
|
|
70
|
+
next
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
remote = ENV.fetch('KK_GIT_REMOTE', 'origin')
|
|
74
|
+
branch = ENV.fetch('KK_GIT_BRANCH', current_branch)
|
|
75
|
+
|
|
76
|
+
# 1) add
|
|
77
|
+
out, err, ok = run_cmd('git', 'add', '.')
|
|
78
|
+
ensure_ok!(ok, 'git add', stdout: out, stderr: err)
|
|
79
|
+
|
|
80
|
+
# 2) 生成 commit message(允许重复 invoke)
|
|
81
|
+
Rake::Task['git:auto_commit'].reenable
|
|
82
|
+
Rake::Task['git:auto_commit'].invoke
|
|
83
|
+
commit_message = ENV['KK_GIT_COMMIT_MESSAGE'].to_s.strip
|
|
84
|
+
commit_message = "chore(repo): 更新项目文件\n\n#{Time.now}" if commit_message.empty?
|
|
85
|
+
|
|
86
|
+
# 3) commit(用临时文件避免转义问题)
|
|
87
|
+
Tempfile.create('commit_message') do |f|
|
|
88
|
+
f.write(commit_message)
|
|
89
|
+
f.flush
|
|
90
|
+
out, err, ok = run_cmd('git', 'commit', '-F', f.path)
|
|
91
|
+
# 没有 staged 变更时 git commit 会失败;这里给出更友好的提示
|
|
92
|
+
unless ok
|
|
93
|
+
if err.include?('nothing to commit') || out.include?('nothing to commit')
|
|
94
|
+
puts '没有暂存变更需要提交'
|
|
95
|
+
next
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
ensure_ok!(ok, 'git commit', stdout: out, stderr: err)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# 4) pull(默认使用 --ff-only,避免非交互环境进入合并流程)
|
|
102
|
+
pull_args = ENV.fetch('KK_GIT_PULL_ARGS', '--ff-only').split
|
|
103
|
+
out, err, ok = run_cmd('git', 'pull', *pull_args)
|
|
104
|
+
ensure_ok!(ok, 'git pull', stdout: out, stderr: err)
|
|
105
|
+
|
|
106
|
+
# 5) push
|
|
107
|
+
out, err, ok = run_cmd('git', 'push', remote, branch)
|
|
108
|
+
ensure_ok!(ok, 'git push', stdout: out, stderr: err)
|
|
109
|
+
|
|
110
|
+
puts "✅ 已推送: #{remote} #{branch}"
|
|
111
|
+
end
|
|
37
112
|
end
|
|
38
113
|
end
|
|
39
114
|
end
|
data/lib/kk/git/version.rb
CHANGED