创建自定义的Repo远程仓库

创建自定义的repo工具的远程仓库

1.获取完整源码:

repo init -u git://github.com/ossxp-com/manifest.git --repo-url=git://codeaurora.org/tools/repo.git --repo-branch=caf-stable

2.将上述源码完整目录复制一份,切换到stable分支,根据下面的介绍,为工具添加remote和push命令,commit源码修改,并且推送到远程仓库

git checkout -b stable
git branch -D default
git add -A
git commit -m "add push and remote subcmd"
git remote set-url origin [email protected]:yexb/repo.git
git push -u origin stable
git push -u origin --tags(不执行这个,pull下来代码运行repo version会提示错误)

3.正常使用方法为,在repo init的时候通过–repo-url指定自建的repo服务器路径
由于google对repo的每个tag都有pgp密钥加密,使用自定义的repo工具,需要使用–no-repo-verify放弃对仓库严格验证

repo init -u [email protected]:APQ8009-Android/manifest_demo.git [email protected]:yexb/repo.git --no-repo-verify

上述命令默认的参数为 -m default.xml -b master,–repo-branch=stable

4.repo init完成,在本地目录中生成.repo目录,后续该目录下的repo操作都会弃用/usr/bin下的谷歌原生repo,使用.repo/repo/目录中的repo脚本。

Repo添加push命令

在project.py的project类的getAddedAndRemovedLogs方法附近,添加UploadNoReview(self, opt, remoebranch,branch=None)方法:

def UploadNoReview(self, opt, branch=None):
    """If not review server defined, uploads the named branch directly to git server.
    """
    if branch is None:
      branch = self.CurrentBranch
    if branch is None:
      raise GitError('not currently on a branch')

    branch = self.GetBranch(branch)

    if not branch.LocalMerge:
      raise GitError('branch %s does not track a remote' % branch.name)

    if not opt.ignore_review and branch.remote.review:
      raise GitError('remote %s has review url, use `repo upload` instead.' % branch.remote.name)

    if opt.new_branch:
      dest_branch = branch.name
    else:
      dest_branch = branch.merge

    if dest_branch.startswith(R_TAGS):
      raise GitError('Can not push to TAGS (%s)! Run repo push with --new flag to create new feature branch.' % dest_branch)
    if not dest_branch.startswith(R_HEADS):
      dest_branch = R_HEADS + dest_branch

    if not branch.remote.projectname:
      branch.remote.projectname = self.name
      branch.remote.Save()

    # save git config branch.name.merge
    if opt.new_branch:
      branch.merge = dest_branch
      branch.Save()

    ref_spec = '%s:%s' % (R_HEADS + branch.name, dest_branch)
    pushurl = self.manifest.manifestProject.config.GetString('repo.%s.pushurl'
              % branch.remote.name)
    if not pushurl:
      pushurl = self.manifest.manifestProject.config.GetString('repo.pushurl')
    if not pushurl:
      pushurl = branch.remote.name
    else:
      pushurl = pushurl.rstrip('/') + '/' + self.name
      remote = self.manifest.remotes.get(branch.remote.name)
      if remote and remote.autodotgit is not False:
        pushurl += ".git"

    cmd = ['push']
    if opt.force:
      cmd.append('--force')
    cmd.append(pushurl)
    cmd.append(ref_spec)

    if GitCommand(self, cmd).Wait() != 0:
      raise UploadError('Upload failed')

    if branch.LocalMerge and branch.LocalMerge.startswith('refs/remotes'):
      self.bare_git.UpdateRef(branch.LocalMerge,
                              R_HEADS + branch.name)

在subcmd目录下添加push.py文件,内容如下:

#
# Copyright (C) 2010 [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import copy
import re
import sys

from command import InteractiveCommand
from editor import Editor
from error import UploadError, GitError
from project import ReviewableBranch

def _ConfirmManyUploads(multiple_branches=False):
  if multiple_branches:
    print "ATTENTION: One or more branches has an unusually high number of commits."
  else:
    print "ATTENTION: You are uploading an unusually high number of commits."
  print "YOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across branches?)"
  answer = raw_input("If you are sure you intend to do this, type 'yes': ").strip()
  return answer == "yes"

def _die(fmt, *args):
  msg = fmt % args
  print >>sys.stderr, 'error: %s' % msg
  sys.exit(1)

def _SplitEmails(values):
  result = []
  for str in values:
    result.extend([s.strip() for s in str.split(',')])
  return result

class Push(InteractiveCommand):
  common = True
  helpSummary = "Upload changes for code review"
  helpUsage="""
%prog [--re --cc] {[]... | --replace }
"""
  helpDescription = """
The '%prog' command is used to send changes to the Gerrit Code
Review system.  It searches for topic branches in local projects
that have not yet been published for review.  If multiple topic
branches are found, '%prog' opens an editor to allow the user to
select which branches to upload.

'%prog' searches for uploadable changes in all projects listed at
the command line.  Projects can be specified either by name, or by
a relative or absolute path to the project's local directory. If no
projects are specified, '%prog' will search for uploadable changes
in all projects listed in the manifest.

If the --reviewers or --cc options are passed, those emails are
added to the respective list of users, and emails are sent to any
new users.  Users passed as --reviewers must already be registered
with the code review system, or the upload will fail.

If the --replace option is passed the user can designate which
existing change(s) in Gerrit match up to the commits in the branch
being uploaded.  For each matched pair of change,commit the commit
will be added as a new patch set, completely replacing the set of
files and description associated with the change in Gerrit.

Configuration
-------------

review.URL.autoupload:

To disable the "Upload ... (y/n)?" prompt, you can set a per-project
or global Git configuration option.  If review.URL.autoupload is set
to "true" then repo will assume you always answer "y" at the prompt,
and will not prompt you further.  If it is set to "false" then repo
will assume you always answer "n", and will abort.

review.URL.autocopy:

To automatically copy a user or mailing list to all uploaded reviews,
you can set a per-project or global Git option to do so. Specifically,
review.URL.autocopy can be set to a comma separated list of reviewers
who you always want copied on all uploads with a non-empty --re
argument.

review.URL.username:

Override the username used to connect to Gerrit Code Review.
By default the local part of the email address is used.

The URL must match the review URL listed in the manifest XML file,
or in the .git/config within the project.  For example:

  [remote "origin"]
    url = git://git.example.com/project.git
    review = http://review.example.com/

  [review "http://review.example.com/"]
    autoupload = true
    autocopy = [email protected],[email protected]

References
----------

Gerrit Code Review:  http://code.google.com/p/gerrit/

"""

  def _Options(self, p):
    p.add_option('--new_branch',
                 dest='new_branch', action='store_true',
                 help='create new feature branch on git server.')
    p.add_option('-f', '--force',
                 dest='force',  action='store_true',
                 help='PUsh without rewind check.')
    p.add_option('--ignore_review',
                 dest='ignore_review', action='store_true',
                 help='run even has review defined.')

  def _SingleBranch(self, opt, branch):
    project = branch.project
    name = branch.name
    remote = project.GetBranch(name).remote

    key = 'review.%s.autoupload' % remote.review
    answer = project.config.GetBoolean(key)

    if answer is False:
      _die("upload blocked by %s = false" % key)

    if answer is None:
      date = branch.date
      list = branch.commits

      print 'Upload project %s/:' % project.relpath
      print '  branch %s (%2d commit%s, %s):' % (
                    name,
                    len(list),
                    len(list) != 1 and 's' or '',
                    date)
      for commit in list:
        print '         %s' % commit

      pushurl = project.manifest.manifestProject.config.GetString('repo.pushurl')
      sys.stdout.write('to %s (y/n)? ' % (pushurl and 'server: ' + pushurl or 'remote') )
      answer = sys.stdin.readline().strip()
      answer = answer in ('y', 'Y', 'yes', '1', 'true', 't')

    if answer:
      self._UploadAndReport(opt, [branch])
    else:
      _die("upload aborted by user")

  def _MultipleBranches(self, opt, pending):
    projects = {}
    branches = {}

    script = []
    script.append('# Uncomment the branches to upload:')
    for project, avail in pending:
      script.append('#')
      script.append('# project %s/:' % project.relpath)

      b = {}
      for branch in avail:
        name = branch.name
        date = branch.date
        list = branch.commits

        if b:
          script.append('#')
        script.append('#  branch %s (%2d commit%s, %s):' % (
                      name,
                      len(list),
                      len(list) != 1 and 's' or '',
                      date))
        for commit in list:
          script.append('#         %s' % commit)
        b[name] = branch

      projects[project.relpath] = project
      branches[project.name] = b
    script.append('')

    script = Editor.EditString("\n".join(script)).split("\n")

    project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$')
    branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*')

    project = None
    todo = []

    for line in script:
      m = project_re.match(line)
      if m:
        name = m.group(1)
        project = projects.get(name)
        if not project:
          _die('project %s not available for upload', name)
        continue

      m = branch_re.match(line)
      if m:
        name = m.group(1)
        if not project:
          _die('project for branch %s not in script', name)
        branch = branches[project.name].get(name)
        if not branch:
          _die('branch %s not in %s', name, project.relpath)
        todo.append(branch)
    if not todo:
      _die("nothing uncommented for upload")

    self._UploadAndReport(opt, todo)

  def _UploadAndReport(self, opt, todo):
    have_errors = False
    for branch in todo:
      try:
        # Check if there are local changes that may have been forgotten
        if branch.project.HasChanges():
            key = 'review.%s.autoupload' % branch.project.remote.review
            answer = branch.project.config.GetBoolean(key)

            # if they want to auto upload, let's not ask because it could be automated
            if answer is None:
                sys.stdout.write('Uncommitted changes in ' + branch.project.name + ' (did you forget to amend?). Continue uploading? (y/n) ')
                a = sys.stdin.readline().strip().lower()
                if a not in ('y', 'yes', 't', 'true', 'on'):
                    print >>sys.stderr, "skipping upload"
                    branch.uploaded = False
                    branch.error = 'User aborted'
                    continue

        branch.project.UploadNoReview(opt, branch=branch.name)
        branch.uploaded = True
      except UploadError, e:
        branch.error = e
        branch.uploaded = False
        have_errors = True
      except GitError, e:
        print >>sys.stderr, "Error: "+ str(e)
        sys.exit(1)


    print >>sys.stderr, ''
    print >>sys.stderr, '--------------------------------------------'

    if have_errors:
      for branch in todo:
        if not branch.uploaded:
          print >>sys.stderr, '[FAILED] %-15s %-15s  (%s)' % (
                 branch.project.relpath + '/', \
                 branch.name, \
                 branch.error)
      print >>sys.stderr, ''

    for branch in todo:
        if branch.uploaded:
          print >>sys.stderr, '[OK    ] %-15s %s' % (
                 branch.project.relpath + '/',
                 branch.name)

    if have_errors:
      sys.exit(1)

  def Execute(self, opt, args):
    project_list = self.GetProjects(args)
    pending = []

    # force push only allow one project
    if opt.force:
      if len(project_list) != 1:
        print >>sys.stderr, \
              'error: --force requires exactly one project'
        sys.exit(1)

    # if not create new branch, check whether branch has new commit.
    for project in project_list:
      if (not opt.new_branch and
          project.GetUploadableBranch(project.CurrentBranch) is None):
        continue
      branch = project.GetBranch(project.CurrentBranch)
      rb = ReviewableBranch(project, branch, branch.LocalMerge)
      pending.append((project, [rb]))

    # run git push
    if not pending:
      print >>sys.stdout, "no branches ready for upload"
    elif len(pending) == 1 and len(pending[0][1]) == 1:
      self._SingleBranch(opt, pending[0][1][0])
    else:
      self._MultipleBranches(opt, pending)

Repo添加remote命令

在sumcmds中添加remote.py,程序如下:

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import sys
from color import Coloring
from command import Command
from progress import Progress








class Remote(Command):
  common = True
  helpSummary = "View current topic branches"
  helpUsage = """
%prog add   [...]
       %prog rm   [...]


--------------


"""
  def str_last(self ,string):
     for c in string:
         last=c
     return last


  def Execute(self, opt, args):
    if not args:
      print >>sys.stderr, "error:..........wrong command........"
      print >>sys.stderr, "Usage:repo remote add   [...]"
      print >>sys.stderr, "      repo remote rm   [...] "          
      print >>sys.stderr, "................................"
      return


    err = []
    operate=args[0]
    #url = args[2]
   # branch_name=args[1]
    if operate == "rm":
       if not len(args) >=2:
         print >>sys.stderr, "error:miss remotebrancname"
         return


       branch_name=args[1]
       projects = args[2:]
    elif operate == "add":
       if not len(args) >=3:
         print >>sys.stderr, "error:miss remotebranchname or url"
         return


       branch_name=args[1]
       projects = args[3:]
    else:
       print >>sys.stderr, "error: the operand is add or rm "
       return
     
    all = self.GetProjects(projects)
   # print >>sys.stderr, all
    pm = Progress('remote %s' % operate, len(all))
    for project in all:
       if operate == "add":
          if self.str_last(args[2])=="/":
             url = args[2]+project.name+'.git'
          else :
             url = args[2]+'/'+project.name+'.git'
       else:
         url = ""


       pm.update() 
       if not project.Remote(operate,branch_name,url):
         err.append(project)
    pm.end()
       
    if err:
      if len(err) == len(all):
        print >>sys.stderr, 'error: no project remote  %s %s' % (operate,branch_name)  
      else:
        for p in err:
          print >>sys.stderr,\
            "error: %s/: cannot remote %s %s " \
            % (p.relpath, operate,branch_name)
      sys.exit(1)

在project.py的project类的UploadNoReview方法附近,添加Remote(operate,branch_name,url)方法:

  def Remote(self,operate,branch_name,url):
    """Prune  topic branches already merged into upstream.
    """
    if url=="":   #rm
      return GitCommand(self,
                        ['remote', operate, branch_name],
                        capture_stdout = True,
                        capture_stderr = True).Wait() == 0
    else:  #add
      return GitCommand(self,
                        ['remote', operate, branch_name,url],
                        capture_stdout = True,
                        capture_stderr = True).Wait() == 0

你可能感兴趣的:(软件使用)