git-restore-filemode 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ """Python launcher package for git-restore-filemode."""
2
+
3
+ __all__ = ["__version__"]
4
+ __version__ = "0.1.0"
@@ -0,0 +1,14 @@
1
+ import subprocess
2
+ import sys
3
+
4
+ from .installer import ensure_executable
5
+
6
+
7
+ def main() -> int:
8
+ executable = ensure_executable()
9
+ completed = subprocess.run([str(executable), *sys.argv[1:]], check=False)
10
+ return completed.returncode
11
+
12
+
13
+ if __name__ == "__main__":
14
+ raise SystemExit(main())
@@ -0,0 +1,189 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import platform
5
+ import shutil
6
+ import subprocess
7
+ import tarfile
8
+ import tempfile
9
+ import urllib.error
10
+ import urllib.request
11
+ from importlib import metadata
12
+ from pathlib import Path
13
+
14
+ from . import __version__
15
+
16
+ BINARY_NAME = "git-restore-filemode"
17
+ REPO_OWNER = os.environ.get("GIT_RESTORE_FILEMODE_REPO_OWNER", "yonathan-ashebir")
18
+ REPO_NAME = os.environ.get("GIT_RESTORE_FILEMODE_REPO_NAME", "git-restore-filemode")
19
+ REPO_URL = f"https://github.com/{REPO_OWNER}/{REPO_NAME}"
20
+
21
+
22
+ def ensure_executable() -> Path:
23
+ target = os.environ.get("GIT_RESTORE_FILEMODE_TARGET") or detect_target()
24
+ release = release_version()
25
+ executable = cache_path(release, target)
26
+
27
+ if executable.exists():
28
+ return executable
29
+
30
+ executable.parent.mkdir(parents=True, exist_ok=True)
31
+ if not install_from_release(executable, release, target):
32
+ install_from_cargo(executable, release, target)
33
+ executable.chmod(0o755)
34
+ return executable
35
+
36
+
37
+ def release_version() -> str:
38
+ override = os.environ.get("GIT_RESTORE_FILEMODE_VERSION")
39
+ if override:
40
+ return override
41
+
42
+ try:
43
+ package_version = metadata.version("git-restore-filemode")
44
+ except metadata.PackageNotFoundError:
45
+ package_version = __version__
46
+
47
+ return f"v{package_version}"
48
+
49
+
50
+ def cache_path(release: str, target: str) -> Path:
51
+ cache_root = os.environ.get("GIT_RESTORE_FILEMODE_CACHE_DIR")
52
+ if cache_root:
53
+ root = Path(cache_root)
54
+ elif platform.system() == "Windows":
55
+ root = Path(os.environ.get("LOCALAPPDATA", Path.home() / "AppData" / "Local"))
56
+ root /= "git-restore-filemode"
57
+ elif platform.system() == "Darwin":
58
+ root = Path.home() / "Library" / "Caches" / "git-restore-filemode"
59
+ else:
60
+ root = Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache"))
61
+ root /= "git-restore-filemode"
62
+
63
+ return root / release / target / executable_name(target)
64
+
65
+
66
+ def detect_target() -> str:
67
+ system = platform.system()
68
+ machine = platform.machine().lower()
69
+
70
+ arch = {
71
+ "x86_64": "x86_64",
72
+ "amd64": "x86_64",
73
+ "aarch64": "aarch64",
74
+ "arm64": "aarch64",
75
+ }.get(machine)
76
+
77
+ if not arch:
78
+ raise RuntimeError(f"unsupported CPU architecture: {platform.machine()}")
79
+
80
+ if system == "Linux":
81
+ return f"{arch}-unknown-linux"
82
+ if system == "Darwin":
83
+ return f"{arch}-apple-darwin"
84
+ if system == "Windows":
85
+ return f"{arch}-pc-windows-msvc"
86
+
87
+ raise RuntimeError(f"unsupported operating system: {system}")
88
+
89
+
90
+ def executable_name(target: str) -> str:
91
+ return f"{BINARY_NAME}.exe" if "windows" in target or "-pc-" in target else BINARY_NAME
92
+
93
+
94
+ def rust_target_for_platform(target: str) -> str:
95
+ if target == "x86_64-unknown-linux":
96
+ return "x86_64-unknown-linux-musl"
97
+ if target == "aarch64-unknown-linux":
98
+ return "aarch64-unknown-linux-musl"
99
+ return target
100
+
101
+
102
+ def install_from_release(destination: Path, release: str, target: str) -> bool:
103
+ asset = f"{BINARY_NAME}-{target}.tar.gz"
104
+ if release == "latest":
105
+ url = f"{REPO_URL}/releases/latest/download/{asset}"
106
+ else:
107
+ url = f"{REPO_URL}/releases/download/{release}/{asset}"
108
+
109
+ with tempfile.TemporaryDirectory() as tmp:
110
+ archive = Path(tmp) / asset
111
+ try:
112
+ download(url, archive)
113
+ extract_binary(archive, destination)
114
+ return True
115
+ except (OSError, RuntimeError, tarfile.TarError, urllib.error.URLError):
116
+ return False
117
+
118
+
119
+ def download(url: str, destination: Path) -> None:
120
+ request = urllib.request.Request(url)
121
+ token = os.environ.get("GITHUB_TOKEN")
122
+ if token:
123
+ request.add_header("Authorization", f"Bearer {token}")
124
+
125
+ with urllib.request.urlopen(request, timeout=60) as response:
126
+ with destination.open("wb") as output:
127
+ shutil.copyfileobj(response, output)
128
+
129
+
130
+ def extract_binary(archive: Path, destination: Path) -> None:
131
+ expected_names = {BINARY_NAME, f"{BINARY_NAME}.exe"}
132
+ with tarfile.open(archive, "r:gz") as tar:
133
+ for member in tar.getmembers():
134
+ if Path(member.name).name not in expected_names or not member.isfile():
135
+ continue
136
+
137
+ source = tar.extractfile(member)
138
+ if source is None:
139
+ continue
140
+
141
+ with source:
142
+ with destination.open("wb") as output:
143
+ shutil.copyfileobj(source, output)
144
+ return
145
+
146
+ raise RuntimeError(f"{archive} did not contain {BINARY_NAME}")
147
+
148
+
149
+ def install_from_cargo(destination: Path, release: str, target: str) -> None:
150
+ cargo = shutil.which("cargo")
151
+ rustc = shutil.which("rustc")
152
+ if not cargo or not rustc:
153
+ raise RuntimeError(
154
+ "no matching release asset was available, and Rust/Cargo was not found"
155
+ )
156
+
157
+ with tempfile.TemporaryDirectory() as tmp:
158
+ cargo_root = Path(tmp) / "cargo-root"
159
+ rust_target = rust_target_for_platform(target)
160
+ rustup = shutil.which("rustup")
161
+ if rustup:
162
+ subprocess.run([rustup, "target", "add", rust_target], check=True)
163
+
164
+ args = [
165
+ cargo,
166
+ "install",
167
+ "--locked",
168
+ "--git",
169
+ REPO_URL,
170
+ "--target",
171
+ rust_target,
172
+ "--bin",
173
+ BINARY_NAME,
174
+ "--root",
175
+ str(cargo_root),
176
+ ]
177
+ if release != "latest":
178
+ args.insert(5, release)
179
+ args.insert(5, "--tag")
180
+
181
+ subprocess.run(args, check=True)
182
+
183
+ built = cargo_root / "bin" / executable_name(target)
184
+ if not built.exists():
185
+ built = cargo_root / "bin" / rust_target / executable_name(target)
186
+ if not built.exists():
187
+ raise RuntimeError(f"cargo install did not produce {built}")
188
+
189
+ shutil.copy2(built, destination)
@@ -0,0 +1,128 @@
1
+ Metadata-Version: 2.4
2
+ Name: git-restore-filemode
3
+ Version: 0.1.0
4
+ Summary: Restore Git-tracked executable file modes without restoring file contents.
5
+ Author: Yonathan Ashebir
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/yonathan-ashebir/git-restore-filemode
8
+ Project-URL: Repository, https://github.com/yonathan-ashebir/git-restore-filemode
9
+ Project-URL: Issues, https://github.com/yonathan-ashebir/git-restore-filemode/issues
10
+ Keywords: git,filemode,chmod,restore
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Rust
16
+ Classifier: Topic :: Software Development :: Version Control :: Git
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Dynamic: license-file
21
+
22
+ # git-restore-filemode
23
+
24
+ `git-restore-filemode` is a simple Git extension command that restores only Git's
25
+ tracked executable bit for regular files. It follows the destination/source
26
+ defaults of `git restore`, but it never restores file contents.
27
+
28
+ ```sh
29
+ curl -fsSL https://raw.githubusercontent.com/yonathan-ashebir/git-restore-filemode/main/install.sh | bash
30
+ ```
31
+
32
+ ## Examples
33
+
34
+ Restore working tree file modes from the index:
35
+
36
+ ```sh
37
+ git restore-filemode -- script.sh
38
+ ```
39
+
40
+ Restore staged file modes from `HEAD`:
41
+
42
+ ```sh
43
+ git restore-filemode --staged -- script.sh
44
+ ```
45
+
46
+ Restore both the index and working tree from a specific commit:
47
+
48
+ ```sh
49
+ git restore-filemode --source=HEAD~1 --staged --worktree -- bin/
50
+ ```
51
+
52
+ ## Supported options
53
+
54
+ - `-s`, `--source <tree-ish>`: restore file modes from a tree-ish.
55
+ - `-S`, `--staged`: restore the index.
56
+ - `-W`, `--worktree`: restore the working tree.
57
+ - `--ignore-unmerged`: skip unmerged index entries when restoring from the index.
58
+ - `--pathspec-from-file <file>`: read pathspecs from a file, or `-` for stdin.
59
+ - `--pathspec-file-nul`: split `--pathspec-from-file` input on NUL bytes.
60
+ - `-q`, `--quiet`: suppress non-fatal messages.
61
+
62
+ Default source selection matches `git restore`:
63
+
64
+ - Working tree restore defaults to the index.
65
+ - Staged restore defaults to `HEAD`.
66
+ - `--source` overrides the default source.
67
+
68
+ ## Install
69
+
70
+ ### Bash script
71
+
72
+ ```sh
73
+ curl -fsSL https://raw.githubusercontent.com/yonathan-ashebir/git-restore-filemode/main/install.sh | bash
74
+ ```
75
+
76
+ Attempts system dependent global (system) installation path, falling back to user. Accepts --user and --system flags to force use either. Downloads compatible binary when available, building from source otherwise (requires cargo/rust)
77
+
78
+ ### Python
79
+
80
+ ```sh
81
+ uv tool install git-restore-filemode
82
+ pip install --user git-restore-filemode
83
+ ```
84
+
85
+ The Python package installs a small launcher named `git-restore-filemode`. On
86
+ first run it downloads the matching release executable into a per-user cache, or
87
+ falls back to Cargo when Rust is installed.
88
+
89
+ ### Node
90
+
91
+ ```sh
92
+ npm install -g git-restore-filemode
93
+ pnpm add -g git-restore-filemode
94
+ bun install -g git-restore-filemode
95
+ ```
96
+
97
+ The npm package follows the same release-download-first behavior and keeps the
98
+ native executable inside the installed package.
99
+
100
+ ### Cargo
101
+
102
+ ```sh
103
+ cargo install --path .
104
+ ```
105
+
106
+ After this package is published to crates.io, the direct install command will
107
+ be:
108
+
109
+ ```sh
110
+ cargo install git-restore-filemode
111
+ ```
112
+
113
+ ## Notes
114
+
115
+ Only regular Git blobs with modes `100644` and `100755` are restorable. Symlinks,
116
+ submodules, missing working tree files, and non-regular paths are not changed.
117
+
118
+ Registry metadata for PyPI and npm is included, but publishing those packages is
119
+ intentionally left for a later release step.
120
+
121
+ ## Release Targets
122
+
123
+ - Linux x64 static: `x86_64-unknown-linux`
124
+ - Linux ARM64 static: `aarch64-unknown-linux`
125
+ - macOS Intel: `x86_64-apple-darwin`
126
+ - macOS Apple Silicon: `aarch64-apple-darwin`
127
+ - Windows x64: `x86_64-pc-windows-msvc`
128
+ - Windows ARM64: `aarch64-pc-windows-msvc`
@@ -0,0 +1,9 @@
1
+ git_restore_filemode/__init__.py,sha256=veWvzJ6mcfvl2Kj2wGq_BpVGisq4_phKkK7KSUIG6UM,105
2
+ git_restore_filemode/__main__.py,sha256=ILEPRrsQhCIpDvYfmjzpxM23aDnY3ZBQ2iE7rsSNO70,297
3
+ git_restore_filemode/installer.py,sha256=iNYxrtEsrXMVriagafFvE3Q5rPqST1iw0SwLtSeK8fk,5898
4
+ git_restore_filemode-0.1.0.dist-info/licenses/LICENSE,sha256=XYV2E81xnZ0ACyZ2Z3CYavh2c9aypUN8jNOYqjhReaM,1073
5
+ git_restore_filemode-0.1.0.dist-info/METADATA,sha256=vFy_g0j1AZPXKfHJmyVDFx93zgiKH5wrVMVfw0UYYKw,3899
6
+ git_restore_filemode-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
7
+ git_restore_filemode-0.1.0.dist-info/entry_points.txt,sha256=0_ZNNyEic8LcqVbQowft6-_0EQmX4UYokOkGGvtS5ok,76
8
+ git_restore_filemode-0.1.0.dist-info/top_level.txt,sha256=YtWCGYOgQtIA-bQiYIcu_vhpRgMFO2erWIHhp0P9CV8,21
9
+ git_restore_filemode-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ git-restore-filemode = git_restore_filemode.__main__:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yonathan Ashebir
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ git_restore_filemode