Consolidated model classes into a single class
[ugit.git] / py / models.py
blob99b45ec73e94bd11dbc7c4d3680559f39b4f6b6d
1 import os
2 import re
3 import commands
4 import cmds
5 from model import Model
7 class GitModel(Model):
8 def __init__ (self):
9 Model.__init__ (self, {
10 # ===========================================
11 # Used in various places
12 # ===========================================
13 'branch': '',
15 # ===========================================
16 # Used primarily by the main UI
17 # ===========================================
18 'name': cmds.git_config('user.name'),
19 'email': cmds.git_config('user.email'),
20 'commitmsg': '',
21 'staged': [],
22 'unstaged': [],
23 'untracked': [],
25 # ===========================================
26 # Used by the create branch dialog
27 # ===========================================
28 'revision': '',
29 'local_branches': cmds.git_branch (remote=False),
30 'remote_branches': cmds.git_branch (remote=True),
31 'tags': cmds.git_tag(),
33 # ===========================================
34 # Used by the repo browser
35 # ===========================================
36 'directory': '',
38 # These are parallel lists
39 'files': [],
40 'sha1s': [],
41 'types': [],
43 # All items below here are re-calculated in
44 # init_browser_data()
45 'directories': [],
46 'directory_entries': {},
48 # These are also parallel lists
49 'item_names': [],
50 'item_sha1s': [],
51 'item_types': [],
55 def init_branch_data (self):
56 remote_branches = cmds.git_branch (remote=True)
57 local_branches = cmds.git_branch (remote=False)
58 tags = cmds.git_tag()
60 self.set_branch ('')
61 self.set_revision ('')
62 self.set_local_branches (local_branches)
63 self.set_remote_branches (remote_branches)
64 self.set_tags (tags)
66 def init_browser_data (self):
67 '''This scans over self.(files, sha1s, types) to generate
68 directories, directory_entries, itmes, item_sha1s,
69 and item_types.'''
71 # Collect data for the model
72 if not self.get_branch(): return
74 self.item_names = []
75 self.item_sha1s = []
76 self.item_types = []
77 self.directories = []
78 self.directory_entries = {}
80 # Lookup the tree info
81 tree_info = cmds.git_ls_tree (self.get_branch())
83 self.set_types (map ( lambda (x): x[1], tree_info ))
84 self.set_sha1s (map ( lambda (x): x[2], tree_info ))
85 self.set_files (map ( lambda (x): x[3], tree_info ))
87 if self.directory: self.directories.append ('..')
89 dir_entries = self.directory_entries
90 dir_regex = re.compile ('([^/]+)/')
91 dirs_seen = {}
92 subdirs_seen = {}
94 for idx, file in enumerate (self.files):
96 orig_file = str (file)
97 if not orig_file.startswith (self.directory): continue
98 file = file[ len (self.directory): ]
100 if file.count ('/'):
101 # This is a directory...
102 match = dir_regex.match (file)
103 if not match: continue
105 dirent = match.group (1) + '/'
106 if dirent not in self.directory_entries:
107 self.directory_entries[dirent] = []
109 if dirent not in dirs_seen:
110 dirs_seen[dirent] = True
111 self.directories.append (dirent)
113 entry = file.replace (dirent, '')
114 entry_match = dir_regex.match (entry)
115 if entry_match:
116 subdir = entry_match.group (1) + '/'
117 if subdir in subdirs_seen: continue
118 subdirs_seen[subdir] = True
119 dir_entries[dirent].append (subdir)
120 else:
121 dir_entries[dirent].append (entry)
122 else:
123 self.item_names.append (file)
124 self.item_sha1s.append (self.sha1s[idx])
125 self.item_types.append (self.types[idx])
127 def add_signoff (self):
128 '''Adds a standard Signed-off by: tag to the end
129 of the current commit message.'''
131 msg = self.get_commitmsg()
132 signoff = ('Signed-off by: %s <%s>'
133 % (self.get_name(), self.get_email()))
135 if signoff not in msg:
136 self.set_commitmsg (msg + '\n\n' + signoff)
138 def set_latest_commitmsg (self):
139 '''Queries git for the latest commit message and sets it in
140 self.commitmsg.'''
141 commit_msg = []
142 commit_lines = cmds.git_show ('HEAD').split ('\n')
143 for idx, msg in enumerate (commit_lines):
144 if idx < 4: continue
145 msg = msg.lstrip()
146 if msg.startswith ('diff --git'):
147 commit_msg.pop()
148 break
149 commit_msg.append (msg)
150 self.set_commitmsg ('\n'.join (commit_msg).rstrip())
152 def get_uncommitted_item (self, row):
153 return (self.get_unstaged() + self.get_untracked())[row]