Skip to content

Commit 193ffa1

Browse files
committed
ADDED: new argument for collision strategy and splitted two types of collisions
1 parent 0938344 commit 193ffa1

File tree

5 files changed

+94
-27
lines changed

5 files changed

+94
-27
lines changed

src/ArgumentParserService.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
from datetime import datetime
55

66
from FileService import is_file_directory_writable, is_file_writable
7-
from defines import FlattenLevel, CollisionStrategy
7+
from defines import FlattenLevel, RenameStrategy, CollisionAction, ProviderType
88
import argparse
99

1010

11+
# TODO: prioritize argument to give priority
1112
def build_argument_parser():
1213
parser = argparse.ArgumentParser(description="Backup your git repos in your local filesystem")
1314

@@ -71,19 +72,41 @@ def build_argument_parser():
7172
nargs="+",
7273
dest="flatten_directories",
7374
choices=[name for name in FlattenLevel])
74-
parser.add_argument("-l", "--handle-collision", "--handle-collision-strategy",
75-
help="Strategy to follow to handle collisions (a repo that because of its name has to be cloned"
76-
" in the same folder path as another one):" +
77-
CollisionStrategy.RENAME.name + ": Use a systematic name (the shortest possible) for the "
78-
"new repo that produces the collision" +
79-
CollisionStrategy.SYSTEMATIC.name + ": Use a systematic name for all repos" +
80-
CollisionStrategy.IGNORE.name + ": Ignores repos that have filename collisions." +
81-
CollisionStrategy.REMOVE.name + ": Removes the repo already cloned and clones the new one "
82-
"with the same name.",
75+
parser.add_argument("-R", "--rename", "--rename-strategy",
76+
help="Strategy to rename the path to clone a repositories that has same path as "
77+
"another:" +
78+
RenameStrategy.SHORTEST.name + ": Use the shortest systematic name that avoids same path"
79+
"for the repo where the same path is detected." +
80+
RenameStrategy.SHORTEST_SYSTEMATIC.name + ": Use the shortest systematic name that avoids "
81+
"same path for both repos that produce the "
82+
"same path." +
83+
RenameStrategy.SYSTEMATIC.name + ": Use the full systematic name for all repos with "
84+
"the same path." +
85+
RenameStrategy.IGNORE.name + ": If a repo is found with the same clone path as another, do"
86+
" not clone the repo where the "
87+
"path coincidence is detected.",
88+
type=str,
89+
nargs='?',
90+
dest="rename_strategy",
91+
choices=[name for name in RenameStrategy])
92+
parser.add_argument("-S", "--collision", "--collision-strategy", "--collision-action",
93+
help="Strategy to follow when finding a repo already cloned in the path that another repo is "
94+
"supposed to be cloned" +
95+
CollisionAction.FULL_UPDATE.name + ": If the new repo to be cloned is different than the "
96+
"one already cloned, remove the one already cloned and"
97+
"clone the new one in its place, if not, update the "
98+
"repo already cloned." +
99+
CollisionAction.UPDATE.name + ": Ignore the new repo to be cloned and updates the repo "
100+
"already cloned." +
101+
CollisionAction.IGNORE.name + ": Ignore the new repo to be cloned and do nothing."
102+
"." +
103+
CollisionAction.REMOVE.name + ": Remove the repo already cloned and clone the new one in "
104+
"the same path.",
83105
type=str,
84106
nargs='?',
85107
dest="collision_strategy",
86-
choices=[name for name in CollisionStrategy])
108+
default=CollisionAction.FULL_UPDATE,
109+
choices=[name for name in CollisionAction])
87110
parser.add_argument("-j", "--json", "--generate-json", "--produce-json",
88111
help="Generates a JSON report of the backup folders and repos.",
89112
# type=bool,
@@ -177,18 +200,18 @@ def parse_arguments(parser: argparse.ArgumentParser):
177200
# When keeping the complete hierarchy we ensure that there will be no collisions
178201
if not args.flatten_directories \
179202
and args.reflect_hierarchy \
180-
and args.collision_strategy:
181-
parser.error("You do not need to supply a collision strategy with -l when keeping the hierarchy with -y and not"
203+
and args.rename_strategy:
204+
parser.error("You do not need to supply a rename strategy with -R when keeping the hierarchy with -y and not"
182205
" flattening any directory (not supplying -F)")
183206

184207
# Supply default collision strategy if needed
185208
if args.reflect_hierarchy \
186209
and args.flatten_directories \
187-
and not args.collision_strategy:
188-
args.collision_strategy = "rename"
210+
and not args.rename_strategy:
211+
args.rename_strategy = RenameStrategy.SHORTEST_SYSTEMATIC
189212

190-
if not args.collision_strategy:
191-
args.collision_strategy = "rename"
213+
if not args.rename_strategy:
214+
args.rename_strategy = RenameStrategy.SHORTEST_SYSTEMATIC
192215

193216
# If path supplied -c implicit
194217
if not args.produce_compressed and args.compressed_path:
@@ -231,7 +254,7 @@ def parse_arguments(parser: argparse.ArgumentParser):
231254
custom_providers = []
232255
for custom_provider in args.custom_providers:
233256
if args.exclude_github:
234-
custom_providers.append({'url': custom_provider, 'provider': "GitLab"})
257+
custom_providers.append({'url': custom_provider, 'provider': ProviderType.GITLAB})
235258
if args.exclude_gitlab:
236-
custom_providers.append({'url': custom_provider, 'provider': "GitHub"})
259+
custom_providers.append({'url': custom_provider, 'provider': ProviderType.GITHUB})
237260
return args

src/Repository.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pathlib import Path
2+
3+
from src.defines import ProviderType
4+
5+
6+
class Repository:
7+
def __init__(self, backup: str, owner: str, provider: ProviderType, organization: str, name: str, link: str):
8+
self.backup = backup
9+
self.owner = owner
10+
self.provider = provider
11+
self.organization = organization
12+
self.name = name
13+
self.link = link
14+
self.path = Path()
15+
16+
def __str__(self):
17+
return (f"Repository(provider='{self.provider}', organization='{self.organization}', "
18+
f"name='{self.name}', repo_link='{self.link}', path='{self.path}')")
19+
20+
'''
21+
/backup/owner/provider/organization/repo
22+
'''
23+
def compute_path(self, ignore_backup: bool = False, ignore_owner: bool = False, ignore_provider: bool = False,
24+
ignore_organization: bool = False):
25+
self.path = Path()
26+
if not ignore_backup:
27+
self.path = self.path / self.backup
28+
if not ignore_owner:
29+
self.path = self.path / self.owner
30+
if not ignore_provider:
31+
self.path = self.path / self.provider.name
32+
if not ignore_organization:
33+
self.path = self.path / self.organization
34+
self.path = self.path / self.name
35+
36+

src/UnparserService.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def print_summary(args):
4343
summary.write("* Directory levels to flatten: " +
4444
", ".join(args.flatten_directories) + "\n")
4545

46-
if args.collision_strategy:
47-
summary.write(f"* Strategy to avoid collision in the folder names of the repos: {args.collision_strategy}\n")
46+
if args.rename_strategy:
47+
summary.write(f"* Strategy to avoid collision in the folder names of the repos: {args.rename_strategy}\n")
4848

4949
summary.write("* Verbose output: ")
5050
summary.write("Yes\n" if args.is_verbose else "No\n")

src/defines.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class ProviderType(Enum):
88
GITLAB = 2
99

1010

11-
class CollisionStrategy(Enum):
12-
RENAME = 1
13-
SYSTEMATIC = 2
14-
IGNORE = 3
15-
REMOVE = 4
11+
class RenameStrategy(Enum):
12+
SHORTEST = 1
13+
SHORTEST_SYSTEMATIC = 2
14+
SYSTEMATIC = 3
15+
IGNORE = 4
1616

1717

1818
class FlattenLevel(Enum):
@@ -22,3 +22,11 @@ class FlattenLevel(Enum):
2222
ORGANIZATION = 4
2323

2424

25+
class CollisionAction(Enum):
26+
FULL_UPDATE = 1
27+
UPDATE = 2
28+
IGNORE = 3
29+
REMOVE = 4
30+
31+
32+

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from GitHubService import GitHubService, build_github_official_provider
88
from ProviderService import ProviderService, build_provider
99
from TokenService import get_github_token
10-
from defines import FlattenLevel, CollisionStrategy, ProviderType
10+
from defines import FlattenLevel, RenameStrategy, ProviderType
1111
from FileService import is_file_directory_writable
1212
from GitLabService import build_gitlab_official_provider
1313
from ArgumentParserService import build_argument_parser, parse_arguments

0 commit comments

Comments
 (0)