StGIT, remotes, and parent information
cover StGIT, remotes, and parent information
Here is a new draft version, which re-implements stgit.config to call git-repo-config (without any caching to get things working right now), and with write support.
The 5 other patches were modified accordingly, so in this state we can see:
stgit$ ./stg branch -c bar origin
Checking for changes in the working directory... done Recording "refs/heads/origin" as parent branch. Using "origin" remote to pull parent from. Branch "bar" created.
stgit$ git-repo-config --get-regexp 'branch\.bar\..*' branch.bar.merge refs/heads/origin branch.bar.remote origin
There are many rough edges when you shake it, but that should give the idea.
It also applies on yesterday's head. I'll rebase and adapt the newly-added config parameters.
Best regards,
Signature · 4 lines
Yann Dirson <[email protected]> | Debian-related: <[email protected]> | Support Debian GNU/Linux: | Freedom, Power, Stability, Gratis http://ydirson.free.fr/ | Check <http://www.debian.org/>
PATCH 1/6 Make stgit.config use git-repo-config.
This is an initial implementation without any sort of caching. If we want that, we could do it with this new API. We could even just simply generalize the use of ConfigOption instead.
I just copypasted __run from stgit.git to avoid too much work, but it's definitely something to cleanup.
---
stgit/commands/common.py | 5 + stgit/commands/mail.py | 27 +++----- stgit/commands/pull.py | 9 +-- stgit/commands/refresh.py | 2 - stgit/config.py | 154 ++++++++++++++++++++++----------------------- stgit/git.py | 10 +-- stgit/gitmergeonefile.py | 7 +- stgit/stack.py | 5 + 8 files changed, 104 insertions(+), 115 deletions(-)
stgit/commands/common.py +3 -2
@@ -311,9 +311,10 @@ def address_or_alias(addr_str): if addr.find('@') >= 0: # it's an e-mail address return addr - if config.has_option('mail "alias"', addr): + alias = config.get('mail.alias.'+addr) + if alias: # it's an alias - return config.get('mail "alias"', addr) + return alias raise CmdException, 'unknown e-mail alias: %s' % addr
stgit/commands/mail.py +11 -16
@@ -129,9 +129,8 @@ def __get_sender(): """Return the 'authname <authemail>' string as read from the configuration file """ - if config.has_option('stgit', 'sender'): - sender = config.get('stgit', 'sender') - else: + sender=config.get('stgit.sender') + if not sender: try: sender = str(git.user()) except git.GitException:
@@ -207,9 +206,8 @@ def __build_address_headers(msg, options, extra_cc = []): cc_addr = '' bcc_addr = '' - if config.has_option('stgit', 'autobcc'): - autobcc = config.get('stgit', 'autobcc') - else: + autobcc = config.get('stgit.autobcc') + if not autobcc: autobcc = '' if options.to:
@@ -283,8 +281,9 @@ def __edit_message(msg): f.close() # the editor - if config.has_option('stgit', 'editor'): - editor = config.get('stgit', 'editor') + editor = config.get('stgit.editor') + if editor: + pass elif 'EDITOR' in os.environ: editor = os.environ['EDITOR'] else:
@@ -469,14 +468,10 @@ def func(parser, options, args): """Send the patches by e-mail using the patchmail.tmpl file as a template """ - smtpserver = config.get('stgit', 'smtpserver') + smtpserver = config.get('stgit.smtpserver') - smtpuser = None - smtppassword = None - if config.has_option('stgit', 'smtpuser'): - smtpuser = config.get('stgit', 'smtpuser') - if config.has_option('stgit', 'smtppassword'): - smtppassword = config.get('stgit', 'smtppassword') + smtpuser = config.get('stgit.smtpuser') + smtppassword = config.get('stgit.smtppassword') applied = crt_series.get_applied()
@@ -511,7 +506,7 @@ def func(parser, options, args): if options.sleep != None: sleep = options.sleep else: - sleep = config.getint('stgit', 'smtpdelay') + sleep = config.getint('stgit.smtpdelay') # send the cover message (if any) if options.cover or options.edit_cover:
stgit/commands/pull.py +3 -6
@@ -53,10 +53,8 @@ def func(parser, options, args): if len(args) >= 1: repository = args[0] else: - section = 'branch "%s"' % git.get_head_file() - if config.has_option(section, 'remote'): - repository = config.get(section, 'remote') - else: + repository = config.get('branch.%s.remote' % git.get_head_file()) + if not repository: repository = 'origin' refspec = None
@@ -88,8 +86,7 @@ def func(parser, options, args): push_patches(applied, options.merged) # maybe tidy up - repack = config.get('stgit', 'keepoptimized') - if repack == 'yes': + if config.get('stgit.keepoptimized') == 'yes': git.repack() print_crt_patch()
stgit/commands/refresh.py +1 -1
@@ -77,7 +77,7 @@ options = [make_option('-f', '--force', def func(parser, options, args): - autoresolved = config.get('stgit', 'autoresolved') + autoresolved = config.get('stgit.autoresolved') if autoresolved != 'yes': check_conflicts()
stgit/config.py +75 -79
179 lines not on this page · raw message
stgit/git.py +5 -5
@@ -449,10 +449,10 @@ def user(): """ global __user if not __user: - if config.has_option('user', 'name') \ - and config.has_option('user', 'email'): - __user = Person(config.get('user', 'name'), - config.get('user', 'email')) + name=config.get('user.name') + email=config.get('user.email') + if name and email: + __user = Person(name, email) else: raise GitException, 'unknown user details' return __user;
@@ -816,7 +816,7 @@ def pull(repository = 'origin', refspec = None): if refspec: args.append(refspec) - if __run(config.get('stgit', 'pullcmd'), args) != 0: + if __run(config.get('stgit.pullcmd'), args) != 0: raise GitException, 'Failed "git-pull %s"' % repository def repack():
stgit/gitmergeonefile.py +3 -4
@@ -102,9 +102,8 @@ def interactive_merge(filename): """Run the interactive merger on the given file. Note that the index should not have any conflicts. """ - try: - imerger = config.get('stgit', 'imerger') - except Exception, err: + imerger = config.get('stgit.imerger') + if not imerger: raise GitMergeException, 'Configuration error: %s' % err extensions = file_extensions()
@@ -185,7 +184,7 @@ def merge(orig_hash, file1_hash, file2_hash, os.system('git-update-index --cacheinfo %s %s %s' % (file1_mode, file1_hash, path)) - if config.get('stgit', 'autoimerge') == 'yes': + if config.get('stgit.autoimerge') == 'yes': print >> sys.stderr, \ 'Trying the interactive merge' try:
stgit/stack.py +3 -2
@@ -92,8 +92,9 @@ def edit_file(series, line, comment, show_patch = True): f.close() # the editor - if config.has_option('stgit', 'editor'): - editor = config.get('stgit', 'editor') + editor = config.get('stgit.editor') + if editor: + pass elif 'EDITOR' in os.environ: editor = os.environ['EDITOR'] else:
PATCH 2/6 Move identification of parent branch's remote def up into stack class.
In reply to Yann DirsonAlso adds the __set_parent_remote() counterpart method, but only private since it is expected to be called only through set_parent, to be introduced in subsequent patch.
---
stgit/commands/pull.py | 4 +--- stgit/stack.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-)
stgit/commands/pull.py +1 -3
@@ -53,9 +53,7 @@ def func(parser, options, args): if len(args) >= 1: repository = args[0] else: - repository = config.get('branch.%s.remote' % git.get_head_file()) - if not repository: - repository = 'origin' + repository = crt_series.get_parent_remote() refspec = None if len(args) == 2:
stgit/stack.py +10 -0
@@ -398,6 +398,16 @@ class Series(StgitObject): def set_description(self, line): self._set_field('description', line) + def get_parent_remote(self): + value = config.get('branch.%s.remote' % self.__name) + if value: + return value + else: + return 'origin' + + def __set_parent_remote(self, remote): + value = config.set('branch.%s.remote' % self.__name, remote) + def __patch_is_current(self, patch): return patch.get_name() == self.get_current()
PATCH 3/6 Allows extraction of information about remotes.
In reply to Yann DirsonWe want to know the list of declared remotes, the local branches they hold, and which remotes holds a given branch. All this regardless of where the information is stored.
If there are any git-1.5 remotes declared in .git/config, we suppose you know what you're doing and they will take precedence on any pre-1.5 remotes.
This does not use git-remote for now, since it is 1.5 material not released yet, does not support legacy branches/ remotes, and does not allow yet to query all of the information we need.
---
stgit/git.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 75 insertions(+), 0 deletions(-)
stgit/git.py +75 -0
@@ -879,3 +879,78 @@ def modifying_revs(files, base_rev): revs = [line.strip() for line in _output_lines(cmd + files)] return revs + + +def __remotes_from_config(): + configremotes = [] + stream = os.popen('git repo-config --get-regexp "^remote\..*\.url$"', 'r') + for line in stream: + m = re.match('^remote\.(.*)\.url ', line) + if m: + configremotes.append(m.group(1)) + stream.close() + return configremotes + +def __remotes_from_dir(dir): + return os.listdir(os.path.join(basedir.get(), dir)) + +def remotes_list(): + """Return the list of remotes in the repository + """ + + return set(__remotes_from_config()) | \ + set(__remotes_from_dir('remotes')) | \ + set(__remotes_from_dir('branches')) + +def remotes_local_branches(remote): + """Returns the list of local branches fetched from given remote + """ + + branches = [] + if remote in __remotes_from_config(): + stream = os.popen('git repo-config --get-all "remote.%s.fetch"' % remote, 'r') + for line in stream: + # FIXME: should factorize refspec handling + m = re.match('^[^:]*:([^:]*)\n$', line) + if m: + branches.append(m.group(1)) + else: + raise GitException, 'Cannot parse refspec "%s"' % line + stream.close() + elif remote in __remotes_from_dir('remotes'): + stream = open(os.path.join(basedir.get(), 'remotes', remote), 'r') + for line in stream: + # Only consider Pull lines + m = re.match('^Pull: (.*)\n$', line) + if m: + refspec = m.group(1) + m = re.match('^[^:]*:([^:]*)$', refspec) + if m: + branches.append(m.group(1)) + else: + raise GitException, 'Cannot parse refspec "%s"' % refspec + stream.close() + pass + elif remote in __remotes_from_dir('branches'): + # old-style branches only declare one branch + branches.append('refs/heads/'+remote); + pass + else: + raise GitException, 'Unknown remote "%s"' % remote + + return branches + +def identify_remote(branchname): + """Return the name for the remote to pull the given branchname + from, or None if we believe it is a local branch. + """ + + for remote in remotes_list(): + if branchname in remotes_local_branches(remote): + return remote + + # FIXME: in the case of local branch we should maybe set remote to + # "." but are we even sure it is the only case left ? + + # if we get here we've found nothing + return None
PATCH 4/6 Basic support for keeping a ref to the parent branch.
In reply to Yann DirsonThis adds a framework to handle the parent branch of a stack, in addition to the parent remote, and to set them when creating a stack.
---
TODO | 2 -- stgit/stack.py | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-)
TODO +0 -2
@@ -20,8 +20,6 @@ The future, when time allows or if someone else does them: - multiple heads in a patch - useful for forking a patch, synchronising with other patches (diff format or in other repositories) -- "pull" argument should default to a sane value, "origin" is wrong in - many cases - commit directly to a patch which is not top - patch synchronisation between between branches (as some people, including me have the same patches based on different branches and
stgit/stack.py +23 -1
@@ -408,6 +408,26 @@ class Series(StgitObject): def __set_parent_remote(self, remote): value = config.set('branch.%s.remote' % self.__name, remote) + def get_parent_branch(self): + value = config.get('branch.%s.merge' % self.__name) + if value: + return value + elif rev_parse('heads/origin'): + return 'heads/origin' + else: + raise StackException, 'Cannot find a parent branch for "%s"' % self.__name + + def __set_parent_branch(self, name): + config.set('branch.%s.merge' % self.__name, name) + + def set_parent(self, remote, localbranch): + if localbranch: + self.__set_parent_branch(localbranch) + if remote: + self.__set_parent_remote(remote) + elif remote: + raise StackException, 'Remote "%s" without a branch cannot be used as parent' % remote + def __patch_is_current(self, patch): return patch.get_name() == self.get_current()
@@ -456,7 +476,7 @@ class Series(StgitObject): """ return os.path.isdir(self.__patch_dir) - def init(self, create_at=False): + def init(self, create_at=False, parent_remote=None, parent_branch=None): """Initialises the stgit series """ bases_dir = os.path.join(self.__base_dir, 'refs', 'bases')
@@ -473,6 +493,8 @@ class Series(StgitObject): os.makedirs(self.__patch_dir) + self.set_parent(parent_remote, parent_branch) + create_dirs(bases_dir) self.create_empty_field('applied')
PATCH 5/6 Have 'stg branch --create' record parent information.
In reply to Yann Dirson---
stgit/commands/branch.py | 37 ++++++++++++++++++++++++++++++++++--- 1 files changed, 34 insertions(+), 3 deletions(-)
stgit/commands/branch.py +34 -3
@@ -123,10 +123,41 @@ def func(parser, options, args): check_head_top_equal() tree_id = None - if len(args) == 2: + if len(args) >= 2: + try: + if git.rev_parse(args[1]) == git.rev_parse('refs/heads/' + args[1]): + # we are for sure refering to a branch + parentbranch = 'refs/heads/' + args[1] + print 'Recording "%s" as parent branch.' % parentbranch + elif git.rev_parse(args[1]) and re.search('/', args[1]): + # FIXME: should the test be more strict ? + parentbranch = args[1] + else: + # Note: this includes refs to StGIT patches + print 'Don\'t know how to determine parent branch from "%s".' % args[1] + parentbranch = None + except git.GitException: + # should use a more specific exception to catch only non-git refs ? + print 'Don\'t know how to determine parent branch from "%s".' % args[1] + parentbranch = None + tree_id = git_id(args[1]) - - stack.Series(args[0]).init(create_at = tree_id) + else: + # branch stack off current branch + parentbranch = git.get_head_file() + + if parentbranch: + parentremote = git.identify_remote(parentbranch) + if parentremote: + print 'Using "%s" remote to pull parent from.' % parentremote + else: + print 'Not identified a remote to pull parent from.' + else: + parentremote = None + + stack.Series(args[0]).init(create_at = tree_id, + parent_remote = parentremote, + parent_branch = parentbranch) print 'Branch "%s" created.' % args[0] return
PATCH 6/6 Make 'stg pull' use git-fetch and not git-pull.
In reply to Yann DirsonWe introduce a new pull-does-rebase setting, as companion to pullcmd. This allows to use both pullcmd's that need a rebase (like git-fetch) and pullcmd's that do not (like git-pull).
To be able to rebase, we rely on parent information being available for the stack.
---
examples/gitconfig | 8 +++++++- stgit/config.py | 3 ++- stgit/git.py | 15 ++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-)
examples/gitconfig +7 -1
@@ -33,8 +33,14 @@ #pager = ~/share/stgit/contrib/diffcol.sh #pager = filterdiff --annotate | colordiff | less -FRX - # GIT pull command (should take the same arguments as git-pull) + # GIT pull command (should take the same arguments as + # git-fetch or git-pull). By default: + #pullcmd = git-fetch + #pull-does-rebase = yes + # Alternative (old behaviour), less intuitive but maybe useful + # for some workflows: #pullcmd = git-pull + #pull-does-rebase = no # The three-way merge tool. Note that the 'output' file contains the # same data as 'branch1'. This is useful for tools that do not take an
stgit/config.py +2 -1
@@ -29,7 +29,8 @@ class GitConfig: 'stgit.autoresolved': 'no', 'stgit.smtpserver': 'localhost:25', 'stgit.smtpdelay': '5', - 'stgit.pullcmd': 'git-pull', + 'stgit.pullcmd': 'git-fetch', + 'stgit.pull-does-rebase': 'yes', 'stgit.merger': 'diff3 -L current -L ancestor -L patched -m -E ' \ '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"', 'stgit.autoimerge': 'no',
stgit/git.py +10 -5
@@ -806,18 +806,23 @@ def reset(files = None, tree_id = None, check_out = True): __set_head(tree_id) def pull(repository = 'origin', refspec = None): - """Pull changes from the remote repository. At the moment, just - use the 'git-pull' command + """Pull changes from the remote repository. Uses 'git-fetch' + and moves the stack base. """ - # 'git-pull' updates the HEAD + # we update the HEAD __clear_head_cache() args = [repository] if refspec: args.append(refspec) - if __run(config.get('stgit.pullcmd'), args) != 0: - raise GitException, 'Failed "git-pull %s"' % repository + command = config.get('stgit.pullcmd') + if __run(command, args) != 0: + raise GitException, 'Failed "%s %s"' % (command, repository) + + if (config.get('stgit.pull-does-rebase')): + # FIXME! + reset(tree_id = rev_parse(repository)) def repack(): """Repack all objects into a single pack