-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #336 from jakirkham/use_repo_index
Stage re-rendering changes
- Loading branch information
Showing
3 changed files
with
137 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from contextlib import contextmanager | ||
import os | ||
import shutil | ||
|
||
|
||
def get_repo(path, search_parent_directories=True): | ||
repo = None | ||
try: | ||
import git | ||
repo = git.Repo( | ||
path, | ||
search_parent_directories=search_parent_directories | ||
) | ||
except ImportError: | ||
pass | ||
except git.InvalidGitRepositoryError: | ||
pass | ||
|
||
return repo | ||
|
||
|
||
def get_file_blob(repo, filename): | ||
idx = repo.index | ||
rel_filepath = os.path.relpath(filename, repo.working_dir) | ||
blob = idx.iter_blobs(lambda _: _[1].path == rel_filepath).next()[1] | ||
return blob | ||
|
||
|
||
def get_mode_file(filename): | ||
repo = get_repo(filename) | ||
if repo: | ||
blob = get_file_blob(repo, filename) | ||
mode = blob.mode | ||
else: | ||
mode = os.stat(filename).st_mode | ||
|
||
return mode | ||
|
||
|
||
def set_mode_file(filename, mode): | ||
repo = get_repo(filename) | ||
if repo: | ||
blob = get_file_blob(repo, filename) | ||
blob.mode |= mode | ||
repo.index.add([blob]) | ||
|
||
os.chmod(filename, mode) | ||
|
||
|
||
@contextmanager | ||
def write_file(filename): | ||
with open(filename, "w") as fh: | ||
yield fh | ||
|
||
repo = get_repo(filename) | ||
if repo: | ||
repo.index.add([filename]) | ||
|
||
|
||
def remove_file(filename): | ||
if os.path.exists(filename): | ||
repo = get_repo(filename) | ||
if repo: | ||
repo.index.remove([filename]) | ||
|
||
os.remove(filename) | ||
|
||
|
||
def copy_file(src, dst): | ||
shutil.copy2(src, dst) | ||
|
||
repo = get_repo(dst) | ||
if repo: | ||
repo.index.add([dst]) |