1
0

GIT.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. import tkinter
  2. import tkinter.messagebox
  3. from tkinter.filedialog import askopenfilename, asksaveasfilename,askdirectory,askopenfilenames
  4. import os
  5. import Git_Ctrl
  6. from tkinter.scrolledtext import ScrolledText
  7. import time
  8. import threading
  9. str_code = 'utf-8'
  10. Git = Git_Ctrl.git_Ctrol()#需要去掉
  11. def Main():
  12. global top,Git,PATH,bg,bbg,fg,Git_List,Last_Name
  13. PATH = os.getcwd()
  14. Git = Git_Ctrl.git_Ctrol()
  15. Git_List = []
  16. top = tkinter.Tk()
  17. Last_Name = None
  18. bg = '#FFFAFA' # 主颜色
  19. bbg = '#FFFAFA' # 按钮颜色
  20. fg = '#000000' # 文字颜色
  21. top["bg"] = bg
  22. FONT = ('黑体', 11) # 设置字体
  23. top.title('CoTan仓库管理器')
  24. top.resizable(width=False, height=False)
  25. top.geometry('+10+10') # 设置所在位置
  26. width_B = 13 # 标准宽度
  27. height_B = 2
  28. a_y = 0
  29. a_x = 0
  30. global clone_url,Git_Box,Git_Dir
  31. global commit_m,head,master,no_ff
  32. global TagName,TagMessage,TagCommit,Show_Key
  33. global RemoteSSH,RemoteName,RemoteBranch,LocalBranch,push_bind,allow_history
  34. global BranchName, StashName, CommitName, BranchNOrigin
  35. tkinter.Button(top, bg=bbg, fg=fg, text='克隆仓库', command=clone_git, font=FONT, width=width_B,
  36. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  37. tkinter.Button(top, bg=bbg, fg=fg, text='打开仓库', command=init_git, font=FONT, width=width_B,
  38. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  39. tkinter.Button(top, bg=bbg, fg=fg, text='查看文件', command=get_Git_Dir, font=FONT, width=width_B,
  40. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  41. a_y += 1
  42. tkinter.Label(top, text='克隆URL:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  43. Dic_Var = tkinter.StringVar()#当前的Dic
  44. clone_url = tkinter.Entry(top, width=width_B * 2, textvariable=Dic_Var)
  45. clone_url.grid(column=a_x+1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  46. a_y += 1
  47. Git_Box = tkinter.Listbox(top, width=width_B * 3, height=height_B * 4)
  48. Git_Box.grid(column=a_x, row=a_y, columnspan=3, rowspan=4, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  49. a_y += 4
  50. tkinter.Label(top, text='【仓库文件列表】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  51. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  52. a_y += 1
  53. Git_Dir = tkinter.Listbox(top, width=width_B * 3, height=height_B * 4)
  54. Git_Dir.grid(column=a_x, row=a_y, columnspan=3, rowspan=4, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  55. a_y += 4
  56. tkinter.Button(top, bg=bbg, fg=fg, text='添加暂存区文件', command=Add_File, font=FONT, width=width_B,
  57. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  58. tkinter.Button(top, bg=bbg, fg=fg, text='移除暂存区文件', command=Reset_File, font=FONT, width=width_B,
  59. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  60. tkinter.Button(top, bg=bbg, fg=fg, text='提交到git', command=Commit_File, font=FONT, width=width_B,
  61. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  62. a_y += 1
  63. tkinter.Button(top, bg=bbg, fg=fg, text='查看执行日志', command=lambda :not_Args(Git.reflog), font=FONT, width=width_B,
  64. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  65. tkinter.Button(top, bg=bbg, fg=fg, text='查看文件日志', command=log, font=FONT, width=width_B,
  66. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  67. tkinter.Button(top, bg=bbg, fg=fg, text='查看状态', command=lambda :not_Args(Git.status), font=FONT, width=width_B,
  68. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  69. global log_Type
  70. a_y += 1
  71. log_Type = []
  72. lable = ['显示轴','commit完全显示','简化显示']#复选框
  73. for i in range(3):
  74. log_Type.append(tkinter.IntVar())
  75. tkinter.Checkbutton(top,bg = bg,fg = fg,activebackground=bg,activeforeground=fg,selectcolor=bg, text=lable[i],
  76. variable=log_Type[-1]).grid(column=a_x+i, row=a_y, sticky=tkinter.W)
  77. log_Type[-1].set(1)
  78. a_y += 1
  79. tkinter.Button(top, bg=bbg, fg=fg, text='版本回退', command=Back_version, font=FONT, width=width_B,
  80. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  81. tkinter.Button(top, bg=bbg, fg=fg, text='文件回退', command=Back_File, font=FONT, width=width_B,
  82. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  83. tkinter.Button(top, bg=bbg, fg=fg, text='删除文件', command=rm_file, font=FONT, width=width_B,
  84. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  85. a_y += 1
  86. tkinter.Button(top, bg=bbg, fg=fg, text='查看分支', command=lambda :not_Args(Git.check_Branch), font=FONT, width=width_B,
  87. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  88. tkinter.Button(top, bg=bbg, fg=fg, text='新建分支', command=make_Branch, font=FONT, width=width_B,
  89. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  90. tkinter.Button(top, bg=bbg, fg=fg, text='切换分支', command=switch_Branch, font=FONT, width=width_B,
  91. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  92. a_y += 1
  93. tkinter.Button(top, bg=bbg, fg=fg, text='删除分支', command=lambda :Delete_Branch(1), font=FONT, width=width_B,
  94. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  95. tkinter.Button(top, bg=bbg, fg=fg, text='丢弃分支', command=lambda :Delete_Branch(0), font=FONT, width=width_B,
  96. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  97. tkinter.Button(top, bg=bbg, fg=fg, text='合并分支', command=switch_Branch, font=FONT, width=width_B,
  98. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  99. no_ff = tkinter.Variable()
  100. a_y += 1
  101. tkinter.Button(top, bg=bbg, fg=fg, text='合并分支', command=merge_Branch, font=FONT, width=width_B,
  102. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  103. tkinter.Button(top, bg=bbg, fg=fg, text='退出冲突处理', command=lambda :not_Args(Git.merge_abort), font=FONT, width=width_B,
  104. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  105. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='使用快速合并',
  106. variable=no_ff).grid(column=a_x + 1, row=a_y, sticky=tkinter.W)
  107. no_ff.set(0)
  108. a_y += 1
  109. tkinter.Button(top, bg=bbg, fg=fg, text='保存工作区', command=lambda :not_Args(Git.Save_stash), font=FONT, width=width_B,
  110. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  111. tkinter.Button(top, bg=bbg, fg=fg, text='恢复工作区', command=lambda :Open_Stash(1), font=FONT, width=width_B,
  112. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  113. tkinter.Button(top, bg=bbg, fg=fg, text='删除工作区', command=lambda :Open_Stash(0), font=FONT, width=width_B,
  114. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  115. a_x += 3
  116. tkinter.Label(top, text='', bg=bg, fg=fg, font=FONT, width=1).grid(column=a_x, row=a_y) # 设置说明
  117. a_x += 1
  118. a_y = 0
  119. tkinter.Label(top, text='【参数操作】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  120. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  121. a_y += 1
  122. tkinter.Label(top, text='提交描述:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  123. commit_m = tkinter.Entry(top, width=width_B * 2)
  124. commit_m.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  125. a_y += 1
  126. tkinter.Label(top, text='diff分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  127. master = tkinter.Entry(top, width=width_B * 2)
  128. master.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  129. a_y += 1
  130. tkinter.Label(top, text='回退版本号:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  131. head = tkinter.Entry(top, width=width_B * 2)
  132. head.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  133. a_y += 1
  134. tkinter.Label(top, text='本地分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  135. BranchName = tkinter.Entry(top, width=width_B * 2)
  136. BranchName.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  137. a_y += 1
  138. tkinter.Label(top, text='远程分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  139. BranchNOrigin = tkinter.Entry(top, width=width_B * 2)
  140. BranchNOrigin.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  141. a_y += 1
  142. tkinter.Label(top, text='远程仓库链接:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  143. RemoteSSH = tkinter.Entry(top, width=width_B)
  144. RemoteSSH.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  145. a_y += 1
  146. tkinter.Label(top, text='远程仓库名:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  147. RemoteName = tkinter.Entry(top, width=width_B)
  148. RemoteName.grid(column=a_x + 1, row=a_y, columnspan=2,sticky=tkinter.E + tkinter.W)
  149. a_y += 1
  150. tkinter.Label(top, text='commit:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  151. CommitName = tkinter.Entry(top, width=width_B)
  152. CommitName.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  153. a_y += 1
  154. tkinter.Label(top, text='标签名字:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  155. TagName = tkinter.Entry(top, width=width_B)
  156. TagName.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  157. a_y += 1
  158. tkinter.Label(top, text='查询关键字:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  159. Show_Key = tkinter.Entry(top, width=width_B)
  160. Show_Key.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  161. a_y += 1
  162. tkinter.Label(top, text='工作区序号:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  163. StashName = tkinter.Entry(top, width=width_B)
  164. StashName.grid(column=a_x + 1, row=a_y,columnspan=2, sticky=tkinter.E + tkinter.W)
  165. a_y += 1
  166. tkinter.Button(top, bg=bbg, fg=fg, text='连接远程仓库', command=Add_remote, font=FONT, width=width_B,
  167. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  168. tkinter.Button(top, bg=bbg, fg=fg, text='推送到远程仓库', command=lambda :Pull_Push_remote(1), font=FONT, width=width_B,
  169. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  170. tkinter.Button(top, bg=bbg, fg=fg, text='从远程仓库抓取', command=lambda :Pull_Push_remote(0), font=FONT, width=width_B,
  171. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  172. push_bind = tkinter.Variable()
  173. allow_history = tkinter.Variable()
  174. a_y += 1
  175. tkinter.Button(top, bg=bbg, fg=fg, text='分支绑定', command=Bind_remote, font=FONT, width=width_B,
  176. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  177. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='无视历史记录',
  178. variable=allow_history).grid(column=a_x + 1, row=a_y, sticky=tkinter.W)
  179. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='推送时绑定',
  180. variable=push_bind).grid(column=a_x + 2, row=a_y, sticky=tkinter.W)
  181. allow_history.set(0)
  182. push_bind.set(0)
  183. a_y += 1
  184. tkinter.Button(top, bg=bbg, fg=fg, text='应用标签', command=add_tag, font=FONT, width=width_B,
  185. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  186. tkinter.Button(top, bg=bbg, fg=fg, text='查看已有标签', command=lambda :show_tag(1), font=FONT, width=width_B,
  187. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  188. tkinter.Button(top, bg=bbg, fg=fg, text='查询commit记录',command=lambda :show_tag(0), font=FONT, width=width_B,
  189. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  190. a_y += 1
  191. tkinter.Button(top, bg=bbg, fg=fg, text='推送标签', command=push_tag, font=FONT, width=width_B,
  192. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  193. tkinter.Button(top, bg=bbg, fg=fg, text='推送所有标签', command=push_alltag, font=FONT, width=width_B,
  194. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  195. tkinter.Button(top, bg=bbg, fg=fg, text='删除本地标签',command=del_Tag, font=FONT, width=width_B,
  196. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  197. a_y += 1
  198. tkinter.Button(top, bg=bbg, fg=fg, text='删除远程标签', command=del_Tag_remote, font=FONT, width=width_B,
  199. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  200. tkinter.Button(top, bg=bbg, fg=fg, text='删除远程分支', command=del_Branch_remote, font=FONT, width=width_B,
  201. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  202. tkinter.Button(top, bg=bbg, fg=fg, text='刷新远程分支', command=Fetch_remote, font=FONT, width=width_B,
  203. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  204. a_y += 1
  205. tkinter.Button(top, bg=bbg, fg=fg, text='commit补丁', command=cherry_pick, font=FONT, width=width_B,
  206. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  207. tkinter.Button(top, bg=bbg, fg=fg, text='删除远程仓库', command=cherry_pick, font=FONT, width=width_B,
  208. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  209. tkinter.Button(top, bg=bbg, fg=fg, text='工作区列表', command=lambda :not_Args(Git.Stash_List), font=FONT, width=width_B,
  210. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  211. # global Customize_Input, th_do, wait_do
  212. # a_y += 1
  213. # th_do = tkinter.Variable()
  214. # wait_do = tkinter.Variable()
  215. # tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='多进程刷新',
  216. # variable=th_do).grid(column=0, row=a_y, sticky=tkinter.W)
  217. # tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='异步显示',
  218. # variable=wait_do).grid(column=1, row=a_y, sticky=tkinter.W)
  219. # Customize_Input = tkinter.Entry(top, width=width_B * 3)
  220. # Customize_Input.grid(column=2, row=a_y, columnspan=4, sticky=tkinter.E + tkinter.W + tkinter.N + tkinter.S)
  221. #
  222. # tkinter.Button(top, bg=bbg, fg=fg, text='执行操作', command=Customize, font=FONT, width=width_B,
  223. # height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  224. # th_do.set(0)
  225. # wait_do.set(1)
  226. TagMessage = commit_m
  227. TagCommit = CommitName
  228. RemoteBranch = BranchNOrigin
  229. LocalBranch = BranchName
  230. top.mainloop()
  231. def clone_git():#克隆仓库
  232. global clone_url
  233. new_Dic = askdirectory(title = '选择仓库地址')
  234. if new_Dic == '':return False
  235. name = Git.Clone_init(new_Dic)
  236. Clone(name, clone_url.get())
  237. Updata_GitBox()
  238. def Clone(name,url):
  239. do_Sys(Git.Clone, (name, url),
  240. break_time=0,text_n=f'{url}:正在执行克隆操作',th=True,wait=True)
  241. Git.After_Clone(name)
  242. update_Git_Dir()
  243. def Customize():
  244. global Git, Customize_Input, th_do, wait_do
  245. command = Customize_Input.get()
  246. do_Sys(Git.Customize, (get_Name(), command),
  247. break_time=0,text_n=f'{command}:操作进行中',th=bool(th_do.get()),wait=bool(wait_do.get()))
  248. update_Git_Dir()
  249. def Fetch_remote():
  250. global RemoteBranch, LocalBranch, Git, RemoteName
  251. Branch = RemoteBranch.get()
  252. Remote = RemoteName.get()
  253. Local = LocalBranch.get()
  254. do_Sys(Git.Fetch, (get_Name(), Local, Remote, Branch),
  255. break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',th=True,wait=True)
  256. update_Git_Dir()
  257. def del_Tag():
  258. global Git, RemoteName, TagName
  259. Tag = TagName.get()
  260. do_Sys(Git.del_tag, (get_Name(),Tag))
  261. update_Git_Dir()
  262. def del_Branch_remote():
  263. global Git, RemoteName, TagName
  264. Remoto = RemoteName.get()
  265. Remoto_Branch = RemoteBranch.get()
  266. do_Sys(Git.del_Branch_remote, (get_Name(),Remoto,Remoto_Branch),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  267. th=True,wait=True)
  268. update_Git_Dir()
  269. def del_Tag_remote():
  270. global Git, RemoteName, TagName
  271. Remoto = RemoteName.get()
  272. Tag = TagName.get()
  273. do_Sys(Git.del_Tag_remote, (get_Name(),Remoto,Tag),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  274. th=True,wait=True)
  275. update_Git_Dir()
  276. def push_alltag():
  277. global Git, RemoteName
  278. Remoto = RemoteName.get()
  279. do_Sys(Git.push_allTag, (get_Name(),Remoto),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  280. th=True,wait=True)
  281. update_Git_Dir()
  282. def push_tag():
  283. global TagName, Git, RemoteName
  284. tag_name = TagName.get()
  285. Remoto = RemoteName.get()
  286. do_Sys(Git.push_Tag, (get_Name(), tag_name,Remoto),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  287. th=True,wait=True)
  288. update_Git_Dir()
  289. def add_tag():
  290. global TagName, Git, commit, tag_message
  291. tag_name = TagName.get()
  292. commit = TagCommit.get()
  293. tag_message = TagMessage.get()
  294. do_Sys(Git.Add_Tag, (get_Name(), tag_name,commit,tag_message),show=False)
  295. update_Git_Dir()
  296. def show_tag(type_):
  297. global Show_Key, Git
  298. key = Show_Key.get()
  299. do_Sys({1:Git.Tag,0:Git.show_new}.get(type_,Git.show_new), (get_Name(), key))
  300. update_Git_Dir()
  301. def Pull_Push_remote(type_):
  302. global RemoteBranch, LocalBranch, Git, allow_history, RemoteName
  303. Branch = RemoteBranch.get()
  304. Remote = RemoteName.get()
  305. Local = LocalBranch.get()
  306. allow = bool(allow_history.get())
  307. u = bool(push_bind.get())
  308. do_Sys({0:Git.Pull_remote,1:Git.Push_remote}.get(type_,Git.Pull_remote), (get_Name(), Local, Remote, Branch, allow, u),
  309. break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',th=True,wait=True)
  310. update_Git_Dir()
  311. def Bind_remote():
  312. global RemoteBranch, LocalBranch, Git
  313. Remote = RemoteBranch.get()
  314. Local = LocalBranch.get()
  315. do_Sys(Git.Bind_remote, (get_Name(), Local, Remote))
  316. update_Git_Dir()
  317. def Add_remote():
  318. global RemoteSSH, RemoteName, Git
  319. SSH = RemoteSSH.get()
  320. name = RemoteName.get()
  321. do_Sys(Git.Add_remote, (get_Name(), SSH, name))
  322. update_Git_Dir()
  323. def cherry_pick():
  324. global CommitName, Git
  325. commit = CommitName.get()
  326. do_Sys(Git.cherry_pick, (get_Name(), commit))
  327. update_Git_Dir()
  328. def Open_Stash(type_):
  329. global StashName, Git
  330. stash_num = StashName.get()
  331. if stash_num == '':stash_num = '0'
  332. do_Sys([Git.Drop_stash,Git.Apply_stash][type_], (get_Name(), stash_num))
  333. update_Git_Dir()
  334. def merge_Branch():
  335. global BranchName, Git, no_ff,commit_m
  336. m = commit_m.get()
  337. no = not bool(no_ff.get()) # 对于no_ff来说,True - 使用快速合并,所以要翻转
  338. if m.replace(' ','') == '' and no:
  339. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!'
  340. '(如果你不想添加描述,请使用快速合并,但我并不建议!)')
  341. return False
  342. name = BranchName.get()
  343. do_Sys(Git.merge_Branch, (get_Name(), name, no, m))
  344. update_Git_Dir()
  345. def Delete_Branch(type_):
  346. global BranchName, Git
  347. name = BranchName.get()
  348. do_Sys(Git.Del_Branch, (get_Name(), name, type_))
  349. update_Git_Dir()
  350. def switch_Branch():
  351. global BranchName,Git
  352. name = BranchName.get()
  353. do_Sys(Git.switch_Branch, (get_Name(), name),break_time=1,show=False)
  354. update_Git_Dir()
  355. def make_Branch():
  356. global BranchName,Git,BranchNOrigin
  357. name = BranchName.get()
  358. origin = BranchNOrigin.get()
  359. do_Sys(Git.new_Branch, (get_Name(), name, origin),break_time=1,show=False)
  360. update_Git_Dir()
  361. def check_Branch():
  362. global Git,head
  363. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  364. if dic == '': return False
  365. do_Sys(Git.rm,(get_Name(),dic))
  366. update_Git_Dir()
  367. def rm_file():
  368. global Git,head
  369. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  370. if dic == '': return False
  371. do_Sys(Git.rm,(get_Name(),dic))
  372. update_Git_Dir()
  373. def Back_File():
  374. global Git,head
  375. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  376. if dic == '': return False
  377. do_Sys(Git.checkout_version,(get_Name(),dic))
  378. update_Git_Dir()
  379. def Back_version():
  380. global Git,head
  381. HEAD = head.get()
  382. if HEAD == '': HEAD = 'HEAD~1'
  383. do_Sys(Git.back_version,(get_Name(),HEAD))
  384. update_Git_Dir()
  385. def do_Sys(func,args,name='CoTan Git',break_time=0,show=True,text_n='',th=False,wait=False,stop=True):
  386. p = func(*args)
  387. flat = True
  388. stopKey = Git_Ctrl.stopKey
  389. def Out_Txt():
  390. nonlocal data
  391. dic = asksaveasfilename(title='选择文件保存位置',filetypes=[("TXT", ".txt")])
  392. try:
  393. if dic == '':return False
  394. if dic[-4] == '.txt':pass
  395. else:raise Exception
  396. except:
  397. dic += '.txt'
  398. with open(dic,'w',encoding='utf-8') as f:
  399. f.write(data)
  400. kb = True
  401. sb = True
  402. def update_b():
  403. if not kb:
  404. b_list[1].config(state=tkinter.DISABLED)
  405. if not sb:
  406. b_list[0].config(state=tkinter.DISABLED)
  407. def Stop():
  408. nonlocal start,kb,sb
  409. start = 0
  410. sb = False
  411. kb = False
  412. update_b()
  413. def keep():
  414. nonlocal start,kb
  415. start = float('inf')
  416. kb = False
  417. update_b()
  418. def pipe():pass
  419. def not_out():
  420. nonlocal text,out_data,data,flat
  421. text.clear()
  422. if flat:
  423. text.insert(tkinter.END,data)
  424. else:
  425. text.insert(tkinter.END, out_data)
  426. flat = not flat
  427. start = time.time()
  428. data = ''
  429. out_data = ''#包含out的data
  430. if show:
  431. text, new_top, b_list = show_Now(Out_Txt,Stop,keep,not_out,pipe,name=name)#[close,keep]
  432. update_b()
  433. if text_n != '':
  434. text.insert('0.0',f'载入前提示>>> {text_n}\n')
  435. out_data += f'载入前提示>>> {text_n}\n'
  436. data += f'{text_n}\n'
  437. new_top.update()
  438. top.update()
  439. def Update():
  440. nonlocal start
  441. while True:
  442. try:
  443. top.update()
  444. if show:
  445. try:
  446. new_top.update()
  447. except:pass
  448. if time.time() - start >= break_time and break_time != 0:
  449. raise Exception
  450. elif break_time == 0 and start == 0:
  451. raise Exception
  452. except:
  453. start = 0
  454. break
  455. if th or not wait:
  456. j = threading.Thread(target=Update)#如果没有启动到多进程的效果,请检查Update是不是加了(),这里需要回调
  457. j.start()
  458. if wait:#等待后显示
  459. if break_time == 0:break_ti = None# 此处break_ti是为了别面覆盖break_time,因为Update进程需要用
  460. else:break_ti = break_time
  461. def wait_p():
  462. nonlocal start
  463. p.wait(break_ti)
  464. start = 0
  465. j = threading.Thread(target=wait_p) # 这么做不是多此一举,如果没有wait,进程并不会退出
  466. j.start()
  467. Update()#遇到sleep等主线程阻塞,top.update等会阻塞子线程,因此,必须保证主线程不会被wait所阻塞
  468. out = p.stdout.read().split('\n')
  469. for i in out:
  470. if show:
  471. try: # 如果界面被关掉了,会报错
  472. new_top.title(f'{name} : 运行中')
  473. except:
  474. text, new_top, b_list = show_Now(Out_Txt, Stop, keep, not_out, pipe,
  475. name=f'{name} : 运行中')
  476. update_b()
  477. text.insert(tkinter.END, out_data)
  478. if stop and i.replace(' ', '').replace('\n', '') != stopKey:
  479. text.insert(tkinter.END, f'[out]> {i}\n')
  480. data += i + '\n'
  481. out_data += f'[out]> {i}\n'
  482. if show:
  483. text.insert(tkinter.END, '[END]')
  484. out_data += f'[END]'
  485. data += f'[END]'
  486. start = 0
  487. else:#即时显示
  488. while True:
  489. #界面设置
  490. try:#如果界面被关掉了,会报错
  491. if show: new_top.title(f'{name} : 运行中')
  492. except:
  493. text, new_top, b_list = show_Now(Out_Txt, Stop, keep, not_out, pipe,
  494. name=f'{name} : 运行中')
  495. update_b()
  496. text.insert(tkinter.END,out_data)
  497. #界面刷新
  498. try:
  499. if not th:
  500. top.update()
  501. if show: new_top.update()
  502. except:
  503. break
  504. #输出字符
  505. try:
  506. i = p.stdout.readline()#.decode(str_code)#不需要decode,因为Popen已经设置了universal_newlines=True
  507. bool_text = i.replace(' ','').replace('\n','')
  508. if bool_text != '':
  509. if stop and bool_text == stopKey:
  510. start = 0
  511. else:
  512. if show: text.insert(tkinter.END, f'[out]> {i}')
  513. data += i
  514. out_data += f'[out]> {i}'
  515. if p.returncode == 0 or (time.time() - start >= break_time and break_time != 0) or (break_time == 0 and start == 0):
  516. if show:
  517. text.insert(tkinter.END,'[END]')
  518. out_data += f'[END]'
  519. data += f'[END]'
  520. break
  521. elif p.returncode != None:
  522. raise Exception
  523. except:
  524. try:
  525. if show:
  526. text.insert(tkinter.END, '[ERROR]')
  527. out_data += f'[ERROR]'
  528. data += f'[ERROR]'
  529. raise Exception
  530. except:break
  531. try: # 如果界面被关掉了,会报错
  532. if show: new_top.title(f'{name} : 运行完毕')
  533. except:pass
  534. p.kill()
  535. try:
  536. if show:
  537. b_list[0].config(state=tkinter.DISABLED)
  538. b_list[1].config(state=tkinter.DISABLED)
  539. except:pass
  540. return data
  541. def log():
  542. global Git,log_Type
  543. name = get_Name()
  544. graph = bool(log_Type[0].get())
  545. abbrev = bool(log_Type[1].get())
  546. pretty = bool(log_Type[2].get())
  547. do_Sys(Git.log,(name,graph,pretty,abbrev))
  548. update_Git_Dir()
  549. def not_Args(func):
  550. global Git
  551. name = get_Name()
  552. do_Sys(func,(name,))
  553. update_Git_Dir()
  554. def Commit_File():
  555. global Git,commit_m
  556. m = commit_m.get()
  557. if m.replace(' ','') == '':
  558. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!')
  559. return False
  560. name = get_Name()
  561. do_Sys(Git.commit_File,(name,m))
  562. update_Git_Dir()
  563. def Diff_File():
  564. global Git,master
  565. MASTER = master.get()
  566. if MASTER == '':MASTER = 'HEAD'
  567. do_Sys(Git.diff_File,(get_Name(),MASTER))
  568. update_Git_Dir()
  569. def Reset_File():
  570. global Git,Last_Name
  571. dic = askopenfilenames(title=f'选择要撤销add的文件(取消为全选)')
  572. if dic == '':dic = '.'
  573. do_Sys(Git.reset_File,(get_Name(),dic))
  574. update_Git_Dir()
  575. def Add_File():
  576. global Git,Last_Name
  577. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  578. if dic == '':dic = '.'
  579. do_Sys(Git.add_File,(get_Name(),dic))
  580. update_Git_Dir()
  581. def update_Git_Dir():
  582. global Last_Name
  583. if Last_Name == None:return False
  584. Git_dir(Last_Name)
  585. def get_Git_Dir():
  586. name = get_Name()
  587. Git_dir(name)
  588. def Git_dir(name):
  589. global Git, Git_Dir,Last_Name
  590. dir_list = Git.get_Dir(name)
  591. try:#窗口可能已经关闭
  592. Git_Dir.delete(0,tkinter.END)
  593. Git_Dir.insert(tkinter.END,*dir_list)
  594. except:
  595. pass
  596. Last_Name = name
  597. def init_git():#创建仓库
  598. global Git
  599. new_Dic = askdirectory(title = '选择仓库地址')
  600. if new_Dic == '':return False
  601. Git.Add_init(new_Dic,)
  602. Updata_GitBox()
  603. def get_Name(): # 获得名字统一接口
  604. global Git,Git_List,Git_Box
  605. try:
  606. return Git_List[Git_Box.curselection()[0]]
  607. except:
  608. try:
  609. return Git_List[0]
  610. except:
  611. return None
  612. def Updata_GitBox():
  613. global Git,Git_List,Git_Box
  614. Git_List = list(Git.get_git_Dic().keys())
  615. Git_Box.delete(0,tkinter.END)
  616. Git_Box.insert(tkinter.END,*Git_List)
  617. def show(data='',name='CoTan_Git'):
  618. global bg, ft1
  619. new_top = tkinter.Toplevel(bg=bg)
  620. new_top.title(name)
  621. new_top.geometry('+10+10') # 设置所在位置
  622. text = ScrolledText(new_top, font=('黑体', 11), height=50)
  623. text.pack(fill=tkinter.BOTH)
  624. text.insert('0.0', data)
  625. text.config(state=tkinter.DISABLED)
  626. new_top.resizable(width=False, height=False)
  627. def show_Now(out_func,close_func,keepFunc,not_out,pipeFunc,name='CoTan_Git >>> 高级命令行'):
  628. global bg
  629. new_top = tkinter.Toplevel(bg=bg)
  630. new_top.title(name)
  631. new_top.geometry('+10+10') # 设置所在位置
  632. new_top.resizable(width=False, height=False)
  633. class ScrolledText_new(ScrolledText):
  634. def __init__(self,*args,**kwargs):
  635. super(ScrolledText_new, self).__init__(*args,**kwargs)
  636. def insert(self, index, chars, *args):
  637. text.config(state=tkinter.NORMAL)
  638. super(ScrolledText_new, self).insert(index, chars, *args)
  639. text.config(state=tkinter.DISABLED)
  640. def clear(self):
  641. text.config(state=tkinter.NORMAL)
  642. self.delete('0.0',tkinter.END)
  643. text.config(state=tkinter.DISABLED)
  644. text = ScrolledText_new(new_top, font=('黑体', 11), height=30,width=100)
  645. text.grid(column=0, row=0,columnspan=5, sticky=tkinter.E + tkinter.W)
  646. text.config(state=tkinter.DISABLED)
  647. tkinter.Button(new_top, bg=bg, fg=fg, text='输出文档', font=('黑体', 11),width=20, height=2, command=out_func).grid(
  648. column=4, row=1, sticky=tkinter.E + tkinter.W)
  649. close = tkinter.Button(new_top, bg=bg, fg=fg, text='关闭子线程连接', font=('黑体', 11),width=20, height=2, command=close_func)
  650. close.grid(column=0, row=1, sticky=tkinter.E + tkinter.W)
  651. keep = tkinter.Button(new_top, bg=bg, fg=fg, text='保持线程连接', font=('黑体', 11),width=20, height=2, command=keepFunc)
  652. keep.grid(column=1, row=1, sticky=tkinter.E + tkinter.W)
  653. tkinter.Button(new_top, bg=bg, fg=fg, text='格式化输出', font=('黑体', 11),width=20, height=2, command=not_out).grid(
  654. column=2, row=1, sticky=tkinter.E + tkinter.W)
  655. tkinter.Button(new_top, bg=bg, fg=fg, text='文件管道输入', font=('黑体', 11),width=20, height=2, command=pipeFunc).grid(
  656. column=3, row=1, sticky=tkinter.E + tkinter.W)
  657. return text,new_top,[close,keep]
  658. if __name__ == '__main__':
  659. Main()