1. 02 8月, 2016 1 次提交
  2. 02 7月, 2016 3 次提交
    • J
      common-main: call git_setup_gettext() · 5ce5f5fa
      Jeff King 提交于
      This should be part of every program, as otherwise users do
      not get translated error messages. However, some external
      commands forgot to do so (e.g., git-credential-store). This
      fixes them, and eliminates the repeated code in programs
      that did remember to use it.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      5ce5f5fa
    • J
      common-main: call git_extract_argv0_path() · 650c4492
      Jeff King 提交于
      Every program which links against libgit.a must call this
      function, or risk hitting an assert() in system_path() that
      checks whether we have configured argv0_path (though only
      when RUNTIME_PREFIX is defined, so essentially only on
      Windows).
      
      Looking at the diff, you can see that putting it into the
      common main() saves us having to do it individually in each
      of the external commands. But what you can't see are the
      cases where we _should_ have been doing so, but weren't
      (e.g., git-credential-store, and all of the t/helper test
      programs).
      
      This has been an accident-waiting-to-happen for a long time,
      but wasn't triggered until recently because it involves one
      of those programs actually calling system_path(). That
      happened with git-credential-store in v2.8.0 with ae5f6776
      (lazily load core.sharedrepository, 2016-03-11). The
      program:
      
        - takes a lock file, which...
      
        - opens a tempfile, which...
      
        - calls adjust_shared_perm to fix permissions, which...
      
        - lazy-loads the config (as of ae5f6776), which...
      
        - calls system_path() to find the location of
          /etc/gitconfig
      
      On systems with RUNTIME_PREFIX, this means credential-store
      reliably hits that assert() and cannot be used.
      
      We never noticed in the test suite, because we set
      GIT_CONFIG_NOSYSTEM there, which skips the system_path()
      lookup entirely.  But if we were to tweak git_config() to
      find /etc/gitconfig even when we aren't going to open it,
      then the test suite shows multiple failures (for
      credential-store, and for some other test helpers). I didn't
      include that tweak here because it's way too specific to
      this particular call to be worth carrying around what is
      essentially dead code.
      
      The implementation is fairly straightforward, with one
      exception: there is exactly one caller (git.c) that actually
      cares about the result of the function, and not the
      side-effect of setting up argv0_path. We can accommodate
      that by simply replacing the value of argv[0] in the array
      we hand down to cmd_main().
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      650c4492
    • J
      add an extra level of indirection to main() · 3f2e2297
      Jeff King 提交于
      There are certain startup tasks that we expect every git
      process to do. In some cases this is just to improve the
      quality of the program (e.g., setting up gettext()). In
      others it is a requirement for using certain functions in
      libgit.a (e.g., system_path() expects that you have called
      git_extract_argv0_path()).
      
      Most commands are builtins and are covered by the git.c
      version of main(). However, there are still a few external
      commands that use their own main(). Each of these has to
      remember to include the correct startup sequence, and we are
      not always consistent.
      
      Rather than just fix the inconsistencies, let's make this
      harder to get wrong by providing a common main() that can
      run this standard startup.
      
      We basically have two options to do this:
      
       - the compat/mingw.h file already does something like this by
         adding a #define that replaces the definition of main with a
         wrapper that calls mingw_startup().
      
         The upside is that the code in each program doesn't need
         to be changed at all; it's rewritten on the fly by the
         preprocessor.
      
         The downside is that it may make debugging of the startup
         sequence a bit more confusing, as the preprocessor is
         quietly inserting new code.
      
       - the builtin functions are all of the form cmd_foo(),
         and git.c's main() calls them.
      
         This is much more explicit, which may make things more
         obvious to somebody reading the code. It's also more
         flexible (because of course we have to figure out _which_
         cmd_foo() to call).
      
         The downside is that each of the builtins must define
         cmd_foo(), instead of just main().
      
      This patch chooses the latter option, preferring the more
      explicit approach, even though it is more invasive. We
      introduce a new file common-main.c, with the "real" main. It
      expects to call cmd_main() from whatever other objects it is
      linked against.
      
      We link common-main.o against anything that links against
      libgit.a, since we know that such programs will need to do
      this setup. Note that common-main.o can't actually go inside
      libgit.a, as the linker would not pick up its main()
      function automatically (it has no callers).
      
      The rest of the patch is just adjusting all of the various
      external programs (mostly in t/helper) to use cmd_main().
      I've provided a global declaration for cmd_main(), which
      means that all of the programs also need to match its
      signature. In particular, many functions need to switch to
      "const char **" instead of "char **" for argv. This effect
      ripples out to a few other variables and functions, as well.
      
      This makes the patch even more invasive, but the end result
      is much better. We should be treating argv strings as const
      anyway, and now all programs conform to the same signature
      (which also matches the way builtins are defined).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      3f2e2297
  3. 28 4月, 2016 1 次提交
    • J
      http: support sending custom HTTP headers · 8cb01e2f
      Johannes Schindelin 提交于
      We introduce a way to send custom HTTP headers with all requests.
      
      This allows us, for example, to send an extra token from build agents
      for temporary access to private repositories. (This is the use case that
      triggered this patch.)
      
      This feature can be used like this:
      
      	git -c http.extraheader='Secret: sssh!' fetch $URL $REF
      
      Note that `curl_easy_setopt(..., CURLOPT_HTTPHEADER, ...)` takes only
      a single list, overriding any previous call. This means we have to
      collect _all_ of the headers we want to use into a single list, and
      feed it to cURL in one shot. Since we already unconditionally set a
      "pragma" header when initializing the curl handles, we can add our new
      headers to that list.
      
      For callers which override the default header list (like probe_rpc),
      we provide `http_copy_default_headers()` so they can do the same
      trick.
      
      Big thanks to Jeff King and Junio Hamano for their outstanding help and
      patient reviews.
      Signed-off-by: NJohannes Schindelin <johannes.schindelin@gmx.de>
      Reviewed-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      8cb01e2f
  4. 26 4月, 2016 1 次提交
  5. 17 3月, 2016 1 次提交
    • J
      http-push: stop using name_path · c6bd2a1d
      Jeff King 提交于
      The graph traversal code here passes along a name_path to
      build up the pathname at which we find each blob. But we
      never actually do anything with the resulting names, making
      it a waste of code and memory.
      
      This usage came in aa1dbc98 (Update http-push functionality,
      2006-03-07), and originally the result was passed to
      "add_object" (which stored it, but didn't really use it,
      either). But we stopped using that function in 1f1e895f (Add
      "named object array" concept, 2006-06-19) in favor of
      storing just the objects themselves.
      
      Moreover, the generation of the name in process_tree() is
      buggy. It sticks "name" onto the end of the name_path linked
      list, and then passes it down again as it recurses (instead
      of "entry.path"). So it's a good thing this was unused, as
      the resulting path for "a/b/c/d" would end up as "a/a/a/a".
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c6bd2a1d
  6. 13 2月, 2016 1 次提交
    • J
      http-push: stop using name_path · 41595938
      Jeff King 提交于
      The graph traversal code here passes along a name_path to
      build up the pathname at which we find each blob. But we
      never actually do anything with the resulting names, making
      it a waste of code and memory.
      
      This usage came in aa1dbc98 (Update http-push functionality,
      2006-03-07), and originally the result was passed to
      "add_object" (which stored it, but didn't really use it,
      either). But we stopped using that function in 1f1e895f (Add
      "named object array" concept, 2006-06-19) in favor of
      storing just the objects themselves.
      
      Moreover, the generation of the name in process_tree() is
      buggy. It sticks "name" onto the end of the name_path linked
      list, and then passes it down again as it recurses (instead
      of "entry.path"). So it's a good thing this was unused, as
      the resulting path for "a/b/c/d" would end up as "a/a/a/a".
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      41595938
  7. 20 11月, 2015 5 次提交
  8. 06 10月, 2015 2 次提交
  9. 26 9月, 2015 4 次提交
    • J
      http-push: replace strcat with xsnprintf · 0cc41428
      Jeff King 提交于
      We account for these strcats in our initial allocation, but
      the code is confusing to follow and verify. Let's remember
      our original allocation length, and then xsnprintf can
      verify that we don't exceed it.
      
      Note that we can't just use xstrfmt here (which would be
      even cleaner) because the code tries to grow the buffer only
      when necessary.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0cc41428
    • J
      http-push: use strbuf instead of fwrite_buffer · 7d0581a9
      Jeff King 提交于
      The http-push code defines an fwrite_buffer function for use
      as a curl callback; it just writes to a strbuf. There's no
      reason we need to use it ourselves, as we know we have a
      strbuf. This lets us format directly into it, rather than
      dealing with an extra temporary buffer (which required
      manual length computation).
      
      While we're here, let's also remove the literal tabs from
      the source in favor of "\t", which is more visually obvious.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      7d0581a9
    • J
      use xsnprintf for generating git object headers · ef1286d3
      Jeff King 提交于
      We generally use 32-byte buffers to format git's "type size"
      header fields. These should not generally overflow unless
      you can produce some truly gigantic objects (and our types
      come from our internal array of constant strings). But it is
      a good idea to use xsnprintf to make sure this is the case.
      
      Note that we slightly modify the interface to
      write_sha1_file_prepare, which nows uses "hdrlen" as an "in"
      parameter as well as an "out" (on the way in it stores the
      allocated size of the header, and on the way out it returns
      the ultimate size of the header).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ef1286d3
    • J
      convert trivial sprintf / strcpy calls to xsnprintf · 5096d490
      Jeff King 提交于
      We sometimes sprintf into fixed-size buffers when we know
      that the buffer is large enough to fit the input (either
      because it's a constant, or because it's numeric input that
      is bounded in size). Likewise with strcpy of constant
      strings.
      
      However, these sites make it hard to audit sprintf and
      strcpy calls for buffer overflows, as a reader has to
      cross-reference the size of the array with the input. Let's
      use xsnprintf instead, which communicates to a reader that
      we don't expect this to overflow (and catches the mistake in
      case we do).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      5096d490
  10. 24 3月, 2015 1 次提交
  11. 06 3月, 2015 1 次提交
  12. 15 1月, 2015 1 次提交
    • J
      http-push: trim trailing newline from remote symref · f6786c8d
      Jeff King 提交于
      When we fetch a symbolic ref file from the remote, we get
      the whole string "ref: refs/heads/master\n", recognize it by
      skipping past the "ref: ", and store the rest. We should
      chomp the trailing newline.
      
      This bug was introduced in ae021d87 (use skip_prefix to avoid
      magic numbers, 2014-06-18), which did not notice that the
      length computation fed to xmemdupz was quietly tweaked by 1
      to account for this.
      
      We can solve it by explicitly trimming the newline, which is
      more obvious. Note that we use strbuf_rtrim here, which will
      actually cut off any trailing whitespace, not just a single
      newline. This is a good thing, though, as it makes our
      parsing more liberal (and spaces are not valid in refnames
      anyway).
      Signed-off-by: NJeff King <peff@peff.net>
      Tested-by: NKyle J. McKay <mackyle@gmail.com>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      f6786c8d
  13. 14 7月, 2014 1 次提交
  14. 21 6月, 2014 2 次提交
    • J
      http-push: refactor parsing of remote object names · 67a31f61
      Jeff King 提交于
      We get loose object names like "objects/??/..." from the
      remote side, and need to convert them to their hex
      representation.
      
      The code to do so is rather hard to follow, as it uses some
      calculated lengths whose origins are hard to understand and
      verify (e.g., the path must be exactly 49 characters long.
      why? Why doesn't the strcpy overflow obj_hex, which is the
      same length as path?).
      
      We can simplify this a bit by using skip_prefix, using standard
      40- and 20-character buffers for hex and binary sha1s, and
      adding some comments.
      
      We also drop a totally bogus comment that claims strlcpy
      cannot be used because "path" is not NUL-terminated. Right
      between a call to strlen(path) and strcpy(path).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      67a31f61
    • J
      use skip_prefix to avoid magic numbers · ae021d87
      Jeff King 提交于
      It's a common idiom to match a prefix and then skip past it
      with a magic number, like:
      
        if (starts_with(foo, "bar"))
      	  foo += 3;
      
      This is easy to get wrong, since you have to count the
      prefix string yourself, and there's no compiler check if the
      string changes.  We can use skip_prefix to avoid the magic
      numbers here.
      
      Note that some of these conversions could be much shorter.
      For example:
      
        if (starts_with(arg, "--foo=")) {
      	  bar = arg + 6;
      	  continue;
        }
      
      could become:
      
        if (skip_prefix(arg, "--foo=", &bar))
      	  continue;
      
      However, I have left it as:
      
        if (skip_prefix(arg, "--foo=", &v)) {
      	  bar = v;
      	  continue;
        }
      
      to visually match nearby cases which need to actually
      process the string. Like:
      
        if (skip_prefix(arg, "--foo=", &v)) {
      	  bar = atoi(v);
      	  continue;
        }
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ae021d87
  15. 20 6月, 2014 2 次提交
  16. 28 5月, 2014 1 次提交
  17. 26 3月, 2014 1 次提交
  18. 06 12月, 2013 1 次提交
    • C
      replace {pre,suf}fixcmp() with {starts,ends}_with() · 59556548
      Christian Couder 提交于
      Leaving only the function definitions and declarations so that any
      new topic in flight can still make use of the old functions, replace
      existing uses of the prefixcmp() and suffixcmp() with new API
      functions.
      
      The change can be recreated by mechanically applying this:
      
          $ git grep -l -e prefixcmp -e suffixcmp -- \*.c |
            grep -v strbuf\\.c |
            xargs perl -pi -e '
              s|!prefixcmp\(|starts_with\(|g;
              s|prefixcmp\(|!starts_with\(|g;
              s|!suffixcmp\(|ends_with\(|g;
              s|suffixcmp\(|!ends_with\(|g;
            '
      
      on the result of preparatory changes in this series.
      Signed-off-by: NChristian Couder <chriscool@tuxfamily.org>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      59556548
  19. 01 10月, 2013 1 次提交
    • J
      http: refactor options to http_get_* · 1bbcc224
      Jeff King 提交于
      Over time, the http_get_strbuf function has grown several
      optional parameters. We now have a bitfield with multiple
      boolean options, as well as an optional strbuf for returning
      the content-type of the response. And a future patch in this
      series is going to add another strbuf option.
      
      Treating these as separate arguments has a few downsides:
      
        1. Most call sites need to add extra NULLs and 0s for the
           options they aren't interested in.
      
        2. The http_get_* functions are actually wrappers around
           2 layers of low-level implementation functions. We have
           to pass these options through individually.
      
        3. The http_get_strbuf wrapper learned these options, but
           nobody bothered to do so for http_get_file, even though
           it is backed by the same function that does understand
           the options.
      
      Let's consolidate the options into a single struct. For the
      common case of the default options, we'll allow callers to
      simply pass a NULL for the options struct.
      
      The resulting code is often a few lines longer, but it ends
      up being easier to read (and to change as we add new
      options, since we do not need to update each call site).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJonathan Nieder <jrnieder@gmail.com>
      1bbcc224
  20. 29 8月, 2013 1 次提交
  21. 20 7月, 2013 1 次提交
  22. 07 6月, 2013 1 次提交
    • J
      clear parsed flag when we free tree buffers · 6e454b9a
      Jeff King 提交于
      Many code paths will free a tree object's buffer and set it
      to NULL after finishing with it in order to keep memory
      usage down during a traversal. However, out of 8 sites that
      do this, only one actually unsets the "parsed" flag back.
      Those sites that don't are setting a trap for later users of
      the tree object; even after calling parse_tree, the buffer
      will remain NULL, causing potential segfaults.
      
      It is not known whether this is triggerable in the current
      code. Most commands do not do an in-memory traversal
      followed by actually using the objects again. However, it
      does not hurt to be safe for future callers.
      
      In most cases, we can abstract this out to a
      "free_tree_buffer" helper. However, there are two
      exceptions:
      
        1. The fsck code relies on the parsed flag to know that we
           were able to parse the object at one point. We can
           switch this to using a flag in the "flags" field.
      
        2. The index-pack code sets the buffer to NULL but does
           not free it (it is freed by a caller). We should still
           unset the parsed flag here, but we cannot use our
           helper, as we do not want to free the buffer.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      6e454b9a
  23. 07 4月, 2013 2 次提交
    • J
      http: drop http_error function · 4df13f69
      Jeff King 提交于
      This function is a single-liner and is only called from one
      place. Just inline it, which makes the code more obvious.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      4df13f69
    • J
      http: simplify http_error helper function · 67d2a7b5
      Jeff King 提交于
      This helper function should really be a one-liner that
      prints an error message, but it has ended up unnecessarily
      complicated:
      
        1. We call error() directly when we fail to start the curl
           request, so we must later avoid printing a duplicate
           error in http_error().
      
           It would be much simpler in this case to just stuff the
           error message into our usual curl_errorstr buffer
           rather than printing it ourselves. This means that
           http_error does not even have to care about curl's exit
           value (the interesting part is in the errorstr buffer
           already).
      
        2. We return the "ret" value passed in to us, but none of
           the callers actually cares about our return value. We
           can just drop this entirely.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      67d2a7b5
  24. 12 2月, 2013 1 次提交
  25. 05 2月, 2013 1 次提交
    • S
      Verify Content-Type from smart HTTP servers · 4656bf47
      Shawn Pearce 提交于
      Before parsing a suspected smart-HTTP response verify the returned
      Content-Type matches the standard. This protects a client from
      attempting to process a payload that smells like a smart-HTTP
      server response.
      
      JGit has been doing this check on all responses since the dawn of
      time. I mistakenly failed to include it in git-core when smart HTTP
      was introduced. At the time I didn't know how to get the Content-Type
      from libcurl. I punted, meant to circle back and fix this, and just
      plain forgot about it.
      Signed-off-by: NShawn Pearce <spearce@spearce.org>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      4656bf47
  26. 27 11月, 2012 1 次提交
  27. 28 8月, 2012 1 次提交