Skip to content

Commit b03d637

Browse files
authored
fix: duplicated file may has different line endings (#77)
* fix: duplicated file may has different line endings Python's stream writing uses OS's line ending. So if you duplicate a Unix line ending file on Windows by writing file content, the duplicate file will use \r\n as its line ending. Why we don't use copy file API at the first place? Signed-off-by: Jack Cherng <jfcherng@gmail.com> * fix: disallow duplicate file with dst = src It just can't be done. Even worse, if the user select "overwrite", the source file gets deleted first, and no destination file will be created because no source file can be opened anymore. Signed-off-by: Jack Cherng <jfcherng@gmail.com> * style: make black happy Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 74be57b commit b03d637

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

commands/create_from_selection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def want_event(self):
4343

4444
def get_path(self, event, for_context_menu=False):
4545
"""
46-
@return (base_path: str, relative_path: str)
46+
@return (base_path: str, relative_path: str)
4747
"""
4848
file_name = None
4949
region = self.view.sel()[0]

commands/duplicate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def run(self, paths=None):
4242
def duplicate(self, dst, input_path):
4343
user_friendly_path = user_friendly(dst)
4444

45+
if os.path.abspath(self.origin) == os.path.abspath(dst):
46+
sublime.error_message("Destination is the same with the source.")
47+
return
48+
4549
if os.path.isdir(self.origin):
4650
if not os.path.exists(dst):
4751
shutil.copytree(self.origin, dst)
@@ -53,9 +57,7 @@ def duplicate(self, dst, input_path):
5357
)
5458
else:
5559
if not os.path.exists(dst):
56-
with open(dst, "w") as fp:
57-
with open(self.origin, "r") as fpread:
58-
fp.write(fpread.read())
60+
shutil.copy2(self.origin, dst)
5961
self.window.open_file(dst)
6062
else:
6163

@@ -68,9 +70,7 @@ def overwrite():
6870
"Unable to send to the trash the item {0}".format(e)
6971
)
7072

71-
with open(dst, "w") as fp:
72-
with open(self.origin, "r") as fpread:
73-
fp.write(fpread.read())
73+
shutil.copy2(self.origin, dst)
7474
self.window.open_file(dst)
7575

7676
def open_file():

0 commit comments

Comments
 (0)