# HG changeset patch # User Alexis S. L. Carvalho # Date 1163901074 7200 # Node ID 1a0fa3914c46bad56c71c6c5f9fbec3123371520 # Parent d94664748bc10a5ffc5a0f96db0598d396e5dbb4 Avoid looking up usernames if the current user owns the .hgrc file Converting uids into usernames may be somewhat expensive when NIS or LDAP is involved. diff -r d94664748bc1 -r 1a0fa3914c46 mercurial/ui.py --- a/mercurial/ui.py Sat Nov 18 23:51:13 2006 -0200 +++ b/mercurial/ui.py Sat Nov 18 23:51:14 2006 -0200 @@ -96,10 +96,12 @@ def _is_trusted(self, fp, f, warn=True): if not self.check_trusted: return True + st = util.fstat(fp) + if util.isowner(fp, st): + return True tusers = self.trusted_users tgroups = self.trusted_groups if (tusers or tgroups) and '*' not in tusers and '*' not in tgroups: - st = util.fstat(fp) user = util.username(st.st_uid) group = util.groupname(st.st_gid) if user not in tusers and group not in tgroups: diff -r d94664748bc1 -r 1a0fa3914c46 mercurial/util.py --- a/mercurial/util.py Sat Nov 18 23:51:13 2006 -0200 +++ b/mercurial/util.py Sat Nov 18 23:51:14 2006 -0200 @@ -654,6 +654,11 @@ def explain_exit(code): return _("exited with status %d") % code, code + # if you change this stub into a real check, please try to implement the + # username and groupname functions above, too. + def isowner(fp, st=None): + return True + try: # override functions with win32 versions if possible from util_win32 import * @@ -765,6 +770,16 @@ return _("stopped by signal %d") % val, val raise ValueError(_("invalid exit code")) + def isowner(fp, st=None): + """Return True if the file object f belongs to the current user. + + The return value of a util.fstat(f) may be passed as the st argument. + """ + if st is None: + st = fstat(f) + return st.st_uid == os.getuid() + + def opener(base, audit=True): """ return a function that opens files relative to base diff -r d94664748bc1 -r 1a0fa3914c46 tests/test-trusted.py --- a/tests/test-trusted.py Sat Nov 18 23:51:13 2006 -0200 +++ b/tests/test-trusted.py Sat Nov 18 23:51:14 2006 -0200 @@ -41,6 +41,10 @@ return group util.groupname = groupname + def isowner(fp, st=None): + return user == cuser + util.isowner = isowner + # try to read everything #print '# File belongs to user %s, group %s' % (user, group) #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)