Skip to content

Commit 19ed416

Browse files
committed
Fix newfile bug
1 parent c7566b4 commit 19ed416

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

__main__.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
config.saveConfig()
2222

2323
options = {
24-
'webdav_hostname': config.getConfig()["server"],
25-
'webdav_login': config.getConfig()["username"],
26-
'webdav_password': config.getConfig()["password"]
24+
'webdav_hostname': config.getConfig()["server"],
25+
'webdav_login': config.getConfig()["username"],
26+
'webdav_password': config.getConfig()["password"]
2727
}
2828
client = Client(options)
2929

@@ -45,11 +45,12 @@ def init():
4545
p = path.join(p, project_name)
4646
with open(path.join(cwd, ".kst-git/config.json"), "w") as f:
4747
standard = {
48-
"changes" : [],
49-
"path" : p,
48+
"changes": [],
49+
"path": p,
5050
}
5151
json.dump(standard, f)
5252
click.echo("Created empty repo")
53+
5354
@cli.command()
5455
def push():
5556
cwd = os.getcwd()
@@ -63,15 +64,30 @@ def push():
6364
click.echo("Uploading changes to server...")
6465

6566
remote_path = repo_config["path"]
67+
68+
def ensure_remote_directory(remote_dir):
69+
# Remove trailing slash (if any) and split the path
70+
remote_dir = remote_dir.rstrip('/')
71+
parts = remote_dir.split('/')
72+
current = ""
73+
for part in parts:
74+
if part: # skip empty parts from leading /
75+
current += "/" + part
76+
if not client.is_dir(current):
77+
try:
78+
client.mkdir(current)
79+
except Exception as e:
80+
click.echo(click.style(f"Error creating directory {current}: {e}", fg="red"))
81+
6682
for root, _, files in os.walk(copy_dir):
6783
for file in files:
6884
local_file_path = path.join(root, file)
6985
relative_path = path.relpath(local_file_path, copy_dir)
7086
remote_file_path = path.join(remote_path, relative_path).replace("\\", "/")
7187
remote_dir_path = path.dirname(remote_file_path)
7288
try:
73-
if not client.is_dir(remote_dir_path):
74-
client.makedirs(remote_dir_path) # Create parent directories if they don't exist
89+
# Ensure all parent directories exist on the server
90+
ensure_remote_directory(remote_dir_path)
7591
client.upload_file(remote_file_path, local_file_path)
7692
click.echo(f"Uploaded: {relative_path}")
7793
except Exception as e:
@@ -102,7 +118,6 @@ def pull():
102118
except Exception as e:
103119
click.echo(click.style(f"Pull failed: {e}", fg="red"))
104120

105-
106121
@cli.command()
107122
def diff():
108123
cwd = os.getcwd()
@@ -124,7 +139,7 @@ def generate_file_list(dir_path):
124139
for root, _, files in os.walk(dir_path):
125140
for file in files:
126141
relative_path = path.relpath(path.join(root, file), dir_path)
127-
if ".kst-git" not in relative_path and ".kst-git" != relative_path and relative_path != "": # Exclude .kst-git directory itself and its contents
142+
if ".kst-git" not in relative_path and ".kst-git" != relative_path and relative_path != "":
128143
file_list.append(relative_path)
129144
return file_list
130145

@@ -149,7 +164,7 @@ def generate_file_list(dir_path):
149164
click.echo(f" + {file}")
150165
local_file_path = path.join(local_dir, file)
151166
remote_file_path = path.join(remote_copy_dir, file)
152-
os.makedirs(path.dirname(remote_file_path), exist_ok=True) # Ensure directory exists in copy
167+
os.makedirs(path.dirname(remote_file_path), exist_ok=True)
153168
shutil.copy2(local_file_path, remote_file_path)
154169

155170
if deleted_files:
@@ -185,6 +200,5 @@ def generate_file_list(dir_path):
185200
json.dump(repo_config, f, indent=4)
186201
click.echo(click.style("\nCommit message and changes added to repo config.", fg="cyan"))
187202

188-
189203
if __name__ == '__main__':
190-
cli()
204+
cli()

0 commit comments

Comments
 (0)