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

Re: [PATCH 3/5] checkout --to: no auto-detach if the ref is already checked out

From
Duy Nguyen <pclouds@gmail.com>
Date
Jul 24, 2014, 10:09 UTC
Message-ID
<CACsJy8CTYyjqmwGgkKZQFX4YC9-X=TGx9Ex5QfNWRNKFjgpKag@mail.gmail.com>
In-Reply-To
<xmqqzjfzdbds.fsf@gitster.dls.corp.google.com>
On Thu, Jul 24, 2014 at 4:16 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 14 quoted lines
>> +     if (strbuf_read_file(&sb, path.buf, 0) <= 0 ||
>> +         !skip_prefix(sb.buf, "ref:", &start))
>> +             goto done;
>>       while (isspace(*start))
>>               start++;
>>       end = start;
>>       while (*end && !isspace(*end))
>>               end++;
>
> Not new in this round of update, and may not even be an issue, but:
>
>  - Earlier, the code returned early on a negative return value from
>    read-file (i.e., an error), but this round it also does so for
>    zero.  Intended?

Yes. But it does not make any difference. strbuf_read_file returns sb.len, if it's empty, the next skip_prefix would fail anyway.

Show 6 quoted lines
>  - The above duplicates the logic in resolve_ref_unsafe() and
>    resolve_gitlink_ref_recursive(); three places now knows what a
>    textual symref looks like (i.e. begins with "ref:", zero or more
>    whitespaces, the target ref and then zero or more trailing
>    whitespaces).  Perhaps we need to consolidate the code further,
>    so that this knowledge does not leak out of refs.c?
OK. Will do (unless Mike has a different opinion about this).
Show 39 quoted lines
>
>> +     if (strncmp(start, new->path, end - start) ||
>> +         new->path[end - start] != '\0')
>> +             goto done;
>> +     if (id) {
>> +             strbuf_reset(&path);
>> +             strbuf_addf(&path, "%s/repos/%s/gitdir",
>> +                         get_git_common_dir(), id);
>> +             if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
>> +                     goto done;
>> +             while (gitdir.len && (gitdir.buf[gitdir.len - 1] == '\n' ||
>> +                                   gitdir.buf[gitdir.len - 1] == '\r'))
>> +                     gitdir.buf[--gitdir.len] = '\0';
>
> Accepting arbitrary numbers of '\r' and '\n' sounds as if the code
> is allowing it, but text editors would not end their files with a
> nonsense sequence like "\r\r\n\r" unless the end-user works to do
> so, and if you are prepared to be lenient to noisy human input, not
> trimming trailing whitespaces does not look it is going far enough
> to help them.
>
> I do not see a good reason to allow random text editors to edit this
> file in the first place, so my preference is:
>
>         if (strbuf_read_file(...) < 0 ||
>             gitdir.len == 0 ||
>             gitdir.buf[gitdir.len - 1] != '\n')
>             goto error_return;
>         gitdir.buf[--gitdir.len] = '\0';
>
> Alternatively, if you are trying to be lenient to human input, I
> would understand:
>
>         if (strbuf_read_file(...) < 0)
>                 goto error_return;
>         strbuf_rtrim(&gitdir);
>
> The code in the patch, which is something in between, does not make
> much sense to me.

I think more about "echo abc > $this_file" where the echo command may output '\r\n' on Windows (wild guess though, I don't use Windows much). I think I'm going with _rtrim.

-- 
Duy
Previous: Junio C HamanoNext: Junio C Hamano
Message 17 of 36 in “nd/multiple-work-trees follow-ups”
  1. 0/5 nd/multiple-work-trees follow-upsNguyễn Thái Ngọc Duy, Jul 23, 2014
  2. 1/5 gitrepository-layout.txt: s/ignored/ignored if/Nguyễn Thái Ngọc Duy, Jul 23, 2014
  3. 2/5 prune --repos: fix uninitialized accessNguyễn Thái Ngọc Duy, Jul 23, 2014
  4. Junio C HamanoJul 23, 2014
  5. Duy NguyenJul 24, 2014
  6. 3/5 checkout --to: no auto-detach if the ref is already checked outNguyễn Thái Ngọc Duy, Jul 23, 2014
  7. Michael J GruberJul 23, 2014
  8. Junio C HamanoJul 23, 2014
  9. Duy NguyenJul 24, 2014
  10. Junio C HamanoJul 24, 2014
  11. Michael J GruberJul 25, 2014
  12. Junio C HamanoJul 30, 2014
  13. Junio C HamanoJul 30, 2014
  14. Duy NguyenAug 27, 2014
  15. Junio C HamanoAug 27, 2014
  16. Junio C HamanoJul 23, 2014
  17. Duy NguyenJul 24, 2014
  18. Junio C HamanoJul 24, 2014
  19. Junio C HamanoJul 24, 2014
  20. 4/5 checkout --to: fix dangling pointers in remove_junk()Nguyễn Thái Ngọc Duy, Jul 23, 2014
  21. 5/5 environment.c: fix incorrect git_graft_file initializationNguyễn Thái Ngọc Duy, Jul 23, 2014
  22. Junio C HamanoJul 23, 2014
  23. 0/8 nd/multiple-work-trees follow-upsNguyễn Thái Ngọc Duy, Jul 29, 2014
  24. 1/8 gitrepository-layout.txt: s/ignored/ignored if/Nguyễn Thái Ngọc Duy, Jul 29, 2014
  25. 2/8 checkout: no need to call check_linked_checkouts if head_ref is NULLNguyễn Thái Ngọc Duy, Jul 29, 2014
  26. 3/8 prune --repos: fix uninitialized accessNguyễn Thái Ngọc Duy, Jul 29, 2014
  27. 4/8 checkout: no auto-detach if the ref is already checked outNguyễn Thái Ngọc Duy, Jul 29, 2014
  28. 5/8 checkout --to: fix dangling pointers in remove_junk()Nguyễn Thái Ngọc Duy, Jul 29, 2014
  29. 6/8 environment.c: fix incorrect git_graft_file initializationNguyễn Thái Ngọc Duy, Jul 29, 2014
  30. 7/8 checkout: prefix --to argument properly when cwd is movedNguyễn Thái Ngọc Duy, Jul 29, 2014
  31. Junio C HamanoJul 29, 2014
  32. Duy NguyenJul 30, 2014
  33. 8/8 checkout --to: do not touch existing target directoryNguyễn Thái Ngọc Duy, Jul 29, 2014
  34. Junio C HamanoJul 30, 2014
  35. Duy NguyenJul 31, 2014
  36. Junio C HamanoJul 31, 2014

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.