Skip to content

Commit 46284dd

Browse files
hvoigtgitster
authored andcommitted
remove the impression of unexpectedness when access is denied
If a server accessed through ssh is denying access git will currently issue the message "fatal: The remote end hung up unexpectedly" as the last line. This sounds as if something really ugly just happened. Since this is a quite typical situation in which users regularly get we do not say that if it happens at the beginning when reading the remote heads. If its in the very first beginning of reading the remote heads it is very likely an authentication error or a missing repository. If it happens later during reading the remote heads we still indicate that it happened during this initial contact phase. Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f174a25 commit 46284dd

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

connect.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1
4949
extra->nr++;
5050
}
5151

52+
static void die_initial_contact(int got_at_least_one_head)
53+
{
54+
if (got_at_least_one_head)
55+
die("The remote end hung up upon initial contact");
56+
else
57+
die("Could not read from remote repository.\n\n"
58+
"Please make sure you have the correct access rights\n"
59+
"and the repository exists.");
60+
}
61+
5262
/*
5363
* Read all the refs from the other end
5464
*/
@@ -57,6 +67,8 @@ struct ref **get_remote_heads(int in, struct ref **list,
5767
unsigned int flags,
5868
struct extra_have_objects *extra_have)
5969
{
70+
int got_at_least_one_head = 0;
71+
6072
*list = NULL;
6173
for (;;) {
6274
struct ref *ref;
@@ -65,7 +77,10 @@ struct ref **get_remote_heads(int in, struct ref **list,
6577
char *name;
6678
int len, name_len;
6779

68-
len = packet_read_line(in, buffer, sizeof(buffer));
80+
len = packet_read(in, buffer, sizeof(buffer));
81+
if (len < 0)
82+
die_initial_contact(got_at_least_one_head);
83+
6984
if (!len)
7085
break;
7186
if (buffer[len-1] == '\n')
@@ -98,6 +113,7 @@ struct ref **get_remote_heads(int in, struct ref **list,
98113
hashcpy(ref->old_sha1, old_sha1);
99114
*list = ref;
100115
list = &ref->next;
116+
got_at_least_one_head = 1;
101117
}
102118
return list;
103119
}

pkt-line.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,19 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
135135
strbuf_add(buf, buffer, n);
136136
}
137137

138-
static void safe_read(int fd, void *buffer, unsigned size)
138+
static int safe_read(int fd, void *buffer, unsigned size, int return_line_fail)
139139
{
140140
ssize_t ret = read_in_full(fd, buffer, size);
141141
if (ret < 0)
142142
die_errno("read error");
143-
else if (ret < size)
143+
else if (ret < size) {
144+
if (return_line_fail)
145+
return -1;
146+
144147
die("The remote end hung up unexpectedly");
148+
}
149+
150+
return ret;
145151
}
146152

147153
static int packet_length(const char *linelen)
@@ -169,12 +175,14 @@ static int packet_length(const char *linelen)
169175
return len;
170176
}
171177

172-
int packet_read_line(int fd, char *buffer, unsigned size)
178+
static int packet_read_internal(int fd, char *buffer, unsigned size, int return_line_fail)
173179
{
174-
int len;
180+
int len, ret;
175181
char linelen[4];
176182

177-
safe_read(fd, linelen, 4);
183+
ret = safe_read(fd, linelen, 4, return_line_fail);
184+
if (return_line_fail && ret < 0)
185+
return ret;
178186
len = packet_length(linelen);
179187
if (len < 0)
180188
die("protocol error: bad line length character: %.4s", linelen);
@@ -185,12 +193,24 @@ int packet_read_line(int fd, char *buffer, unsigned size)
185193
len -= 4;
186194
if (len >= size)
187195
die("protocol error: bad line length %d", len);
188-
safe_read(fd, buffer, len);
196+
ret = safe_read(fd, buffer, len, return_line_fail);
197+
if (return_line_fail && ret < 0)
198+
return ret;
189199
buffer[len] = 0;
190200
packet_trace(buffer, len, 0);
191201
return len;
192202
}
193203

204+
int packet_read(int fd, char *buffer, unsigned size)
205+
{
206+
return packet_read_internal(fd, buffer, size, 1);
207+
}
208+
209+
int packet_read_line(int fd, char *buffer, unsigned size)
210+
{
211+
return packet_read_internal(fd, buffer, size, 0);
212+
}
213+
194214
int packet_get_line(struct strbuf *out,
195215
char **src_buf, size_t *src_len)
196216
{

pkt-line.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void packet_buf_flush(struct strbuf *buf);
1313
void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
1414

1515
int packet_read_line(int fd, char *buffer, unsigned size);
16+
int packet_read(int fd, char *buffer, unsigned size);
1617
int packet_get_line(struct strbuf *out, char **src_buf, size_t *src_len);
1718
ssize_t safe_write(int, const void *, ssize_t);
1819

t/t5512-ls-remote.sh

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,16 @@ test_expect_success 'use branch.<name>.remote if possible' '
104104

105105
cat >exp <<EOF
106106
fatal: 'refs*master' does not appear to be a git repository
107-
fatal: The remote end hung up unexpectedly
107+
fatal: Could not read from remote repository.
108+
109+
Please make sure you have the correct access rights
110+
and the repository exists.
108111
EOF
109112
test_expect_success 'confuses pattern as remote when no remote specified' '
110113
#
111-
# Do not expect "git ls-remote <pattern>" to work; ls-remote, correctly,
112-
# confuses <pattern> for <remote>. Although ugly, this behaviour is akin
113-
# to the confusion of refspecs for remotes by git-fetch and git-push,
114-
# eg:
115-
#
116-
# $ git fetch branch
117-
#
118-
114+
# Do not expect "git ls-remote <pattern>" to work; ls-remote needs
115+
# <remote> if you want to feed <pattern>, just like you cannot say
116+
# fetch <branch>.
119117
# We could just as easily have used "master"; the "*" emphasizes its
120118
# role as a pattern.
121119
test_must_fail git ls-remote refs*master >actual 2>&1 &&

0 commit comments

Comments
 (0)