-
Notifications
You must be signed in to change notification settings - Fork 2
/
GitWrapper.py
107 lines (85 loc) · 2.83 KB
/
GitWrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import git
import FreeCAD
import os
if FreeCAD.GuiUp:
import FreeCADGui
def commitchanges():
"""
Commit the changes of the current active file
"""
f = FreeCAD.ActiveDocument.FileName
if len(f) == 0:
# Not saved yet...
return
try:
repo = git.Repo(os.path.dirname(f), search_parent_directories=True)
except git.InvalidGitRepositoryError:
FreeCAD.Console.PrintMessage(u"{} is not a git repo. do nothing.\n".format(os.path.dirname(f)))
return
except git.NoSuchPathError:
FreeCAD.Console.PrintError(u"Ooops! The folder does not exist.\n")
return
# Add and commit the file
index = repo.index
index.add([f])
index.commit("[FreeCAD autocommit]")
def initrepo():
"""
Initialize a repo at the current file location
"""
f = FreeCAD.ActiveDocument.FileName
if len(f) == 0:
# Not saved yet...
return
repo_dir = os.path.dirname(f)
# TODO show dialogs, not just console output
try:
# TODO do we need this check? It seems like it doesnt matter if we init
# again... Repo still there, no harm taken.
repo = git.Repo(repo_dir, search_parent_directories=True)
FreeCAD.Console.PrintMessage(u"{} is already a git repo. do nothing.\n".format(repo_dir))
return
except git.InvalidGitRepositoryError:
pass
repo = git.Repo.init(repo_dir)
FreeCAD.Console.PrintMessage(u"Initialized a git repo at: {}".format(repo_dir))
# A commit should work now
commitchanges()
class CommandCommit:
"""
Command to commit the current file
"""
def GetResources(self):
return {'Pixmap': os.path.join(os.path.dirname(__file__),"icons",'git-commit.svg'),
'MenuText': "Commit",
'ToolTip': 'Commit the current file in git',
}
def Activated(self):
commitchanges()
class CommandTag:
"""
Command to create a new tag
"""
def GetResources(self):
return {'Pixmap': os.path.join(os.path.dirname(__file__),"icons",'tag.svg'),
'MenuText': "Tag",
'ToolTip': 'Tag the current commit',
}
def Activated(self):
# TODO show some dialog, enter a tag name, save
pass
class CommandCreate:
"""
Init a new git repo in the current folder
"""
def GetResources(self):
return {'Pixmap': os.path.join(os.path.dirname(__file__),"icons",'git.svg'),
'MenuText': "Init",
'ToolTip': 'Initialize a new git repository',
}
def Activated(self):
initrepo()
if FreeCAD.GuiUp:
FreeCADGui.addCommand('GitProject_CommandCommit',CommandCommit())
FreeCADGui.addCommand('GitProject_CommandTag',CommandTag())
FreeCADGui.addCommand('GitProject_CommandCreate',CommandCreate())