This project is mirrored from https://github.com/git/git.git. Pull mirroring updated .
  1. 14 Feb, 2018 1 commit
  2. 15 Nov, 2017 1 commit
    • Junio C Hamano's avatar
      branch: correctly reject refs/heads/{-dash,HEAD} · a625b092
      Junio C Hamano authored
      strbuf_check_branch_ref() is the central place where many codepaths
      see if a proposed name is suitable for the name of a branch.  It was
      designed to allow us to get stricter than the check_refname_format()
      check used for refnames in general, and we already use it to reject
      a branch whose name begins with a '-'.  The function gets a strbuf
      and a string "name", and returns non-zero if the name is not
      appropriate as the name for a branch.  When the name is good, it
      places the full refname for the branch with the proposed name in the
      strbuf before it returns.
      
      However, it turns out that one caller looks at what is in the strbuf
      even when the function returns an error.  Make the function populate
      the strbuf even when it returns an error.  That way, when "-dash" is
      given as name, "refs/heads/-dash" is placed in the strbuf when
      returning an error to copy_or_rename_branch(), which notices that
      the user is trying to recover with "git branch -m -- -dash dash" to
      rename "-dash" to "dash".
      
      While at it, use the same mechanism to also reject "HEAD" as a
      branch name.
      Helped-by: default avatarJeff King <[email protected]>
      Helped-by: Kaartic Sivaraam's avatarKaartic Sivaraam <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      a625b092
  3. 17 Oct, 2017 1 commit
    • Junio C Hamano's avatar
      check-ref-format --branch: do not expand @{...} outside repository · 7c3f847a
      Junio C Hamano authored
      Running "git check-ref-format --branch @{-1}" from outside any
      repository produces
      
      	$ git check-ref-format --branch @{-1}
      	BUG: environment.c:182: git environment hasn't been setup
      
      This is because the expansion of @{-1} must come from the HEAD reflog,
      which involves opening the repository.  @{u} and @{push} (which are
      more unusual because they typically would not expand to a local
      branch) trigger the same assertion.
      
      This has been broken since day one.  Before v2.13.0-rc0~48^2
      (setup_git_env: avoid blind fall-back to ".git", 2016-10-02), the
      breakage was more subtle: Git would read reflogs from ".git" within
      the current directory even if it was not a valid repository.  Usually
      that is harmless because Git is not being run from the root directory
      of an invalid repository, but in edge cases such accesses can be
      confusing or harmful.  Since v2.13.0, the problem is easier to
      diagnose because Git aborts with a BUG message.
      
      Erroring out is the right behavior: when asked to interpret a branch
      name like "@{-1}", there is no reasonable answer in this context.
      But we should print a message saying so instead of an assertion failure.
      
      We do not forbid "check-ref-format --branch" from outside a repository
      altogether because it is ok for a script to pre-process branch
      arguments without @{...} in such a context.  For example, with
      pre-2.13 Git, a script that does
      
      	branch='master'; # default value
      	parse_options
      	branch=$(git check-ref-format --branch "$branch")
      
      to normalize an optional branch name provided by the user would work
      both inside a repository (where the user could provide '@{-1}') and
      outside (where '@{-1}' should not be accepted).
      
      So disable the "expand @{...}" half of the feature when run outside a
      repository, but keep the check of the syntax of a proposed branch
      name. This way, when run from outside a repository, "git
      check-ref-format --branch @{-1}" will gracefully fail:
      
      	$ git check-ref-format --branch @{-1}
      	fatal: '@{-1}' is not a valid branch name
      
      and "git check-ref-format --branch master" will succeed as before:
      
      	$ git check-ref-format --branch master
      	master
      
      restoring the usual pre-2.13 behavior.
      
      [jn: split out from a larger patch; moved conditional to
       strbuf_check_branch_ref instead of its caller; fleshed out commit
       message; some style tweaks in tests]
      Reported-by: Marko Kungla's avatarMarko Kungla <[email protected]>
      Helped-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      Signed-off-by: default avatarJonathan Nieder <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      7c3f847a
  4. 16 Oct, 2017 3 commits
  5. 13 Oct, 2017 3 commits
    • Derrick Stolee's avatar
      sha1_name: minimize OID comparisons during disambiguation · 0e87b856
      Derrick Stolee authored
      Minimize OID comparisons during disambiguation of packfile OIDs.
      
      Teach git to use binary search with the full OID to find the object's
      position (or insertion position, if not present) in the pack-index.
      The object before and immediately after (or the one at the insertion
      position) give the maximum common prefix.  No subsequent linear search
      is required.
      
      Take care of which two to inspect, in case the object id exists in the
      packfile.
      
      If the input to find_unique_abbrev_r() is a partial prefix, then the
      OID used for the binary search is padded with zeroes so the object will
      not exist in the repo (with high probability) and the same logic
      applies.
      
      This commit completes a series of three changes to OID abbreviation
      code, and the overall change can be seen using standard commands for
      large repos. Below we report performance statistics for perf test 4211.6
      from p4211-line-log.sh using three copies of the Linux repo:
      
      | Packs | Loose  | HEAD~3   | HEAD     | Rel%  |
      |-------|--------|----------|----------|-------|
      |  1    |      0 |  41.27 s |  38.93 s | -4.8% |
      | 24    |      0 |  98.04 s |  91.35 s | -5.7% |
      | 23    | 323952 | 117.78 s | 112.18 s | -4.8% |
      Signed-off-by: Derrick Stolee's avatarDerrick Stolee <[email protected]>
      Reviewed-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      0e87b856
    • Derrick Stolee's avatar
      sha1_name: parse less while finding common prefix · a42d6fd2
      Derrick Stolee authored
      Create get_hex_char_from_oid() to parse oids one hex character at a
      time. This prevents unnecessary copying of hex characters in
      extend_abbrev_len() when finding the length of a common prefix.
      Signed-off-by: Derrick Stolee's avatarDerrick Stolee <[email protected]>
      Reviewed-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      a42d6fd2
    • Derrick Stolee's avatar
      sha1_name: unroll len loop in find_unique_abbrev_r() · 5b20ace6
      Derrick Stolee authored
      Unroll the while loop inside find_unique_abbrev_r to avoid iterating
      through all loose objects and packfiles multiple times when the short
      name is longer than the predicted length.
      
      Instead, inspect each object that collides with the estimated
      abbreviation to find the longest common prefix.
      
      The focus of this change is to refactor the existing method in a way
      that clearly does not change the current behavior. In some cases, the
      new method is slower than the previous method. Later changes will
      correct all performance loss.
      Signed-off-by: Derrick Stolee's avatarDerrick Stolee <[email protected]>
      Reviewed-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      5b20ace6
  6. 09 Oct, 2017 1 commit
  7. 23 Aug, 2017 1 commit
  8. 17 Jul, 2017 3 commits
    • brian m. carlson's avatar
      sha1_name: convert uses of 40 to GIT_SHA1_HEXSZ · ac53fe86
      brian m. carlson authored
      There are several uses of the constant 40 in find_unique_abbrev_r.
      Convert them to GIT_SHA1_HEXSZ.
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      ac53fe86
    • brian m. carlson's avatar
      sha1_name: convert GET_SHA1* flags to GET_OID* · 321c89bf
      brian m. carlson authored
      Convert the flags for get_oid_with_context and friends to use "OID"
      instead of "SHA1" in their names.
      
      This transform was made by running the following one-liner on the
      affected files:
      
        perl -pi -e 's/GET_SHA1/GET_OID/g'
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      321c89bf
    • brian m. carlson's avatar
      sha1_name: convert get_sha1* to get_oid* · e82caf38
      brian m. carlson authored
      Now that all the callers of get_sha1 directly or indirectly use struct
      object_id, rename the functions starting with get_sha1 to start with
      get_oid.  Convert the internals in sha1_name.c to use struct object_id
      as well, and eliminate explicit length checks where possible.  Convert a
      use of 40 in get_oid_basic to GIT_SHA1_HEXSZ.
      
      Outside of sha1_name.c and cache.h, this transition was made with the
      following semantic patch:
      
      @@
      expression E1, E2;
      @@
      - get_sha1(E1, E2.hash)
      + get_oid(E1, &E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1(E1, E2->hash)
      + get_oid(E1, E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_committish(E1, E2.hash)
      + get_oid_committish(E1, &E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_committish(E1, E2->hash)
      + get_oid_committish(E1, E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_treeish(E1, E2.hash)
      + get_oid_treeish(E1, &E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_treeish(E1, E2->hash)
      + get_oid_treeish(E1, E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_commit(E1, E2.hash)
      + get_oid_commit(E1, &E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_commit(E1, E2->hash)
      + get_oid_commit(E1, E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_tree(E1, E2.hash)
      + get_oid_tree(E1, &E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_tree(E1, E2->hash)
      + get_oid_tree(E1, E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_blob(E1, E2.hash)
      + get_oid_blob(E1, &E2)
      
      @@
      expression E1, E2;
      @@
      - get_sha1_blob(E1, E2->hash)
      + get_oid_blob(E1, E2)
      
      @@
      expression E1, E2, E3, E4;
      @@
      - get_sha1_with_context(E1, E2, E3.hash, E4)
      + get_oid_with_context(E1, E2, &E3, E4)
      
      @@
      expression E1, E2, E3, E4;
      @@
      - get_sha1_with_context(E1, E2, E3->hash, E4)
      + get_oid_with_context(E1, E2, E3, E4)
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      e82caf38
  9. 10 Jul, 2017 1 commit
  10. 24 Jun, 2017 1 commit
  11. 22 Jun, 2017 1 commit
    • René Scharfe's avatar
      sha1_name: cache readdir(3) results in find_short_object_filename() · cc817ca3
      René Scharfe authored
      Read each loose object subdirectory at most once when looking for unique
      abbreviated hashes.  This speeds up commands like "git log --pretty=%h"
      considerably, which previously caused one readdir(3) call for each
      candidate, even for subdirectories that were visited before.
      
      The new cache is kept until the program ends and never invalidated.  The
      same is already true for pack indexes.  The inherent racy nature of
      finding unique short hashes makes it still fit for this purpose -- a
      conflicting new object may be added at any time.  Tasks with higher
      consistency requirements should not use it, though.
      
      The cached object names are stored in an oid_array, which is quite
      compact.  The bitmap for remembering which subdir was already read is
      stored as a char array, with one char per directory -- that's not quite
      as compact, but really simple and incurs only an overhead equivalent to
      11 hashes after all.
      Suggested-by: default avatarJeff King <[email protected]>
      Helped-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarRene Scharfe <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      cc817ca3
  12. 15 Jun, 2017 1 commit
  13. 30 May, 2017 1 commit
  14. 24 May, 2017 3 commits
    • Jeff King's avatar
      get_sha1_with_context: dynamically allocate oc->path · dc944b65
      Jeff King authored
      When a sha1 lookup returns the tree path via "struct
      object_context", it just copies it into a fixed-size buffer.
      This means the result can be truncated, and it means our
      "struct object_context" consumes a lot of stack space.
      
      Instead, let's allocate a string on the heap. Because most
      callers don't care about this information, we'll avoid doing
      it by default (so they don't all have to start calling
      free() on the result). There are basically two options for
      the caller to signal to us that it's interested:
      
        1. By setting a pointer to storage in the object_context.
      
        2. By passing a flag in another parameter.
      
      Doing (1) would match the way that sha1_object_info_extended()
      works. But it would mean that every caller would have to
      initialize the object_context, which they don't currently
      have to do.
      
      This patch does (2), and adds a new bit to the function's
      flags field. All of the callers that look at the "path"
      field are updated to pass the new flag.
      Signed-off-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      dc944b65
    • Jeff King's avatar
      get_sha1_with_context: always initialize oc->symlink_path · d72cae12
      Jeff King authored
      The get_sha1_with_context() function zeroes out the
      oc->symlink_path strbuf, but doesn't use strbuf_init() to
      set up the usual invariants (like pointing to the slopbuf).
      We don't actually write to the oc->symlink_path strbuf
      unless we call get_tree_entry_follow_symlinks(), and that
      function does initialize it. However, readers may still look
      at the zero'd strbuf.
      
      In practice this isn't a triggerable bug. The only caller
      that looks at it only does so when the mode we found is 0.
      This doesn't happen for non-tree-entries (where we return
      S_IFINVALID). A broken tree entry could have a mode of 0,
      but canon_mode() quietly rewrites that into S_IFGITLINK.
      So the "0" mode should only come up when we did indeed find
      a symlink.
      
      This is mostly just an accident of how the code happens to
      work, though. Let's future-proof ourselves to make sure the
      strbuf is properly initialized for all calls (it's only a
      few struct member assignments, not a heap allocation).
      Signed-off-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      d72cae12
    • Jeff King's avatar
      sha1_name: consistently refer to object_context as "oc" · c0a487ea
      Jeff King authored
      An early version of the patch to add object_context used the
      name object_resolve_context. This was later shortened to
      just object_context, but the "orc" variable name stuck in a
      few places.  Let's use "oc", which is used elsewhere in the
      code.
      Signed-off-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      c0a487ea
  15. 08 May, 2017 5 commits
    • brian m. carlson's avatar
      object: convert parse_object* to take struct object_id · c251c83d
      brian m. carlson authored
      Make parse_object, parse_object_or_die, and parse_object_buffer take a
      pointer to struct object_id.  Remove the temporary variables inserted
      earlier, since they are no longer necessary.  Transform all of the
      callers using the following semantic patch:
      
      @@
      expression E1;
      @@
      - parse_object(E1.hash)
      + parse_object(&E1)
      
      @@
      expression E1;
      @@
      - parse_object(E1->hash)
      + parse_object(E1)
      
      @@
      expression E1, E2;
      @@
      - parse_object_or_die(E1.hash, E2)
      + parse_object_or_die(&E1, E2)
      
      @@
      expression E1, E2;
      @@
      - parse_object_or_die(E1->hash, E2)
      + parse_object_or_die(E1, E2)
      
      @@
      expression E1, E2, E3, E4, E5;
      @@
      - parse_object_buffer(E1.hash, E2, E3, E4, E5)
      + parse_object_buffer(&E1, E2, E3, E4, E5)
      
      @@
      expression E1, E2, E3, E4, E5;
      @@
      - parse_object_buffer(E1->hash, E2, E3, E4, E5)
      + parse_object_buffer(E1, E2, E3, E4, E5)
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      c251c83d
    • brian m. carlson's avatar
    • brian m. carlson's avatar
      Convert lookup_tag to struct object_id · d3101b53
      brian m. carlson authored
      Convert lookup_tag to take a pointer to struct object_id.
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      d3101b53
    • brian m. carlson's avatar
      Convert lookup_commit* to struct object_id · bc83266a
      brian m. carlson authored
      Convert lookup_commit, lookup_commit_or_die,
      lookup_commit_reference, and lookup_commit_reference_gently to take
      struct object_id arguments.
      
      Introduce a temporary in parse_object buffer in order to convert this
      function.  This is required since in order to convert parse_object and
      parse_object_buffer, lookup_commit_reference_gently and
      lookup_commit_or_die would need to be converted.  Not introducing a
      temporary would therefore require that lookup_commit_or_die take a
      struct object_id *, but lookup_commit would take unsigned char *,
      leaving a confusing and hard-to-use interface.
      
      parse_object_buffer will lose this temporary in a later patch.
      
      This commit was created with manual changes to commit.c, commit.h, and
      object.c, plus the following semantic patch:
      
      @@
      expression E1, E2;
      @@
      - lookup_commit_reference_gently(E1.hash, E2)
      + lookup_commit_reference_gently(&E1, E2)
      
      @@
      expression E1, E2;
      @@
      - lookup_commit_reference_gently(E1->hash, E2)
      + lookup_commit_reference_gently(E1, E2)
      
      @@
      expression E1;
      @@
      - lookup_commit_reference(E1.hash)
      + lookup_commit_reference(&E1)
      
      @@
      expression E1;
      @@
      - lookup_commit_reference(E1->hash)
      + lookup_commit_reference(E1)
      
      @@
      expression E1;
      @@
      - lookup_commit(E1.hash)
      + lookup_commit(&E1)
      
      @@
      expression E1;
      @@
      - lookup_commit(E1->hash)
      + lookup_commit(E1)
      
      @@
      expression E1, E2;
      @@
      - lookup_commit_or_die(E1.hash, E2)
      + lookup_commit_or_die(&E1, E2)
      
      @@
      expression E1, E2;
      @@
      - lookup_commit_or_die(E1->hash, E2)
      + lookup_commit_or_die(E1, E2)
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      bc83266a
    • brian m. carlson's avatar
      Convert remaining callers of lookup_commit_reference* to object_id · 1e43ed98
      brian m. carlson authored
      There are a small number of remaining callers of lookup_commit_reference
      and lookup_commit_reference_gently that still need to be converted to
      struct object_id.  Convert these.
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      1e43ed98
  16. 27 Apr, 2017 1 commit
    • Johannes Schindelin's avatar
      timestamp_t: a new data type for timestamps · dddbad72
      Johannes Schindelin authored
      Git's source code assumes that unsigned long is at least as precise as
      time_t. Which is incorrect, and causes a lot of problems, in particular
      where unsigned long is only 32-bit (notably on Windows, even in 64-bit
      versions).
      
      So let's just use a more appropriate data type instead. In preparation
      for this, we introduce the new `timestamp_t` data type.
      
      By necessity, this is a very, very large patch, as it has to replace all
      timestamps' data type in one go.
      
      As we will use a data type that is not necessarily identical to `time_t`,
      we need to be very careful to use `time_t` whenever we interact with the
      system functions, and `timestamp_t` everywhere else.
      Signed-off-by: Johannes Schindelin's avatarJohannes Schindelin <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      dddbad72
  17. 31 Mar, 2017 3 commits
  18. 28 Mar, 2017 2 commits
  19. 27 Mar, 2017 3 commits
    • Ævar Arnfjörð Bjarmason's avatar
      rev-parse: match @{upstream}, @{u} and @{push} case-insensitively · 244ea1b5
      Ævar Arnfjörð Bjarmason authored
      Change the revision parsing logic to match @{upstream}, @{u} & @{push}
      case-insensitively.
      
      Before this change supplying anything except the lower-case forms
      emits an "unknown revision or path not in the working tree"
      error. This change makes upper-case & mixed-case versions equivalent
      to the lower-case versions.
      
      The use-case for this is being able to hold the shift key down while
      typing @{u} on certain keyboard layouts, which makes the sequence
      easier to type, and reduces cases where git throws an error at the
      user where it could do what he means instead.
      
      These suffixes now join various other suffixes & special syntax
      documented in gitrevisions(7) that matches case-insensitively. A table
      showing the status of the various forms documented there before &
      after this patch is shown below. The key for the table is:
      
       - CI  = Case Insensitive
       - CIP = Case Insensitive Possible (without ambiguities)
       - AG  = Accepts Garbage (.e.g. @{./.4.minutes./.})
      
      Before this change:
      
          |----------------+-----+------+-----|
          | What?          | CI? | CIP? | AG? |
          |----------------+-----+------+-----|
          | @{<date>}      | Y   | Y    | Y   |
          | @{upstream}    | N   | Y    | N   |
          | @{push}        | N   | Y    | N   |
          |----------------+-----+------+-----|
      
      After it:
      
          |----------------+-----+------+-----|
          | What?          | CI? | CIP? | AG? |
          |----------------+-----+------+-----|
          | @{<date>}      | Y   | Y    | Y   |
          | @{upstream}    | Y   | Y    | N   |
          | @{push}        | Y   | Y    | N   |
          |----------------+-----+------+-----|
      
      The ^{<type>} suffix is not made case-insensitive, because other
      places that take <type> like "cat-file -t <type>" do want them case
      sensitively (after all we never declared that type names are case
      insensitive). Allowing case-insensitive typename only with this syntax
      will make the resulting Git as a whole inconsistent.
      
      This change was independently authored to scratch a longtime itch, but
      when I was about to submit it I discovered that a similar patch had
      been submitted unsuccessfully before by Conrad Irwin in August 2011 as
      "rev-parse: Allow @{U} as a synonym for
      @{u}" (<[email protected]>).
      
      The tests for this patch are more exhaustive than in the 2011
      submission. The starting point for them was to first change the code
      to only support upper-case versions of the existing words, seeing what
      broke, and amending the breaking tests to check upper case & mixed
      case as appropriate, and where not redundant to other similar
      tests. The implementation itself is equivalent.
      Signed-off-by: Ævar Arnfjörð Bjarmason's avatarÆvar Arnfjörð Bjarmason <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      244ea1b5
    • brian m. carlson's avatar
      Convert GIT_SHA1_RAWSZ used for allocation to GIT_MAX_RAWSZ · cd02599c
      brian m. carlson authored
      Since we will likely be introducing a new hash function at some point,
      and that hash function might be longer than 20 bytes, use the constant
      GIT_MAX_RAWSZ, which is designed to be suitable for allocations, instead
      of GIT_SHA1_RAWSZ.  This will ease the transition down the line by
      distinguishing between places where we need to allocate memory suitable
      for the largest hash from those where we need to handle the current
      hash.
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      cd02599c
    • brian m. carlson's avatar
      Convert GIT_SHA1_HEXSZ used for allocation to GIT_MAX_HEXSZ · dc01505f
      brian m. carlson authored
      Since we will likely be introducing a new hash function at some point,
      and that hash function might be longer than 40 hex characters, use the
      constant GIT_MAX_HEXSZ, which is designed to be suitable for
      allocations, instead of GIT_SHA1_HEXSZ.  This will ease the transition
      down the line by distinguishing between places where we need to allocate
      memory suitable for the largest hash from those where we need to handle
      the current hash.
      Signed-off-by: brian m. carlson's avatarbrian m. carlson <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      dc01505f
  20. 02 Mar, 2017 4 commits
    • Jeff King's avatar
      strbuf_check_ref_format(): expand only local branches · 7d5c960b
      Jeff King authored
      This function asks strbuf_branchname() to expand any @-marks
      in the branchname, and then we blindly stick refs/heads/ in
      front of the result. This is obviously nonsense if the
      expansion is "HEAD" or a ref in refs/remotes/.
      
      The most obvious end-user effect is that creating or
      renaming a branch with an expansion may have confusing
      results (e.g., creating refs/heads/origin/master from
      "@{upstream}" when the operation should be disallowed).
      
      We can fix this by telling strbuf_branchname() that we are
      only interested in local expansions. Any unexpanded bits are
      then fed to check_ref_format(), which either disallows them
      (in the case of "@{upstream}") or lets them through
      ("refs/heads/@" is technically valid, if a bit silly).
      Signed-off-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      7d5c960b
    • Jeff King's avatar
      interpret_branch_name: allow callers to restrict expansions · 0e9f62da
      Jeff King authored
      The interpret_branch_name() function converts names like
      @{-1} and @{upstream} into branch names. The expanded ref
      names are not fully qualified, and may be outside of the
      refs/heads/ namespace (e.g., "@" expands to "HEAD", and
      "@{upstream}" is likely to be in "refs/remotes/").
      
      This is OK for callers like dwim_ref() which are primarily
      interested in resolving the resulting name, no matter where
      it is. But callers like "git branch" treat the result as a
      branch name in refs/heads/.  When we expand to a ref outside
      that namespace, the results are very confusing (e.g., "git
      branch @" tries to create refs/heads/HEAD, which is
      nonsense).
      
      Callers can't know from the returned string how the
      expansion happened (e.g., did the user really ask for a
      branch named "HEAD", or did we do a bogus expansion?). One
      fix would be to return some out-parameters describing the
      types of expansion that occurred. This has the benefit that
      the caller can generate precise error messages ("I
      understood @{upstream} to mean origin/master, but that is a
      remote tracking branch, so you cannot create it as a local
      name").
      
      However, out-parameters make the function interface somewhat
      cumbersome. Instead, let's do the opposite: let the caller
      tell us which elements to expand. That's easier to pass in,
      and none of the callers give more precise error messages
      than "@{upstream} isn't a valid branch name" anyway (which
      should be sufficient).
      
      The strbuf_branchname() function needs a similar parameter,
      as most of the callers access interpret_branch_name()
      through it.
      
      We can break the callers down into two groups:
      
        1. Callers that are happy with any kind of ref in the
           result. We pass "0" here, so they continue to work
           without restrictions. This includes merge_name(),
           the reflog handling in add_pending_object_with_path(),
           and substitute_branch_name(). This last is what powers
           dwim_ref().
      
        2. Callers that have funny corner cases (mostly in
           git-branch and git-checkout). These need to make use of
           the new parameter, but I've left them as "0" in this
           patch, and will address them individually in follow-on
           patches.
      Signed-off-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      0e9f62da
    • Jeff King's avatar
      strbuf_branchname: drop return value · 311fc748
      Jeff King authored
      The return value from strbuf_branchname() is confusing and
      useless: it's 0 if the whole name was consumed by an @-mark,
      but otherwise is the length of the original name we fed.
      
      No callers actually look at the return value, so let's just
      get rid of it.
      Signed-off-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      311fc748
    • Jeff King's avatar
      interpret_branch_name: move docstring to header file · e322b60d
      Jeff King authored
      We generally put docstrings with function declarations,
      because it's the callers who need to know how the function
      works. Let's do so for interpret_branch_name().
      Signed-off-by: default avatarJeff King <[email protected]>
      Signed-off-by: default avatarJunio C Hamano <[email protected]>
      e322b60d