GIT.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. Git = Git_Ctrl.git_Ctrol()#需要去掉
  8. def Main():
  9. global top,Git,PATH,bg,bbg,fg,Git_List,Last_Name
  10. PATH = os.getcwd()
  11. Git = Git_Ctrl.git_Ctrol()
  12. Git_List = []
  13. top = tkinter.Tk()
  14. Last_Name = None
  15. bg = '#FFFAFA' # 主颜色
  16. bbg = '#FFFAFA' # 按钮颜色
  17. fg = '#000000' # 文字颜色
  18. top["bg"] = bg
  19. FONT = ('黑体', 11) # 设置字体
  20. top.title('CoTan仓库管理器')
  21. top.resizable(width=False, height=False)
  22. top.geometry('+10+10') # 设置所在位置
  23. width_B = 13 # 标准宽度
  24. height_B = 2
  25. a_y = 0
  26. a_x = 0
  27. global clone_url
  28. a_y += 1
  29. tkinter.Label(top, text='克隆URL:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  30. Dic_Var = tkinter.StringVar()#当前的Dic
  31. clone_url = tkinter.Entry(top, width=width_B * 2, textvariable=Dic_Var)
  32. clone_url.grid(column=a_x+1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  33. a_y += 1
  34. tkinter.Button(top, bg=bbg, fg=fg, text='克隆仓库', command=clone_git, font=FONT, width=width_B,
  35. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  36. tkinter.Button(top, bg=bbg, fg=fg, text='打开仓库', command=init_git, font=FONT, width=width_B,
  37. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  38. tkinter.Button(top, bg=bbg, fg=fg, text='查看文件', command=get_Git_Dir, font=FONT, width=width_B,
  39. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  40. global Git_Box,Git_Dir
  41. a_y += 1
  42. Git_Box = tkinter.Listbox(top, width=width_B * 3, height=height_B * 5)
  43. Git_Box.grid(column=a_x, row=a_y, columnspan=3, rowspan=5, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  44. a_y += 5
  45. tkinter.Label(top, text='【仓库文件列表】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  46. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  47. a_y += 1
  48. Git_Dir = tkinter.Listbox(top, width=width_B * 3, height=height_B * 5)
  49. Git_Dir.grid(column=a_x, row=a_y, columnspan=3, rowspan=5, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  50. a_y += 5
  51. tkinter.Button(top, bg=bbg, fg=fg, text='添加暂存区文件', command=Add_File, font=FONT, width=width_B,
  52. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  53. tkinter.Button(top, bg=bbg, fg=fg, text='移除暂存区文件', command=init_git, font=FONT, width=width_B,
  54. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  55. tkinter.Button(top, bg=bbg, fg=fg, text='提交到git', command=Commit_File, font=FONT, width=width_B,
  56. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  57. global commit_m,head,master
  58. a_y += 1
  59. tkinter.Label(top, text='提交描述:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  60. commit_m = tkinter.Entry(top, width=width_B * 2)
  61. commit_m.grid(column=a_x + 1, row=a_y, columnspan=2, 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=lambda :not_Args(Git.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. a_y += 1
  70. tkinter.Button(top, bg=bbg, fg=fg, text='版本回退', command=Back_version, font=FONT, width=width_B,
  71. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  72. tkinter.Button(top, bg=bbg, fg=fg, text='文件回退', command=Back_File, font=FONT, width=width_B,
  73. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  74. tkinter.Button(top, bg=bbg, fg=fg, text='删除文件', command=rm_file, font=FONT, width=width_B,
  75. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  76. a_y += 1
  77. tkinter.Label(top, text='diff分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  78. master = tkinter.Entry(top, width=width_B * 2)
  79. master.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  80. a_y += 1
  81. tkinter.Label(top, text='回退版本号:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  82. head = tkinter.Entry(top, width=width_B * 2)
  83. head.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  84. top.mainloop()
  85. def rm_file():
  86. global Git,head
  87. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  88. if dic == '': return False
  89. do_Sys(Git.rm,(get_Name(),dic))
  90. update_Git_Dir()
  91. def Back_File():
  92. global Git,head
  93. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  94. if dic == '': return False
  95. do_Sys(Git.checkout_version,(get_Name(),dic))
  96. update_Git_Dir()
  97. def Back_version():
  98. global Git,head
  99. HEAD = head.get()
  100. if HEAD == '': HEAD = 'HEAD~1'
  101. do_Sys(Git.back_version,(get_Name(),HEAD))
  102. update_Git_Dir()
  103. def do_Sys(func,args,name='CoTan Git'):
  104. p = func(*args)
  105. p.wait()
  106. if p.returncode != 0: print(p.returncode)
  107. else:
  108. print('success')
  109. data = ''
  110. while True:
  111. i = p.stdout.readline().decode('utf-8')
  112. if i == '': break
  113. data += i
  114. if data.replace('\n','').replace(' ','') != '':show(data,name)
  115. def not_Args(func):
  116. global Git
  117. name = get_Name()
  118. do_Sys(func,(name,))
  119. update_Git_Dir()
  120. def Commit_File():
  121. global Git,commit_m
  122. m = commit_m.get()
  123. if m.replace(' ','') == '':
  124. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!')
  125. return False
  126. name = get_Name()
  127. do_Sys(Git.commit_File,(name,m))
  128. update_Git_Dir()
  129. def Diff_File():
  130. global Git,master
  131. MASTER = master.get()
  132. if MASTER == '':MASTER = 'HEAD'
  133. do_Sys(Git.diff_File,(get_Name(),MASTER))
  134. update_Git_Dir()
  135. def Add_File():
  136. global Git,Last_Name
  137. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  138. if dic == '':dic = '*'
  139. do_Sys(Git.add_File,(get_Name(),dic))
  140. update_Git_Dir()
  141. def update_Git_Dir():
  142. global Last_Name
  143. if Last_Name == None:return False
  144. Git_dir(Last_Name)
  145. def get_Git_Dir():
  146. name = get_Name()
  147. Git_dir(name)
  148. def Git_dir(name):
  149. global Git, Git_Dir,Last_Name
  150. dir_list = Git.get_Dir(name)
  151. Git_Dir.delete(0,tkinter.END)
  152. Git_Dir.insert(tkinter.END,*dir_list)
  153. Last_Name = name
  154. def clone_git():#克隆仓库
  155. global clone_url
  156. new_Dic = askdirectory(title = '选择仓库地址')
  157. if new_Dic == '':return False
  158. Git.Clone_init(new_Dic, clone_url.get())
  159. Updata_GitBox()
  160. def init_git():#创建仓库
  161. global Git
  162. new_Dic = askdirectory(title = '选择仓库地址')
  163. if new_Dic == '':return False
  164. Git.Add_init(new_Dic,)
  165. Updata_GitBox()
  166. def get_Name(): # 获得名字统一接口
  167. global Git,Git_List,Git_Box
  168. try:
  169. return Git_List[Git_Box.curselection()[0]]
  170. except:
  171. try:
  172. return Git_List[0]
  173. except:
  174. return None
  175. def Updata_GitBox():
  176. global Git,Git_List,Git_Box
  177. Git_List = list(Git.get_git_Dic().keys())
  178. Git_Box.delete(0,tkinter.END)
  179. Git_Box.insert(tkinter.END,*Git_List)
  180. def show(data,name):
  181. global bg, ft1
  182. new_top = tkinter.Toplevel(bg=bg)
  183. new_top.title(name)
  184. new_top.geometry('+10+10') # 设置所在位置
  185. text = ScrolledText(new_top, font=('黑体', 11), height=50)
  186. text.pack(fill=tkinter.BOTH)
  187. text.insert('0.0', data)
  188. text.config(state=tkinter.DISABLED)
  189. new_top.resizable(width=False, height=False)
  190. if __name__ == '__main__':
  191. Main()