Skip to content

Commit ff0ae3e

Browse files
committed
Fix inability to restore files when newly added files are selected
We have to use the unstaged files from `git reset`
1 parent 41dca18 commit ff0ae3e

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

qgitc/gitutils.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -898,26 +898,56 @@ def versionEQ(major: int, minor: int, patch: int):
898898
@staticmethod
899899
def restoreStagedFiles(repoDir, files):
900900
"""restore staged files
901-
return error message if any
901+
return (errorMessage, filesToRestore)
902+
filesToRestore contains files that have unstaged changes after reset
903+
and can be restored with 'git restore'
902904
"""
903905
# `restore --staged` is much slower than reset HEAD
904906
args = ["reset", "HEAD", "--"]
905907
args.extend(files)
906-
process = GitProcess(repoDir or Git.REPO_DIR, args)
907-
_, error = process.communicate()
908-
if process.returncode != 0 and error is not None:
909-
return error.decode("utf-8")
910-
return None
908+
process = GitProcess(repoDir or Git.REPO_DIR, args, text=True)
909+
output, error = process.communicate()
910+
911+
filesToRestore = []
912+
if process.returncode != 0:
913+
if error:
914+
return error, filesToRestore
915+
return None, filesToRestore
916+
917+
# Parse output to find files with unstaged changes
918+
# Output format:
919+
# Unstaged changes after reset:
920+
# M file1.txt
921+
# M file2.txt
922+
if output:
923+
lines = output.splitlines()
924+
inUnstagedSection = False
925+
for line in lines:
926+
if 'Unstaged changes after reset:' in line:
927+
inUnstagedSection = True
928+
continue
929+
if inUnstagedSection and line.strip():
930+
# Line format: "M filename" or "M\tfilename"
931+
parts = line.split(None, 1)
932+
if len(parts) >= 2:
933+
filesToRestore.append(parts[1])
934+
935+
return None, filesToRestore
911936

912937
@staticmethod
913938
def restoreFiles(repoDir, files, staged=False):
914939
"""restore files
915940
return error message if any
916941
"""
917942
if staged:
918-
error = Git.restoreStagedFiles(repoDir, files)
943+
error, filesToRestore = Git.restoreStagedFiles(repoDir, files)
919944
if error:
920945
return error
946+
# Only restore files that exist in HEAD (newly added files won't be in this list)
947+
files = filesToRestore
948+
949+
if not files:
950+
return None
921951

922952
args = ["restore", "--"]
923953
args.extend(files)

0 commit comments

Comments
 (0)