Code Duplication    Length = 90-99 lines in 2 locations

git_app_version/_version.py 1 location

@@ 233-331 (lines=99) @@
230
    }
231
232
233
@register_vcs_handler("git", "pieces_from_vcs")
234
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
235
    """Get version from 'git describe' in the root of the source tree.
236
237
    This only gets called if the git-archive 'subst' keywords were *not*
238
    expanded, and _version.py hasn't already been rewritten with a short
239
    version string, meaning we're inside a checked out source tree.
240
    """
241
    GITS = ["git"]
242
    if sys.platform == "win32":
243
        GITS = ["git.cmd", "git.exe"]
244
245
    out, rc = run_command(
246
        GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True
247
    )
248
    if rc != 0:
249
        if verbose:
250
            print("Directory %s not under git control" % root)
251
        raise NotThisMethod("'git rev-parse --git-dir' returned error")
252
253
    # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
254
    # if there isn't one, this yields HEX[-dirty] (no NUM)
255
    describe_out, rc = run_command(
256
        GITS, [
257
            "describe", "--tags", "--dirty", "--always", "--long", "--match",
258
            "%s*" % tag_prefix
259
        ],
260
        cwd=root
261
    )
262
    # --long was added in git-1.5.5
263
    if describe_out is None:
264
        raise NotThisMethod("'git describe' failed")
265
    describe_out = describe_out.strip()
266
    full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
267
    if full_out is None:
268
        raise NotThisMethod("'git rev-parse' failed")
269
    full_out = full_out.strip()
270
271
    pieces = {}
272
    pieces["long"] = full_out
273
    pieces["short"] = full_out[:7]  # maybe improved later
274
    pieces["error"] = None
275
276
    # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
277
    # TAG might have hyphens.
278
    git_describe = describe_out
279
280
    # look for -dirty suffix
281
    dirty = git_describe.endswith("-dirty")
282
    pieces["dirty"] = dirty
283
    if dirty:
284
        git_describe = git_describe[:git_describe.rindex("-dirty")]
285
286
    # now we have TAG-NUM-gHEX or HEX
287
288
    if "-" in git_describe:
289
        # TAG-NUM-gHEX
290
        mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
291
        if not mo:
292
            # unparseable. Maybe git-describe is misbehaving?
293
            pieces["error"] = (
294
                "unable to parse git-describe output: '%s'" % describe_out
295
            )
296
            return pieces
297
298
        # tag
299
        full_tag = mo.group(1)
300
        if not full_tag.startswith(tag_prefix):
301
            if verbose:
302
                fmt = "tag '%s' doesn't start with prefix '%s'"
303
                print(fmt % (full_tag, tag_prefix))
304
            pieces["error"] = (
305
                "tag '%s' doesn't start with prefix '%s'" %
306
                (full_tag, tag_prefix)
307
            )
308
            return pieces
309
        pieces["closest-tag"] = full_tag[len(tag_prefix):]
310
311
        # distance: number of commits since tag
312
        pieces["distance"] = int(mo.group(2))
313
314
        # commit: short hex revision ID
315
        pieces["short"] = mo.group(3)
316
317
    else:
318
        # HEX: no tags
319
        pieces["closest-tag"] = None
320
        count_out, rc = run_command(
321
            GITS, ["rev-list", "HEAD", "--count"], cwd=root
322
        )
323
        pieces["distance"] = int(count_out)  # total number of commits
324
325
    # commit date: see ISO-8601 comment in git_versions_from_keywords()
326
    date = run_command(
327
        GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root
328
    )[0].strip()
329
    pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
330
331
    return pieces
332
333
334
def plus_or_dot(pieces):

versioneer.py 1 location

@@ 1028-1117 (lines=90) @@
1025
            "dirty": False, "error": "no suitable tags", "date": None}
1026
1027
1028
@register_vcs_handler("git", "pieces_from_vcs")
1029
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
1030
    """Get version from 'git describe' in the root of the source tree.
1031
1032
    This only gets called if the git-archive 'subst' keywords were *not*
1033
    expanded, and _version.py hasn't already been rewritten with a short
1034
    version string, meaning we're inside a checked out source tree.
1035
    """
1036
    GITS = ["git"]
1037
    if sys.platform == "win32":
1038
        GITS = ["git.cmd", "git.exe"]
1039
1040
    out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,
1041
                          hide_stderr=True)
1042
    if rc != 0:
1043
        if verbose:
1044
            print("Directory %s not under git control" % root)
1045
        raise NotThisMethod("'git rev-parse --git-dir' returned error")
1046
1047
    # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
1048
    # if there isn't one, this yields HEX[-dirty] (no NUM)
1049
    describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",
1050
                                          "--always", "--long",
1051
                                          "--match", "%s*" % tag_prefix],
1052
                                   cwd=root)
1053
    # --long was added in git-1.5.5
1054
    if describe_out is None:
1055
        raise NotThisMethod("'git describe' failed")
1056
    describe_out = describe_out.strip()
1057
    full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
1058
    if full_out is None:
1059
        raise NotThisMethod("'git rev-parse' failed")
1060
    full_out = full_out.strip()
1061
1062
    pieces = {}
1063
    pieces["long"] = full_out
1064
    pieces["short"] = full_out[:7]  # maybe improved later
1065
    pieces["error"] = None
1066
1067
    # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
1068
    # TAG might have hyphens.
1069
    git_describe = describe_out
1070
1071
    # look for -dirty suffix
1072
    dirty = git_describe.endswith("-dirty")
1073
    pieces["dirty"] = dirty
1074
    if dirty:
1075
        git_describe = git_describe[:git_describe.rindex("-dirty")]
1076
1077
    # now we have TAG-NUM-gHEX or HEX
1078
1079
    if "-" in git_describe:
1080
        # TAG-NUM-gHEX
1081
        mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
1082
        if not mo:
1083
            # unparseable. Maybe git-describe is misbehaving?
1084
            pieces["error"] = ("unable to parse git-describe output: '%s'"
1085
                               % describe_out)
1086
            return pieces
1087
1088
        # tag
1089
        full_tag = mo.group(1)
1090
        if not full_tag.startswith(tag_prefix):
1091
            if verbose:
1092
                fmt = "tag '%s' doesn't start with prefix '%s'"
1093
                print(fmt % (full_tag, tag_prefix))
1094
            pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
1095
                               % (full_tag, tag_prefix))
1096
            return pieces
1097
        pieces["closest-tag"] = full_tag[len(tag_prefix):]
1098
1099
        # distance: number of commits since tag
1100
        pieces["distance"] = int(mo.group(2))
1101
1102
        # commit: short hex revision ID
1103
        pieces["short"] = mo.group(3)
1104
1105
    else:
1106
        # HEX: no tags
1107
        pieces["closest-tag"] = None
1108
        count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"],
1109
                                    cwd=root)
1110
        pieces["distance"] = int(count_out)  # total number of commits
1111
1112
    # commit date: see ISO-8601 comment in git_versions_from_keywords()
1113
    date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"],
1114
                       cwd=root)[0].strip()
1115
    pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
1116
1117
    return pieces
1118
1119
1120
def do_vcs_install(manifest_in, versionfile_source, ipy):