Skip to content

Commit 5b7f85b

Browse files
Add more
1 parent 2176cb4 commit 5b7f85b

File tree

2 files changed

+124
-26
lines changed

2 files changed

+124
-26
lines changed

__main__.py

Lines changed: 122 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import getpass
66
import click
77
import json
8-
import directory_diff
98
import shutil
9+
import filecmp
10+
import datetime
1011

1112
os.makedirs(path.expanduser("~/.kst-git"), exist_ok=True)
1213

@@ -52,39 +53,134 @@ def init():
5253
@cli.command()
5354
def push():
5455
cwd = os.getcwd()
55-
click.echo("Push command not implemented yet.")
56+
config_path = path.join(cwd, ".kst-git/config.json")
57+
with open(config_path, "r") as f:
58+
repo_config = json.load(f)
59+
copy_dir = path.join(cwd, ".kst-git", "copy")
60+
repo_config_copy_path = path.join(copy_dir, ".kst-git")
61+
os.makedirs(repo_config_copy_path, exist_ok=True)
62+
shutil.copy2(config_path, path.join(repo_config_copy_path, "config.json"))
63+
click.echo("Uploading changes to server...")
64+
65+
remote_path = repo_config["path"]
66+
for root, _, files in os.walk(copy_dir):
67+
for file in files:
68+
local_file_path = path.join(root, file)
69+
relative_path = path.relpath(local_file_path, copy_dir)
70+
remote_file_path = path.join(remote_path, relative_path)
71+
try:
72+
client.upload_file(remote_file_path, local_file_path)
73+
click.echo(f"Uploaded: {relative_path}")
74+
except Exception as e:
75+
click.echo(click.style(f"Error uploading {relative_path}: {e}", fg="red"))
76+
77+
click.echo(click.style("Push complete.", fg="green"))
78+
79+
@cli.command()
80+
def pull():
81+
cwd = os.getcwd()
82+
config_path = path.join(cwd, ".kst-git/config.json")
83+
with open(config_path, "r") as f:
84+
repo_config = json.load(f)
85+
remote_path = repo_config["path"]
86+
click.echo("Downloading latest version from server...")
87+
try:
88+
# Remove all local files except .kst-git directory
89+
for item in os.listdir(cwd):
90+
item_path = path.join(cwd, item)
91+
if item != ".kst-git":
92+
if path.isfile(item_path) or path.islink(item_path):
93+
os.unlink(item_path)
94+
elif path.isdir(item_path):
95+
shutil.rmtree(item_path)
96+
97+
client.download_directory(remote_path, cwd)
98+
click.echo(click.style("Pull complete. Local repository updated to latest version.", fg="green"))
99+
except Exception as e:
100+
click.echo(click.style(f"Pull failed: {e}", fg="red"))
101+
56102

57103
@cli.command()
58104
def diff():
59105
cwd = os.getcwd()
60-
with open(path.join(cwd, ".kst-git/config.json"), "r") as f:
61-
config = json.load(f)
106+
config_path = path.join(cwd, ".kst-git/config.json")
107+
with open(config_path, "r") as f:
108+
repo_config = json.load(f)
62109
if path.exists(path.join(cwd, ".kst-git", "copy")):
63110
shutil.rmtree(path.join(cwd, ".kst-git", "copy"), ignore_errors=True)
64111
# pull from server
65-
click.echo("Downloading remote state...")
66-
client.download_directory(config["path"], path.join(cwd, ".kst-git", "copy"))
67-
click.echo("Comparing directories...")
68-
# Example using basic comparison:
69-
basic_diff = directory_diff.compare_directories_basic(cwd, path.join(cwd, ".kst-git", "copy"), [".kst-git", ".kst-git/copy"])
70-
if basic_diff:
71-
only_in_dir1, only_in_dir2, common = basic_diff
72-
if only_in_dir1:
73-
click.echo("\nFiles only in local directory (New files):")
74-
for item in only_in_dir1:
75-
click.echo(f" + {item}")
76-
if only_in_dir2:
77-
click.echo("\nFiles only in remote directory (Potentially deleted locally or new remote files):")
78-
for item in only_in_dir2:
79-
click.echo(f" - {item}")
80-
if common:
81-
click.echo("\nCommon files:")
82-
# For a basic diff, we are not checking content differences in common files.
83-
# If you want to check content differences, you would need a more detailed comparison.
84-
click.echo(" (Content comparison for common files is not implemented in this basic diff.)")
112+
click.echo("Downloading server version...")
113+
client.download_directory(repo_config["path"], path.join(cwd, ".kst-git", "copy"))
114+
click.echo("Comparing local changes...")
115+
116+
local_dir = cwd
117+
remote_copy_dir = path.join(cwd, ".kst-git", "copy")
118+
119+
def generate_file_list(dir_path):
120+
file_list = []
121+
for root, _, files in os.walk(dir_path):
122+
for file in files:
123+
relative_path = path.relpath(path.join(root, file), dir_path)
124+
if ".kst-git" not in relative_path and ".kst-git" != relative_path and relative_path != "": # Exclude .kst-git directory itself and its contents
125+
file_list.append(relative_path)
126+
return file_list
127+
128+
local_files = generate_file_list(local_dir)
129+
remote_files = generate_file_list(remote_copy_dir)
130+
131+
added_files = list(set(local_files) - set(remote_files))
132+
deleted_files = list(set(remote_files) - set(local_files))
133+
modified_files = []
134+
common_files = set(local_files) & set(remote_files)
135+
136+
for file in common_files:
137+
local_file_path = path.join(local_dir, file)
138+
remote_file_path = path.join(remote_copy_dir, file)
139+
if not filecmp.cmp(local_file_path, remote_file_path, shallow=False):
140+
modified_files.append(file)
141+
modified_files = list(modified_files)
142+
143+
if added_files:
144+
click.echo(click.style("\nAdded files:", fg="green"))
145+
for file in added_files:
146+
click.echo(f" + {file}")
147+
local_file_path = path.join(local_dir, file)
148+
remote_file_path = path.join(remote_copy_dir, file)
149+
os.makedirs(path.dirname(remote_file_path), exist_ok=True) # Ensure directory exists in copy
150+
shutil.copy2(local_file_path, remote_file_path)
151+
152+
if deleted_files:
153+
click.echo(click.style("\nDeleted files:", fg="red"))
154+
for file in deleted_files:
155+
click.echo(f" - {file}")
156+
remote_file_path = path.join(remote_copy_dir, file)
157+
os.remove(remote_file_path)
158+
159+
if modified_files:
160+
click.echo(click.style("\nModified files:", fg="yellow"))
161+
for file in modified_files:
162+
click.echo(f" ~ {file}")
163+
local_file_path = path.join(local_dir, file)
164+
remote_file_path = path.join(remote_copy_dir, file)
165+
shutil.copy2(local_file_path, remote_file_path)
166+
167+
if not added_files and not deleted_files and not modified_files:
168+
click.echo(click.style("\nNo changes detected.", fg="cyan"))
85169
else:
86-
click.echo("No differences found between local and remote directories.")
87-
click.echo("Diff complete.")
170+
click.echo(click.style("\nChanges staged to copy.", fg="cyan"))
171+
commit_message = click.prompt("Enter commit message")
172+
now = datetime.datetime.now()
173+
commit_data = {
174+
"message": commit_message,
175+
"added": added_files,
176+
"deleted": deleted_files,
177+
"modified": modified_files,
178+
"timestamp": now.isoformat()
179+
}
180+
repo_config["changes"].append(commit_data)
181+
with open(config_path, "w") as f:
182+
json.dump(repo_config, f, indent=4)
183+
click.echo(click.style("\nCommit message and changes added to repo config.", fg="cyan"))
88184

89185

90186
if __name__ == '__main__':

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
webdav3client
2+
click

0 commit comments

Comments
 (0)