File: tag.py

package info (click to toggle)
git-ubuntu 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,688 kB
  • sloc: python: 13,378; sh: 480; makefile: 2
file content (158 lines) | stat: -rw-r--r-- 5,117 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import argparse
import logging
import os
import sys

import pygit2

from gitubuntu.git_repository import GitUbuntuRepository, git_dep14_tag

def parse_args(subparsers=None, base_subparsers=None):
    kwargs = dict(description='Given a %(prog)s import\'d tree, tag a commit respecting DEP14',
                  formatter_class=argparse.RawTextHelpFormatter,
                  epilog='By default, an upload tag is created. Working tree must be clean.'
                 )
    if base_subparsers:
        kwargs['parents'] = base_subparsers
    if subparsers:
        parser = subparsers.add_parser('tag', **kwargs)
        parser.set_defaults(func=cli_main)
    else:
        parser = argparse.ArgumentParser(**kwargs)

    parser.add_argument('commitish',
                        help='Commitish to tag',
                        type=str,
                        default='HEAD',
                        nargs='?'
                       )
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--logical', action='store_true',
                       help='Equivalent to --format=\'logical/%%(version)s\', with version extracted from the commit tagged as old/ubuntu')
    group.add_argument('--split', action='store_true',
                       help='Equivalent to --format=\'split/%%(version)s\'')
    group.add_argument('--reconstruct', action='store_true',
                       help='Equivalent to --format=\'reconstruct/%%(version)s\'')
    group.add_argument('--upload', action='store_true',
                       help='Equivalent to --format=\'upload/%%(version)s\'')
    group.add_argument('--format', type=str,
                       help='Use FORMAT when tagging commit'
                      )
    parser.add_argument('--bug',
                        help='Launchpad bug for the tags (primarily '
                             'used in the merge workflow)',
                        type=str)
    parser.add_argument('-d', '--directory', type=str,
                        help='Use git repository at specified location.',
                        default=os.path.abspath(os.getcwd())
                       )
    parser.add_argument('-f', '--force',
                        help='Overwrite existing tags and branches, where '
                             'applicable.',
                        action='store_true'
                       )
    parser.add_argument(
        '--print-name-only',
        help="No-op: only print the name of the tag that would be created",
        action='store_true',
    )
    if not subparsers:
        return parser.parse_args()
    return 'tag - %s' % kwargs['description']

def cli_main(args):
    return main(
        args.directory,
        args.commitish,
        args.force,
        args.bug,
        args.logical,
        args.reconstruct,
        args.split,
        args.upload,
        args.format,
        args.print_name_only,
    )

def main(
    directory,
    commitish,
    force,
    bug,
    logical,
    reconstruct,
    split,
    upload,
    _format,
    print_name_only,
):
    # Suppress INFO logging in case --print-name-only has been used
    logging.getLogger().setLevel(logging.WARNING)

    repo = GitUbuntuRepository(directory)

    changelog_commitish = commitish

    msg_format = None
    tag_prefix = ''
    if bug:
        tag_prefix = 'lp%s/' % bug

    if logical:
        fmt = '%slogical/%%(version)s' % tag_prefix
        changelog_commitish = 'old/ubuntu'
        msg_format = 'Logical delta of %(version)s'
    elif reconstruct:
        fmt = '%sreconstruct/%%(version)s' % tag_prefix
        msg_format = 'Reconstruct delta of %(version)s'
    elif split:
        fmt = '%ssplit/%%(version)s' % tag_prefix
        msg_format = 'split delta of %(version)s'
    elif upload:
        fmt = '%supload/%%(version)s' % tag_prefix
        msg_format = 'Upload of %(version)s to %(dist)s'
    elif _format:
        fmt = '%s%s' % (tag_prefix, _format)
    else:
        fmt = '%supload/%%(version)s' % tag_prefix
        msg_format = 'Upload of %(version)s to %(dist)s'

    try:
        repo.get_commitish(commitish)
        repo.get_commitish(changelog_commitish)
    except KeyError:
        logging.error(
            "%s is not a defined object in this git repository.",
            commitish
        )
        return 1

    work_tree_is_clean = all(
        s == pygit2.GIT_STATUS_IGNORED
        for s in repo.raw_repo.status().values()
    )
    if not work_tree_is_clean:
        logging.error('Working tree must be clean to continue.')
        return 1

    version, _ = repo.get_changelog_versions_from_treeish(changelog_commitish)
    dist = repo.get_changelog_distribution_from_treeish(changelog_commitish)
    tag = fmt % {'version': git_dep14_tag(version)}
    msg = None
    if msg_format:
        msg = msg_format % {'version': version, 'dist': dist}

    if print_name_only:
        print(tag)
    else:
        try:
            repo.annotated_tag(
                tag,
                commitish,
                force,
                msg,
            )
        except:
            return 1

    return 0