GIT.py 33 KB

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