Skip to content

Commit 472fa4c

Browse files
iabervongitster
authored andcommitted
Fix branches file configuration
Fetched remote branch from .git/branches/foo should fetch into refs/heads/foo. Also when partial URL is given, the fetched head should always be remote HEAD, and the result should not be stored anywhere. Signed-off-by: Daniel Barkalow <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c091b3d commit 472fa4c

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

remote.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static void read_branches_file(struct remote *remote)
171171
{
172172
const char *slash = strchr(remote->name, '/');
173173
char *frag;
174-
char *branch;
174+
struct strbuf branch;
175175
int n = slash ? slash - remote->name : 1000;
176176
FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
177177
char *s, *p;
@@ -197,17 +197,33 @@ static void read_branches_file(struct remote *remote)
197197
strcpy(p, s);
198198
if (slash)
199199
strcat(p, slash);
200+
201+
/*
202+
* With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
203+
* reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
204+
* the partial URL obtained from the branches file plus
205+
* "/netdev-2.6" and does not store it in any tracking ref.
206+
* #branch specifier in the file is ignored.
207+
*
208+
* Otherwise, the branches file would have URL and optionally
209+
* #branch specified. The "master" (or specified) branch is
210+
* fetched and stored in the local branch of the same name.
211+
*/
212+
strbuf_init(&branch, 0);
200213
frag = strchr(p, '#');
201214
if (frag) {
202215
*(frag++) = '\0';
203-
branch = xmalloc(strlen(frag) + 12);
204-
strcpy(branch, "refs/heads/");
205-
strcat(branch, frag);
216+
strbuf_addf(&branch, "refs/heads/%s", frag);
217+
} else
218+
strbuf_addstr(&branch, "refs/heads/master");
219+
if (!slash) {
220+
strbuf_addf(&branch, ":refs/heads/%s", remote->name);
206221
} else {
207-
branch = "refs/heads/master";
222+
strbuf_reset(&branch);
223+
strbuf_addstr(&branch, "HEAD:");
208224
}
209225
add_url(remote, p);
210-
add_fetch_refspec(remote, branch);
226+
add_fetch_refspec(remote, strbuf_detach(&branch, 0));
211227
remote->fetch_tags = 1; /* always auto-follow */
212228
}
213229

0 commit comments

Comments
 (0)