Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding alias support to the plugin.

Improving some error handling, cleaning and adding more code comments.
  • Loading branch information...
commit 07d2a3e9d9eb391b5e3bb74e4c796ae8b68d90fe 1 parent ce37318
Miguel Araujo authored October 25, 2010

Showing 1 changed file with 43 additions and 15 deletions. Show diff stats Hide diff stats

  1. 58  mercurialchangeset/admin.py
58  mercurialchangeset/admin.py
@@ -96,7 +96,7 @@ def get_repository_id(self, repository):
96 96
             """
97 97
             self.cursor.execute(sql_string)
98 98
             row = self.cursor.fetchone()
99  
-            # Default is an special situation, we need to handle different
  99
+            # Default is an special situation, we need to handle it different
100 100
             # Default dir could be specified in repository table or in trac.ini
101 101
             self.default = True
102 102
             return row[0]
@@ -122,11 +122,11 @@ def get_repository_id(self, repository):
122 122
             # If the repository is not controlled by Trac
123 123
             # We exit with successful execution. This way we can make a generic 
124 124
             # Mercurial hook that will work with all repositories in the machine
125  
-            if not row:
  125
+            if row[0] is None:
126 126
                 printout("[E] Path",repository,"was not found in repository table, neither in Trac's config. Sync will not be executed")
127 127
                 sys.exit(0)
128 128
 
129  
-        # Otherwise it should be the name of the repository
  129
+        # Otherwise repository is the name of the repository or an alias
130 130
         else:
131 131
             sql_string = """
132 132
                 SELECT id
@@ -136,16 +136,16 @@ def get_repository_id(self, repository):
136 136
             self.cursor.execute(sql_string, (repository,))
137 137
             row = self.cursor.fetchone()
138 138
         
139  
-            if not row:
  139
+            if row[0] is None:
140 140
                 printout("[E] Sorry, repository name not found")
141 141
                 sys.exit(1)
142 142
         
143 143
         return row[0]
144 144
     
145  
-    def get_mercurial_repository(self, repository_id):
  145
+    def get_mercurial_repository(self):
146 146
         """
147  
-        Returns a Mercurial repository API object pointing at the repository
148  
-        given by the trac-admin parameter. 
  147
+        Returns a Mercurial repository API object pointing at the repository directory
  148
+        associated to self.repository_id
149 149
         """
150 150
         try:
151 151
             repository_dir = None
@@ -160,11 +160,38 @@ def get_mercurial_repository(self, repository_id):
160 160
                      FROM repository
161 161
                      WHERE name = 'dir' AND id = %s
162 162
                 """
163  
-                self.cursor.execute(sql_string, (repository_id,))
  163
+                self.cursor.execute(sql_string, (self.repository_id,))
164 164
                 row = self.cursor.fetchone()
165  
-                if not row:
166  
-                    printout("[E] Sorry, but repository name is not defined in repository table")
167  
-                    sys.exit(1)
  165
+
  166
+                if row[0] is None:
  167
+                    # It could be an alias
  168
+                    # We get the id of the repository it points to if it's an alias
  169
+                    # We are not supporting recursive alias (they are unnecessary) 
  170
+                    sql_string = """
  171
+                        SELECT id
  172
+                         FROM repository
  173
+                         WHERE name = 'name' AND value = (SELECT value FROM repository WHERE name = 'alias' AND id = %s)
  174
+                    """
  175
+                    self.cursor.execute(sql_string, (self.repository_id,))
  176
+                    row = self.cursor.fetchone()
  177
+
  178
+                    if row[0] is None:
  179
+                        printout("[E] Sorry, but repository name is not defined in repository table")
  180
+                        sys.exit(1)
  181
+                    else:
  182
+                        # We need to update the id, to sync the repository the alias is pointing
  183
+                        self.repository_id = row[0]
  184
+                        sql_string = """
  185
+                            SELECT value
  186
+                             FROM repository
  187
+                             WHERE name = 'dir' AND id = %s
  188
+                        """
  189
+                        self.cursor.execute(sql_string, (self.repository_id,))
  190
+                        row = self.cursor.fetchone()
  191
+
  192
+                        if row[0] is None:
  193
+                            printout("[E] Sorry, but alias directory was not found, this plugin does not support recursive alias")
  194
+                            sys.exit(1)
168 195
 
169 196
                 repository_dir = row[0]
170 197
 
@@ -182,12 +209,12 @@ def initialize_repository(self, repository):
182 209
         call this method before performing any action on the repository.
183 210
         """
184 211
         self.repository_id = self.get_repository_id(repository)
185  
-        self.repository = self.get_mercurial_repository(self.repository_id) 
  212
+        self.repository = self.get_mercurial_repository() 
186 213
 
187 214
     def _get_ctx_from_repo(self, node):
188 215
         """
189  
-        Recives binary bin(rev_hash) or node.
190  
-        Return list changeset data for commit in sql table
  216
+        Receives binary bin(rev_hash) or node.
  217
+        Returns a list containing changeset's data
191 218
         """
192 219
         # Let's get its change context object from the repository
193 220
         ctx = self.repository.changectx(node)
@@ -250,7 +277,8 @@ def sync_last_revision(self, repository):
250 277
     def sync_repository(self, repository):
251 278
         """
252 279
         Synchronize the whole Mercurial repository changelog into Trac's DB
253  
-        revision table. 
  280
+        revision table. It finds out what revisions need to be synced and
  281
+        inserts them int Trac's revision table.
254 282
         """
255 283
         self.initialize_repository(repository)
256 284
         

0 notes on commit 07d2a3e

Please sign in to comment.
Something went wrong with that request. Please try again.