Skip to content

Commit

Permalink
Merge pull request #24 from poponealex/handle_git
Browse files Browse the repository at this point in the history
Handle `git` repos
  • Loading branch information
laowantong authored Mar 6, 2022
2 parents 229775f + 731f0e0 commit b938566
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Well, yet another achievement for Suprenam, I guess. 🎉
Suprenam is not as straightforward as it seems. It supports:

- **Complex renamings.** As long as the desired final state is possible (e.g. no name clash), Suprenam will abide. Under the hood, this may require it to go through intermediate names (e.g. for swapping `"foo.pdf"` and `"bar.pdf"`).
- **Versioning.** When an item is tracked by git, Suprenam will use `git mv` to keep it under version control.
- **Rollback.** If something goes wrong during the actual process (e.g. a file is moved), don't worry: the work already completed will automatically be rolled back to the initial state.
- **Undo.** Likewise, you can always undo the previous renaming session. To that end, simply click the Suprenam icon without dropping anything on it.

Expand Down
17 changes: 16 additions & 1 deletion src/renamings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import subprocess
from pathlib import Path
from typing import List

Expand Down Expand Up @@ -76,7 +77,21 @@ def get_arcs_for_undoing(
def rename_and_log_all_files(self, arcs: List[Arc]):
self.arcs_to_rollback: List[Arc] = []
for (source, target) in arcs:
source.rename(target)
try:
subprocess.run(
[
"git", # git fails when launched from a non-versioned directory.
"-C", # This option runs git as if it was started...
source.parent, # ... in the source's parent directory.
"mv",
source.name,
target.name,
],
check=True,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError:
source.rename(target)
self.arcs_to_rollback.insert(0, Arc(target, source))
logger.info(f"SOURCE:{source}\tTARGET:{target}")
self.print_arcs(arcs)
Expand Down

0 comments on commit b938566

Please sign in to comment.