Esempio n. 1
0
def load_generation_numbers(targets):
  """Populates the caches of get_num and get_number_tree so they contain
  the results for |targets|.

  Loads cached numbers from disk, and calculates missing numbers if one or
  more of |targets| is newer than the cached calculations.

  Args:
    targets - An iterable of binary-encoded full git commit hashes.
  """
  # In case they pass us a generator, listify targets.
  targets = list(targets)

  if all(get_num(t) is not None for t in targets):
    return

  if git.tree(REF) is None:
    empty = git.mktree({})
    commit_hash = git.run(
        # Git user.name and/or user.email may not be configured, so specifying
        # them explicitly. They are not used, but requried by Git.
        '-c', 'user.name=%s' % AUTHOR_NAME,
        '-c', 'user.email=%s' % AUTHOR_EMAIL,
        'commit-tree',
        '-m', 'Initial commit from git-number',
        empty)
    git.run('update-ref', REF, commit_hash)

  with git.ScopedPool(kind=POOL_KIND) as pool:
    preload_iter = pool.imap_unordered(preload_tree, all_prefixes())

    rev_list = []

    with git.ProgressPrinter('Loading commits: %(count)d') as inc:
      # Curiously, buffering the list into memory seems to be the fastest
      # approach in python (as opposed to iterating over the lines in the
      # stdout as they're produced). GIL strikes again :/
      cmd = [
        'rev-list', '--topo-order', '--parents', '--reverse', '^' + REF,
      ] + [binascii.hexlify(target).decode() for target in targets]
      for line in git.run(*cmd).splitlines():
        tokens = [binascii.unhexlify(token) for token in line.split()]
        rev_list.append((tokens[0], tokens[1:]))
        inc()

    get_number_tree.update(preload_iter)

  with git.ProgressPrinter('Counting: %%(count)d/%d' % len(rev_list)) as inc:
    for commit_hash, pars in rev_list:
      num = max(map(get_num, pars)) + 1 if pars else 0

      prefix = commit_hash[:PREFIX_LEN]
      get_number_tree(prefix)[commit_hash] = num
      DIRTY_TREES[prefix] += 1
      get_num.set(commit_hash, num)

      inc()
Esempio n. 2
0
def load_generation_numbers(targets):
  """Populates the caches of get_num and get_number_tree so they contain
  the results for |targets|.

  Loads cached numbers from disk, and calculates missing numbers if one or
  more of |targets| is newer than the cached calculations.

  Args:
    targets - An iterable of binary-encoded full git commit hashes.
  """
  # In case they pass us a generator, listify targets.
  targets = list(targets)

  if all(get_num(t) is not None for t in targets):
    return

  if git.tree(REF) is None:
    empty = git.mktree({})
    commit_hash = git.run(
        # Git user.name and/or user.email may not be configured, so specifying
        # them explicitly. They are not used, but requried by Git.
        '-c', 'user.name=%s' % AUTHOR_NAME,
        '-c', 'user.email=%s' % AUTHOR_EMAIL,
        'commit-tree',
        '-m', 'Initial commit from git-number',
        empty)
    git.run('update-ref', REF, commit_hash)

  with git.ScopedPool(kind=POOL_KIND) as pool:
    preload_iter = pool.imap_unordered(preload_tree, all_prefixes())

    rev_list = []

    with git.ProgressPrinter('Loading commits: %(count)d') as inc:
      # Curiously, buffering the list into memory seems to be the fastest
      # approach in python (as opposed to iterating over the lines in the
      # stdout as they're produced). GIL strikes again :/
      cmd = [
        'rev-list', '--topo-order', '--parents', '--reverse', '^' + REF,
      ] + map(binascii.hexlify, targets)
      for line in git.run(*cmd).splitlines():
        tokens = map(binascii.unhexlify, line.split())
        rev_list.append((tokens[0], tokens[1:]))
        inc()

    get_number_tree.update(preload_iter)

  with git.ProgressPrinter('Counting: %%(count)d/%d' % len(rev_list)) as inc:
    for commit_hash, pars in rev_list:
      num = max(map(get_num, pars)) + 1 if pars else 0

      prefix = commit_hash[:PREFIX_LEN]
      get_number_tree(prefix)[commit_hash] = num
      DIRTY_TREES[prefix] += 1
      get_num.set(commit_hash, num)

      inc()