1. 02 2月, 2016 1 次提交
    • J
      apply, ls-files: simplify "-z" parsing · 1f3c79a9
      Jeff King 提交于
      As a short option, we cannot handle negation. Thus a callback
      handling "unset" is overkill, and we can just use OPT_SET_INT
      instead to handle setting the option.
      
      Anybody who adds "--nul" synonym to this later would need to be
      careful not to break "--no-nul", which should mean that lines are
      terminated with LF at the end.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      1f3c79a9
  2. 06 10月, 2015 1 次提交
    • J
      apply: convert root string to strbuf · 6c31c22c
      Jeff King 提交于
      We use manual computation and strcpy to allocate the "root"
      variable. This would be much simpler using xstrfmt.  But
      since we store the length, too, we can just use a strbuf,
      which handles that for us.
      
      Note that we stop distinguishing between "no root" and
      "empty root" in some cases, but that's OK; the results are
      the same (e.g., inserting an empty string is a noop).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      6c31c22c
  3. 26 9月, 2015 1 次提交
  4. 02 6月, 2015 1 次提交
    • J
      apply: reject a hunk that does not do anything · ad6e8ed3
      Junio C Hamano 提交于
      A hunk like this in a hand-edited patch without correctly adjusting
      the line counts:
      
           @@ -660,2 +660,2 @@ inline struct sk_buff *ieee80211_authentic...
                   auth = (struct ieee80211_authentication *)
                           skb_put(skb, sizeof(struct ieee80211_authentication));
           -       some old text
           +       some new text
           --
           2.1.0
      
           dev mailing list
      
      at the end of the input does not have a good way for us to diagnose
      it as a corrupt patch.  We just read two context lines and discard
      the remainder as cruft, which we must do in order to ignore the
      e-mail footer.  Notice that the patch does not change anything and
      signal an error.
      
      Note that this fix will not help if the hand-edited hunk header were
      "@@ -660,3, +660,2" to include the removal.  We would just remove
      the old text without adding the new one, and treat "+ some new text"
      and everything after that line as trailing cruft.  So it is dubious
      that this patch alone would help very much in practice, but it may
      be better than nothing.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ad6e8ed3
  5. 24 3月, 2015 1 次提交
  6. 14 3月, 2015 1 次提交
  7. 11 2月, 2015 4 次提交
    • J
      apply: do not touch a file beyond a symbolic link · e0d201b6
      Junio C Hamano 提交于
      Because Git tracks symbolic links as symbolic links, a path that
      has a symbolic link in its leading part (e.g. path/to/dir/file,
      where path/to/dir is a symbolic link to somewhere else, be it
      inside or outside the working tree) can never appear in a patch
      that validly applies, unless the same patch first removes the
      symbolic link to allow a directory to be created there.
      
      Detect and reject such a patch.
      
      Things to note:
      
       - Unfortunately, we cannot reuse the has_symlink_leading_path()
         from dir.c, as that is only about the working tree, but "git
         apply" can be told to apply the patch only to the index or to
         both the index and to the working tree.
      
       - We cannot directly use has_symlink_leading_path() even when we
         are applying only to the working tree, as an early patch of a
         valid input may remove a symbolic link path/to/dir and then a
         later patch of the input may create a path path/to/dir/file, but
         "git apply" first checks the input without touching either the
         index or the working tree.  The leading symbolic link check must
         be done on the interim result we compute in-core (i.e. after the
         first patch, there is no path/to/dir symbolic link and it is
         perfectly valid to create path/to/dir/file).
      
         Similarly, when an input creates a symbolic link path/to/dir and
         then creates a file path/to/dir/file, we need to flag it as an
         error without actually creating path/to/dir symbolic link in the
         filesystem.
      
      Instead, for any patch in the input that leaves a path (i.e. a non
      deletion) in the result, we check all leading paths against the
      resulting tree that the patch would create by inspecting all the
      patches in the input and then the target of patch application
      (either the index or the working tree).
      
      This way, we catch a mischief or a mistake to add a symbolic link
      path/to/dir and a file path/to/dir/file at the same time, while
      allowing a valid patch that removes a symbolic link path/to/dir and
      then adds a file path/to/dir/file.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      e0d201b6
    • J
      apply: do not read from beyond a symbolic link · fdc2c3a9
      Junio C Hamano 提交于
      We should reject a patch, whether it renames/copies dir/file to
      elsewhere with or without modificiation, or updates dir/file in
      place, if "dir/" part is actually a symbolic link to elsewhere,
      by making sure that the code to read the preimage does not read
      from a path that is beyond a symbolic link.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      fdc2c3a9
    • J
      apply: do not read from the filesystem under --index · 3c37a2e3
      Junio C Hamano 提交于
      We currently read the preimage to apply a patch from the index only
      when the --cached option is given.  Do so also when the command is
      running under the --index option.  With --index, the index entry and
      the working tree file for a path that is involved in a patch must be
      identical, so this should not affect the result, but by reading from
      the index, we will get the protection to avoid reading an unintended
      path beyond a symbolic link automatically.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      3c37a2e3
    • J
      apply: reject input that touches outside the working area · c536c075
      Junio C Hamano 提交于
      By default, a patch that affects outside the working area (either a
      Git controlled working tree, or the current working directory when
      "git apply" is used as a replacement of GNU patch) is rejected as a
      mistake (or a mischief).  Git itself does not create such a patch,
      unless the user bends over backwards and specifies a non-standard
      prefix to "git diff" and friends.
      
      When `git apply` is used as a "better GNU patch", the user can pass
      the `--unsafe-paths` option to override this safety check. This
      option has no effect when `--index` or `--cached` is in use.
      
      The new test was stolen from Jeff King with slight enhancements.
      Note that a few new tests for touching outside the working area by
      following a symbolic link are still expected to fail at this step,
      but will be fixed in later steps.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c536c075
  8. 23 1月, 2015 4 次提交
    • J
      apply: detect and mark whitespace errors in context lines when fixing · 0a80bc9f
      Junio C Hamano 提交于
      When the incoming patch has whitespace errors in a common context
      line (i.e. a line that is expected to be found and is not modified
      by the patch), "apply --whitespace=fix" corrects the whitespace
      errors the line has, in addition to the whitespace error on a line
      that is updated by the patch.  However, we did not count and report
      that we fixed whitespace errors on such lines.
      
      [jc: This is iffy.  What if the whitespace error has been fixed in
      the target since the patch was written?  A common context line we
      see in the patch has errors, and it matches a line in the target
      that has the errors already corrected, resulting in no change, which
      we may not want to count after all.  On the other hand, we are
      reporting whitespace errors _in_ the incoming patch, so...]
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0a80bc9f
    • J
      apply: count the size of postimage correctly · 407a792e
      Junio C Hamano 提交于
      Under --whitespace=fix option, match_fragment() function examines
      the preimage (the common context and the removed lines in the patch)
      and the file being patched and checks if they match after correcting
      all whitespace errors.  When they are found to match, the common
      context lines in the preimage is replaced with the fixed copy,
      because these lines will then be copied to the corresponding place
      in the postimage by a later call to update_pre_post_images().  Lines
      that are added in the postimage, under --whitespace=fix, have their
      whitespace errors already fixed when apply_one_fragment() prepares
      the preimage and the postimage, so in the end, application of the
      patch can be done by replacing the block of text in the file being
      patched that matched the preimage with what is in the postimage that
      was updated by update_pre_post_images().
      
      In the earlier days, fixing whitespace errors always resulted in
      reduction of size, either collapsing runs of spaces in the indent to
      a tab or removing the trailing whitespaces.  These days, however,
      some whitespace error fix results in extending the size.
      
      250b3c6c (apply --whitespace=fix: avoid running over the postimage
      buffer, 2013-03-22) tried to compute the final postimage size but
      its math was flawed.  It counted the size of the block of text in
      the original being patched after fixing the whitespace errors on its
      lines that correspond to the preimage.  That number does not have
      much to do with how big the final postimage would be.
      
      Instead count (1) the added lines in the postimage, whose size is
      the same as in the final patch result because their whitespace
      errors have already been corrected, and (2) the fixed size of the
      lines that are common.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      407a792e
    • J
      apply: make update_pre_post_images() sanity check the given postlen · 2988289f
      Junio C Hamano 提交于
      "git apply --whitespace=fix" used to be able to assume that fixing
      errors will always reduce the size by e.g. stripping whitespaces at
      the end of lines or collapsing runs of spaces into tabs at the
      beginning of lines.  An update to accomodate fixes that lengthens
      the result by e.g. expanding leading tabs into spaces were made long
      time ago but the logic miscounted the necessary space after such
      whitespace fixes, leading to either under-allocation or over-usage
      of already allocated space.
      
      Illustrate this with a runtime sanity-check to protect us from
      future breakage.  The test was stolen from Kyle McKay who helped
      to identify the problem.
      Helped-by: N"Kyle J. McKay" <mackyle@gmail.com>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      2988289f
    • J
      apply.c: typofix · 923fc5ab
      Junio C Hamano 提交于
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      923fc5ab
  9. 15 1月, 2015 1 次提交
  10. 14 1月, 2015 1 次提交
  11. 26 11月, 2014 1 次提交
  12. 18 11月, 2014 1 次提交
  13. 08 10月, 2014 1 次提交
  14. 02 10月, 2014 1 次提交
  15. 19 9月, 2014 1 次提交
  16. 14 8月, 2014 1 次提交
  17. 08 8月, 2014 3 次提交
    • J
      apply: omit ws check for excluded paths · 477a08af
      Junio C Hamano 提交于
      Whitespace breakages are checked while the patch is being parsed.
      Disable them at the beginning of parse_chunk(), where each
      individual patch is parsed, immediately after we learn the name of
      the file the patch applies to and before we start parsing the diff
      contained in the patch.
      
      One may naively think that we should be able to not just skip the
      whitespace checks but simply fast-forward to the next patch without
      doing anything once use_patch() tells us that this patch is not
      going to be used.  But in reality we cannot really skip much of the
      parsing in order to do such a "fast-forward", primarily because
      parsing "@@ -k,l +m,n @@" lines and counting the input lines is how
      we determine the boundaries of individual patches.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      477a08af
    • J
      apply: hoist use_patch() helper for path exclusion up · 3ee2ad14
      Junio C Hamano 提交于
      We will be adding a caller to the function a bit earlier in this
      file in a later patch.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      3ee2ad14
    • J
      apply: use the right attribute for paths in non-Git patches · d487b0ba
      Junio C Hamano 提交于
      We parse each patchfile and find the name of the path the patch
      applies to, and then use that name to consult the attribute system
      to find the whitespace rules to be used, and also the target file
      (either in the working tree or in the index) to replay the changes
      against.
      
      Unlike a Git-generated patch, a non-Git patch is taken to have the
      pathnames relative to the current working directory.  The names
      found in such a patch are modified by prepending the prefix by the
      prefix_patches() helper function introduced in 56185f49 (git-apply:
      require -p<n> when working in a subdirectory., 2007-02-19).
      
      However, this prefixing is done after the patch is fully parsed and
      affects only what target files are patched.  Because the attributes
      are checked against the names found in the patch during the parsing,
      not against the final pathname, the whitespace check that is done
      during parsing ends up using attributes for a wrong path for non-Git
      patches.
      
      Fix this by doing the prefix much earlier, immediately after the
      header part of each patch is parsed and we learn the name of the
      path the patch affects.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      d487b0ba
  18. 25 7月, 2014 1 次提交
    • J
      apply: avoid possible bogus pointer · 31bb6d37
      Jeff King 提交于
      When parsing "index" lines from a git-diff, we look for a
      space followed by the mode. If we don't have a space, then
      we set our pointer to the end-of-line. However, we don't
      double-check that our end-of-line pointer is valid (e.g., if
      we got a truncated diff input), which could lead to some
      wrap-around pointer arithmetic.
      
      In most cases this would probably get caught by our "40 <
      len" check later in the function, but to be on the safe
      side, let's just use strchrnul to treat end-of-string the
      same as end-of-line.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      31bb6d37
  19. 22 7月, 2014 1 次提交
  20. 21 6月, 2014 1 次提交
    • J
      apply: use skip_prefix instead of raw addition · ce2ecf29
      Jeff King 提交于
      A submodule diff generally has content like:
      
        -Subproject commit [0-9a-f]{40}
        +Subproject commit [0-9a-f]{40}
      
      When we are using "git apply --index" with a submodule, we
      first apply the textual diff, and then parse that result to
      figure out the new sha1.
      
      If the diff has bogus input like:
      
        -Subproject commit 1234567890123456789012345678901234567890
        +bogus
      
      we will parse the "bogus" portion. Our parser assumes that
      the buffer starts with "Subproject commit", and blindly
      skips past it using strlen(). This can cause us to read
      random memory after the buffer.
      
      This problem was unlikely to have come up in practice (since
      it requires a malformed diff), and even when it did, we
      likely noticed the problem anyway as the next operation was
      to call get_sha1_hex on the random memory.
      
      However, we can easily fix it by using skip_prefix to notice
      the parsing error.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ce2ecf29
  21. 20 6月, 2014 1 次提交
  22. 14 6月, 2014 2 次提交
  23. 01 4月, 2014 1 次提交
  24. 27 3月, 2014 1 次提交
    • J
      apply --ignore-space-change: lines with and without leading whitespaces do not match · 14d3bb49
      Junio C Hamano 提交于
      The fuzzy_matchlines() function is used when attempting to resurrect
      a patch that is whitespace-damaged, or when applying a patch that
      was produced against an old codebase to the codebase after
      indentation change.
      
      The patch may want to change a line "a_bc" ("_" is used throught
      this description for a whitespace to make it stand out) in the
      original into something else, and we may not find "a_bc" in the
      current source, but there may be "a__bc" (two spaces instead of one
      the whitespace-damaged patch claims to expect).  By ignoring the
      amount of whitespaces, it forces "git apply" to consider that "a_bc"
      in the broken patch meant to refer to "a__bc" in reality.
      
      However, the implementation special cases a run of whitespaces at
      the beginning of a line and makes "abc" match "_abc", even though a
      whitespace in the middle of string never matches a 0-width gap,
      e.g. "a_bc" does not match "abc".  A run of whitespace at the end of
      one string does not match a 0-width end of line on the other line,
      either, e.g. "abc_" does not match "abc".
      
      Fix this inconsistency by making the code skip leading whitespaces
      only when both strings begin with a whitespace.  This makes the
      option mean the same as the option of the same name in "diff" and
      "git diff".
      
      Note that I am not sure if anybody sane should use this option in
      the first place.  The fuzzy match logic may be able to find the
      original line that the patch author may have meant to touch because
      it does not fully trust what the original lines say (i.e. context
      lines prefixed by " " and old lines prefixed by "-" does not have to
      exactly match the contents the patch is applied to).  There is no
      reason for us to trust what the replacement lines (i.e. new lines
      prefixed by "+") say, either, but with this option enabled, we end
      up copying these new lines with suspicious whitespace distributions
      literally into the patched result.  But as long as we keep it, we
      should make it do its insane thing consistently.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      14d3bb49
  25. 21 2月, 2014 1 次提交
  26. 01 2月, 2014 1 次提交
  27. 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
  28. 06 8月, 2013 1 次提交
  29. 23 7月, 2013 1 次提交
  30. 20 7月, 2013 1 次提交
  31. 19 7月, 2013 1 次提交