GIT.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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=Del_remote, 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. f = tkinter.messagebox.askokcancel('提示', f'是否需要强制推送?(强制推送不被建议)')
  309. do_Sys({0:Git.Pull_remote,1:Git.Push_remote}.get(type_,Git.Pull_remote), (get_Name(), Local, Remote, Branch, allow, u, f),
  310. break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',th=True,wait=True)
  311. update_Git_Dir()
  312. def Bind_remote():
  313. global RemoteBranch, LocalBranch, Git
  314. Remote = RemoteBranch.get()
  315. Local = LocalBranch.get()
  316. do_Sys(Git.Bind_remote, (get_Name(), Local, Remote))
  317. update_Git_Dir()
  318. def Del_remote():
  319. global RemoteSSH, RemoteName, Git
  320. name = RemoteName.get()
  321. do_Sys(Git.Del_remote, (get_Name(), name))
  322. update_Git_Dir()
  323. def Add_remote():
  324. global RemoteSSH, RemoteName, Git
  325. SSH = RemoteSSH.get()
  326. name = RemoteName.get()
  327. do_Sys(Git.Add_remote, (get_Name(), SSH, name))
  328. update_Git_Dir()
  329. def cherry_pick():
  330. global CommitName, Git
  331. commit = CommitName.get()
  332. do_Sys(Git.cherry_pick, (get_Name(), commit))
  333. update_Git_Dir()
  334. def Open_Stash(type_):
  335. global StashName, Git
  336. stash_num = StashName.get()
  337. if stash_num == '':stash_num = '0'
  338. do_Sys([Git.Drop_stash,Git.Apply_stash][type_], (get_Name(), stash_num))
  339. update_Git_Dir()
  340. def merge_Branch():
  341. global BranchName, Git, no_ff,commit_m
  342. m = commit_m.get()
  343. no = not bool(no_ff.get()) # 对于no_ff来说,True - 使用快速合并,所以要翻转
  344. if m.replace(' ','') == '' and no:
  345. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!'
  346. '(如果你不想添加描述,请使用快速合并,但我并不建议!)')
  347. return False
  348. name = BranchName.get()
  349. do_Sys(Git.merge_Branch, (get_Name(), name, no, m))
  350. update_Git_Dir()
  351. def Delete_Branch(type_):
  352. global BranchName, Git
  353. name = BranchName.get()
  354. do_Sys(Git.Del_Branch, (get_Name(), name, type_))
  355. update_Git_Dir()
  356. def switch_Branch():
  357. global BranchName,Git
  358. name = BranchName.get()
  359. do_Sys(Git.switch_Branch, (get_Name(), name),break_time=1,show=False)
  360. update_Git_Dir()
  361. def make_Branch():
  362. global BranchName,Git,BranchNOrigin
  363. name = BranchName.get()
  364. origin = BranchNOrigin.get()
  365. do_Sys(Git.new_Branch, (get_Name(), name, origin),break_time=1,show=False)
  366. update_Git_Dir()
  367. def check_Branch():
  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 rm_file():
  374. global Git,head
  375. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  376. if dic == '': return False
  377. do_Sys(Git.rm,(get_Name(),dic))
  378. update_Git_Dir()
  379. def Back_File():
  380. global Git,head
  381. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  382. if dic == '': return False
  383. do_Sys(Git.checkout_version,(get_Name(),dic))
  384. update_Git_Dir()
  385. def Back_version():
  386. global Git,head
  387. HEAD = head.get()
  388. if HEAD == '': HEAD = 'HEAD~1'
  389. do_Sys(Git.back_version,(get_Name(),HEAD))
  390. update_Git_Dir()
  391. def do_Sys(func,args,name='CoTan Git',break_time=0,show=True,text_n='',th=False,wait=False,stop=True):
  392. p = func(*args)
  393. flat = True
  394. stopKey = Git_Ctrl.stopKey
  395. def Out_Txt():
  396. nonlocal data
  397. dic = asksaveasfilename(title='选择文件保存位置',filetypes=[("TXT", ".txt")])
  398. try:
  399. if dic == '':return False
  400. if dic[-4] == '.txt':pass
  401. else:raise Exception
  402. except:
  403. dic += '.txt'
  404. with open(dic,'w',encoding='utf-8') as f:
  405. f.write(data)
  406. kb = True
  407. sb = True
  408. def update_b():
  409. if not kb:
  410. b_list[1].config(state=tkinter.DISABLED)
  411. if not sb:
  412. b_list[0].config(state=tkinter.DISABLED)
  413. def Stop():
  414. nonlocal start,kb,sb
  415. start = 0
  416. sb = False
  417. kb = False
  418. update_b()
  419. def keep():
  420. nonlocal start,kb
  421. start = float('inf')
  422. kb = False
  423. update_b()
  424. def pipe():pass
  425. def not_out():
  426. nonlocal text,out_data,data,flat
  427. text.clear()
  428. if flat:
  429. text.insert(tkinter.END,data)
  430. else:
  431. text.insert(tkinter.END, out_data)
  432. flat = not flat
  433. start = time.time()
  434. data = ''
  435. out_data = ''#包含out的data
  436. if show:
  437. text, new_top, b_list = show_Now(Out_Txt,Stop,keep,not_out,pipe,name=name)#[close,keep]
  438. update_b()
  439. if text_n != '':
  440. text.insert('0.0',f'载入前提示>>> {text_n}\n')
  441. out_data += f'载入前提示>>> {text_n}\n'
  442. data += f'{text_n}\n'
  443. new_top.update()
  444. top.update()
  445. def Update():
  446. nonlocal start
  447. while True:
  448. try:
  449. top.update()
  450. if show:
  451. try:
  452. new_top.update()
  453. except:pass
  454. if time.time() - start >= break_time and break_time != 0:
  455. raise Exception
  456. elif break_time == 0 and start == 0:
  457. raise Exception
  458. except:
  459. start = 0
  460. break
  461. if th or not wait:
  462. j = threading.Thread(target=Update)#如果没有启动到多进程的效果,请检查Update是不是加了(),这里需要回调
  463. j.start()
  464. if wait:#等待后显示
  465. if break_time == 0:break_ti = None# 此处break_ti是为了别面覆盖break_time,因为Update进程需要用
  466. else:break_ti = break_time
  467. def wait_p():
  468. nonlocal start
  469. p.wait(break_ti)
  470. start = 0
  471. j = threading.Thread(target=wait_p) # 这么做不是多此一举,如果没有wait,进程并不会退出
  472. j.start()
  473. Update()#遇到sleep等主线程阻塞,top.update等会阻塞子线程,因此,必须保证主线程不会被wait所阻塞
  474. out = p.stdout.read().split('\n')
  475. for i in out:
  476. if show:
  477. try: # 如果界面被关掉了,会报错
  478. new_top.title(f'{name} : 运行中')
  479. except:
  480. text, new_top, b_list = show_Now(Out_Txt, Stop, keep, not_out, pipe,
  481. name=f'{name} : 运行中')
  482. update_b()
  483. text.insert(tkinter.END, out_data)
  484. if stop and i.replace(' ', '').replace('\n', '') != stopKey:
  485. text.insert(tkinter.END, f'[out]> {i}\n')
  486. data += i + '\n'
  487. out_data += f'[out]> {i}\n'
  488. if show:
  489. text.insert(tkinter.END, '[END]')
  490. out_data += f'[END]'
  491. data += f'[END]'
  492. start = 0
  493. else:#即时显示
  494. while True:
  495. #界面设置
  496. try:#如果界面被关掉了,会报错
  497. if show: new_top.title(f'{name} : 运行中')
  498. except:
  499. text, new_top, b_list = show_Now(Out_Txt, Stop, keep, not_out, pipe,
  500. name=f'{name} : 运行中')
  501. update_b()
  502. text.insert(tkinter.END,out_data)
  503. #界面刷新
  504. try:
  505. if not th:
  506. top.update()
  507. if show: new_top.update()
  508. except:
  509. break
  510. #输出字符
  511. try:
  512. i = p.stdout.readline()#.decode(str_code)#不需要decode,因为Popen已经设置了universal_newlines=True
  513. bool_text = i.replace(' ','').replace('\n','')
  514. if bool_text != '':
  515. if stop and bool_text == stopKey:
  516. start = 0
  517. else:
  518. if show: text.insert(tkinter.END, f'[out]> {i}')
  519. data += i
  520. out_data += f'[out]> {i}'
  521. if p.returncode == 0 or (time.time() - start >= break_time and break_time != 0) or (break_time == 0 and start == 0):
  522. if show:
  523. text.insert(tkinter.END,'[END]')
  524. out_data += f'[END]'
  525. data += f'[END]'
  526. break
  527. elif p.returncode != None:
  528. raise Exception
  529. except:
  530. try:
  531. if show:
  532. text.insert(tkinter.END, '[ERROR]')
  533. out_data += f'[ERROR]'
  534. data += f'[ERROR]'
  535. raise Exception
  536. except:break
  537. try: # 如果界面被关掉了,会报错
  538. if show: new_top.title(f'{name} : 运行完毕')
  539. except:pass
  540. p.kill()
  541. try:
  542. if show:
  543. b_list[0].config(state=tkinter.DISABLED)
  544. b_list[1].config(state=tkinter.DISABLED)
  545. except:pass
  546. return data
  547. def log():
  548. global Git,log_Type
  549. name = get_Name()
  550. graph = bool(log_Type[0].get())
  551. abbrev = bool(log_Type[1].get())
  552. pretty = bool(log_Type[2].get())
  553. do_Sys(Git.log,(name,graph,pretty,abbrev))
  554. update_Git_Dir()
  555. def not_Args(func):
  556. global Git
  557. name = get_Name()
  558. do_Sys(func,(name,))
  559. update_Git_Dir()
  560. def Commit_File():
  561. global Git,commit_m
  562. m = commit_m.get()
  563. if m.replace(' ','') == '':
  564. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!')
  565. return False
  566. name = get_Name()
  567. do_Sys(Git.commit_File,(name,m))
  568. update_Git_Dir()
  569. def Diff_File():
  570. global Git,master
  571. MASTER = master.get()
  572. if MASTER == '':MASTER = 'HEAD'
  573. do_Sys(Git.diff_File,(get_Name(),MASTER))
  574. update_Git_Dir()
  575. def Reset_File():
  576. global Git,Last_Name
  577. dic = askopenfilenames(title=f'选择要撤销add的文件(取消为全选)')
  578. if dic == '':dic = '.'
  579. do_Sys(Git.reset_File,(get_Name(),dic))
  580. update_Git_Dir()
  581. def Add_File():
  582. global Git,Last_Name
  583. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  584. if dic == '':dic = '.'
  585. do_Sys(Git.add_File,(get_Name(),dic))
  586. update_Git_Dir()
  587. def update_Git_Dir():
  588. global Last_Name
  589. if Last_Name == None:return False
  590. Git_dir(Last_Name)
  591. def get_Git_Dir():
  592. name = get_Name()
  593. Git_dir(name)
  594. def Git_dir(name):
  595. global Git, Git_Dir,Last_Name
  596. dir_list = Git.get_Dir(name)
  597. try:#窗口可能已经关闭
  598. Git_Dir.delete(0,tkinter.END)
  599. Git_Dir.insert(tkinter.END,*dir_list)
  600. except:
  601. pass
  602. Last_Name = name
  603. def init_git():#创建仓库
  604. global Git
  605. new_Dic = askdirectory(title = '选择仓库地址')
  606. if new_Dic == '':return False
  607. Git.Add_init(new_Dic,)
  608. Updata_GitBox()
  609. def get_Name(): # 获得名字统一接口
  610. global Git,Git_List,Git_Box
  611. try:
  612. return Git_List[Git_Box.curselection()[0]]
  613. except:
  614. try:
  615. return Git_List[0]
  616. except:
  617. return None
  618. def Updata_GitBox():
  619. global Git,Git_List,Git_Box
  620. Git_List = list(Git.get_git_Dic().keys())
  621. Git_Box.delete(0,tkinter.END)
  622. Git_Box.insert(tkinter.END,*Git_List)
  623. def show(data='',name='CoTan_Git'):
  624. global bg, ft1
  625. new_top = tkinter.Toplevel(bg=bg)
  626. new_top.title(name)
  627. new_top.geometry('+10+10') # 设置所在位置
  628. text = ScrolledText(new_top, font=('黑体', 11), height=50)
  629. text.pack(fill=tkinter.BOTH)
  630. text.insert('0.0', data)
  631. text.config(state=tkinter.DISABLED)
  632. new_top.resizable(width=False, height=False)
  633. def show_Now(out_func,close_func,keepFunc,not_out,pipeFunc,name='CoTan_Git >>> 高级命令行'):
  634. global bg
  635. new_top = tkinter.Toplevel(bg=bg)
  636. new_top.title(name)
  637. new_top.geometry('+10+10') # 设置所在位置
  638. new_top.resizable(width=False, height=False)
  639. class ScrolledText_new(ScrolledText):
  640. def __init__(self,*args,**kwargs):
  641. super(ScrolledText_new, self).__init__(*args,**kwargs)
  642. def insert(self, index, chars, *args):
  643. text.config(state=tkinter.NORMAL)
  644. super(ScrolledText_new, self).insert(index, chars, *args)
  645. text.config(state=tkinter.DISABLED)
  646. def clear(self):
  647. text.config(state=tkinter.NORMAL)
  648. self.delete('0.0',tkinter.END)
  649. text.config(state=tkinter.DISABLED)
  650. text = ScrolledText_new(new_top, font=('黑体', 11), height=30,width=100)
  651. text.grid(column=0, row=0,columnspan=5, sticky=tkinter.E + tkinter.W)
  652. text.config(state=tkinter.DISABLED)
  653. tkinter.Button(new_top, bg=bg, fg=fg, text='输出文档', font=('黑体', 11),width=20, height=2, command=out_func).grid(
  654. column=4, row=1, sticky=tkinter.E + tkinter.W)
  655. close = tkinter.Button(new_top, bg=bg, fg=fg, text='关闭子线程连接', font=('黑体', 11),width=20, height=2, command=close_func)
  656. close.grid(column=0, row=1, sticky=tkinter.E + tkinter.W)
  657. keep = tkinter.Button(new_top, bg=bg, fg=fg, text='保持线程连接', font=('黑体', 11),width=20, height=2, command=keepFunc)
  658. keep.grid(column=1, row=1, sticky=tkinter.E + tkinter.W)
  659. tkinter.Button(new_top, bg=bg, fg=fg, text='格式化输出', font=('黑体', 11),width=20, height=2, command=not_out).grid(
  660. column=2, row=1, sticky=tkinter.E + tkinter.W)
  661. tkinter.Button(new_top, bg=bg, fg=fg, text='文件管道输入', font=('黑体', 11),width=20, height=2, command=pipeFunc).grid(
  662. column=3, row=1, sticky=tkinter.E + tkinter.W)
  663. return text,new_top,[close,keep]
  664. if __name__ == '__main__':
  665. Main()