1. 11 8月, 2015 2 次提交
    • J
      prefer git_pathdup to git_path in some possibly-dangerous cases · fcd12db6
      Jeff King 提交于
      Because git_path uses a static buffer that is shared with
      calls to git_path, mkpath, etc, it can be dangerous to
      assign the result to a variable or pass it to a non-trivial
      function. The value may change unexpectedly due to other
      calls.
      
      None of the cases changed here has a known bug, but they're
      worth converting away from git_path because:
      
        1. It's easy to use git_pathdup in these cases.
      
        2. They use constructs (like assignment) that make it
           hard to tell whether they're safe or not.
      
      The extra malloc overhead should be trivial, as an
      allocation should be an order of magnitude cheaper than a
      system call (which we are clearly about to make, since we
      are constructing a filename). The real cost is that we must
      remember to free the result.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      fcd12db6
    • J
      sha1_file.c: rename move_temp_to_file() to finalize_object_file() · cb5add58
      Junio C Hamano 提交于
      Since 5a688fe4 ("core.sharedrepository = 0mode" should set, not
      loosen, 2009-03-25), we kept reminding ourselves:
      
          NEEDSWORK: this should be renamed to finalize_temp_file() as
          "moving" is only a part of what it does, when no patch between
          master to pu changes the call sites of this function.
      
      without doing anything about it.  Let's do so.
      
      The purpose of this function was not to move but to finalize.  The
      detail of the primarily implementation of finalizing was to link the
      temporary file to its final name and then to unlink, which wasn't
      even "moving".  The alternative implementation did "move" by calling
      rename(2), which is a fun tangent.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      cb5add58
  2. 14 7月, 2015 1 次提交
    • M
      fast-import: do less work when given "from" matches current branch head · 0df32457
      Mike Hommey 提交于
      When building a fast-import stream, it's easy to forget the fact
      that for non-merge commits happening on top of the current branch
      head, there is no need for a "from" command. That is corroborated by
      the fact that at least git-p4, hg-fast-export and felipec's
      git-remote-hg all unconditionally use a "from" command.
      
      Unfortunately, giving a "from" command always resets the branch
      tree, forcing it to be re-read, and in many cases, the pack is also
      closed and reopened through gfi_unpack_entry.  Both are unnecessary
      overhead, and the latter is particularly slow at least on OSX.
      
      Avoid resetting the tree when it's unmodified, and avoid calling
      gfi_unpack_entry when the given mark points to the same commit as
      the current branch head.
      Signed-off-by: NMike Hommey <mh@glandium.org>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0df32457
  3. 02 7月, 2015 1 次提交
    • M
      fast-import: add a get-mark command · 28c7b1f7
      Michael Haggerty 提交于
      It is sometimes useful for importers to be able to read the SHA-1
      corresponding to a mark that they have created via fast-import. For
      example, they might want to embed the SHA-1 into the commit message of
      a later commit. Or it might be useful for internal bookkeeping uses,
      or for logging.
      
      Add a "get-mark" command to "git fast-import" that allows the importer
      to ask for the value of a mark that has been created earlier.
      Signed-off-by: NMichael Haggerty <mhagger@alum.mit.edu>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      28c7b1f7
  4. 30 6月, 2015 1 次提交
    • J
      convert "enum date_mode" into a struct · a5481a6c
      Jeff King 提交于
      In preparation for adding date modes that may carry extra
      information beyond the mode itself, this patch converts the
      date_mode enum into a struct.
      
      Most of the conversion is fairly straightforward; we pass
      the struct as a pointer and dereference the type field where
      necessary. Locations that declare a date_mode can use a "{}"
      constructor.  However, the tricky case is where we use the
      enum labels as constants, like:
      
        show_date(t, tz, DATE_NORMAL);
      
      Ideally we could say:
      
        show_date(t, tz, &{ DATE_NORMAL });
      
      but of course C does not allow that. Likewise, we cannot
      cast the constant to a struct, because we need to pass an
      actual address. Our options are basically:
      
        1. Manually add a "struct date_mode d = { DATE_NORMAL }"
           definition to each caller, and pass "&d". This makes
           the callers uglier, because they sometimes do not even
           have their own scope (e.g., they are inside a switch
           statement).
      
        2. Provide a pre-made global "date_normal" struct that can
           be passed by address. We'd also need "date_rfc2822",
           "date_iso8601", and so forth. But at least the ugliness
           is defined in one place.
      
        3. Provide a wrapper that generates the correct struct on
           the fly. The big downside is that we end up pointing to
           a single global, which makes our wrapper non-reentrant.
           But show_date is already not reentrant, so it does not
           matter.
      
      This patch implements 3, along with a minor macro to keep
      the size of the callers sane.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      a5481a6c
  5. 23 6月, 2015 1 次提交
  6. 06 3月, 2015 1 次提交
  7. 18 2月, 2015 1 次提交
  8. 11 2月, 2015 1 次提交
    • J
      fast-import: avoid running end_packfile recursively · 5e915f30
      Jeff King 提交于
      When an import has finished, we run end_packfile() to
      finalize the data and move the packfile into place. If this
      process fails, we call die() and end up in our die_nicely()
      handler.  Which unfortunately includes running end_packfile
      to save any progress we made. We enter the function again,
      and start operating on the pack_data struct while it is in
      an inconsistent state, leading to a segfault.
      
      One way to trigger this is to simply start two identical
      fast-imports at the same time. They will both create the
      same packfiles, which will then try to create identically
      named ".keep" files. One will win the race, and the other
      will die(), and end up with the segfault.
      
      Since 3c078b9c, we already reset the pack_data pointer to
      NULL at the end of end_packfile. That covers the case of us
      calling die() right after end_packfile, before we have
      reinitialized the pack_data pointer. This new problem is
      quite similar, except that we are worried about calling
      die() _during_ end_packfile, not right after. Ideally we
      would simply set pack_data to NULL as soon as we enter the
      function, and operate on a copy of the pointer.
      
      Unfortunately, it is not so easy. pack_data is a global, and
      end_packfile calls into other functions which operate on the
      global directly. We would have to teach each of these to
      take an argument, and there is no guarantee that we would
      catch all of the spots.
      
      Instead, we can simply use a static flag to avoid
      recursively entering the function. This is a little less
      elegant, but it's short and fool-proof.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      5e915f30
  9. 02 12月, 2014 2 次提交
  10. 16 10月, 2014 1 次提交
  11. 02 10月, 2014 3 次提交
  12. 19 9月, 2014 1 次提交
  13. 04 9月, 2014 2 次提交
  14. 30 8月, 2014 1 次提交
  15. 28 8月, 2014 1 次提交
    • J
      date: use strbufs in date-formatting functions · c33ddc2e
      Jeff King 提交于
      Many of the date functions write into fixed-size buffers.
      This is a minor pain, as we have to take special
      precautions, and frequently end up copying the result into a
      strbuf or heap-allocated buffer anyway (for which we
      sometimes use strcpy!).
      
      Let's instead teach parse_date, datestamp, etc to write to a
      strbuf. The obvious downside is that we might need to
      perform a heap allocation where we otherwise would not need
      to. However, it turns out that the only two new allocations
      required are:
      
        1. In test-date.c, where we don't care about efficiency.
      
        2. In determine_author_info, which is not performance
           critical (and where the use of a strbuf will help later
           refactoring).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c33ddc2e
  16. 26 8月, 2014 2 次提交
    • J
      fast-import: fix buffer overflow in dump_tags · c2527859
      Jeff King 提交于
      When creating a new annotated tag, we sprintf the refname
      into a static-sized buffer. If we have an absurdly long
      tagname, like:
      
        git init repo &&
        cd repo &&
        git commit --allow-empty -m foo &&
        git tag -m message mytag &&
        git fast-export mytag |
        perl -lpe '/^tag/ and s/mytag/"a" x 8192/e' |
        git fast-import <input
      
      we'll overflow the buffer. We can fix it by using a strbuf.
      Signed-off-by: NJeff King <peff@peff.net>
      Reviewed-by: NMichael Haggerty <mhagger@alum.mit.edu>
      Reviewed-by: NRonnie Sahlberg <sahlberg@google.com>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c2527859
    • J
      fast-import: clean up pack_data pointer in end_packfile · 3c078b9c
      Jeff King 提交于
      We have a global pointer pack_data pointing to the current
      pack we have open. Inside end_packfile we have two new
      pointers, old_p and new_p. The latter points to pack_data,
      and the former points to the new "installed" version of the
      packfile we get when we hand the file off to the regular
      sha1_file machinery. When then free old_p.
      
      Presumably the extra old_p pointer was there so that we
      could overwrite pack_data with new_p and still free old_p,
      but we don't do that. We just leave pack_data pointing to
      bogus memory, and don't overwrite it until we call
      start_packfile again (if ever).
      
      This can cause problems for our die routine, which calls
      end_packfile to clean things up. If we die at the wrong
      moment, we can end up looking at invalid memory in
      pack_data left after the last end_packfile().
      
      Instead, let's make sure we set pack_data to NULL after we
      free it, and make calling endfile() again with a NULL
      pack_data a noop (there is nothing to end).
      
      We can further make things less confusing by dropping old_p
      entirely, and moving new_p closer to its point of use.
      Signed-off-by: NJeff King <peff@peff.net>
      Reviewed-by: NRonnie Sahlberg <sahlberg@google.com>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      3c078b9c
  17. 14 8月, 2014 1 次提交
  18. 19 7月, 2014 1 次提交
  19. 21 6月, 2014 4 次提交
    • J
      fast-import: refactor parsing of spaces · e814c39c
      Jeff King 提交于
      When we see a file change in a commit, we expect one of:
      
        1. A mark.
      
        2. An "inline" keyword.
      
        3. An object sha1.
      
      The handling of spaces is inconsistent between the three
      options. Option 1 calls a sub-function which checks for the
      space, but doesn't parse past it. Option 2 parses the space,
      then deliberately avoids moving the pointer past it. Option
      3 detects the space locally but doesn't move past it.
      
      This is confusing, because it looks like option 1 forgets to
      check for the space (it's just buried). And option 2 checks
      for "inline ", but only moves strlen("inline") characters
      forward, which looks like a bug but isn't.
      
      We can make this more clear by just having each branch move
      past the space as it is checked (and we can replace the
      doubled use of "inline" with a call to skip_prefix).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      e814c39c
    • J
      fast-import: use skip_prefix for parsing input · 97313bef
      Jeff King 提交于
      Fast-import does a lot of parsing of commands and
      dispatching to sub-functions. For example, given "option
      foo", we might recognize "option " using starts_with, and
      then hand it off to parse_option() to do the rest.
      
      However, we do not let parse_option know that we have parsed
      the first part already. It gets the full buffer, and has to
      skip past the uninteresting bits. Some functions simply add
      a magic constant:
      
        char *option = command_buf.buf + 7;
      
      Others use strlen:
      
        char *option = command_buf.buf + strlen("option ");
      
      And others use strchr:
      
        char *option = strchr(command_buf.buf, ' ') + 1;
      
      All of these are brittle and easy to get wrong (especially
      given that the starts_with call and the code that assumes
      the presence of the prefix are far apart). Instead, we can
      use skip_prefix, and just pass each handler a pointer to its
      arguments.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      97313bef
    • 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
    • J
      fast-import: fix read of uninitialized argv memory · ff45c0d4
      Jeff King 提交于
      Fast-import shares code between its command-line parser and
      the "option" command. To do so, it strips the "--" from any
      command-line options and passes them to the option parser.
      However, it does not confirm that the option even begins
      with "--" before blindly passing "arg + 2".
      
      It does confirm that the option starts with "-", so the only
      affected case was:
      
        git fast-import -
      
      which would read uninitialized memory after the argument. We
      can fix it by using skip_prefix and checking the result. As
      a bonus, this gets rid of some magic numbers.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ff45c0d4
  20. 22 4月, 2014 1 次提交
  21. 10 3月, 2014 1 次提交
  22. 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
  23. 05 9月, 2013 2 次提交
  24. 31 8月, 2013 1 次提交
  25. 24 6月, 2013 3 次提交
  26. 08 5月, 2013 1 次提交
  27. 29 4月, 2013 1 次提交
    • R
      sparse: Fix mingw_main() argument number/type errors · 84d32bf7
      Ramsay Jones 提交于
      Sparse issues 68 errors (two errors for each main() function) such
      as the following:
      
            SP git.c
        git.c:510:5: error: too many arguments for function mingw_main
        git.c:510:5: error: symbol 'mingw_main' redeclared with different type \
          (originally declared at git.c:510) - different argument counts
      
      The errors are caused by the 'main' macro used by the MinGW build
      to provide a replacement main() function. The original main function
      is effectively renamed to 'mingw_main' and is called from the new
      main function. The replacement main is used to execute certain actions
      common to all git programs on MinGW (e.g. ensure the standard I/O
      streams are in binary mode).
      
      In order to suppress the errors, we change the macro to include the
      parameters in the declaration of the mingw_main function.
      
      Unfortunately, this change provokes both sparse and gcc to complain
      about 9 calls to mingw_main(), such as the following:
      
            CC git.o
        git.c: In function 'main':
        git.c:510: warning: passing argument 2 of 'mingw_main' from \
          incompatible pointer type
        git.c:510: note: expected 'const char **' but argument is of \
          type 'char **'
      
      In order to suppress these warnings, since both of the main
      functions need to be declared with the same prototype, we
      change the declaration of the 9 main functions, thus:
      
          int main(int argc, char **argv)
      Signed-off-by: NRamsay Jones <ramsay@ramsay1.demon.co.uk>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      84d32bf7
  28. 30 3月, 2013 1 次提交
    • R
      fast-import: Fix an gcc -Wuninitialized warning · 0a34594c
      Ramsay Jones 提交于
      Commit cbfd5e1c ("drop some obsolete "x = x" compiler warning hacks",
      21-03-2013) removed a gcc hack that suppressed an "might be used
      uninitialized" warning issued by older versions of gcc.
      
      However, commit 3aa99df8 ('fast-import: clarify "inline" logic in
      file_change_m', 21-03-2013) addresses an (almost) identical issue
      (with very similar code), but includes additional code in it's
      resolution. The solution used by this commit, unlike that used by
      commit cbfd5e1c, also suppresses the -Wuninitialized warning on
      older versions of gcc.
      
      In order to suppress the warning (against the 'oe' symbol) in the
      note_change_n() function, we adopt the same solution used by commit
      3aa99df8.
      Signed-off-by: NRamsay Jones <ramsay@ramsay1.demon.co.uk>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0a34594c