Skip to content

Commit 5bda410

Browse files
committed
run-command: be helpful with Git LFS fails on Windows 7
Git LFS is now built with Go 1.21 which no longer supports Windows 7. However, Git for Windows still wants to support Windows 7. Ideally, Git LFS would re-introduce Windows 7 support until Git for Windows drops support for Windows 7, but that's not going to happen: git-for-windows#4996 (comment) The next best thing we can do is to let the users know what is happening, and how to get out of their fix, at least. This is not quite as easy as it would first seem because programs compiled with Go 1.21 or newer will simply throw an exception and fail with an Access Violation on Windows 7. The only way I found to address this is to replicate the logic from Go's very own `version` command (which can determine the Go version with which a given executable was built) to detect the situation, and in that case offer a helpful error message. This addresses git-for-windows#4996. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 15e18f2 commit 5bda410

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

compat/win32/path-utils.c

+139
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include "../../git-compat-util.h"
2+
#include "../../wrapper.h"
3+
#include "../../strbuf.h"
4+
#include "../../versioncmp.h"
25

36
int win32_has_dos_drive_prefix(const char *path)
47
{
@@ -50,3 +53,139 @@ int win32_offset_1st_component(const char *path)
5053

5154
return pos + is_dir_sep(*pos) - path;
5255
}
56+
57+
static int read_at(int fd, char *buffer, size_t offset, size_t size)
58+
{
59+
if (lseek(fd, offset, SEEK_SET) < 0) {
60+
fprintf(stderr, "could not seek to 0x%x\n", (unsigned int)offset);
61+
return -1;
62+
}
63+
64+
return read_in_full(fd, buffer, size);
65+
}
66+
67+
static size_t le16(const char *buffer)
68+
{
69+
unsigned char *u = (unsigned char *)buffer;
70+
return u[0] | (u[1] << 8);
71+
}
72+
73+
static size_t le32(const char *buffer)
74+
{
75+
return le16(buffer) | (le16(buffer + 2) << 16);
76+
}
77+
78+
/*
79+
* Determine the Go version of a given executable, if it was built with Go.
80+
*
81+
* This recapitulates the logic from
82+
* https://github.com/golang/go/blob/master/src/cmd/go/internal/version/version.go
83+
* (without requiring the user to install `go.exe` to find out).
84+
*/
85+
static ssize_t get_go_version(const char *path, char *go_version, size_t go_version_size)
86+
{
87+
int fd = open(path, O_RDONLY);
88+
char buffer[1024];
89+
off_t offset;
90+
size_t num_sections, opt_header_size, i;
91+
char *p = NULL, *q;
92+
ssize_t res = -1;
93+
94+
if (fd < 0)
95+
return -1;
96+
97+
if (read_in_full(fd, buffer, 2) < 0)
98+
goto fail;
99+
100+
if (buffer[0] != 'M' || buffer[1] != 'Z')
101+
goto fail;
102+
103+
if (read_at(fd, buffer, 0x3c, 4) < 0)
104+
goto fail;
105+
106+
offset = le32(buffer);
107+
if (read_at(fd, buffer, offset, 24) < 0)
108+
goto fail;
109+
110+
if (buffer[0] != 'P' || buffer[1] != 'E' || buffer[2] != '\0' || buffer[3] != '\0')
111+
goto fail;
112+
113+
num_sections = le16(buffer + 6);
114+
opt_header_size = le16(buffer + 20);
115+
offset += 24; /* skip file header */
116+
117+
if (read_at(fd, buffer, offset, 2) < 0)
118+
goto fail;
119+
120+
offset += opt_header_size;
121+
122+
for (i = 0; i < num_sections; i++) {
123+
if (read_at(fd, buffer, offset + i * 40, 40) < 0)
124+
goto fail;
125+
126+
if ((le32(buffer + 36) /* characteristics */ & ~0x600000) /* IMAGE_SCN_ALIGN_32BYTES */ ==
127+
(/* IMAGE_SCN_CNT_INITIALIZED_DATA */ 0x00000040 |
128+
/* IMAGE_SCN_MEM_READ */ 0x40000000 |
129+
/* IMAGE_SCN_MEM_WRITE */ 0x80000000)) {
130+
size_t size = le32(buffer + 16);
131+
132+
p = malloc(size);
133+
134+
if (!p || read_at(fd, p, le32(buffer + 20), size) < 0)
135+
goto fail;
136+
137+
q = memmem(p, size, "\xff Go buildinf:", 14);
138+
if (!q)
139+
goto fail;
140+
if (q[14] == 8 && q[15] == 2) {
141+
if ((q[32] & 0x80) ||
142+
!q[32] ||
143+
q[32] + 1 > go_version_size)
144+
goto fail;
145+
res = q[32];
146+
memcpy(go_version, q + 33, res);
147+
go_version[res] = '\0';
148+
break;
149+
}
150+
}
151+
}
152+
153+
fail:
154+
free(p);
155+
close(fd);
156+
return res;
157+
}
158+
159+
void win32_warn_about_git_lfs_on_windows7(int exit_code, const char *argv0)
160+
{
161+
char buffer[128], *git_lfs = NULL;
162+
const char *p;
163+
164+
/*
165+
* Git LFS v3.5.1 fails with an Access Violation on Windows 7; That
166+
* would usually show up as an exit code 0xc0000005. For some reason
167+
* (probably because at this point, we no longer have the _original_
168+
* HANDLE that was returned by `CreateProcess()`) we get 0xb00 instead.
169+
*/
170+
if (exit_code != 0x0b00)
171+
return;
172+
if (GetVersion() >> 16 > 9200)
173+
return; /* Warn only on Windows 7 or 8 or older */
174+
if (!starts_with(argv0, "git-lfs ") ||
175+
!(git_lfs = locate_in_PATH("git-lfs")))
176+
return;
177+
if (get_go_version(git_lfs, buffer, sizeof(buffer)) > 0 &&
178+
skip_prefix(buffer, "go", &p) &&
179+
versioncmp("1.21.0", p) <= 0)
180+
warning("This program was built with Go v%s\n"
181+
"i.e. without support for this Windows version:\n"
182+
"\n\t%s\n"
183+
"\n"
184+
"To work around this, you can download and install a"
185+
"working version from\n"
186+
"\n"
187+
"\thttps://github.com/git-lfs/git-lfs/releases/tag/"
188+
"v3.4.1\n",
189+
p, git_lfs);
190+
free(git_lfs);
191+
}

compat/win32/path-utils.h

+3
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ static inline int win32_has_dir_sep(const char *path)
3030
int win32_offset_1st_component(const char *path);
3131
#define offset_1st_component win32_offset_1st_component
3232

33+
void win32_warn_about_git_lfs_on_windows7(int exit_code, const char *argv0);
34+
#define warn_about_git_lfs_on_windows7 win32_warn_about_git_lfs_on_windows7
35+
3336
#endif

git-compat-util.h

+7
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,13 @@ static inline int git_offset_1st_component(const char *path)
520520
#define offset_1st_component git_offset_1st_component
521521
#endif
522522

523+
#ifndef warn_about_git_lfs_on_windows7
524+
static inline void warn_about_git_lfs_on_windows7(int exit_code, const char *argv0)
525+
{
526+
(void);
527+
}
528+
#endif
529+
523530
#ifndef is_valid_path
524531
#define is_valid_path(path) 1
525532
#endif

run-command.c

+1
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
566566
*/
567567
code += 128;
568568
} else if (WIFEXITED(status)) {
569+
warn_about_git_lfs_on_windows7(status, argv0);
569570
code = WEXITSTATUS(status);
570571
} else {
571572
if (!in_signal)

0 commit comments

Comments
 (0)