git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCHv4 1/2] submodule: port resolve_relative_url from shell to C

From
Stefan Beller <sbeller@google.com>
Date
Jan 28, 2016, 22:11 UTC
Message-ID
<CAGZ79kYbYEgW+O-wxp0LeT6W5XweV8e0jWzYDRS6vunnOJBKng@mail.gmail.com>
In-Reply-To
<xmqqzivpl5uo.fsf@gitster.mtv.corp.google.com>
On Thu, Jan 28, 2016 at 2:03 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 61 quoted lines
> Stefan Beller <sbeller@google.com> writes:
>
>> +static char *relative_url(const char *remote_url,
>> +                             const char *url,
>> +                             const char *up_path)
>> +{
>> +     int is_relative = 0;
>> +     int colonsep = 0;
>> +     char *out;
>> +     char *remoteurl = xstrdup(remote_url);
>> +     struct strbuf sb = STRBUF_INIT;
>> +     size_t len = strlen(remoteurl);
>> +
>> +     if (is_dir_sep(remoteurl[len]))
>> +             remoteurl[len] = '\0';
>> +
>> +     if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
>> +             is_relative = 0;
>> +     else {
>> +             is_relative = 1;
>> +             /*
>> +              * Prepend a './' to ensure all relative
>> +              * remoteurls start with './' or '../'
>> +              */
>> +             if (!starts_with_dot_slash(remoteurl) &&
>> +                 !starts_with_dot_dot_slash(remoteurl)) {
>> +                     strbuf_reset(&sb);
>> +                     strbuf_addf(&sb, "./%s", remoteurl);
>> +                     free(remoteurl);
>> +                     remoteurl = strbuf_detach(&sb, NULL);
>> +             }
>> +     }
>> +     /*
>> +      * When the url starts with '../', remove that and the
>> +      * last directory in remoteurl.
>> +      */
>> +     while (url) {
>> +             if (starts_with_dot_dot_slash(url)) {
>> +                     url += 3;
>> +                     colonsep |= chop_last_dir(&remoteurl, is_relative);
>> +             } else if (starts_with_dot_slash(url))
>> +                     url += 2;
>> +             else
>> +                     break;
>> +     }
>> +     strbuf_reset(&sb);
>> +     strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
>> +
>> +     if (starts_with_dot_slash(sb.buf))
>> +             out = xstrdup(sb.buf + 2);
>> +     else
>> +             out = xstrdup(sb.buf);
>> +     strbuf_reset(&sb);
>> +
>> +     free(remoteurl);
>
> This is a rather strange place to put this free(), as you are done
> with it a bit earlier, but it's OK.  I briefly wondered if the code
> becomes easier to follow with fewer free(remoteurl) if this local
> variable is made into a strbuf, but I didn't seriously think it
> through.

Right. As I did not touch that particular free with the resend, I wondered how it came there, too. And I think I had it at the end of the function and then realized the return just after the current position would leak it, so I moved it minimally up. If I'll resend again, I'll move it up to where it was last used.

>
> Otherwise looking good.
>
> Thanks.

Thanks, Stefan

Previous: Junio C HamanoNext: Stefan Beller
Message 8 of 10 in “[PATCHv3 0/2] Port `git submodule init` from shell to C”
  1. Stefan BellerJan 22, 2016
  2. 1/2 submodule: port resolve_relative_url from shell to CStefan Beller, Jan 22, 2016
  3. 2/2 submodule: port init from shell to CStefan Beller, Jan 22, 2016
  4. Junio C HamanoJan 25, 2016
  5. 0/2 Port `git submodule init` from shell to CStefan Beller, Jan 28, 2016
  6. 1/2 submodule: port resolve_relative_url from shell to CStefan Beller, Jan 28, 2016
  7. Junio C HamanoJan 28, 2016
  8. Stefan BellerJan 28, 2016
  9. 2/2 submodule: port init from shell to CStefan Beller, Jan 28, 2016
  10. Duy NguyenFeb 27, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.