Code Duplication    Length = 82-82 lines in 2 locations

versioneer.py 1 location

@@ 1043-1124 (lines=82) @@
1040
    if verbose:
1041
        print("likely tags: %s" % ",".join(sorted(tags)))
1042
    for ref in sorted(tags):
1043
        # sorting will prefer e.g. "2.0" over "2.0rc1"
1044
        if ref.startswith(tag_prefix):
1045
            r = ref[len(tag_prefix):]
1046
            if verbose:
1047
                print("picking %s" % r)
1048
            return {"version": r,
1049
                    "full-revisionid": keywords["full"].strip(),
1050
                    "dirty": False, "error": None,
1051
                    "time": keywords["time"].strip()}
1052
    # no suitable tags, so version is "0+unknown", but full hex is still there
1053
    if verbose:
1054
        print("no suitable tags, using unknown + full revision id")
1055
    return {"version": "0+unknown",
1056
            "full-revisionid": keywords["full"].strip(),
1057
            "dirty": False, "error": "no suitable tags", "time": None}
1058
1059
1060
@register_vcs_handler("git", "pieces_from_vcs")
1061
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
1062
    """Get version from 'git describe' in the root of the source tree.
1063
1064
    This only gets called if the git-archive 'subst' keywords were *not*
1065
    expanded, and _version.py hasn't already been rewritten with a short
1066
    version string, meaning we're inside a checked out source tree.
1067
    """
1068
    if not os.path.exists(os.path.join(root, ".git")):
1069
        if verbose:
1070
            print("no .git in %s" % root)
1071
        raise NotThisMethod("no .git directory")
1072
1073
    GITS = ["git"]
1074
    if sys.platform == "win32":
1075
        GITS = ["git.cmd", "git.exe"]
1076
    # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
1077
    # if there isn't one, this yields HEX[-dirty] (no NUM)
1078
    describe_out = run_command(GITS, ["describe", "--tags", "--dirty",
1079
                                      "--always", "--long",
1080
                                      "--match", "%s*" % tag_prefix],
1081
                               cwd=root)
1082
    # --long was added in git-1.5.5
1083
    if describe_out is None:
1084
        raise NotThisMethod("'git describe' failed")
1085
    describe_out = describe_out.strip()
1086
    full_out = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
1087
    if full_out is None:
1088
        raise NotThisMethod("'git rev-parse' failed")
1089
    full_out = full_out.strip()
1090
1091
    pieces = {}
1092
    pieces["long"] = full_out
1093
    pieces["short"] = full_out[:7]  # maybe improved later
1094
    pieces["error"] = None
1095
1096
    # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
1097
    # TAG might have hyphens.
1098
    git_describe = describe_out
1099
1100
    # look for -dirty suffix
1101
    dirty = git_describe.endswith("-dirty")
1102
    pieces["dirty"] = dirty
1103
    if dirty:
1104
        git_describe = git_describe[:git_describe.rindex("-dirty")]
1105
1106
    # now we have TAG-NUM-gHEX or HEX
1107
1108
    if "-" in git_describe:
1109
        # TAG-NUM-gHEX
1110
        mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
1111
        if not mo:
1112
            # unparseable. Maybe git-describe is misbehaving?
1113
            pieces["error"] = ("unable to parse git-describe output: '%s'"
1114
                               % describe_out)
1115
            return pieces
1116
1117
        # tag
1118
        full_tag = mo.group(1)
1119
        if not full_tag.startswith(tag_prefix):
1120
            if verbose:
1121
                fmt = "tag '%s' doesn't start with prefix '%s'"
1122
                print(fmt % (full_tag, tag_prefix))
1123
            pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
1124
                               % (full_tag, tag_prefix))
1125
            return pieces
1126
        pieces["closest-tag"] = full_tag[len(tag_prefix):]
1127

bbarchivist/_version.py 1 location

@@ 191-272 (lines=82) @@
188
                    "time": keywords["time"].strip()}
189
    # no suitable tags, so version is "0+unknown", but full hex is still there
190
    if verbose:
191
        print("no suitable tags, using unknown + full revision id")
192
    return {"version": "0+unknown",
193
            "full-revisionid": keywords["full"].strip(),
194
            "dirty": False, "error": "no suitable tags", "time": None}
195
196
197
@register_vcs_handler("git", "pieces_from_vcs")
198
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
199
    """Get version from 'git describe' in the root of the source tree.
200
201
    This only gets called if the git-archive 'subst' keywords were *not*
202
    expanded, and _version.py hasn't already been rewritten with a short
203
    version string, meaning we're inside a checked out source tree.
204
    """
205
    if not os.path.exists(os.path.join(root, ".git")):
206
        if verbose:
207
            print("no .git in %s" % root)
208
        raise NotThisMethod("no .git directory")
209
210
    GITS = ["git"]
211
    if sys.platform == "win32":
212
        GITS = ["git.cmd", "git.exe"]
213
    # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
214
    # if there isn't one, this yields HEX[-dirty] (no NUM)
215
    describe_out = run_command(GITS, ["describe", "--tags", "--dirty",
216
                                      "--always", "--long",
217
                                      "--match", "%s*" % tag_prefix],
218
                               cwd=root)
219
    # --long was added in git-1.5.5
220
    if describe_out is None:
221
        raise NotThisMethod("'git describe' failed")
222
    describe_out = describe_out.strip()
223
    full_out = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
224
    if full_out is None:
225
        raise NotThisMethod("'git rev-parse' failed")
226
    full_out = full_out.strip()
227
228
    pieces = {}
229
    pieces["long"] = full_out
230
    pieces["short"] = full_out[:7]  # maybe improved later
231
    pieces["error"] = None
232
233
    # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
234
    # TAG might have hyphens.
235
    git_describe = describe_out
236
237
    # look for -dirty suffix
238
    dirty = git_describe.endswith("-dirty")
239
    pieces["dirty"] = dirty
240
    if dirty:
241
        git_describe = git_describe[:git_describe.rindex("-dirty")]
242
243
    # now we have TAG-NUM-gHEX or HEX
244
245
    if "-" in git_describe:
246
        # TAG-NUM-gHEX
247
        mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
248
        if not mo:
249
            # unparseable. Maybe git-describe is misbehaving?
250
            pieces["error"] = ("unable to parse git-describe output: '%s'"
251
                               % describe_out)
252
            return pieces
253
254
        # tag
255
        full_tag = mo.group(1)
256
        if not full_tag.startswith(tag_prefix):
257
            if verbose:
258
                fmt = "tag '%s' doesn't start with prefix '%s'"
259
                print(fmt % (full_tag, tag_prefix))
260
            pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
261
                               % (full_tag, tag_prefix))
262
            return pieces
263
        pieces["closest-tag"] = full_tag[len(tag_prefix):]
264
265
        # distance: number of commits since tag
266
        pieces["distance"] = int(mo.group(2))
267
268
        # commit: short hex revision ID
269
        pieces["short"] = mo.group(3)
270
271
    else:
272
        # HEX: no tags
273
        pieces["closest-tag"] = None
274
        count_out = run_command(GITS, ["rev-list", "HEAD", "--count"],
275
                                cwd=root)