Create stgit/basedir.py for determining the .git directory
authorCatalin Marinas <catalin.marinas@gmail.com>
Tue, 28 Mar 2006 21:01:02 +0000 (22:01 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Tue, 28 Mar 2006 21:01:02 +0000 (22:01 +0100)
basedir.get() function is used in many files and leaving it only in
stgit.git caused some cyclic imports.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
stgit/basedir.py [new file with mode: 0644]
stgit/commands/branch.py
stgit/commands/common.py
stgit/commands/export.py
stgit/commands/mail.py
stgit/commands/resolved.py
stgit/config.py
stgit/git.py
stgit/gitmergeonefile.py
stgit/stack.py

diff --git a/stgit/basedir.py b/stgit/basedir.py
new file mode 100644 (file)
index 0000000..5abe0ff
--- /dev/null
@@ -0,0 +1,47 @@
+"""Access to the GIT base directory
+"""
+
+__copyright__ = """
+Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 2 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+import os
+
+class BaseDirException(Exception):
+    pass
+
+def __output(cmd):
+    f = os.popen(cmd, 'r')
+    string = f.readline().rstrip()
+    if f.close():
+        raise BaseDirException, 'Error: failed to execute "%s"' % cmd
+    return string
+
+# GIT_DIR value cached
+__base_dir = None
+
+def get():
+    """Return the .git directory location
+    """
+    global __base_dir
+
+    if not __base_dir:
+        if 'GIT_DIR' in os.environ:
+            __base_dir = os.environ['GIT_DIR']
+        else:
+            __base_dir = __output('git-rev-parse --git-dir')
+
+    return __base_dir
index cc74122..c4b5945 100644 (file)
@@ -23,7 +23,7 @@ from optparse import OptionParser, make_option
 
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git
+from stgit import stack, git, basedir
 
 
 help = 'manage development branches'
@@ -173,7 +173,7 @@ def func(parser, options, args):
         if len(args) != 0:
             parser.error('incorrect number of arguments')
 
-        branches = os.listdir(os.path.join(git.get_base_dir(), 'refs', 'heads'))
+        branches = os.listdir(os.path.join(basedir.get(), 'refs', 'heads'))
         branches.sort()
         max_len = max([len(i) for i in branches])
 
index 4bfa4dd..b8aa1c0 100644 (file)
@@ -22,7 +22,7 @@ import sys, os, re
 from optparse import OptionParser, make_option
 
 from stgit.utils import *
-from stgit import stack, git
+from stgit import stack, git, basedir
 
 crt_series = None
 
@@ -96,7 +96,7 @@ def check_head_top_equal():
               '  are doing, use the "refresh -f" command'
 
 def check_conflicts():
-    if os.path.exists(os.path.join(git.get_base_dir(), 'conflicts')):
+    if os.path.exists(os.path.join(basedir.get(), 'conflicts')):
         raise CmdException, 'Unsolved conflicts. Please resolve them first'
 
 def print_crt_patch(branch = None):
@@ -130,7 +130,7 @@ def resolved_all(reset = None):
     if conflicts:
         for filename in conflicts:
             resolved(filename, reset)
-        os.remove(os.path.join(git.get_base_dir(), 'conflicts'))
+        os.remove(os.path.join(basedir.get(), 'conflicts'))
 
 def push_patches(patches, check_merged = False):
     """Push multiple patches onto the stack. This function is shared
index 549145e..e7de902 100644 (file)
@@ -23,7 +23,7 @@ from optparse import OptionParser, make_option
 
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git
+from stgit import stack, git, basedir
 
 
 help = 'exports a series of patches to <dir> (or patches)'
@@ -144,7 +144,7 @@ def func(parser, options, args):
     else:
         patch_tmpl_list = []
 
-    patch_tmpl_list += [os.path.join(git.get_base_dir(), 'patchexport.tmpl'),
+    patch_tmpl_list += [os.path.join(basedir.get(), 'patchexport.tmpl'),
                         os.path.join(sys.prefix,
                                      'share/stgit/templates/patchexport.tmpl')]
     tmpl = ''
index 456c4b1..2fb88bc 100644 (file)
@@ -20,7 +20,7 @@ from optparse import OptionParser, make_option
 
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git
+from stgit import stack, git, basedir
 from stgit.config import config
 
 
@@ -434,7 +434,7 @@ def func(parser, options, args):
         if options.cover:
             tfile_list = [options.cover]
         else:
-            tfile_list = [os.path.join(git.get_base_dir(), 'covermail.tmpl'),
+            tfile_list = [os.path.join(basedir.get(), 'covermail.tmpl'),
                           os.path.join(sys.prefix,
                                        'share/stgit/templates/covermail.tmpl')]
 
@@ -466,7 +466,7 @@ def func(parser, options, args):
     if options.template:
         tfile_list = [options.template]
     else:
-        tfile_list = [os.path.join(git.get_base_dir(), 'patchmail.tmpl'),
+        tfile_list = [os.path.join(basedir.get(), 'patchmail.tmpl'),
                       os.path.join(sys.prefix,
                                    'share/stgit/templates/patchmail.tmpl')]
     tmpl = None
index 585c51b..2c968d0 100644 (file)
@@ -21,7 +21,7 @@ from optparse import OptionParser, make_option
 
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git
+from stgit import stack, git, basedir
 
 
 help = 'mark a file conflict as solved'
@@ -65,8 +65,8 @@ def func(parser, options, args):
 
     # save or remove the conflicts file
     if conflicts == []:
-        os.remove(os.path.join(git.get_base_dir(), 'conflicts'))
+        os.remove(os.path.join(basedir.get(), 'conflicts'))
     else:
-        f = file(os.path.join(git.get_base_dir(), 'conflicts'), 'w+')
+        f = file(os.path.join(basedir.get(), 'conflicts'), 'w+')
         f.writelines([line + '\n' for line in conflicts])
         f.close()
index e6c9538..e28633d 100644 (file)
@@ -20,11 +20,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 import os, ConfigParser
 
+from stgit import basedir
 
-if 'GIT_DIR' in os.environ:
-    __git_dir = os.environ['GIT_DIR']
-else:
-    __git_dir = '.git'
 
 config = ConfigParser.RawConfigParser()
 
@@ -41,7 +38,7 @@ config.set('stgit', 'keeporig', 'yes')
 # Read the configuration files (if any) and override the default settings
 config.read('/etc/stgitrc')
 config.read(os.path.expanduser('~/.stgitrc'))
-config.read(os.path.join(__git_dir, 'stgitrc'))
+config.read(os.path.join(basedir.get(), 'stgitrc'))
 
 # [gitmergeonefile] section is deprecated. In case it exists copy the
 # options/values to the [stgit] one
index 7b47267..0f08d96 100644 (file)
@@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 import sys, os, popen2, re, gitmergeonefile
 
+from stgit import basedir
 from stgit.utils import *
 
 # git exception class
@@ -82,22 +83,6 @@ __commits = dict()
 # Functions
 #
 
-# GIT_DIR value cached
-__base_dir = None
-
-def get_base_dir():
-    """Different start-up variables read from the environment
-    """
-    global __base_dir
-
-    if not __base_dir:
-        if 'GIT_DIR' in os.environ:
-            __base_dir = os.environ['GIT_DIR']
-        else:
-            __base_dir = _output_one_line('git-rev-parse --git-dir')
-
-    return __base_dir
-
 def get_commit(id_hash):
     """Commit objects factory. Save/look-up them in the __commits
     dictionary
@@ -114,7 +99,7 @@ def get_commit(id_hash):
 def get_conflicts():
     """Return the list of file conflicts
     """
-    conflicts_file = os.path.join(get_base_dir(), 'conflicts')
+    conflicts_file = os.path.join(basedir.get(), 'conflicts')
     if os.path.isfile(conflicts_file):
         f = file(conflicts_file)
         names = [line.strip() for line in f.readlines()]
@@ -190,7 +175,7 @@ def __tree_status(files = None, tree_id = 'HEAD', unknown = False,
 
     # unknown files
     if unknown:
-        exclude_file = os.path.join(get_base_dir(), 'info', 'exclude')
+        exclude_file = os.path.join(basedir.get(), 'info', 'exclude')
         base_exclude = ['--exclude=%s' % s for s in
                         ['*.[ao]', '*.pyc', '.*', '*~', '#*', 'TAGS', 'tags']]
         base_exclude.append('--exclude-per-directory=.gitignore')
@@ -307,8 +292,8 @@ def create_branch(new_branch, tree_id = None):
     if tree_id:
         switch(tree_id)
 
-    if os.path.isfile(os.path.join(get_base_dir(), 'MERGE_HEAD')):
-        os.remove(os.path.join(get_base_dir(), 'MERGE_HEAD'))
+    if os.path.isfile(os.path.join(basedir.get(), 'MERGE_HEAD')):
+        os.remove(os.path.join(basedir.get(), 'MERGE_HEAD'))
 
 def switch_branch(name):
     """Switch to a git branch
@@ -327,8 +312,8 @@ def switch_branch(name):
         __head = tree_id
     set_head_file(new_head)
 
-    if os.path.isfile(os.path.join(get_base_dir(), 'MERGE_HEAD')):
-        os.remove(os.path.join(get_base_dir(), 'MERGE_HEAD'))
+    if os.path.isfile(os.path.join(basedir.get(), 'MERGE_HEAD')):
+        os.remove(os.path.join(basedir.get(), 'MERGE_HEAD'))
 
 def delete_branch(name):
     """Delete a git branch
@@ -336,7 +321,7 @@ def delete_branch(name):
     branch_head = os.path.join('refs', 'heads', name)
     if not branch_exists(branch_head):
         raise GitException, 'Branch "%s" does not exist' % name
-    os.remove(os.path.join(get_base_dir(), branch_head))
+    os.remove(os.path.join(basedir.get(), branch_head))
 
 def rename_branch(from_name, to_name):
     """Rename a git branch
@@ -350,8 +335,8 @@ def rename_branch(from_name, to_name):
 
     if get_head_file() == from_name:
         set_head_file(to_head)
-    os.rename(os.path.join(get_base_dir(), from_head), \
-              os.path.join(get_base_dir(), to_head))
+    os.rename(os.path.join(basedir.get(), from_head), \
+              os.path.join(basedir.get(), to_head))
 
 def add(names):
     """Add the files or recursively add the directory contents
index 3b3175b..c603ff8 100644 (file)
@@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
 import sys, os
+from stgit import basedir
 from stgit.config import config
 from stgit.utils import append_string
 
@@ -87,21 +88,10 @@ def __remove_files(orig_hash, file1_hash, file2_hash):
         os.remove(src2)
     pass
 
-# GIT_DIR value cached
-__base_dir = None
-
 def __conflict(path):
     """Write the conflict file for the 'path' variable and exit
     """
-    global __base_dir
-
-    if not __base_dir:
-        if 'GIT_DIR' in os.environ:
-            __base_dir = os.environ['GIT_DIR']
-        else:
-            __base_dir = __output('git-rev-parse --git-dir')
-
-    append_string(os.path.join(__base_dir, 'conflicts'), path)
+    append_string(os.path.join(basedir.get(), 'conflicts'), path)
 
 
 #
index 165b5a7..c9d29b7 100644 (file)
@@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 import sys, os
 
 from stgit.utils import *
-from stgit import git
+from stgit import git, basedir
 from stgit.config import config
 
 
@@ -66,7 +66,7 @@ def __clean_comments(f):
 
 def edit_file(series, line, comment, show_patch = True):
     fname = '.stgit.msg'
-    tmpl = os.path.join(git.get_base_dir(), 'patchdescr.tmpl')
+    tmpl = os.path.join(basedir.get(), 'patchdescr.tmpl')
 
     f = file(fname, 'w+')
     if line:
@@ -292,7 +292,7 @@ class Series:
                 self.__name = name
             else:
                 self.__name = git.get_head_file()
-            self.__base_dir = git.get_base_dir()
+            self.__base_dir = basedir.get()
         except git.GitException, ex:
             raise StackException, 'GIT tree not initialised: %s' % ex