add gbp-pull
authorGuido Günther <agx@sigxcpu.org>
Sat, 22 Aug 2009 11:53:28 +0000 (13:53 +0200)
committerGuido Günther <agx@sigxcpu.org>
Sat, 24 Oct 2009 17:56:44 +0000 (19:56 +0200)
Adresses another part of #540185.

examples/gbp-pull [new file with mode: 0755]

diff --git a/examples/gbp-pull b/examples/gbp-pull
new file mode 100755 (executable)
index 0000000..7aecbed
--- /dev/null
@@ -0,0 +1,99 @@
+#!/usr/bin/python -u
+# vim: set fileencoding=utf-8 :
+#
+# (C) 2009 Guido Guenther <agx@sigxcpu.org>
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    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
+#
+# heavily inspired by dom-safe-pull which is © 2009 Stéphane Glondu <steph@glondu.net>
+#
+"""fast forward debian, upstream and pristine-tar branch"""
+
+import sys
+import os, os.path
+from gbp.command_wrappers import (GitFetch, GitMerge, Command, CommandExecFailed)
+from gbp.config import (GbpOptionParser, GbpOptionGroup)
+from gbp.errors import GbpError
+from gbp.git import (GitRepositoryError, GitRepository)
+
+def fast_forward_branch(branch, repo, options):
+    remote = repo.get_merge_branch(branch)
+    fast_forward = repo.is_fast_forward(branch, remote)
+    if not fast_forward:
+        if options.force:
+            print "Non-fast forwarding '%s' due to --force" % branch
+            fast_forward = True
+        else:
+            print "Skipping non-fast forward of '%s' - use --force" % branch
+    if fast_forward:
+        repo.set_branch(branch)
+        GitMerge(remote)()
+
+def main(argv):
+    changelog = 'debian/changelog'
+    retval = 0
+    pristine_tar = 'pristine-tar'
+
+    parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix='',
+                             usage='%prog [options] - safely update a repository from remote')
+    branch_group = GbpOptionGroup(parser, "branch options", "branch layout options")
+    parser.add_option_group(branch_group)
+    branch_group.add_config_file_option(option_name="upstream-branch", dest="upstream_branch")
+    branch_group.add_config_file_option(option_name="debian-branch", dest="debian_branch")
+    branch_group.add_boolean_config_file_option(option_name="pristine-tar", dest="pristine_tar")
+    parser.add_option("--force", action="store_true", dest="force", default=False,
+                      help="force update even if not fast forward")
+    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
+                      help="verbose command execution")
+
+    (options, args) = parser.parse_args(argv)
+
+    if options.verbose:
+        Command.verbose = True
+
+    try:
+        repo = GitRepository(os.path.curdir)
+    except GitRepositoryError:
+        print >>sys.stderr, "%s is not a git repository" % (os.path.abspath('.'))
+        return 1
+
+    try:
+        branches = []
+        current = repo.get_branch()
+
+        for branch in [ options.debian_branch, options.upstream_branch ]:
+            if repo.has_branch(branch):
+                branches += [ branch ]
+
+        if repo.has_branch(pristine_tar) and options.pristine_tar:
+            branches += [ pristine_tar ]
+
+        GitFetch()()
+        for branch in branches:
+            fast_forward_branch(branch, repo, options)
+
+        repo.set_branch(current)
+    except CommandExecFailed:
+        retval = 1
+    except GbpError, err:
+        if len(err.__str__()):
+            print >>sys.stderr, err
+        retval = 1
+
+    return retval
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
+
+# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: