gbs submit support
authorZhang Qiang <qiang.z.zhang@intel.com>
Mon, 6 Aug 2012 12:27:04 +0000 (20:27 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Mon, 6 Aug 2012 12:27:04 +0000 (20:27 +0800)
workflow:
1) Check opts: -m is a must option
2) if --target specified, use specified target branch name, use
   current branch name
3) create tag on target branch and commit ID, tag format is:
   submit/${branch}/${date}.${time}
4) push tag to gerrit.

gitbuildsys/cmd_submit.py
tools/gbs

index 0a68e05abbca75adb1480661923ff6d774a32c3d..dfc4993dbeb29fe57309a53d0dd225f07d770b1e 100644 (file)
 
 import os
 import time
-from collections import namedtuple
-from gitbuildsys.cmd_changelog import do as gbs_changelog
 
-import gbp.rpm as rpm
-from gbp.errors import GbpError
 from gbp.rpm.git import GitRepositoryError, RpmGitRepository
 import msger
-import utils
+import errors
 
 def do(opts, args):
 
-    workdir = os.path.abspath(os.getcwd())
+    workdir = os.getcwd()
     if len(args) > 1:
         msger.error('only one work directory can be specified in args.')
     if len(args) == 1:
         workdir = os.path.abspath(args[0])
 
-    specfile = utils.guess_spec(workdir, None)
-    try:
-        spec = rpm.parse_spec(specfile)
-    except rpm.GbpError, err:
-        msger.error('%s' % err)
-
-    if not spec.name or not spec.version:
-        msger.error('can\'t get correct name or version from spec file.')
+    if opts.msg is None:
+        raise errors.Usage('message for tag must be specified with -m option')
 
     try:
         repo = RpmGitRepository(workdir)
-    except GitRepositoryError:
-        msger.error('%s is not a git dir' % workdir)
+        commit = repo.rev_parse(opts.commit)
+        if opts.target:
+            target_branch = opts.target
+        else:
+            target_branch = repo.get_branch()
+    except GitRepositoryError, err:
+        msger.error(str(err))
 
-    # Create log info from commits
-    if opts.changelog:
+    if not opts.target:
         try:
-            gbs_changelog(namedtuple('Opts', 'since')(None), None)
-        except SystemExit:
-            msger.error("Failed to update changelog")
-
-    changesfile = os.path.join(workdir, 'packaging', '%s.changes' % spec.name)
-    if not os.path.exists(changesfile):
-        msger.error('No changelog file, so not be allowed to submit')
+            upstream = repo.get_upstream([target_branch])
+            upstream_branch = upstream[target_branch]
+            if upstream_branch and upstream_branch.startswith(opts.remote):
+                target_branch = os.path.basename(upstream_branch)
+            else:
+                msger.warning('can\'t find upstream branch for current branch '\
+                              '%s. Gbs will try to find it by name. Please '\
+                              'consider to use git-branch --set-upstream to '\
+                              'set upstream remote branch.' % target_branch)
+        except GitRepositoryError:
+            pass
 
-    changelog_file = changesfile.replace('%s/' % workdir, '')
     try:
-        changelog_status = repo.status([changelog_file])
-    except GbpError, err:
-        msger.error('failed to get the status of change log file: %s' % err)
-
-    if changelog_status:
-        status = changelog_status.keys()[0]
-    else:
-        status = None
-
-    if status and status in ['A ', 'AM', ' M']:
-        # Changelog file have been modified, so commit at local first
-        try:
-            if not opts.msg:
-                msger.error('commit message must be specified using -m')
-            repo.add_files([changesfile])
-            repo.commit_files([changesfile], opts.msg)
-        except GitRepositoryError:
-            msger.error('git commit changelog error, please check manually '\
-                        'maybe not changed or not exist')
-    else:
-        # changelog file has not been modified, then
-        # Check if the latest commit contains changelog file's update
-        try:
-            commit_info = repo.get_commit_info('HEAD')
-            changed_files = commit_info['files']
-            if not (changelog_file in changed_files['M'] or \
-                    changelog_file in changed_files['A']):
-                msger.error('changelog file must be updated, use --changelog '\
-                            'opts or update manually')
-        except GitRepositoryError, err:
-            msger.error('failed to get latest commit info: %s' % err)
+        if target_branch == 'master':
+            target_branch = 'trunk'
+        tagname = 'submit/%s/%s' % (target_branch, time.strftime( \
+                                    '%Y%m%d.%H%M%S', time.localtime()))
+        msger.info('creating tag: %s' % tagname)
+        repo.create_tag(tagname, msg=opts.msg, commit=commit, sign=opts.sign,
+                                                 keyid=opts.user_key)
+    except GitRepositoryError, err:
+        msger.error('failed to create tag %s: %s ' % (tagname, str(err)))
 
     try:
-        repo.push('origin', 'HEAD', 'refs/for/%s' % opts.target_branch)
-        tagmsg = ''
-        if opts.tag:
-            tagmsg = 'build/%s' % time.strftime('%Y%m%d.%H%M%S', time.gmtime())
-            repo.create_tag(tagmsg)
-            repo.push_tag('origin', tagmsg)
-    except GitRepositoryError:
-        msger.error('failed to submit local changes to server')
+        msger.info('pushing tag to remote server')
+        repo.push_tag(opts.remote, tagname)
+    except GitRepositoryError, err:
+        repo.delete_tag(tagname)
+        msger.error('failed to push tag %s :%s' % (tagname, str(err)))
 
     msger.info('done.')
index 41641f9a95001a0d868bdf7693daceaf13ed7951..4d5bae6bc26b4048fb9ff1e7ffdc39f4ab6aadad 100755 (executable)
--- a/tools/gbs
+++ b/tools/gbs
@@ -80,40 +80,52 @@ class Gbs(cmdln.Cmdln):
 
         if self.options.conf:
             configmgr.reset_from_conf(self.options.conf)
-    '''
     @cmdln.alias('sr')
-    @cmdln.option('--changelog',
-                  action='store_true',
-                  default=False,
-                  dest='changelog',
-                  help='invoke gbs changelog to create changelog')
     @cmdln.option('-m', '--msg',
                   default=None,
                   dest='msg',
-                  help='specify commit message info')
-    @cmdln.option('--tag',
+                  help='specify tag message info')
+    @cmdln.option('-c', '--commit',
+                  default='HEAD',
+                  dest='commit',
+                  help='specify a commit ID to submit')
+    @cmdln.option('-s', '--sign',
                   action='store_true',
                   default=False,
-                  help='make a tag before submit')
-    @cmdln.option('--branch',
-                  default='master',
-                  dest='target_branch',
-                  help='specify the target branch for submit')
-
+                  dest='sign',
+                  help='make a GPG-signed tag')
+    @cmdln.option('-u', '--user-key',
+                  default=None,
+                  dest='user_key',
+                  help='using the given key to make a GPG-signed tag')
+    @cmdln.option('-t', '--target',
+                  default=None,
+                  dest='target',
+                  help='specify target version to submit, eg: trunk.')
+    @cmdln.option('-r', '--remote',
+                  default='origin',
+                  dest='remote',
+                  help='specify gerrit project server, default value is '\
+                  'origin for example:\nssh://user@review.tizen.org:29418'\
+                  '/public/base/gcc')
     def do_submit(self, _subcmd, opts, *args):
-        """${cmd_name}: submit commit request to gerrit for review
+        """${cmd_name}: submit tag to gerrit and trigger building in OBS
 
         Usage:
-            gbs submit [-m] [--changelog] [--tag] [--branch]
+            gbs submit -m <message for tag> [options]
 
-        Note:
+        Examples:
+            gbs submit -m 'release for 0.1'
+            gbs submit -c <commit_ID> -m 'release for 0.2'
+            gbs submit -m 'release for 0.3' -s
+            gbs submit -r ssh://user@review.tizen.org:29418/public/base/gcc\
+ -m 'release for 0.4'
 
         ${cmd_option_list}
         """
 
         from gitbuildsys import cmd_submit as cmd
         cmd.do(opts, args)
-    '''
 
     @cmdln.alias('ex')
     @cmdln.option('-o', '--outdir',