mbox series

[v2,0/7] strbuf cleanups

Message ID 20230503184849.1809304-1-calvinwan@google.com (mailing list archive)
Headers show
Series strbuf cleanups | expand

Message

Calvin Wan May 3, 2023, 6:48 p.m. UTC
Strbuf is a widely used basic structure that should only interact with
other primitives in strbuf.[ch]. Over time certain functions inside of
strbuf.[ch] have been added to interact with higher level objects and
functions. This series cleans up some of those higher level interactions
by moving the offending functions to the files they interact with and
adding documentation to strbuf.h. With the goal of eventually being able
to stand up strbuf as a libary, this series also removes the use of
environment variables from strbuf.

Calvin Wan (7):
  strbuf: clarify API boundary
  abspath: move related functions to abspath
  credential-store: move related functions to credential-store file
  object-name: move related functions to object-name
  path: move related function to path
  strbuf: clarify dependency
  strbuf: remove environment variables

 abspath.c                  |  36 +++++++++++++
 abspath.h                  |  21 ++++++++
 add-patch.c                |  12 +++--
 builtin/am.c               |   2 +-
 builtin/branch.c           |   4 +-
 builtin/commit.c           |   2 +-
 builtin/credential-store.c |  19 +++++++
 builtin/merge.c            |  10 ++--
 builtin/notes.c            |  16 +++---
 builtin/rebase.c           |   2 +-
 builtin/stripspace.c       |   6 ++-
 builtin/tag.c              |   9 ++--
 fmt-merge-msg.c            |   9 ++--
 gpg-interface.c            |   5 +-
 hook.c                     |   1 +
 object-name.c              |  15 ++++++
 object-name.h              |   9 ++++
 path.c                     |  20 +++++++
 path.h                     |   5 ++
 pretty.c                   |   1 +
 rebase-interactive.c       |  15 +++---
 sequencer.c                |  24 +++++----
 strbuf.c                   | 106 +++----------------------------------
 strbuf.h                   |  53 +++++--------------
 tempfile.c                 |   1 +
 wt-status.c                |   6 +--
 26 files changed, 220 insertions(+), 189 deletions(-)

Range-diff against v1:
-:  ---------- > 1:  e0dd3f5295 strbuf: clarify API boundary
1:  283771c088 ! 2:  ec1ea6ae4f abspath: move related functions to abspath
    @@ Commit message
         abspath: move related functions to abspath
     
         Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
    -    they do not belong in a low-level library.
    +    paths are not primitive objects and therefore strbuf should not interact
    +    with them.
     
      ## abspath.c ##
     @@ abspath.c: char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
2:  58f78b8ae0 = 3:  2d74561b91 credential-store: move related functions to credential-store file
3:  88ab90c079 ! 4:  30b5e635cb object-name: move related functions to object-name
    @@ Commit message
         object-name: move related functions to object-name
     
         Move object-name-related functions from strbuf.[ch] to object-name.[ch]
    -    since they do not belong in a low-level library.
    +    since paths are not a primitive object that strbuf should directly
    +    interact with.
     
      ## object-name.c ##
     @@ object-name.c: static void find_abbrev_len_packed(struct min_abbrev_data *mad)
4:  30b7de5a81 ! 5:  6905618470 path: move related function to path
    @@ Metadata
      ## Commit message ##
         path: move related function to path
     
    -    Move path-related function from strbuf.[ch] to path.[ch] since it does
    -    not belong in a low-level library.
    +    Move path-related function from strbuf.[ch] to path.[ch] since path is
    +    not a primitive object and therefore strbuf should not directly interact
    +    with it.
     
      ## path.c ##
     @@ path.c: int normalize_path_copy(char *dst, const char *src)
5:  7b6d6353de ! 6:  caf3482bf7 strbuf: clarify dependency
    @@ Metadata
      ## Commit message ##
         strbuf: clarify dependency
     
    +    refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
    +    move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
    +    included thru refs.h, so removing refs.h requires strbuf.h to be added
    +    back.
    +
      ## strbuf.c ##
     @@
      #include "environment.h"
6:  ffacd1cbe5 ! 7:  a7f23488f8 strbuf: remove enviroment variables
    @@ Metadata
     Author: Calvin Wan <calvinwan@google.com>
     
      ## Commit message ##
    -    strbuf: remove enviroment variables
    +    strbuf: remove environment variables
     
    -    As a lower level library, strbuf should not directly access environment
    -    variables within its functions. Therefore, add an additional variable to
    -    function signatures for functions that use an environment variable and
    -    refactor callers to pass in the environment variable.
    +    As a library that only interacts with other primitives, strbuf should
    +    not directly access environment variables within its
    +    functions. Therefore, add an additional variable to function signatures
    +    for functions that use an environment variable and refactor callers to
    +    pass in the environment variable.
     
      ## add-patch.c ##
     @@ add-patch.c: static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
    @@ strbuf.h: void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
      __attribute__((format (printf,2,0)))
      void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
     @@ strbuf.h: int strbuf_normalize_path(struct strbuf *sb);
    + 
    + /**
       * Strip whitespace from a buffer. The second parameter controls if
    -  * comments are considered contents to be removed or not.
    +- * comments are considered contents to be removed or not.
    ++ * comments are considered contents to be removed or not. The third parameter
    ++ * is the comment character that determines whether a line is a comment or not.
       */
     -void strbuf_stripspace(struct strbuf *buf, int skip_comments);
     +void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);

Comments

Junio C Hamano May 5, 2023, 10:33 p.m. UTC | #1
Calvin Wan <calvinwan@google.com> writes:

> Strbuf is a widely used basic structure that should only interact with
> other primitives in strbuf.[ch]. Over time certain functions inside of
> strbuf.[ch] have been added to interact with higher level objects and
> functions. This series cleans up some of those higher level interactions
> by moving the offending functions to the files they interact with and
> adding documentation to strbuf.h. With the goal of eventually being able
> to stand up strbuf as a libary, this series also removes the use of
> environment variables from strbuf.

This round hasn't seen any comments (mine does not count ;-).  The
7/7-only v3 still says "environment variable" to refer to a global
variable, so I am not sure where we stand.  Is this back-burnered?
Elijah Newren May 7, 2023, 12:40 a.m. UTC | #2
On Wed, May 3, 2023 at 11:48 AM Calvin Wan <calvinwan@google.com> wrote:
>
> Strbuf is a widely used basic structure that should only interact with
> other primitives in strbuf.[ch].

"should"?  I agree with moving things in this direction, but the
wording here suggests there's a pre-existing directive or guideline;
but if so, I'm unfamiliar with it.  Maybe this should be reworded to
"This series modifies strbuf to focus on string manipulation with
minimal other dependencies"?

> Over time certain functions inside of
> strbuf.[ch] have been added to interact with higher level objects and
> functions. This series cleans up some of those higher level interactions
> by moving the offending functions to the files they interact with and
> adding documentation to strbuf.h. With the goal of eventually being able
> to stand up strbuf as a libary, this series also removes the use of
> environment variables from strbuf.

As Junio pointed out, "environment variables" is misleading; see below.

> Range-diff against v1:
> -:  ---------- > 1:  e0dd3f5295 strbuf: clarify API boundary

I read this separately; this patch looks good to me.

> 1:  283771c088 ! 2:  ec1ea6ae4f abspath: move related functions to abspath
>     @@ Commit message
>          abspath: move related functions to abspath
>
>          Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
>     -    they do not belong in a low-level library.
>     +    paths are not primitive objects and therefore strbuf should not interact
>     +    with them.

This applies to patches 4 & 5 as well:

Would this perhaps be better worded as:

    Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
    strbuf is focused on string manipulation routines with minimal dependencies.

?

>       ## abspath.c ##
>      @@ abspath.c: char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
> 2:  58f78b8ae0 = 3:  2d74561b91 credential-store: move related functions to credential-store file
> 3:  88ab90c079 ! 4:  30b5e635cb object-name: move related functions to object-name
>     @@ Commit message
>          object-name: move related functions to object-name
>
>          Move object-name-related functions from strbuf.[ch] to object-name.[ch]
>     -    since they do not belong in a low-level library.
>     +    since paths are not a primitive object that strbuf should directly
>     +    interact with.
>
>       ## object-name.c ##
>      @@ object-name.c: static void find_abbrev_len_packed(struct min_abbrev_data *mad)
> 4:  30b7de5a81 ! 5:  6905618470 path: move related function to path
>     @@ Metadata
>       ## Commit message ##
>          path: move related function to path
>
>     -    Move path-related function from strbuf.[ch] to path.[ch] since it does
>     -    not belong in a low-level library.
>     +    Move path-related function from strbuf.[ch] to path.[ch] since path is
>     +    not a primitive object and therefore strbuf should not directly interact
>     +    with it.
>
>       ## path.c ##
>      @@ path.c: int normalize_path_copy(char *dst, const char *src)
> 5:  7b6d6353de ! 6:  caf3482bf7 strbuf: clarify dependency
>     @@ Metadata
>       ## Commit message ##
>          strbuf: clarify dependency
>
>     +    refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
>     +    move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
>     +    included thru refs.h, so removing refs.h requires strbuf.h to be added
>     +    back.
>     +

Thanks.

> 6:  ffacd1cbe5 ! 7:  a7f23488f8 strbuf: remove enviroment variables
>     @@ Metadata
>      Author: Calvin Wan <calvinwan@google.com>
>
>       ## Commit message ##
>     -    strbuf: remove enviroment variables
>     +    strbuf: remove environment variables
>
>     -    As a lower level library, strbuf should not directly access environment
>     -    variables within its functions. Therefore, add an additional variable to
>     -    function signatures for functions that use an environment variable and
>     -    refactor callers to pass in the environment variable.
>     +    As a library that only interacts with other primitives, strbuf should
>     +    not directly access environment variables within its
>     +    functions. Therefore, add an additional variable to function signatures
>     +    for functions that use an environment variable and refactor callers to
>     +    pass in the environment variable.

"environment variable" makes one think not of "variable from
environment.c [that happens to be a global]" but of
https://en.wikipedia.org/wiki/Environment_variable.  The fact that the
variable is defined in environment.c isn't even relevant here, though,
while the fact that it is a global is relevant.  I'd just
s/environment variable/global variable/ in all 4 places (er, 5 if you
include the cover letter).

Your v3 7/7 has an important correction, but you didn't send out a
range diff for v3 so I'm putting my comments on your v2 range-diff.
:-)

>     + /**
>        * Strip whitespace from a buffer. The second parameter controls if
>     -  * comments are considered contents to be removed or not.
>     +- * comments are considered contents to be removed or not.
>     ++ * comments are considered contents to be removed or not. The third parameter
>     ++ * is the comment character that determines whether a line is a comment or not.
>        */

Thanks.


This series fixes two of my comments on v1, and the other was just
"here's another cleanup that could be added" -- it's fine to punt on.

I only had a couple minor comments on this v2/v3 that should be easy
to fix up, all included in this response to the cover letter.
Felipe Contreras May 7, 2023, 9:47 p.m. UTC | #3
Elijah Newren wrote:
> On Wed, May 3, 2023 at 11:48 AM Calvin Wan <calvinwan@google.com> wrote:
> >
> > Strbuf is a widely used basic structure that should only interact with
> > other primitives in strbuf.[ch].
> 
> "should"?  I agree with moving things in this direction, but the
> wording here suggests there's a pre-existing directive or guideline;

No, it doesn't.

"You should see a doctor about that cough" is not a directive, it's merely an
advice.

> Maybe this should be reworded to "This series modifies strbuf to focus on
> string manipulation with minimal other dependencies"?

And ironoically you use the word "should" to provide your own advice on how to
avoid using "should" as an advice.

I agree it would be ideal if strbuf interacted only with other primitives in
strbuf.[ch], so I agree "should" is a perfectly fine word to describe such a
normative statement.
Calvin Wan May 8, 2023, 4:38 p.m. UTC | #4
I intend to reroll it, as well as rewording the commit patches 4 and 5
as per Elijah's suggestions. I didn't originally realize you were
referring to s/environment variable/global variable as the misnomer
(since the dependency I was removing was from environment.h)

On Fri, May 5, 2023 at 3:33 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Calvin Wan <calvinwan@google.com> writes:
>
> > Strbuf is a widely used basic structure that should only interact with
> > other primitives in strbuf.[ch]. Over time certain functions inside of
> > strbuf.[ch] have been added to interact with higher level objects and
> > functions. This series cleans up some of those higher level interactions
> > by moving the offending functions to the files they interact with and
> > adding documentation to strbuf.h. With the goal of eventually being able
> > to stand up strbuf as a libary, this series also removes the use of
> > environment variables from strbuf.
>
> This round hasn't seen any comments (mine does not count ;-).  The
> 7/7-only v3 still says "environment variable" to refer to a global
> variable, so I am not sure where we stand.  Is this back-burnered?
>