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
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
107
108
109
110
111
112
113
|
# File 'lib/kk/git/rake_tasks.rb', line 41
def self.install!
extend Rake::DSL
namespace :git do
desc '根据暂存区变更生成 commit message(Conventional Commits)'
task :commit_message do
msg = KKGit::CommitMessage.generate(mode: :staged)
ENV['KK_GIT_COMMIT_MESSAGE'] = msg.to_s
puts msg if msg
end
desc '根据工作区变更生成 commit message(含 untracked)'
task :commit_message_worktree do
msg = KKGit::CommitMessage.generate(mode: :worktree)
ENV['KK_GIT_COMMIT_MESSAGE'] = msg.to_s
puts msg if msg
end
desc '合并暂存区+工作区变更生成 commit message'
task :auto_commit do
msg = KKGit::CommitMessage.generate(mode: :all)
ENV['KK_GIT_COMMIT_MESSAGE'] = msg.to_s
puts msg if msg
end
desc '自动 add/commit/pull/push(基于 git:auto_commit)'
task :auto_commit_push do
if working_tree_clean?
puts '没有变更需要提交'
next
end
remote = ENV.fetch('KK_GIT_REMOTE', 'origin')
branch = ENV.fetch('KK_GIT_BRANCH', current_branch)
out, err, ok = run_cmd('git', 'add', '.')
ensure_ok!(ok, 'git add', stdout: out, stderr: err)
Rake::Task['git:auto_commit'].reenable
Rake::Task['git:auto_commit'].invoke
commit_message = ENV['KK_GIT_COMMIT_MESSAGE'].to_s.strip
commit_message = "chore(repo): 更新项目文件\n\n#{Time.now}" if commit_message.empty?
Tempfile.create('commit_message') do |f|
f.write(commit_message)
f.flush
out, err, ok = run_cmd('git', 'commit', '-F', f.path)
unless ok
if err.include?('nothing to commit') || out.include?('nothing to commit')
puts '没有暂存变更需要提交'
next
end
end
ensure_ok!(ok, 'git commit', stdout: out, stderr: err)
end
pull_args = ENV.fetch('KK_GIT_PULL_ARGS', '--ff-only').split
out, err, ok = run_cmd('git', 'pull', *pull_args)
ensure_ok!(ok, 'git pull', stdout: out, stderr: err)
out, err, ok = run_cmd('git', 'push', remote, branch)
ensure_ok!(ok, 'git push', stdout: out, stderr: err)
puts "✅ 已推送: #{remote} #{branch}"
end
end
end
|