From d76ad6a1f4c81ad1c01b570189a7a729f1b60af3 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Feb 05 2016 13:06:51 +0000 Subject: [PATCH 1/3] Sorting Release using the commit time --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 9cc1988..9e9165f 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -1195,9 +1195,7 @@ def get_git_tags(project): def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. - The list is sorted using the tag name and returns the highest release - version first. - """ + The list is sorted using the time of the commit associated to the tag """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = [ @@ -1210,17 +1208,14 @@ def get_git_tags_objects(project): tags_sort = {} for tag in tags: - # Split the git tag name using any non-alphanumeric character - # and store each element returned in a tuple. - splitted_tag = re.split('[^a-zA-Z0-9]+', tag.name) - sorting_tuple = () - for item in splitted_tag: - # Cast the result to an integer when possible - if item.isdigit(): - sorting_tuple += (int(item),) - else: - sorting_tuple += (item,) - tags_sort[sorting_tuple] = tag + # If the object is a tag, get his associated commit time + if tag.type == pygit2.GIT_OBJ_TAG: + tags_sort[tag.get_object().commit_time] = tag + elif tag.type == pygit2.GIT_OBJ_COMMIT: + tags_sort[tag.commit_time] = tag + # If object is neither a tag or commit return an unsorted list + else: + return tags for tag in sorted(tags_sort, reverse=True): sorted_tags.append(tags_sort[tag]) From 40ad40c4f2ebafcffc6cf61bee197f8f9cdf3bb9 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Feb 05 2016 14:58:00 +0000 Subject: [PATCH 2/3] Sorting Release (git tags) by time + unit tests --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 9e9165f..995764f 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -1209,9 +1209,9 @@ def get_git_tags_objects(project): for tag in tags: # If the object is a tag, get his associated commit time - if tag.type == pygit2.GIT_OBJ_TAG: + if isinstance(tag, pygit2.Tag): tags_sort[tag.get_object().commit_time] = tag - elif tag.type == pygit2.GIT_OBJ_COMMIT: + elif isinstance(tag, pygit2.Commit): tags_sort[tag.commit_time] = tag # If object is neither a tag or commit return an unsorted list else: diff --git a/tests/test_pagure_lib_git_get_tags_objects.py b/tests/test_pagure_lib_git_get_tags_objects.py index 1563523..3547307 100644 --- a/tests/test_pagure_lib_git_get_tags_objects.py +++ b/tests/test_pagure_lib_git_get_tags_objects.py @@ -11,6 +11,7 @@ import unittest import sys import os +import time import pygit2 @@ -29,11 +30,15 @@ def get_tag_name(tags): return output -def add_repo_tag(repo, tags): +def add_repo_tag(repo, tags, repo_name): """ Use a list to create multiple tags on a git repo """ - first_commit = repo.revparse_single('HEAD') - tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0) - for tag in tags: + for tag in reversed(tags): + time.sleep(1) + tests.add_commit_git_repo( + os.path.join(tests.HERE, 'repos', repo_name), + ncommits=1) + first_commit = repo.revparse_single('HEAD') + tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0) repo.create_tag( tag, first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, "Release " + tag) @@ -62,43 +67,33 @@ class PagureLibGitGetTagstests(tests.Modeltests): tests.create_projects(self.session) tests.create_projects_git(os.path.join(tests.HERE, 'repos'), bare=True) project = pagure.lib.get_project(self.session, 'test') - - # Case 1 - project does not contains tags - exp = [] - tags = pagure.lib.git.get_git_tags_objects(project) - self.assertEqual(exp, get_tag_name(tags)) - - # Case 2 - Simple sort tests.add_readme_git_repo(os.path.join(os.path.join( tests.HERE, 'repos'), 'test.git')) repo = pygit2.Repository(os.path.join(os.path.join( tests.HERE, 'repos'), 'test.git')) - exp = ['0.1.0', '0.0.21', '0.0.12', '0.0.11', '0.0.3', '0.0.2', - '0.0.1'] - add_repo_tag(repo, exp) + # Case 1 - project does not contains tags + exp = [] tags = pagure.lib.git.get_git_tags_objects(project) self.assertEqual(exp, get_tag_name(tags)) - # Case 3 Sorting with alphas and betas - exp = ['0.1.0', '0.0.21', '0.0.12-beta', '0.0.12-alpha', '0.0.12', - '0.0.11', '0.0.3', '0.0.2', '0.0.1'] - add_repo_tag(repo, ['0.0.12-beta', '0.0.12-alpha']) + # Case 2 - Simple sort + exp = ['0.1.0', 'test-0.0.21', '0.0.12-beta', '0.0.12-alpha', '0.0.12', + '0.0.11', '0.0.3', 'foo-0.0.2', '0.0.1'] + add_repo_tag(repo, exp, 'test.git') tags = pagure.lib.git.get_git_tags_objects(project) self.assertEqual(exp, get_tag_name(tags)) - # Case 4 - Sorting with different splitting characters + # Case 3 - Sorting with different splitting characters project = pagure.lib.get_project(self.session, 'test2') tests.add_readme_git_repo(os.path.join(os.path.join( tests.HERE, 'repos'), 'test2.git')) repo = pygit2.Repository(os.path.join(os.path.join( tests.HERE, 'repos'), 'test2.git')) - exp = ['15.1-0_0', '1.0-0_14', '1.0-0_2', '1.0-0_1', '0.1-1_0', - '0.1-0_0', '0.0-2_0', '0.0-1_34', '0.0-1_11', '0.0-1_3', - '0.0-1_2', '0.0-1_1'] - add_repo_tag(repo, exp) - + exp = ['1.0-0_2', '1.0-0_1', '0.1-1_0', '0.1-0_0', '0.0-2_0', + '0.0-1_34', '0.0-1_11', '0.0-1_3', '0.0-1_2', '0.0-1_1'] + add_repo_tag(repo, exp, 'test2.git') tags = pagure.lib.git.get_git_tags_objects(project) self.assertEqual(exp, get_tag_name(tags)) From ebd4f9c1e051ae0de80d333f02a4e25ca3faff31 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Feb 05 2016 17:12:29 +0000 Subject: [PATCH 3/3] Added new test case to test sorting tag on an empty repo --- diff --git a/tests/test_pagure_lib_git_get_tags_objects.py b/tests/test_pagure_lib_git_get_tags_objects.py index 3547307..02ce2c4 100644 --- a/tests/test_pagure_lib_git_get_tags_objects.py +++ b/tests/test_pagure_lib_git_get_tags_objects.py @@ -67,24 +67,30 @@ class PagureLibGitGetTagstests(tests.Modeltests): tests.create_projects(self.session) tests.create_projects_git(os.path.join(tests.HERE, 'repos'), bare=True) project = pagure.lib.get_project(self.session, 'test') + + # Case 1 - Empty repo with no tags + exp = [] + tags = pagure.lib.git.get_git_tags_objects(project) + self.assertEqual(exp, get_tag_name(tags)) + tests.add_readme_git_repo(os.path.join(os.path.join( tests.HERE, 'repos'), 'test.git')) repo = pygit2.Repository(os.path.join(os.path.join( tests.HERE, 'repos'), 'test.git')) - # Case 1 - project does not contains tags + # Case 2 - Repo with one commit and no tags exp = [] tags = pagure.lib.git.get_git_tags_objects(project) self.assertEqual(exp, get_tag_name(tags)) - # Case 2 - Simple sort + # Case 3 - Simple sort exp = ['0.1.0', 'test-0.0.21', '0.0.12-beta', '0.0.12-alpha', '0.0.12', '0.0.11', '0.0.3', 'foo-0.0.2', '0.0.1'] add_repo_tag(repo, exp, 'test.git') tags = pagure.lib.git.get_git_tags_objects(project) self.assertEqual(exp, get_tag_name(tags)) - # Case 3 - Sorting with different splitting characters + # Case 4 - Sorting with different splitting characters project = pagure.lib.get_project(self.session, 'test2') tests.add_readme_git_repo(os.path.join(os.path.join( tests.HERE, 'repos'), 'test2.git'))