--- /dev/null
+Git 2.15 Release Notes
+======================
+
+Backward compatibility notes and other notable changes.
+
+ * Use of an empty string as a pathspec element that is used for
+ 'everything matches' is still warned and Git asks users to use a
+ more explicit '.' for that instead. The hope is that existing
+ users will not mind this change, and eventually the warning can be
+ turned into a hard error, upgrading the deprecation into removal of
+ this (mis)feature. That is now scheduled to happen in the upcoming
+ release.
+
+ * Git now avoids blindly falling back to ".git" when the setup
+ sequence said we are _not_ in Git repository. A corner case that
+ happens to work right now may be broken by a call to die("BUG").
+ We've tried hard to locate such cases and fixed them, but there
+ might still be cases that need to be addressed--bug reports are
+ greatly appreciated.
+
+
+Updates since v2.14
+-------------------
+
+UI, Workflows & Features
+
+ * An example that is now obsolete has been removed from a sample hook,
+ and an old example in it that added a sign-off manually has been
+ improved to use the interpret-trailers command.
+
+ * The advice message given when "git rebase" stops for conflicting
+ changes has been improved.
+
+ * The "rerere-train" script (in contrib/) learned the "--overwrite"
+ option to allow overwriting existing recorded resolutions.
+ (merge ad53bf79aa rg/rerere-train-overwrite later to maint).
+
+ * "git contacts" (in contrib/) now lists the address on the
+ "Reported-by:" trailer to its output, in addition to those on
+ S-o-b: and other trailers, to make it easier to notify (and thank)
+ the original bug reporter.
+ (merge 09ac673788 eb/contacts-reported-by later to maint).
+
+
+Performance, Internal Implementation, Development Support etc.
+
+ * Conversion from uchar[20] to struct object_id continues.
+
+ * Start using selected c99 constructs in small, stable and
+ essentialpart of the system to catch people who care about
+ older compilers that do not grok them.
+
+ * The filter-process interface learned to allow a process with long
+ latency give a "delayed" response.
+
+ * Many uses of comparision callback function the hashmap API uses
+ cast the callback function type when registering it to
+ hashmap_init(), which defeats the compile time type checking when
+ the callback interface changes (e.g. gaining more parameters).
+ The callback implementations have been updated to take "void *"
+ pointers and cast them to the type they expect instead.
+
+ * Because recent Git for Windows do come with a real msgfmt, the
+ build procedure for git-gui has been updated to use it instead of a
+ hand-rolled substitute.
+ (merge 90dbf226ba js/git-gui-msgfmt-on-windows later to maint).
+
+
+Also contains various documentation updates and code clean-ups.
+
+
+Fixes since v2.14
+-----------------
+
+ * "%C(color name)" in the pretty print format always produced ANSI
+ color escape codes, which was an early design mistake. They now
+ honor the configuration (e.g. "color.ui = never") and also tty-ness
+ of the output medium.
+ (merge 11b087adfd jk/ref-filter-colors later to maint).
+
+ * The http.{sslkey,sslCert} configuration variables are to be
+ interpreted as a pathname that honors "~[username]/" prefix, but
+ weren't, which has been fixed.
+ (merge 8d1549643e jc/http-sslkey-and-ssl-cert-are-paths later to maint).
+
+ * Numerous bugs in walking of reflogs via "log -g" and friends have
+ been fixed.
+ (merge de239446b6 jk/reflog-walk later to maint).
+
+ * "git commit" when seeing an totally empty message said "you did not
+ edit the message", which is clearly wrong. The message has been
+ corrected.
+ (merge bc17f35f8c ks/commit-abort-on-empty-message-fix later to maint).
+
+ * When a directory is not readable, "gitweb" fails to build the
+ project list. Work this around by skipping such a directory.
+ (merge 46a13857fc hb/gitweb-project-list later to maint).
+
+ * Some versions of GnuPG fails to kill gpg-agent it auto-spawned
+ and such a left-over agent can interfere with a test. Work it
+ around by attempting to kill one before starting a new test.
+ (merge 29ff1f8f74 st/lib-gpg-kill-stray-agent later to maint).
+
+ * A recently added test for the "credential-cache" helper revealed
+ that EOF detection done around the time the connection to the cache
+ daemon is torn down were flaky. This was fixed by reacting to
+ ECONNRESET and behaving as if we got an EOF.
+ (merge 1f180e5eb9 dl/credential-cache-socket-in-xdg-cache later to maint).
+
+ * "git log --tag=no-such-tag" showed log starting from HEAD, which
+ has been fixed---it now shows nothing.
+ (merge 5d34d1ac06 jk/rev-list-empty-input later to maint).
+
+ * The "tag.pager" configuration variable was useless for those who
+ actually create tag objects, as it interfered with the use of an
+ editor. A new mechanism has been introduced for commands to enable
+ pager depending on what operation is being carried out to fix this,
+ and then "git tag -l" is made to run pager by default.
+ (merge 595d59e2b5 ma/pager-per-subcommand-action later to maint).
+
+ * Other minor doc, test and build updates and code cleanups.
+ (merge 5b114f3bb0 rs/bswap-ubsan-fix later to maint).
+ (merge 168e63554c rs/move-array later to maint).
+ (merge 268ba20110 rs/stat-data-unaligned-reads-fix later to maint).
+ (merge 78e7b98f45 jt/fsck-code-cleanup later to maint).
+ (merge c7b0780545 rs/pack-objects-pbase-cleanup later to maint).
+ (merge c1e860f1dc js/run-process-parallel-api-fix later to maint).
+ (merge 7a40a95eb4 cc/ref-is-hidden-microcleanup later to maint).
+ (merge c0bb6d9cef ah/doc-wserrorhighlight later to maint).
+ (merge edd64ef4f7 dc/fmt-merge-msg-microcleanup later to maint).
+ (merge fa64a2fdbe jt/subprocess-handshake later to maint).
+ (merge 0ba9c9a0fb jb/t8008-cleanup later to maint).
+ (merge a7c28a2161 jt/t1450-fsck-corrupt-packfile later to maint).
synonyms are accepted for 'true' and 'false'; these are all
case-insensitive.
- true;; Boolean true can be spelled as `yes`, `on`, `true`,
- or `1`. Also, a variable defined without `= <value>`
+ true;; Boolean true literals are `yes`, `on`, `true`,
+ and `1`. Also, a variable defined without `= <value>`
is taken as true.
- false;; Boolean false can be spelled as `no`, `off`,
- `false`, or `0`.
+ false;; Boolean false literals are `no`, `off`, `false`,
+ `0` and the empty string.
+
When converting value to the canonical form using `--bool` type
-specifier; 'git config' will ensure that the output is "true" or
+specifier, 'git config' will ensure that the output is "true" or
"false" (spelled in lowercase).
integer::
'git-diff-{asterisk}' plumbing commands. Can be overridden on the
command line with the `--color[=<when>]` option.
+diff.colorMoved::
+ If set to either a valid `<mode>` or a true value, moved lines
+ in a diff are colored differently, for details of valid modes
+ see '--color-moved' in linkgit:git-diff[1]. If simply set to
+ true the default color mode will be used. When set to false,
+ moved lines are not colored.
+
color.diff.<slot>::
Use customized color for diff colorization. `<slot>` specifies
which part of the patch to use the specified color, and is one
of `context` (context text - `plain` is a historical synonym),
`meta` (metainformation), `frag`
(hunk header), 'func' (function in hunk header), `old` (removed lines),
- `new` (added lines), `commit` (commit headers), or `whitespace`
- (highlighting whitespace errors).
+ `new` (added lines), `commit` (commit headers), `whitespace`
+ (highlighting whitespace errors), `oldMoved` (deleted lines),
+ `newMoved` (added lines), `oldMovedDimmed`, `oldMovedAlternative`,
+ `oldMovedAlternativeDimmed`, `newMovedDimmed`, `newMovedAlternative`
+ and `newMovedAlternativeDimmed` (See the '<mode>'
+ setting of '--color-moved' in linkgit:git-diff[1] for details).
color.decorate.<slot>::
Use customized color for 'git log --decorate' output. `<slot>` is one
+
diff.wsErrorHighlight::
- A comma separated list of `old`, `new`, `context`, that
- specifies how whitespace errors on lines are highlighted
- with `color.diff.whitespace`. Can be overridden by the
- command line option `--ws-error-highlight=<kind>`
+ Highlight whitespace errors in the `context`, `old` or `new`
+ lines of the diff. Multiple values are separated by comma,
+ `none` resets previous values, `default` reset the list to
+ `new` and `all` is a shorthand for `old,new,context`. The
+ whitespace errors are colored with `color.diff.whitespace`.
+ The command line option `--ws-error-highlight=<kind>`
+ overrides this setting.
endif::git-diff[]
It is the same as `--color=never`.
+--color-moved[=<mode>]::
+ Moved lines of code are colored differently.
+ifdef::git-diff[]
+ It can be changed by the `diff.colorMoved` configuration setting.
+endif::git-diff[]
+ The <mode> defaults to 'no' if the option is not given
+ and to 'zebra' if the option with no mode is given.
+ The mode must be one of:
++
+--
+no::
+ Moved lines are not highlighted.
+default::
+ Is a synonym for `zebra`. This may change to a more sensible mode
+ in the future.
+plain::
+ Any line that is added in one location and was removed
+ in another location will be colored with 'color.diff.newMoved'.
+ Similarly 'color.diff.oldMoved' will be used for removed lines
+ that are added somewhere else in the diff. This mode picks up any
+ moved line, but it is not very useful in a review to determine
+ if a block of code was moved without permutation.
+zebra::
+ Blocks of moved code are detected greedily. The detected blocks are
+ painted using either the 'color.diff.{old,new}Moved' color or
+ 'color.diff.{old,new}MovedAlternative'. The change between
+ the two colors indicates that a new block was detected. If there
+ are fewer than 3 adjacent moved lines, they are not marked up
+ as moved, but the regular colors 'color.diff.{old,new}' will be
+ used.
+dimmed_zebra::
+ Similar to 'zebra', but additional dimming of uninteresting parts
+ of moved code is performed. The bordering lines of two adjacent
+ blocks are considered interesting, the rest is uninteresting.
+--
+
--word-diff[=<mode>]::
Show a word diff, using the <mode> to delimit changed words.
By default, words are delimited by whitespace; see
with --exit-code.
--ws-error-highlight=<kind>::
- Highlight whitespace errors on lines specified by <kind>
- in the color specified by `color.diff.whitespace`. <kind>
- is a comma separated list of `old`, `new`, `context`. When
- this option is not given, only whitespace errors in `new`
- lines are highlighted. E.g. `--ws-error-highlight=new,old`
- highlights whitespace errors on both deleted and added lines.
- `all` can be used as a short-hand for `old,new,context`.
- The `diff.wsErrorHighlight` configuration variable can be
- used to specify the default behaviour.
+ Highlight whitespace errors in the `context`, `old` or `new`
+ lines of the diff. Multiple values are separated by comma,
+ `none` resets previous values, `default` reset the list to
+ `new` and `all` is a shorthand for `old,new,context`. When
+ this option is not given, and the configuration variable
+ `diff.wsErrorHighlight` is not set, only whitespace errors in
+ `new` lines are highlighted. The whitespace errors are colored
+ whith `color.diff.whitespace`.
endif::git-format-patch[]
otherwise.
color::
- Change output color. Followed by `:<colorname>`, where names
- are described in `color.branch.*`.
+ Change output color. Followed by `:<colorname>`, where color
+ names are described under Values in the "CONFIGURATION FILE"
+ section of linkgit:git-config[1]. For example,
+ `%(color:bold red)`.
align::
Left-, middle-, or right-align the content between
[(--reroll-count|-v) <n>]
[--to=<email>] [--cc=<email>]
[--[no-]cover-letter] [--quiet] [--notes[=<ref>]]
+ [--progress]
[<common diff options>]
[ <since> | <revision range> ]
range are always formatted as creation patches, independently
of this flag.
+--progress::
+ Show progress reports on stderr as patches are generated.
+
CONFIGURATION
-------------
You can specify extra mail header lines to be added to each message,
<tree> option the prefix of all submodule output will be the name of
the parent project's <tree> object.
---parent-basename <basename>::
- For internal use only. In order to produce uniform output with the
- --recurse-submodules option, this option can be used to provide the
- basename of a parent's <tree> object to a submodule so the submodule
- can prefix its output with the parent's name rather than the SHA1 of
- the submodule.
-
-a::
--text::
Process binary files as if they were text.
-------
include::merge-options.txt[]
+--signoff::
+ Add Signed-off-by line by the committer at the end of the commit
+ log message. The meaning of a signoff depends on the project,
+ but it typically certifies that committer has
+ the rights to submit this work under the same license and
+ agrees to a Developer Certificate of Origin
+ (see http://developercertificate.org/ for more information).
+
-S[<keyid>]::
--gpg-sign[=<keyid>]::
GPG-sign the resulting merge commit. The `keyid` argument is
'git push' [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
[--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
[-u | --set-upstream] [--push-option=<string>]
- [--[no-]signed|--sign=(true|false|if-asked)]
+ [--[no-]signed|--signed=(true|false|if-asked)]
[--force-with-lease[=<refname>[:<expect>]]]
[--no-verify] [<repository> [<refspec>...]]
information, see `push.followTags` in linkgit:git-config[1].
--[no-]signed::
---sign=(true|false|if-asked)::
+--signed=(true|false|if-asked)::
GPG-sign the push request to update refs on the receiving
side, to allow it to be checked by the hooks and/or be
logged. If `false` or `--no-signed`, no signing will be
[verse]
'git send-pack' [--all] [--dry-run] [--force] [--receive-pack=<git-receive-pack>]
[--verbose] [--thin] [--atomic]
- [--[no-]signed|--sign=(true|false|if-asked)]
+ [--[no-]signed|--signed=(true|false|if-asked)]
[<host>:]<directory> [<ref>...]
DESCRIPTION
refs.
--[no-]signed::
---sign=(true|false|if-asked)::
+--signed=(true|false|if-asked)::
GPG-sign the push request to update refs on the receiving
side, to allow it to be checked by the hooks and/or be
logged. If `false` or `--no-signed`, no signing will be
signingKey = <gpg-keyid>
-------------------------------------
+`pager.tag` is only respected when listing tags, i.e., when `-l` is
+used or implied. The default is to use a pager.
+See linkgit:git-config[1].
DISCUSSION
----------
Note that omitting the `=` in `git -c foo.bar ...` is allowed and sets
`foo.bar` to the boolean true value (just like `[foo]bar` would in a
config file). Including the equals but with an empty value (like `git -c
-foo.bar= ...`) sets `foo.bar` to the empty string.
+foo.bar= ...`) sets `foo.bar` to the empty string which ` git config
+--bool` will convert to `false`.
--exec-path[=<path>]::
Path to wherever your core Git programs are installed.
packet: git< capability=smudge
packet: git< 0000
------------------------
-Supported filter capabilities in version 2 are "clean" and
-"smudge".
+Supported filter capabilities in version 2 are "clean", "smudge",
+and "delay".
Afterwards Git sends a list of "key=value" pairs terminated with
a flush packet. The list will contain at least the filter command
with the next file that needs to be processed. Depending on the
`filter.<driver>.required` flag Git will interpret that as error.
-After the filter has processed a blob it is expected to wait for
-the next "key=value" list containing a command. Git will close
+After the filter has processed a command it is expected to wait for
+a "key=value" list containing the next command. Git will close
the command pipe on exit. The filter is expected to detect EOF
and exit gracefully on its own. Git will wait until the filter
process has stopped.
+Delay
+^^^^^
+
+If the filter supports the "delay" capability, then Git can send the
+flag "can-delay" after the filter command and pathname. This flag
+denotes that the filter can delay filtering the current blob (e.g. to
+compensate network latencies) by responding with no content but with
+the status "delayed" and a flush packet.
+------------------------
+packet: git> command=smudge
+packet: git> pathname=path/testfile.dat
+packet: git> can-delay=1
+packet: git> 0000
+packet: git> CONTENT
+packet: git> 0000
+packet: git< status=delayed
+packet: git< 0000
+------------------------
+
+If the filter supports the "delay" capability then it must support the
+"list_available_blobs" command. If Git sends this command, then the
+filter is expected to return a list of pathnames representing blobs
+that have been delayed earlier and are now available.
+The list must be terminated with a flush packet followed
+by a "success" status that is also terminated with a flush packet. If
+no blobs for the delayed paths are available, yet, then the filter is
+expected to block the response until at least one blob becomes
+available. The filter can tell Git that it has no more delayed blobs
+by sending an empty list. As soon as the filter responds with an empty
+list, Git stops asking. All blobs that Git has not received at this
+point are considered missing and will result in an error.
+
+------------------------
+packet: git> command=list_available_blobs
+packet: git> 0000
+packet: git< pathname=path/testfile.dat
+packet: git< pathname=path/otherfile.dat
+packet: git< 0000
+packet: git< status=success
+packet: git< 0000
+------------------------
+
+After Git received the pathnames, it will request the corresponding
+blobs again. These requests contain a pathname and an empty content
+section. The filter is expected to respond with the smudged content
+in the usual way as explained above.
+------------------------
+packet: git> command=smudge
+packet: git> pathname=path/testfile.dat
+packet: git> 0000
+packet: git> 0000 # empty content!
+packet: git< status=success
+packet: git< 0000
+packet: git< SMUDGED_CONTENT
+packet: git< 0000
+packet: git< 0000 # empty list, keep "status=success" unchanged!
+------------------------
+
+Example
+^^^^^^^
+
A long running filter demo implementation can be found in
`contrib/long-running-filter/example.pl` located in the Git
core repository. If you develop your own long running filter
means a failure of the hook and aborts the commit. It should not
be used as replacement for pre-commit hook.
-The sample `prepare-commit-msg` hook that comes with Git comments
-out the `Conflicts:` part of a merge's commit message.
+The sample `prepare-commit-msg` hook that comes with Git removes the
+help message found in the commented portion of the commit template.
commit-msg
~~~~~~~~~~
- '%Cblue': switch color to blue
- '%Creset': reset color
- '%C(...)': color specification, as described under Values in the
- "CONFIGURATION FILE" section of linkgit:git-config[1];
- adding `auto,` at the beginning (e.g. `%C(auto,red)`) will emit
- color only when colors are enabled for log output (by `color.diff`,
- `color.ui`, or `--color`, and respecting the `auto` settings of the
- former if we are going to a terminal). `auto` alone (i.e.
- `%C(auto)`) will turn on auto coloring on the next placeholders
- until the color is switched again.
+ "CONFIGURATION FILE" section of linkgit:git-config[1].
+ By default, colors are shown only when enabled for log output (by
+ `color.diff`, `color.ui`, or `--color`, and respecting the `auto`
+ settings of the former if we are going to a terminal). `%C(auto,...)`
+ is accepted as a historical synonym for the default (e.g.,
+ `%C(auto,red)`). Specifying `%C(always,...) will show the colors
+ even when color is not otherwise enabled (though consider
+ just using `--color=always` to enable color for the whole output,
+ including this format and anything else git might color). `auto`
+ alone (i.e. `%C(auto)`) will turn on auto coloring on the next
+ placeholders until the color is switched again.
- '%m': left (`<`), right (`>`) or boundary (`-`) mark
- '%n': newline
- '%%': a raw '%'
+++ /dev/null
-builtin API
-===========
-
-Adding a new built-in
----------------------
-
-There are 4 things to do to add a built-in command implementation to
-Git:
-
-. Define the implementation of the built-in command `foo` with
- signature:
-
- int cmd_foo(int argc, const char **argv, const char *prefix);
-
-. Add the external declaration for the function to `builtin.h`.
-
-. Add the command to the `commands[]` table defined in `git.c`.
- The entry should look like:
-
- { "foo", cmd_foo, <options> },
-+
-where options is the bitwise-or of:
-
-`RUN_SETUP`::
- If there is not a Git directory to work on, abort. If there
- is a work tree, chdir to the top of it if the command was
- invoked in a subdirectory. If there is no work tree, no
- chdir() is done.
-
-`RUN_SETUP_GENTLY`::
- If there is a Git directory, chdir as per RUN_SETUP, otherwise,
- don't chdir anywhere.
-
-`USE_PAGER`::
-
- If the standard output is connected to a tty, spawn a pager and
- feed our output to it.
-
-`NEED_WORK_TREE`::
-
- Make sure there is a work tree, i.e. the command cannot act
- on bare repositories.
- This only makes sense when `RUN_SETUP` is also set.
-
-. Add `builtin/foo.o` to `BUILTIN_OBJS` in `Makefile`.
-
-Additionally, if `foo` is a new command, there are 3 more things to do:
-
-. Add tests to `t/` directory.
-
-. Write documentation in `Documentation/git-foo.txt`.
-
-. Add an entry for `git-foo` to `command-list.txt`.
-
-. Add an entry for `/git-foo` to `.gitignore`.
-
-
-How a built-in is called
-------------------------
-
-The implementation `cmd_foo()` takes three parameters, `argc`, `argv,
-and `prefix`. The first two are similar to what `main()` of a
-standalone command would be called with.
-
-When `RUN_SETUP` is specified in the `commands[]` table, and when you
-were started from a subdirectory of the work tree, `cmd_foo()` is called
-after chdir(2) to the top of the work tree, and `prefix` gets the path
-to the subdirectory the command started from. This allows you to
-convert a user-supplied pathname (typically relative to that directory)
-to a pathname relative to the top of the work tree.
-
-The return value from `cmd_foo()` becomes the exit status of the
-command.
an `is_bool` flag is unset.
`git_config_maybe_bool`::
+Deprecated. Use `git_parse_maybe_bool` instead. They are exactly the
+same, except this function takes an unused argument `name`.
+
+`git_parse_maybe_bool`::
Same as `git_config_bool`, except that it returns -1 on error rather
than dying.
+++ /dev/null
-sub-process API
-===============
-
-The sub-process API makes it possible to run background sub-processes
-for the entire lifetime of a Git invocation. If Git needs to communicate
-with an external process multiple times, then this can reduces the process
-invocation overhead. Git and the sub-process communicate through stdin and
-stdout.
-
-The sub-processes are kept in a hashmap by command name and looked up
-via the subprocess_find_entry function. If an existing instance can not
-be found then a new process should be created and started. When the
-parent git command terminates, all sub-processes are also terminated.
-
-This API is based on the run-command API.
-
-Data structures
----------------
-
-* `struct subprocess_entry`
-
-The sub-process structure. Members should not be accessed directly.
-
-Types
------
-
-'int(*subprocess_start_fn)(struct subprocess_entry *entry)'::
-
- User-supplied function to initialize the sub-process. This is
- typically used to negotiate the interface version and capabilities.
-
-
-Functions
----------
-
-`cmd2process_cmp`::
-
- Function to test two subprocess hashmap entries for equality.
-
-`subprocess_start`::
-
- Start a subprocess and add it to the subprocess hashmap.
-
-`subprocess_stop`::
-
- Kill a subprocess and remove it from the subprocess hashmap.
-
-`subprocess_find_entry`::
-
- Find a subprocess in the subprocess hashmap.
-
-`subprocess_get_child_process`::
-
- Get the underlying `struct child_process` from a subprocess.
-
-`subprocess_read_status`::
-
- Helper function to read packets looking for the last "status=<foo>"
- key/value pair.
`fill_tree_descriptor`::
- Initialize a `tree_desc` and decode its first entry given the sha1 of
- a tree. Returns the `buffer` member if the sha1 is a valid tree
- identifier and NULL otherwise.
+ Initialize a `tree_desc` and decode its first entry given the
+ object ID of a tree. Returns the `buffer` member if the latter
+ is a valid tree identifier and NULL otherwise.
`setup_traverse_info`::
#!/bin/sh
GVF=GIT-VERSION-FILE
-DEF_VER=v2.14.1
+DEF_VER=v2.14.GIT
LF='
'
LIB_OBJS += refs.o
LIB_OBJS += refs/files-backend.o
LIB_OBJS += refs/iterator.o
+LIB_OBJS += refs/packed-backend.o
LIB_OBJS += refs/ref-cache.o
LIB_OBJS += ref-filter.o
LIB_OBJS += remote.o
-Documentation/RelNotes/2.14.1.txt
\ No newline at end of file
+Documentation/RelNotes/2.15.0.txt
\ No newline at end of file
{
memset(state, 0, sizeof(*state));
state->prefix = prefix;
- state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
state->lock_file = lock_file;
state->newfd = -1;
state->apply = 1;
* Does it begin with "a/$our-prefix" and such? Then this is
* very likely to apply to our directory.
*/
- if (!strncmp(name, state->prefix, state->prefix_length))
+ if (starts_with(name, state->prefix))
val = count_slashes(state->prefix);
else {
cp++;
- if (!strncmp(cp, state->prefix, state->prefix_length))
+ if (starts_with(cp, state->prefix))
val = count_slashes(state->prefix) + 1;
}
}
int i;
/* Paths outside are not touched regardless of "--include" */
- if (0 < state->prefix_length) {
- int pathlen = strlen(pathname);
- if (pathlen <= state->prefix_length ||
- memcmp(state->prefix, pathname, state->prefix_length))
+ if (state->prefix && *state->prefix) {
+ const char *rest;
+ if (!skip_prefix(pathname, state->prefix, &rest) || !*rest)
return 0;
}
img->line_allocated = img->line;
}
if (preimage_limit != postimage->nr)
- memmove(img->line + applied_pos + postimage->nr,
- img->line + applied_pos + preimage_limit,
- (img->nr - (applied_pos + preimage_limit)) *
- sizeof(*img->line));
- memcpy(img->line + applied_pos,
- postimage->line,
- postimage->nr * sizeof(*img->line));
+ MOVE_ARRAY(img->line + applied_pos + postimage->nr,
+ img->line + applied_pos + preimage_limit,
+ img->nr - (applied_pos + preimage_limit));
+ COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->nr);
if (!state->allow_overlap)
for (i = 0; i < postimage->nr; i++)
img->line[applied_pos + i].flag |= LINE_PATCHED;
/* Preimage the patch was prepared for */
if (patch->is_new)
write_sha1_file("", 0, blob_type, pre_oid.hash);
- else if (get_sha1(patch->old_sha1_prefix, pre_oid.hash) ||
+ else if (get_oid(patch->old_sha1_prefix, &pre_oid) ||
read_blob_object(&buf, &pre_oid, patch->old_mode))
return error(_("repository lacks the necessary blob to fall back on 3-way merge."));
else
return error(_("sha1 information is lacking or "
"useless for submodule %s"), name);
- } else if (!get_sha1_blob(patch->old_sha1_prefix, oid.hash)) {
+ } else if (!get_oid_blob(patch->old_sha1_prefix, &oid)) {
; /* ok */
} else if (!patch->lines_added && !patch->lines_deleted) {
/* mode-only change: update the current */
struct apply_state {
const char *prefix;
- int prefix_length;
/* These are lock_file related */
struct lock_file *lock_file;
free(ref);
}
- if (get_sha1(name, oid.hash))
+ if (get_oid(name, &oid))
die("Not a valid object name");
commit = lookup_commit_reference_gently(&oid, 1);
};
/* attr_hashmap comparison function */
-static int attr_hash_entry_cmp(void *unused_cmp_data,
- const struct attr_hash_entry *a,
- const struct attr_hash_entry *b,
- void *unused_keydata)
+static int attr_hash_entry_cmp(const void *unused_cmp_data,
+ const void *entry,
+ const void *entry_or_key,
+ const void *unused_keydata)
{
+ const struct attr_hash_entry *a = entry;
+ const struct attr_hash_entry *b = entry_or_key;
return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
}
/* Initialize an 'attr_hashmap' object */
static void attr_hashmap_init(struct attr_hashmap *map)
{
- hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
+ hashmap_init(&map->map, attr_hash_entry_cmp, NULL, 0);
}
/*
return res;
}
-static int bisect_checkout(const unsigned char *bisect_rev, int no_checkout)
+static int bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
{
char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
- memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1);
- update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+ memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1);
+ update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev->hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
argv_checkout[2] = bisect_rev_hex;
if (no_checkout) {
- update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+ update_ref(NULL, "BISECT_HEAD", bisect_rev->hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
} else {
int res;
res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
handle_skipped_merge_base(mb);
} else {
printf(_("Bisecting: a merge base must be tested\n"));
- exit(bisect_checkout(mb->hash, no_checkout));
+ exit(bisect_checkout(mb, no_checkout));
}
}
struct rev_info revs;
struct commit_list *tried;
int reaches = 0, all = 0, nr, steps;
- const unsigned char *bisect_rev;
+ struct object_id *bisect_rev;
char *steps_msg;
read_bisect_terms(&term_bad, &term_good);
exit(4);
}
- bisect_rev = revs.commits->item->object.oid.hash;
+ bisect_rev = &revs.commits->item->object.oid;
- if (!hashcmp(bisect_rev, current_bad_oid->hash)) {
+ if (!oidcmp(bisect_rev, current_bad_oid)) {
exit_if_skipped_commits(tried, current_bad_oid);
- printf("%s is the first %s commit\n", sha1_to_hex(bisect_rev),
+ printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
term_bad);
show_diff_tree(prefix, revs.commits->item);
/* This means the bisection process succeeded. */
#include "cache.h"
#include "commit.h"
+/*
+ * builtin API
+ * ===========
+ *
+ * Adding a new built-in
+ * ---------------------
+ *
+ * There are 4 things to do to add a built-in command implementation to
+ * Git:
+ *
+ * . Define the implementation of the built-in command `foo` with
+ * signature:
+ *
+ * int cmd_foo(int argc, const char **argv, const char *prefix);
+ *
+ * . Add the external declaration for the function to `builtin.h`.
+ *
+ * . Add the command to the `commands[]` table defined in `git.c`.
+ * The entry should look like:
+ *
+ * { "foo", cmd_foo, <options> },
+ *
+ * where options is the bitwise-or of:
+ *
+ * `RUN_SETUP`:
+ * If there is not a Git directory to work on, abort. If there
+ * is a work tree, chdir to the top of it if the command was
+ * invoked in a subdirectory. If there is no work tree, no
+ * chdir() is done.
+ *
+ * `RUN_SETUP_GENTLY`:
+ * If there is a Git directory, chdir as per RUN_SETUP, otherwise,
+ * don't chdir anywhere.
+ *
+ * `USE_PAGER`:
+ *
+ * If the standard output is connected to a tty, spawn a pager and
+ * feed our output to it.
+ *
+ * `NEED_WORK_TREE`:
+ *
+ * Make sure there is a work tree, i.e. the command cannot act
+ * on bare repositories.
+ * This only makes sense when `RUN_SETUP` is also set.
+ *
+ * `SUPPORT_SUPER_PREFIX`:
+ *
+ * The built-in supports `--super-prefix`.
+ *
+ * `DELAY_PAGER_CONFIG`:
+ *
+ * If RUN_SETUP or RUN_SETUP_GENTLY is set, git.c normally handles
+ * the `pager.<cmd>`-configuration. If this flag is used, git.c
+ * will skip that step, instead allowing the built-in to make a
+ * more informed decision, e.g., by ignoring `pager.<cmd>` for
+ * certain subcommands.
+ *
+ * . Add `builtin/foo.o` to `BUILTIN_OBJS` in `Makefile`.
+ *
+ * Additionally, if `foo` is a new command, there are 4 more things to do:
+ *
+ * . Add tests to `t/` directory.
+ *
+ * . Write documentation in `Documentation/git-foo.txt`.
+ *
+ * . Add an entry for `git-foo` to `command-list.txt`.
+ *
+ * . Add an entry for `/git-foo` to `.gitignore`.
+ *
+ *
+ * How a built-in is called
+ * ------------------------
+ *
+ * The implementation `cmd_foo()` takes three parameters, `argc`, `argv,
+ * and `prefix`. The first two are similar to what `main()` of a
+ * standalone command would be called with.
+ *
+ * When `RUN_SETUP` is specified in the `commands[]` table, and when you
+ * were started from a subdirectory of the work tree, `cmd_foo()` is called
+ * after chdir(2) to the top of the work tree, and `prefix` gets the path
+ * to the subdirectory the command started from. This allows you to
+ * convert a user-supplied pathname (typically relative to that directory)
+ * to a pathname relative to the top of the work tree.
+ *
+ * The return value from `cmd_foo()` becomes the exit status of the
+ * command.
+ */
+
#define DEFAULT_MERGE_LOG_LEN 20
extern const char git_usage_string[];
extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
struct fmt_merge_msg_opts *);
+/**
+ * If a built-in has DELAY_PAGER_CONFIG set, the built-in should call this early
+ * when it wishes to respect the `pager.foo`-config. The `cmd` is the name of
+ * the built-in, e.g., "foo". If a paging-choice has already been setup, this
+ * does nothing. The default in `def` should be 0 for "pager off", 1 for "pager
+ * on" or -1 for "punt".
+ *
+ * You should most likely use a default of 0 or 1. "Punt" (-1) could be useful
+ * to be able to fall back to some historical compatibility name.
+ */
+extern void setup_auto_pager(const char *cmd, int def);
+
extern int is_builtin(const char *s);
extern int cmd_add(int argc, const char **argv, const char *prefix);
int add_errors;
};
-static void chmod_pathspec(struct pathspec *pathspec, int force_mode)
+static void chmod_pathspec(struct pathspec *pathspec, char flip)
{
int i;
if (pathspec && !ce_path_match(ce, pathspec, NULL))
continue;
- if (chmod_cache_entry(ce, force_mode) < 0)
- fprintf(stderr, "cannot chmod '%s'", ce->name);
+ if (chmod_cache_entry(ce, flip) < 0)
+ fprintf(stderr, "cannot chmod %cx '%s'\n", flip, ce->name);
}
}
read_state_file(&sb, state, "utf8", 1);
state->utf8 = !strcmp(sb.buf, "t");
+ if (file_exists(am_path(state, "rerere-autoupdate"))) {
+ read_state_file(&sb, state, "rerere-autoupdate", 1);
+ state->allow_rerere_autoupdate = strcmp(sb.buf, "t") ?
+ RERERE_NOAUTOUPDATE : RERERE_AUTOUPDATE;
+ } else {
+ state->allow_rerere_autoupdate = 0;
+ }
+
read_state_file(&sb, state, "keep", 1);
if (!strcmp(sb.buf, "t"))
state->keep = KEEP_TRUE;
write_state_bool(state, "sign", state->signoff);
write_state_bool(state, "utf8", state->utf8);
+ if (state->allow_rerere_autoupdate)
+ write_state_bool(state, "rerere-autoupdate",
+ state->allow_rerere_autoupdate == RERERE_AUTOUPDATE);
+
switch (state->keep) {
case KEEP_FALSE:
str = "f";
struct object_id head;
int i;
- if (!get_sha1_tree("HEAD", head.hash)) {
+ if (!get_oid_tree("HEAD", &head)) {
struct diff_options opt;
diff_setup(&opt);
*/
static void am_append_signoff(struct am_state *state)
{
- char *cp;
- struct strbuf mine = STRBUF_INIT;
struct strbuf sb = STRBUF_INIT;
strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len);
-
- /* our sign-off */
- strbuf_addf(&mine, "\n%s%s\n",
- sign_off_header,
- fmt_name(getenv("GIT_COMMITTER_NAME"),
- getenv("GIT_COMMITTER_EMAIL")));
-
- /* Does sb end with it already? */
- if (mine.len < sb.len &&
- !strcmp(mine.buf, sb.buf + sb.len - mine.len))
- goto exit; /* no need to duplicate */
-
- /* Does it have any Signed-off-by: in the text */
- for (cp = sb.buf;
- cp && *cp && (cp = strstr(cp, sign_off_header)) != NULL;
- cp = strchr(cp, '\n')) {
- if (sb.buf == cp || cp[-1] == '\n')
- break;
- }
-
- strbuf_addstr(&sb, mine.buf + !!cp);
-exit:
- strbuf_release(&mine);
+ append_signoff(&sb, 0, 0);
state->msg = strbuf_detach(&sb, &state->msg_len);
}
struct rev_info rev_info;
FILE *fp;
- if (!get_sha1_tree("HEAD", head.hash))
+ if (!get_oid_tree("HEAD", &head))
tree = lookup_tree(&head);
else
tree = lookup_tree(&empty_tree_oid);
if (write_cache_as_tree(tree.hash, 0, NULL))
die(_("git write-tree failed to write a tree"));
- if (!get_sha1_commit("HEAD", parent.hash)) {
+ if (!get_oid_commit("HEAD", &parent)) {
old_oid = &parent;
commit_list_insert(lookup_commit(&parent), &parents);
} else {
return config_error_nonbool(var);
return color_parse(value, branch_colors[slot]);
}
- return git_color_default_config(var, value, cb);
+ return git_default_config(var, value, cb);
}
static const char *branch_get_color(enum color_branch ix)
return strbuf_detach(&fmt, NULL);
}
-static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
+static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
{
int i;
struct ref_array array;
if (filter->verbose)
maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
- if (!format)
- format = to_free = build_format(filter, maxwidth, remote_prefix);
- verify_ref_format(format);
+ if (!format->format)
+ format->format = to_free = build_format(filter, maxwidth, remote_prefix);
+ format->use_color = branch_use_color;
+
+ if (verify_ref_format(format))
+ die(_("unable to parse format string"));
ref_array_sort(sorting, &array);
for (i = 0; i < array.nr; i++) {
- format_ref_array_item(array.items[i], format, 0, &out);
+ format_ref_array_item(array.items[i], format, &out);
if (column_active(colopts)) {
assert(!filter->verbose && "--column and --verbose are incompatible");
/* format to a string_list to let print_columns() do its job */
struct ref_filter filter;
int icase = 0;
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
- const char *format = NULL;
+ struct ref_format format = REF_FORMAT_INIT;
struct option options[] = {
OPT_GROUP(N_("Generic options")),
N_("print only branches of the object"), 0, parse_opt_object_name
},
OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
- OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
+ OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
OPT_END(),
};
if (!sorting)
sorting = ref_default_sorting();
sorting->ignore_case = icase;
- print_ref_list(&filter, sorting, format);
+ print_ref_list(&filter, sorting, &format);
print_columns(&output, colopts, NULL);
string_list_clear(&output, 0);
return 0;
if (unknown_type)
flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
- if (get_sha1_with_context(obj_name, GET_SHA1_RECORD_PATH,
- oid.hash, &obj_context))
+ if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
+ &oid, &obj_context))
die("Not a valid object name %s", obj_name);
if (!path)
struct expand_data *data)
{
struct object_context ctx;
- int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
+ int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
enum follow_symlinks_result result;
- result = get_sha1_with_context(obj_name, flags, data->oid.hash, &ctx);
+ result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
if (result != FOUND) {
switch (result) {
case MISSING_OBJECT:
state.force = 1;
state.refresh_cache = 1;
state.istate = &the_index;
+
+ enable_delayed_checkout(&state);
for (pos = 0; pos < active_nr; pos++) {
struct cache_entry *ce = active_cache[pos];
if (ce->ce_flags & CE_MATCHED) {
pos = skip_same_name(ce, pos) - 1;
}
}
+ errs |= finish_delayed_checkout(&state);
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die(_("unable to write new index file"));
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
static const char *msg_warn_remove_failed = N_("failed to remove %s");
-static int clean_use_color = -1;
-static char clean_colors[][COLOR_MAXLEN] = {
- GIT_COLOR_RESET,
- GIT_COLOR_NORMAL, /* PLAIN */
- GIT_COLOR_BOLD_BLUE, /* PROMPT */
- GIT_COLOR_BOLD, /* HEADER */
- GIT_COLOR_BOLD_RED, /* HELP */
- GIT_COLOR_BOLD_RED, /* ERROR */
-};
enum color_clean {
CLEAN_COLOR_RESET = 0,
CLEAN_COLOR_PLAIN = 1,
CLEAN_COLOR_ERROR = 5
};
+static int clean_use_color = -1;
+static char clean_colors[][COLOR_MAXLEN] = {
+ [CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
+ [CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,
+ [CLEAN_COLOR_HELP] = GIT_COLOR_BOLD_RED,
+ [CLEAN_COLOR_PLAIN] = GIT_COLOR_NORMAL,
+ [CLEAN_COLOR_PROMPT] = GIT_COLOR_BOLD_BLUE,
+ [CLEAN_COLOR_RESET] = GIT_COLOR_RESET,
+};
+
#define MENU_OPTS_SINGLETON 01
#define MENU_OPTS_IMMEDIATE 02
#define MENU_OPTS_LIST_ONLY 04
return 0;
}
- /* inspect the color.ui config variable and others */
- return git_color_default_config(var, value, cb);
+ return git_default_config(var, value, cb);
}
static const char *clean_get_color(enum color_clean ix)
if (submodule_progress)
argv_array_push(&args, "--progress");
+ if (option_verbosity < 0)
+ argv_array_push(&args, "--quiet");
+
err = run_command_v_opt(args.argv, RUN_GIT_CMD);
argv_array_clear(&args);
}
struct object_id oid;
if (argc <= ++i)
usage(commit_tree_usage);
- if (get_sha1_commit(argv[i], oid.hash))
+ if (get_oid_commit(argv[i], &oid))
die("Not a valid object name %s", argv[i]);
assert_sha1_type(oid.hash, OBJ_COMMIT);
new_parent(lookup_commit(&oid), &parents);
continue;
}
- if (get_sha1_tree(arg, tree_oid.hash))
+ if (get_oid_tree(arg, &tree_oid))
die("Not a valid object name %s", arg);
if (got_tree)
die("Cannot give more than one trees");
s->index_file = index_file;
s->fp = fp;
s->nowarn = nowarn;
- s->is_initial = get_sha1(s->reference, oid.hash) ? 1 : 0;
+ s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
if (!s->is_initial)
hashcpy(s->sha1_commit, oid.hash);
s->status_format = status_format;
if (amend)
parent = "HEAD^1";
- if (get_sha1(parent, oid.hash)) {
+ if (get_oid(parent, &oid)) {
int i, ita_nr = 0;
for (i = 0; i < active_nr; i++)
fd = hold_locked_index(&index_lock, 0);
- s.is_initial = get_sha1(s.reference, oid.hash) ? 1 : 0;
+ s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
if (!s.is_initial)
hashcpy(s.sha1_commit, oid.hash);
status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
s.colopts = 0;
- if (get_sha1("HEAD", oid.hash))
+ if (get_oid("HEAD", &oid))
current_head = NULL;
else {
current_head = lookup_commit_or_die(&oid, "HEAD");
if (verbose || /* Truncate the message just before the diff, if any. */
cleanup_mode == CLEANUP_SCISSORS)
strbuf_setlen(&sb, wt_status_locate_end(sb.buf, sb.len));
-
if (cleanup_mode != CLEANUP_NONE)
strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);
- if (template_untouched(&sb) && !allow_empty_message) {
+
+ if (message_is_empty(&sb) && !allow_empty_message) {
rollback_index_files();
- fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
+ fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
exit(1);
}
- if (message_is_empty(&sb) && !allow_empty_message) {
+ if (template_untouched(&sb) && !allow_empty_message) {
rollback_index_files();
- fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
+ fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
exit(1);
}
};
static int commit_name_cmp(const void *unused_cmp_data,
- const struct commit_name *cn1,
- const struct commit_name *cn2,
+ const void *entry,
+ const void *entry_or_key,
const void *peeled)
{
+ const struct commit_name *cn1 = entry;
+ const struct commit_name *cn2 = entry_or_key;
+
return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
}
return cmd_name_rev(args.argc, args.argv, prefix);
}
- hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
+ hashmap_init(&names, commit_name_cmp, NULL, 0);
for_each_rawref(get_name, NULL);
if (!names.size && !always)
die(_("No names found, cannot describe anything."));
};
static int working_tree_entry_cmp(const void *unused_cmp_data,
- struct working_tree_entry *a,
- struct working_tree_entry *b,
- void *unused_keydata)
+ const void *entry,
+ const void *entry_or_key,
+ const void *unused_keydata)
{
+ const struct working_tree_entry *a = entry;
+ const struct working_tree_entry *b = entry_or_key;
return strcmp(a->path, b->path);
}
};
static int pair_cmp(const void *unused_cmp_data,
- struct pair_entry *a, struct pair_entry *b,
- void *unused_keydata)
+ const void *entry,
+ const void *entry_or_key,
+ const void *unused_keydata)
{
+ const struct pair_entry *a = entry;
+ const struct pair_entry *b = entry_or_key;
+
return strcmp(a->path, b->path);
}
};
static int path_entry_cmp(const void *unused_cmp_data,
- struct path_entry *a, struct path_entry *b,
- void *key)
+ const void *entry,
+ const void *entry_or_key,
+ const void *key)
{
+ const struct path_entry *a = entry;
+ const struct path_entry *b = entry_or_key;
+
return strcmp(a->path, key ? key : b->path);
}
rdir_len = rdir.len;
wtdir_len = wtdir.len;
- hashmap_init(&working_tree_dups,
- (hashmap_cmp_fn)working_tree_entry_cmp, NULL, 0);
- hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, NULL, 0);
- hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, NULL, 0);
+ hashmap_init(&working_tree_dups, working_tree_entry_cmp, NULL, 0);
+ hashmap_init(&submodules, pair_cmp, NULL, 0);
+ hashmap_init(&symlinks2, pair_cmp, NULL, 0);
child.no_stdin = 1;
child.git_cmd = 1;
* in the common case of --symlinks and the difftool updating
* files through the symlink.
*/
- hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
- NULL, wtindex.cache_nr);
- hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
- NULL, wtindex.cache_nr);
+ hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
+ hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
for (i = 0; i < wtindex.cache_nr; i++) {
struct hashmap_entry dummy;
static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
static int progress = -1;
static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
-static int max_children = -1;
+static int max_children = 1;
static enum transport_family family;
static const char *depth;
static const char *deepen_since;
recurse_submodules = r;
}
+ if (!strcmp(k, "submodule.fetchjobs")) {
+ max_children = parse_submodule_fetchjobs(k, v);
+ return 0;
+ } else if (!strcmp(k, "fetch.recursesubmodules")) {
+ recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
+ return 0;
+ }
+
return git_default_config(k, v, cb);
}
+static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
+{
+ if (!strcmp(var, "submodule.fetchjobs")) {
+ max_children = parse_submodule_fetchjobs(var, value);
+ return 0;
+ } else if (!strcmp(var, "fetch.recursesubmodules")) {
+ recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
+ return 0;
+ }
+
+ return 0;
+}
+
static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
{
ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
for (i = 1; i < argc; i++)
strbuf_addf(&default_rla, " %s", argv[i]);
+ config_from_gitmodules(gitmodules_fetch_config, NULL);
git_config(git_fetch_config, NULL);
argc = parse_options(argc, argv, prefix,
deepen = 1;
if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
- set_config_fetch_recurse_submodules(recurse_submodules_default);
gitmodules_config();
git_config(submodule_config, NULL);
}
result = fetch_populated_submodules(&options,
submodule_prefix,
recurse_submodules,
+ recurse_submodules_default,
verbosity < 0,
max_children);
argv_array_clear(&options);
}
static void fmt_merge_msg_title(struct strbuf *out,
- const char *current_branch) {
+ const char *current_branch)
+{
int i = 0;
char *sep = "";
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
{
int i;
- const char *format = "%(objectname) %(objecttype)\t%(refname)";
struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
- int maxcount = 0, quote_style = 0, icase = 0;
+ int maxcount = 0, icase = 0;
struct ref_array array;
struct ref_filter filter;
+ struct ref_format format = REF_FORMAT_INIT;
struct option opts[] = {
- OPT_BIT('s', "shell", "e_style,
+ OPT_BIT('s', "shell", &format.quote_style,
N_("quote placeholders suitably for shells"), QUOTE_SHELL),
- OPT_BIT('p', "perl", "e_style,
+ OPT_BIT('p', "perl", &format.quote_style,
N_("quote placeholders suitably for perl"), QUOTE_PERL),
- OPT_BIT(0 , "python", "e_style,
+ OPT_BIT(0 , "python", &format.quote_style,
N_("quote placeholders suitably for python"), QUOTE_PYTHON),
- OPT_BIT(0 , "tcl", "e_style,
+ OPT_BIT(0 , "tcl", &format.quote_style,
N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
OPT_GROUP(""),
OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
- OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
+ OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
N_("field name to sort on"), &parse_opt_ref_sorting),
OPT_CALLBACK(0, "points-at", &filter.points_at,
memset(&array, 0, sizeof(array));
memset(&filter, 0, sizeof(filter));
+ format.format = "%(objectname) %(objecttype)\t%(refname)";
+
+ git_config(git_default_config, NULL);
+
parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
if (maxcount < 0) {
error("invalid --count argument: `%d'", maxcount);
usage_with_options(for_each_ref_usage, opts);
}
- if (HAS_MULTI_BITS(quote_style)) {
+ if (HAS_MULTI_BITS(format.quote_style)) {
error("more than one quoting style?");
usage_with_options(for_each_ref_usage, opts);
}
- if (verify_ref_format(format))
+ if (verify_ref_format(&format))
usage_with_options(for_each_ref_usage, opts);
if (!sorting)
sorting->ignore_case = icase;
filter.ignore_case = icase;
- /* for warn_ambiguous_refs */
- git_config(git_default_config, NULL);
-
filter.name_patterns = argv;
filter.match_as_path = 1;
filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
if (!maxcount || array.nr < maxcount)
maxcount = array.nr;
for (i = 0; i < maxcount; i++)
- show_ref_array_item(array.items[i], format, quote_style);
+ show_ref_array_item(array.items[i], &format);
ref_array_clear(&array);
return 0;
}
#define REACHABLE 0x0001
#define SEEN 0x0002
#define HAS_OBJ 0x0004
+/* This flag is set if something points to this object. */
+#define USED 0x0008
static int show_root;
static int show_tags;
static int traverse_one_object(struct object *obj)
{
- int result;
- struct tree *tree = NULL;
-
- if (obj->type == OBJ_TREE) {
- tree = (struct tree *)obj;
- if (parse_tree(tree) < 0)
- return 1; /* error already displayed */
- }
- result = fsck_walk(obj, obj, &fsck_walk_options);
- if (tree)
- free_tree_buffer(tree);
- return result;
+ return fsck_walk(obj, obj, &fsck_walk_options);
}
static int traverse_reachable(void)
{
if (!obj)
return 1;
- obj->used = 1;
+ obj->flags |= USED;
return 0;
}
}
/*
- * "!used" means that nothing at all points to it, including
+ * "!USED" means that nothing at all points to it, including
* other unreachable objects. In other words, it's the "tip"
* of some set of unreachable objects, usually a commit that
* got dropped.
* deleted a branch by mistake, this is a prime candidate to
* start looking at, for example.
*/
- if (!obj->used) {
+ if (!(obj->flags & USED)) {
if (show_dangling)
printf("dangling %s %s\n", printable_type(obj),
describe_object(obj));
static int fsck_obj(struct object *obj)
{
+ int err;
+
if (obj->flags & SEEN)
return 0;
obj->flags |= SEEN;
if (fsck_walk(obj, NULL, &fsck_obj_options))
objerror(obj, "broken links");
- if (fsck_object(obj, NULL, 0, &fsck_obj_options))
- return -1;
-
- if (obj->type == OBJ_TREE) {
- struct tree *item = (struct tree *) obj;
-
- free_tree_buffer(item);
- }
+ err = fsck_object(obj, NULL, 0, &fsck_obj_options);
+ if (err)
+ goto out;
if (obj->type == OBJ_COMMIT) {
struct commit *commit = (struct commit *) obj;
- free_commit_buffer(commit);
-
if (!commit->parents && show_root)
printf("root %s\n", describe_object(&commit->object));
}
}
}
- return 0;
+out:
+ if (obj->type == OBJ_TREE)
+ free_tree_buffer((struct tree *)obj);
+ if (obj->type == OBJ_COMMIT)
+ free_commit_buffer((struct commit *)obj);
+ return err;
}
static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
errors_found |= ERROR_OBJECT;
return error("%s: object corrupt or missing", oid_to_hex(oid));
}
- obj->flags = HAS_OBJ;
+ obj->flags &= ~(REACHABLE | SEEN);
+ obj->flags |= HAS_OBJ;
return fsck_obj(obj);
}
add_decoration(fsck_walk_options.object_names,
obj,
xstrfmt("%s@{%"PRItime"}", refname, timestamp));
- obj->used = 1;
+ obj->flags |= USED;
mark_object_reachable(obj);
} else {
error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
errors_found |= ERROR_REFS;
}
default_refs++;
- obj->used = 1;
+ obj->flags |= USED;
if (name_objects)
add_decoration(fsck_walk_options.object_names,
obj, xstrdup(refname));
return 0; /* keep checking other objects */
}
- obj->flags = HAS_OBJ;
+ obj->flags &= ~(REACHABLE | SEEN);
+ obj->flags |= HAS_OBJ;
if (fsck_obj(obj))
errors_found |= ERROR_OBJECT;
return 0;
errors_found |= ERROR_REFS;
return 1;
}
- obj->used = 1;
+ obj->flags |= USED;
if (name_objects)
add_decoration(fsck_walk_options.object_names,
obj, xstrdup(":"));
int cmd_fsck(int argc, const char **argv, const char *prefix)
{
- int i, heads;
+ int i;
struct alternate_object_database *alt;
errors_found = 0;
}
}
- heads = 0;
for (i = 0; i < argc; i++) {
const char *arg = argv[i];
- unsigned char sha1[20];
- if (!get_sha1(arg, sha1)) {
- struct object *obj = lookup_object(sha1);
+ struct object_id oid;
+ if (!get_oid(arg, &oid)) {
+ struct object *obj = lookup_object(oid.hash);
if (!obj || !(obj->flags & HAS_OBJ)) {
- error("%s: object missing", sha1_to_hex(sha1));
+ error("%s: object missing", oid_to_hex(&oid));
errors_found |= ERROR_OBJECT;
continue;
}
- obj->used = 1;
+ obj->flags |= USED;
if (name_objects)
add_decoration(fsck_walk_options.object_names,
obj, xstrdup(arg));
mark_object_reachable(obj);
- heads++;
continue;
}
error("invalid parameter: expected sha1, got '%s'", arg);
if (!blob)
continue;
obj = &blob->object;
- obj->used = 1;
+ obj->flags |= USED;
if (name_objects)
add_decoration(fsck_walk_options.object_names,
obj,
NULL
};
-static const char *super_prefix;
static int recurse_submodules;
-static struct argv_array submodule_options = ARGV_ARRAY_INIT;
-static const char *parent_basename;
-
-static int grep_submodule_launch(struct grep_opt *opt,
- const struct grep_source *gs);
#define GREP_NUM_THREADS_DEFAULT 8
static int num_threads;
break;
opt->output_priv = w;
- if (w->source.type == GREP_SOURCE_SUBMODULE)
- hit |= grep_submodule_launch(opt, &w->source);
- else
- hit |= grep_source(opt, &w->source);
+ hit |= grep_source(opt, &w->source);
grep_source_clear_data(&w->source);
work_done(w);
}
static int grep_cmd_config(const char *var, const char *value, void *cb)
{
int st = grep_config(var, value, cb);
- if (git_color_default_config(var, value, cb) < 0)
+ if (git_default_config(var, value, cb) < 0)
st = -1;
if (!strcmp(var, "grep.threads")) {
{
struct strbuf pathbuf = STRBUF_INIT;
- if (super_prefix) {
- strbuf_add(&pathbuf, filename, tree_name_len);
- strbuf_addstr(&pathbuf, super_prefix);
- strbuf_addstr(&pathbuf, filename + tree_name_len);
+ if (opt->relative && opt->prefix_length) {
+ quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
+ strbuf_insert(&pathbuf, 0, filename, tree_name_len);
} else {
strbuf_addstr(&pathbuf, filename);
}
- if (opt->relative && opt->prefix_length) {
- char *name = strbuf_detach(&pathbuf, NULL);
- quote_path_relative(name + tree_name_len, opt->prefix, &pathbuf);
- strbuf_insert(&pathbuf, 0, name, tree_name_len);
- free(name);
- }
-
#ifndef NO_PTHREADS
if (num_threads) {
add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
{
struct strbuf buf = STRBUF_INIT;
- if (super_prefix)
- strbuf_addstr(&buf, super_prefix);
- strbuf_addstr(&buf, filename);
-
- if (opt->relative && opt->prefix_length) {
- char *name = strbuf_detach(&buf, NULL);
- quote_path_relative(name, opt->prefix, &buf);
- free(name);
- }
+ if (opt->relative && opt->prefix_length)
+ quote_path_relative(filename, opt->prefix, &buf);
+ else
+ strbuf_addstr(&buf, filename);
#ifndef NO_PTHREADS
if (num_threads) {
exit(status);
}
-static void compile_submodule_options(const struct grep_opt *opt,
- const char **argv,
- int cached, int untracked,
- int opt_exclude, int use_index,
- int pattern_type_arg)
-{
- struct grep_pat *pattern;
-
- if (recurse_submodules)
- argv_array_push(&submodule_options, "--recurse-submodules");
-
- if (cached)
- argv_array_push(&submodule_options, "--cached");
- if (!use_index)
- argv_array_push(&submodule_options, "--no-index");
- if (untracked)
- argv_array_push(&submodule_options, "--untracked");
- if (opt_exclude > 0)
- argv_array_push(&submodule_options, "--exclude-standard");
-
- if (opt->invert)
- argv_array_push(&submodule_options, "-v");
- if (opt->ignore_case)
- argv_array_push(&submodule_options, "-i");
- if (opt->word_regexp)
- argv_array_push(&submodule_options, "-w");
- switch (opt->binary) {
- case GREP_BINARY_NOMATCH:
- argv_array_push(&submodule_options, "-I");
- break;
- case GREP_BINARY_TEXT:
- argv_array_push(&submodule_options, "-a");
- break;
- default:
- break;
- }
- if (opt->allow_textconv)
- argv_array_push(&submodule_options, "--textconv");
- if (opt->max_depth != -1)
- argv_array_pushf(&submodule_options, "--max-depth=%d",
- opt->max_depth);
- if (opt->linenum)
- argv_array_push(&submodule_options, "-n");
- if (!opt->pathname)
- argv_array_push(&submodule_options, "-h");
- if (!opt->relative)
- argv_array_push(&submodule_options, "--full-name");
- if (opt->name_only)
- argv_array_push(&submodule_options, "-l");
- if (opt->unmatch_name_only)
- argv_array_push(&submodule_options, "-L");
- if (opt->null_following_name)
- argv_array_push(&submodule_options, "-z");
- if (opt->count)
- argv_array_push(&submodule_options, "-c");
- if (opt->file_break)
- argv_array_push(&submodule_options, "--break");
- if (opt->heading)
- argv_array_push(&submodule_options, "--heading");
- if (opt->pre_context)
- argv_array_pushf(&submodule_options, "--before-context=%d",
- opt->pre_context);
- if (opt->post_context)
- argv_array_pushf(&submodule_options, "--after-context=%d",
- opt->post_context);
- if (opt->funcname)
- argv_array_push(&submodule_options, "-p");
- if (opt->funcbody)
- argv_array_push(&submodule_options, "-W");
- if (opt->all_match)
- argv_array_push(&submodule_options, "--all-match");
- if (opt->debug)
- argv_array_push(&submodule_options, "--debug");
- if (opt->status_only)
- argv_array_push(&submodule_options, "-q");
-
- switch (pattern_type_arg) {
- case GREP_PATTERN_TYPE_BRE:
- argv_array_push(&submodule_options, "-G");
- break;
- case GREP_PATTERN_TYPE_ERE:
- argv_array_push(&submodule_options, "-E");
- break;
- case GREP_PATTERN_TYPE_FIXED:
- argv_array_push(&submodule_options, "-F");
- break;
- case GREP_PATTERN_TYPE_PCRE:
- argv_array_push(&submodule_options, "-P");
- break;
- case GREP_PATTERN_TYPE_UNSPECIFIED:
- break;
- default:
- die("BUG: Added a new grep pattern type without updating switch statement");
- }
-
- for (pattern = opt->pattern_list; pattern != NULL;
- pattern = pattern->next) {
- switch (pattern->token) {
- case GREP_PATTERN:
- argv_array_pushf(&submodule_options, "-e%s",
- pattern->pattern);
- break;
- case GREP_AND:
- case GREP_OPEN_PAREN:
- case GREP_CLOSE_PAREN:
- case GREP_NOT:
- case GREP_OR:
- argv_array_push(&submodule_options, pattern->pattern);
- break;
- /* BODY and HEAD are not used by git-grep */
- case GREP_PATTERN_BODY:
- case GREP_PATTERN_HEAD:
- break;
- }
- }
-
- /*
- * Limit number of threads for child process to use.
- * This is to prevent potential fork-bomb behavior of git-grep as each
- * submodule process has its own thread pool.
- */
- argv_array_pushf(&submodule_options, "--threads=%d",
- DIV_ROUND_UP(num_threads, 2));
-
- /* Add Pathspecs */
- argv_array_push(&submodule_options, "--");
- for (; *argv; argv++)
- argv_array_push(&submodule_options, *argv);
-}
+static int grep_cache(struct grep_opt *opt, struct repository *repo,
+ const struct pathspec *pathspec, int cached);
+static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
+ struct tree_desc *tree, struct strbuf *base, int tn_len,
+ int check_attr, struct repository *repo);
-/*
- * Launch child process to grep contents of a submodule
- */
-static int grep_submodule_launch(struct grep_opt *opt,
- const struct grep_source *gs)
+static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
+ const struct pathspec *pathspec,
+ const struct object_id *oid,
+ const char *filename, const char *path)
{
- struct child_process cp = CHILD_PROCESS_INIT;
- int status, i;
- const char *end_of_base;
- const char *name;
- struct strbuf child_output = STRBUF_INIT;
-
- end_of_base = strchr(gs->name, ':');
- if (gs->identifier && end_of_base)
- name = end_of_base + 1;
- else
- name = gs->name;
+ struct repository submodule;
+ int hit;
- prepare_submodule_repo_env(&cp.env_array);
- argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
+ if (!is_submodule_active(superproject, path))
+ return 0;
- if (opt->relative && opt->prefix_length)
- argv_array_pushf(&cp.env_array, "%s=%s",
- GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
- opt->prefix);
+ if (repo_submodule_init(&submodule, superproject, path))
+ return 0;
- /* Add super prefix */
- argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
- super_prefix ? super_prefix : "",
- name);
- argv_array_push(&cp.args, "grep");
+ repo_read_gitmodules(&submodule);
/*
- * Add basename of parent project
- * When performing grep on a tree object the filename is prefixed
- * with the object's name: 'tree-name:filename'. In order to
- * provide uniformity of output we want to pass the name of the
- * parent project's object name to the submodule so the submodule can
- * prefix its output with the parent's name and not its own OID.
+ * NEEDSWORK: This adds the submodule's object directory to the list of
+ * alternates for the single in-memory object store. This has some bad
+ * consequences for memory (processed objects will never be freed) and
+ * performance (this increases the number of pack files git has to pay
+ * attention to, to the sum of the number of pack files in all the
+ * repositories processed so far). This can be removed once the object
+ * store is no longer global and instead is a member of the repository
+ * object.
*/
- if (gs->identifier && end_of_base)
- argv_array_pushf(&cp.args, "--parent-basename=%.*s",
- (int) (end_of_base - gs->name),
- gs->name);
+ add_to_alternates_memory(submodule.objectdir);
- /* Add options */
- for (i = 0; i < submodule_options.argc; i++) {
- /*
- * If there is a tree identifier for the submodule, add the
- * rev after adding the submodule options but before the
- * pathspecs. To do this we listen for the '--' and insert the
- * oid before pushing the '--' onto the child process argv
- * array.
- */
- if (gs->identifier &&
- !strcmp("--", submodule_options.argv[i])) {
- argv_array_push(&cp.args, oid_to_hex(gs->identifier));
- }
+ if (oid) {
+ struct object *object;
+ struct tree_desc tree;
+ void *data;
+ unsigned long size;
+ struct strbuf base = STRBUF_INIT;
- argv_array_push(&cp.args, submodule_options.argv[i]);
- }
+ object = parse_object_or_die(oid, oid_to_hex(oid));
- cp.git_cmd = 1;
- cp.dir = gs->path;
+ grep_read_lock();
+ data = read_object_with_reference(object->oid.hash, tree_type,
+ &size, NULL);
+ grep_read_unlock();
- /*
- * Capture output to output buffer and check the return code from the
- * child process. A '0' indicates a hit, a '1' indicates no hit and
- * anything else is an error.
- */
- status = capture_command(&cp, &child_output, 0);
- if (status && (status != 1)) {
- /* flush the buffer */
- write_or_die(1, child_output.buf, child_output.len);
- die("process for submodule '%s' failed with exit code: %d",
- gs->name, status);
- }
+ if (!data)
+ die(_("unable to read tree (%s)"), oid_to_hex(&object->oid));
- opt->output(opt, child_output.buf, child_output.len);
- strbuf_release(&child_output);
- /* invert the return code to make a hit equal to 1 */
- return !status;
-}
+ strbuf_addstr(&base, filename);
+ strbuf_addch(&base, '/');
-/*
- * Prep grep structures for a submodule grep
- * oid: the oid of the submodule or NULL if using the working tree
- * filename: name of the submodule including tree name of parent
- * path: location of the submodule
- */
-static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
- const char *filename, const char *path)
-{
- if (!is_submodule_active(the_repository, path))
- return 0;
- if (!is_submodule_populated_gently(path, NULL)) {
- /*
- * If searching history, check for the presence of the
- * submodule's gitdir before skipping the submodule.
- */
- if (oid) {
- const struct submodule *sub =
- submodule_from_path(null_sha1, path);
- if (sub)
- path = git_path("modules/%s", sub->name);
-
- if (!(is_directory(path) && is_git_directory(path)))
- return 0;
- } else {
- return 0;
- }
+ init_tree_desc(&tree, data, size);
+ hit = grep_tree(opt, pathspec, &tree, &base, base.len,
+ object->type == OBJ_COMMIT, &submodule);
+ strbuf_release(&base);
+ free(data);
+ } else {
+ hit = grep_cache(opt, &submodule, pathspec, 1);
}
-#ifndef NO_PTHREADS
- if (num_threads) {
- add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, oid);
- return 0;
- } else
-#endif
- {
- struct grep_source gs;
- int hit;
-
- grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
- filename, path, oid);
- hit = grep_submodule_launch(opt, &gs);
-
- grep_source_clear(&gs);
- return hit;
- }
+ repo_clear(&submodule);
+ return hit;
}
-static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec,
- int cached)
+static int grep_cache(struct grep_opt *opt, struct repository *repo,
+ const struct pathspec *pathspec, int cached)
{
int hit = 0;
int nr;
struct strbuf name = STRBUF_INIT;
int name_base_len = 0;
- if (super_prefix) {
- name_base_len = strlen(super_prefix);
- strbuf_addstr(&name, super_prefix);
+ if (repo->submodule_prefix) {
+ name_base_len = strlen(repo->submodule_prefix);
+ strbuf_addstr(&name, repo->submodule_prefix);
}
- read_cache();
+ repo_read_index(repo);
- for (nr = 0; nr < active_nr; nr++) {
- const struct cache_entry *ce = active_cache[nr];
+ for (nr = 0; nr < repo->index->cache_nr; nr++) {
+ const struct cache_entry *ce = repo->index->cache[nr];
strbuf_setlen(&name, name_base_len);
strbuf_addstr(&name, ce->name);
ce_skip_worktree(ce)) {
if (ce_stage(ce) || ce_intent_to_add(ce))
continue;
- hit |= grep_oid(opt, &ce->oid, ce->name,
- 0, ce->name);
+ hit |= grep_oid(opt, &ce->oid, name.buf,
+ 0, name.buf);
} else {
- hit |= grep_file(opt, ce->name);
+ hit |= grep_file(opt, name.buf);
}
} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
submodule_path_match(pathspec, name.buf, NULL)) {
- hit |= grep_submodule(opt, NULL, ce->name, ce->name);
+ hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name);
} else {
continue;
}
if (ce_stage(ce)) {
do {
nr++;
- } while (nr < active_nr &&
- !strcmp(ce->name, active_cache[nr]->name));
+ } while (nr < repo->index->cache_nr &&
+ !strcmp(ce->name, repo->index->cache[nr]->name));
nr--; /* compensate for loop control */
}
if (hit && opt->status_only)
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
struct tree_desc *tree, struct strbuf *base, int tn_len,
- int check_attr)
+ int check_attr, struct repository *repo)
{
int hit = 0;
enum interesting match = entry_not_interesting;
int old_baselen = base->len;
struct strbuf name = STRBUF_INIT;
int name_base_len = 0;
- if (super_prefix) {
- strbuf_addstr(&name, super_prefix);
+ if (repo->submodule_prefix) {
+ strbuf_addstr(&name, repo->submodule_prefix);
name_base_len = name.len;
}
strbuf_addch(base, '/');
init_tree_desc(&sub, data, size);
hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
- check_attr);
+ check_attr, repo);
free(data);
} else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
- hit |= grep_submodule(opt, entry.oid, base->buf,
- base->buf + tn_len);
+ hit |= grep_submodule(opt, repo, pathspec, entry.oid,
+ base->buf, base->buf + tn_len);
}
strbuf_setlen(base, old_baselen);
}
static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
- struct object *obj, const char *name, const char *path)
+ struct object *obj, const char *name, const char *path,
+ struct repository *repo)
{
if (obj->type == OBJ_BLOB)
return grep_oid(opt, &obj->oid, name, 0, path);
if (!data)
die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
- /* Use parent's name as base when recursing submodules */
- if (recurse_submodules && parent_basename)
- name = parent_basename;
-
len = name ? strlen(name) : 0;
strbuf_init(&base, PATH_MAX + len + 1);
if (len) {
}
init_tree_desc(&tree, data, size);
hit = grep_tree(opt, pathspec, &tree, &base, base.len,
- obj->type == OBJ_COMMIT);
+ obj->type == OBJ_COMMIT, repo);
strbuf_release(&base);
free(data);
return hit;
}
static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
+ struct repository *repo,
const struct object_array *list)
{
unsigned int i;
/* load the gitmodules file for this rev */
if (recurse_submodules) {
submodule_free();
- gitmodules_config_sha1(real_obj->oid.hash);
+ gitmodules_config_oid(&real_obj->oid);
}
- if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path)) {
+ if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path,
+ repo)) {
hit = 1;
if (opt->status_only)
break;
N_("ignore files specified via '.gitignore'"), 1),
OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
N_("recursively search in each submodule")),
- OPT_STRING(0, "parent-basename", &parent_basename,
- N_("basename"),
- N_("prepend parent project's basename to output")),
OPT_GROUP(""),
OPT_BOOL('v', "invert-match", &opt.invert,
N_("show non-matching lines")),
init_grep_defaults();
git_config(grep_cmd_config, NULL);
grep_init(&opt, prefix);
- super_prefix = get_super_prefix();
/*
* If there is no -- then the paths must exist in the working
break;
}
- if (get_sha1_with_context(arg, GET_SHA1_RECORD_PATH,
- oid.hash, &oc)) {
+ if (get_oid_with_context(arg, GET_OID_RECORD_PATH,
+ &oid, &oc)) {
if (seen_dashdash)
die(_("unable to resolve revision: %s"), arg);
break;
if (recurse_submodules) {
gitmodules_config();
- compile_submodule_options(&opt, argv + i, cached, untracked,
- opt_exclude, use_index,
- pattern_type_arg);
}
if (show_in_pager && (cached || list.nr))
if (!cached)
setup_work_tree();
- hit = grep_cache(&opt, &pathspec, cached);
+ hit = grep_cache(&opt, the_repository, &pathspec, cached);
} else {
if (cached)
die(_("both --cached and trees are given."));
- hit = grep_objects(&opt, &pathspec, &list);
+
+ hit = grep_objects(&opt, &pathspec, the_repository, &list);
}
if (num_threads)
#include "version.h"
#include "mailmap.h"
#include "gpg-interface.h"
+#include "progress.h"
/* Set a default date-time format for git log ("log.date" config variable) */
static const char *default_date_mode = NULL;
return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS : 0;
}
-static int parse_decoration_style(const char *var, const char *value)
+static int parse_decoration_style(const char *value)
{
- switch (git_config_maybe_bool(var, value)) {
+ switch (git_parse_maybe_bool(value)) {
case 1:
return DECORATE_SHORT_REFS;
case 0:
if (unset)
decoration_style = 0;
else if (arg)
- decoration_style = parse_decoration_style("command line", arg);
+ decoration_style = parse_decoration_style(arg);
else
decoration_style = DECORATE_SHORT_REFS;
*/
rev->max_count++;
if (!rev->reflog_info) {
- /* we allow cycles in reflog ancestry */
+ /*
+ * We may show a given commit multiple times when
+ * walking the reflogs.
+ */
free_commit_buffer(commit);
+ free_commit_list(commit->parents);
+ commit->parents = NULL;
}
- free_commit_list(commit->parents);
- commit->parents = NULL;
if (saved_nrl < rev->diffopt.needed_rename_limit)
saved_nrl = rev->diffopt.needed_rename_limit;
if (rev->diffopt.degraded_cc_to_c)
if (!strcmp(var, "log.date"))
return git_config_string(&default_date_mode, var, value);
if (!strcmp(var, "log.decorate")) {
- decoration_style = parse_decoration_style(var, value);
+ decoration_style = parse_decoration_style(value);
if (decoration_style < 0)
decoration_style = 0; /* maybe warn? */
return 0;
!DIFF_OPT_TST(&rev->diffopt, ALLOW_TEXTCONV))
return stream_blob_to_fd(1, oid, NULL, 0);
- if (get_sha1_with_context(obj_name, GET_SHA1_RECORD_PATH,
- oidc.hash, &obj_context))
+ if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
+ &oidc, &obj_context))
die(_("Not a valid object name %s"), obj_name);
if (!obj_context.path ||
!textconv_object(obj_context.path, obj_context.mode, &oidc, 1, &buf, &size)) {
return 0;
}
if (!strcmp(var, "format.from")) {
- int b = git_config_maybe_bool(var, value);
+ int b = git_parse_maybe_bool(value);
free(from);
if (b < 0)
from = xstrdup(value);
char *branch_name = NULL;
char *base_commit = NULL;
struct base_tree_info bases;
+ int show_progress = 0;
+ struct progress *progress = NULL;
const struct option builtin_format_patch_options[] = {
{ OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
OPT_FILENAME(0, "signature-file", &signature_file,
N_("add a signature from a file")),
OPT__QUIET(&quiet, N_("don't print the patch filenames")),
+ OPT_BOOL(0, "progress", &show_progress,
+ N_("show progress while generating patches")),
OPT_END()
};
start_number--;
}
rev.add_signoff = do_signoff;
+
+ if (show_progress)
+ progress = start_progress_delay(_("Generating patches"), total, 0, 2);
while (0 <= --nr) {
int shown;
+ display_progress(progress, total - nr);
commit = list[nr];
rev.nr = total - nr + (start_number - 1);
/* Make the second and subsequent mails replies to the first */
if (!use_stdout)
fclose(rev.diffopt.file);
}
+ stop_progress(&progress);
free(list);
free(branch_name);
string_list_clear(&extra_to, 0);
int pos;
unsigned int first, last;
- if (!prefix)
+ if (!prefix || !istate->cache_nr)
return;
pos = index_name_pos(istate, prefix, prefixlen);
if (pos < 0)
}
last = next;
}
- memmove(istate->cache, istate->cache + pos,
- (last - pos) * sizeof(struct cache_entry *));
+ MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
istate->cache_nr = last - pos;
}
newbase = traverse_path(info, p);
-#define ENTRY_SHA1(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid->hash : NULL)
- buf0 = fill_tree_descriptor(t+0, ENTRY_SHA1(n + 0));
- buf1 = fill_tree_descriptor(t+1, ENTRY_SHA1(n + 1));
- buf2 = fill_tree_descriptor(t+2, ENTRY_SHA1(n + 2));
-#undef ENTRY_SHA1
+#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
+ buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
+ buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
+ buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
+#undef ENTRY_OID
merge_trees(t, newbase);
static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
{
- unsigned char sha1[20];
+ struct object_id oid;
void *buf;
- if (get_sha1(rev, sha1))
+ if (get_oid(rev, &oid))
die("unknown rev %s", rev);
- buf = fill_tree_descriptor(desc, sha1);
+ buf = fill_tree_descriptor(desc, &oid);
if (!buf)
die("%s is not a tree", rev);
return buf;
static int allow_unrelated_histories;
static int show_progress = -1;
static int default_to_upstream = 1;
+static int signoff;
static const char *sign_commit;
static struct strategy all_strategy[] = {
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
+ OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")),
OPT_END()
};
die(_("Bad branch.%s.mergeoptions string: %s"), branch,
split_cmdline_strerror(argc));
REALLOC_ARRAY(argv, argc + 2);
- memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
+ MOVE_ARRAY(argv + 1, argv, argc + 1);
argc++;
argv[0] = "branch.*.mergeoptions";
parse_options(argc, argv, NULL, builtin_merge_options,
else if (!strcmp(k, "merge.renormalize"))
option_renormalize = git_config_bool(k, v);
else if (!strcmp(k, "merge.ff")) {
- int boolval = git_config_maybe_bool(k, v);
+ int boolval = git_parse_maybe_bool(v);
if (0 <= boolval) {
fast_forward = boolval ? FF_ALLOW : FF_NO;
} else if (v && !strcmp(v, "only")) {
strbuf_addch(&msg, '\n');
if (0 < option_edit)
strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
+ if (signoff)
+ append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
git_path_merge_msg(), "merge", NULL))
return 0;
if (e) {
- int v = git_config_maybe_bool(name, e);
+ int v = git_parse_maybe_bool(e);
if (v < 0)
die(_("Bad value '%s' in environment '%s'"), e, name);
return v;
* current branch.
*/
branch = branch_to_free = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
- if (branch && starts_with(branch, "refs/heads/"))
- branch += 11;
+ if (branch)
+ skip_prefix(branch, "refs/heads/", &branch);
if (!branch || is_null_oid(&head_oid))
head_commit = NULL;
else
struct strbuf submodule_dotgit = STRBUF_INIT;
if (!S_ISGITLINK(active_cache[first]->ce_mode))
die(_("Directory %s is in index and no submodule?"), src);
- if (!is_staging_gitmodules_ok())
+ if (!is_staging_gitmodules_ok(&the_index))
die(_("Please stage your changes to .gitmodules or stash them to proceed"));
strbuf_addf(&submodule_dotgit, "%s/.git", src);
*submodule_gitfile = read_gitfile(submodule_dotgit.buf);
oid_to_hex(&object));
}
- prepare_note_data(&object, &d, note->hash);
+ prepare_note_data(&object, &d, note ? note->hash : NULL);
if (d.buf.len || allow_empty) {
write_note_data(&d, new_note.hash);
if (add_note(t, &object, &new_note, combine_notes_overwrite))
static int check_pbase_path(unsigned hash)
{
- int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
+ int pos = done_pbase_path_pos(hash);
if (0 <= pos)
return 1;
pos = -pos - 1;
done_pbase_paths_alloc);
done_pbase_paths_num++;
if (pos < done_pbase_paths_num)
- memmove(done_pbase_paths + pos + 1,
- done_pbase_paths + pos,
- (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
+ MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
+ done_pbase_paths_num - pos - 1);
done_pbase_paths[pos] = hash;
return 0;
}
static enum rebase_type parse_config_rebase(const char *key, const char *value,
int fatal)
{
- int v = git_config_maybe_bool("pull.rebase", value);
+ int v = git_parse_maybe_bool(value);
if (!v)
return REBASE_FALSE;
if (git_config_get_value("pull.ff", &value))
return NULL;
- switch (git_config_maybe_bool("pull.ff", value)) {
+ switch (git_parse_maybe_bool(value)) {
case 0:
return "--no-ff";
case 1:
} else if (!strcmp(k, "push.gpgsign")) {
const char *value;
if (!git_config_get_value("push.gpgsign", &value)) {
- switch (git_config_maybe_bool("push.gpgsign", value)) {
+ switch (git_parse_maybe_bool(value)) {
case 0:
set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
break;
*/
static int head_has_history(void)
{
- unsigned char sha1[20];
+ struct object_id oid;
- return !get_sha1("HEAD", sha1);
+ return !get_oid("HEAD", &oid);
}
static const char *push_to_deploy(unsigned char *sha1,
}
if (ref_transaction_delete(transaction,
namespaced_name,
- old_oid->hash,
+ old_oid ? old_oid->hash : NULL,
0, "push", &err)) {
rp_error("%s", err.buf);
strbuf_release(&err);
}
string_list_append(&info->merge, xstrdup(value));
} else {
- int v = git_config_maybe_bool(orig_key, value);
+ int v = git_parse_maybe_bool(value);
if (v >= 0)
info->rebase = v;
else if (!strcmp(value, "preserve"))
struct object_id object;
enum object_type obj_type, repl_type;
- if (get_sha1(refname, object.hash))
+ if (get_oid(refname, &object))
return error("Failed to resolve '%s' as a valid ref.", refname);
obj_type = sha1_object_info(object.hash, NULL);
/* iterate over new parents */
for (i = 1; i < mergetag_data->argc; i++) {
struct object_id oid;
- if (get_sha1(mergetag_data->argv[i], oid.hash) < 0)
+ if (get_oid(mergetag_data->argv[i], &oid) < 0)
die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]);
if (!oidcmp(&tag->tagged->oid, &oid))
return; /* found */
struct object_id head_oid;
if (get_oid("HEAD", &head_oid))
return error(_("You do not have a valid HEAD."));
- if (!fill_tree_descriptor(desc, head_oid.hash))
+ if (!fill_tree_descriptor(desc, &head_oid))
return error(_("Failed to find tree of HEAD."));
nr++;
opts.fn = twoway_merge;
}
- if (!fill_tree_descriptor(desc + nr - 1, oid->hash))
+ if (!fill_tree_descriptor(desc + nr - 1, oid))
return error(_("Failed to find tree of %s."), oid_to_hex(oid));
if (unpack_trees(nr, desc, &opts))
return -1;
* has to be unambiguous. If there is a single argument, it
* can not be a tree
*/
- else if ((!argv[1] && !get_sha1_committish(argv[0], unused.hash)) ||
- (argv[1] && !get_sha1_treeish(argv[0], unused.hash))) {
+ else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
+ (argv[1] && !get_oid_treeish(argv[0], &unused))) {
/*
* Ok, argv[0] looks like a commit/tree; it should not
* be a filename.
load_submodule_cache();
- unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", oid.hash);
+ unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
if (unborn) {
/* reset on unborn branch: treat as reset to empty tree */
hashcpy(oid.hash, EMPTY_TREE_SHA1_BIN);
} else if (!pathspec.nr) {
struct commit *commit;
- if (get_sha1_committish(rev, oid.hash))
+ if (get_oid_committish(rev, &oid))
die(_("Failed to resolve '%s' as a valid revision."), rev);
commit = lookup_commit_reference(&oid);
if (!commit)
oidcpy(&oid, &commit->object.oid);
} else {
struct tree *tree;
- if (get_sha1_treeish(rev, oid.hash))
+ if (get_oid_treeish(rev, &oid))
die(_("Failed to resolve '%s' as a valid tree."), rev);
tree = parse_tree_indirect(&oid);
if (!tree)
#include "graph.h"
#include "bisect.h"
#include "progress.h"
+#include "reflog-walk.h"
static const char rev_list_usage[] =
"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
ctx.date_mode_explicit = revs->date_mode_explicit;
ctx.fmt = revs->commit_format;
ctx.output_encoding = get_log_output_encoding();
+ ctx.color = revs->diffopt.use_color;
pretty_print_commit(&ctx, commit, &buf);
if (buf.len) {
if (revs->commit_format != CMIT_FMT_ONELINE)
/* Only --header was specified */
revs.commit_format = CMIT_FMT_RAW;
- if ((!revs.commits &&
+ if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
(!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
- !revs.pending.nr)) ||
+ !revs.pending.nr) &&
+ !revs.rev_input_given) ||
revs.diff)
usage(rev_list_usage);
return 0;
}
- if (!get_sha1_committish(this, oid.hash) && !get_sha1_committish(next, end.hash)) {
+ if (!get_oid_committish(this, &oid) && !get_oid_committish(next, &end)) {
show_rev(NORMAL, &end, next);
show_rev(symmetric ? NORMAL : REVERSED, &oid, this);
if (symmetric) {
return 0;
*dotdot = 0;
- if (get_sha1_committish(arg, oid.hash)) {
+ if (get_oid_committish(arg, &oid)) {
*dotdot = '^';
return 0;
}
}
if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
quiet = 1;
- flags |= GET_SHA1_QUIETLY;
+ flags |= GET_OID_QUIETLY;
continue;
}
if (opt_with_value(arg, "--short", &arg)) {
name++;
type = REVERSED;
}
- if (!get_sha1_with_context(name, flags, oid.hash, &unused)) {
+ if (!get_oid_with_context(name, flags, &oid, &unused)) {
if (verify)
revs_count++;
else
"--strategy-option", opts->xopts ? 1 : 0,
"-x", opts->record_origin,
"--ff", opts->allow_ff,
+ "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
+ "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
NULL);
}
list.entry[list.nr].name = xstrdup(ce->name);
list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
if (list.entry[list.nr++].is_submodule &&
- !is_staging_gitmodules_ok())
+ !is_staging_gitmodules_ok(&the_index))
die (_("Please stage your changes to .gitmodules or stash them to proceed"));
}
if (!strcmp(k, "push.gpgsign")) {
const char *value;
if (!git_config_get_value("push.gpgsign", &value)) {
- switch (git_config_maybe_bool("push.gpgsign", value)) {
+ switch (git_parse_maybe_bool(value)) {
case 0:
args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
break;
/* If both heads/foo and tags/foo exists, get_sha1 would
* get confused.
*/
- if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
+ if (get_oid(refname + ofs, &tmp) || oidcmp(&tmp, oid))
ofs = 5;
return append_ref(refname + ofs, oid, 0);
}
/* If both heads/foo and tags/foo exists, get_sha1 would
* get confused.
*/
- if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
+ if (get_oid(refname + ofs, &tmp) || oidcmp(&tmp, oid))
ofs = 5;
return append_ref(refname + ofs, oid, 0);
}
static void append_one_rev(const char *av)
{
struct object_id revkey;
- if (!get_sha1(av, revkey.hash)) {
+ if (!get_oid(av, &revkey)) {
append_ref(av, &revkey, 0);
return;
}
return 0;
}
- return git_color_default_config(var, value, cb);
+ return git_default_config(var, value, cb);
}
static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
die(Q_("cannot handle more than %d rev.",
"cannot handle more than %d revs.",
MAX_REVS), MAX_REVS);
- if (get_sha1(ref_name[num_rev], revkey.hash))
+ if (get_oid(ref_name[num_rev], &revkey))
die(_("'%s' is not a valid ref."), ref_name[num_rev]);
commit = lookup_commit_reference(&revkey);
if (!commit)
} else
displaypath = xstrdup(path);
- sub = submodule_from_path(null_sha1, path);
+ sub = submodule_from_path(&null_oid, path);
if (!sub)
die(_("No url found for submodule path '%s' in .gitmodules"),
usage(_("git submodule--helper name <path>"));
gitmodules_config();
- sub = submodule_from_path(null_sha1, argv[1]);
+ sub = submodule_from_path(&null_oid, argv[1]);
if (!sub)
die(_("no submodule mapping found in .gitmodules for path '%s'"),
goto cleanup;
}
- sub = submodule_from_path(null_sha1, ce->name);
+ sub = submodule_from_path(&null_oid, ce->name);
if (suc->recursive_prefix)
displaypath = relative_path(suc->recursive_prefix,
const struct cache_entry *ce;
struct submodule_update_clone *suc = suc_cb;
- int *idxP = *(int**)idx_task_cb;
+ int *idxP = idx_task_cb;
int idx = *idxP;
free(idxP);
return 0;
}
+static int gitmodules_update_clone_config(const char *var, const char *value,
+ void *cb)
+{
+ int *max_jobs = cb;
+ if (!strcmp(var, "submodule.fetchjobs"))
+ *max_jobs = parse_submodule_fetchjobs(var, value);
+ return 0;
+}
+
static int update_clone(int argc, const char **argv, const char *prefix)
{
const char *update = NULL;
- int max_jobs = -1;
+ int max_jobs = 1;
struct string_list_item *item;
struct pathspec pathspec;
struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
};
suc.prefix = prefix;
+ config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
+ git_config(gitmodules_update_clone_config, &max_jobs);
+
argc = parse_options(argc, argv, prefix, module_update_clone_options,
git_submodule_helper_usage, 0);
gitmodules_config();
git_config(submodule_config, NULL);
- if (max_jobs < 0)
- max_jobs = parallel_submodules();
-
run_processes_parallel(max_jobs,
update_clone_get_next_task,
update_clone_start_failure,
gitmodules_config();
git_config(submodule_config, NULL);
- sub = submodule_from_path(null_sha1, path);
+ sub = submodule_from_path(&null_oid, path);
if (!sub)
return NULL;
static int push_check(int argc, const char **argv, const char *prefix)
{
struct remote *remote;
+ const char *superproject_head;
+ char *head;
+ int detached_head = 0;
+ struct object_id head_oid;
- if (argc < 2)
- die("submodule--helper push-check requires at least 1 argument");
+ if (argc < 3)
+ die("submodule--helper push-check requires at least 2 arguments");
+
+ /*
+ * superproject's resolved head ref.
+ * if HEAD then the superproject is in a detached head state, otherwise
+ * it will be the resolved head ref.
+ */
+ superproject_head = argv[1];
+ argv++;
+ argc--;
+ /* Get the submodule's head ref and determine if it is detached */
+ head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
+ if (!head)
+ die(_("Failed to resolve HEAD as a valid ref."));
+ if (!strcmp(head, "HEAD"))
+ detached_head = 1;
/*
* The remote must be configured.
if (rs->pattern || rs->matching)
continue;
- /*
- * LHS must match a single ref
- * NEEDSWORK: add logic to special case 'HEAD' once
- * working with submodules in a detached head state
- * ceases to be the norm.
- */
- if (count_refspec_match(rs->src, local_refs, NULL) != 1)
+ /* LHS must match a single ref */
+ switch (count_refspec_match(rs->src, local_refs, NULL)) {
+ case 1:
+ break;
+ case 0:
+ /*
+ * If LHS matches 'HEAD' then we need to ensure
+ * that it matches the same named branch
+ * checked out in the superproject.
+ */
+ if (!strcmp(rs->src, "HEAD")) {
+ if (!detached_head &&
+ !strcmp(head, superproject_head))
+ break;
+ die("HEAD does not match the named branch in the superproject");
+ }
+ default:
die("src refspec '%s' must name a ref",
rs->src);
+ }
}
free_refspec(refspec_nr, refspec);
}
+ free(head);
return 0;
}
static unsigned int colopts;
static int force_sign_annotate;
-static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
+static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
+ struct ref_format *format)
{
struct ref_array array;
char *to_free = NULL;
if (filter->lines == -1)
filter->lines = 0;
- if (!format) {
+ if (!format->format) {
if (filter->lines) {
to_free = xstrfmt("%s %%(contents:lines=%d)",
"%(align:15)%(refname:lstrip=2)%(end)",
filter->lines);
- format = to_free;
+ format->format = to_free;
} else
- format = "%(refname:lstrip=2)";
+ format->format = "%(refname:lstrip=2)";
}
- verify_ref_format(format);
+ if (verify_ref_format(format))
+ die(_("unable to parse format string"));
filter->with_commit_tag_algo = 1;
filter_refs(&array, filter, FILTER_REFS_TAGS);
ref_array_sort(sorting, &array);
for (i = 0; i < array.nr; i++)
- show_ref_array_item(array.items[i], format, 0);
+ show_ref_array_item(array.items[i], format);
ref_array_clear(&array);
free(to_free);
const struct object_id *oid, const void *cb_data)
{
int flags;
- const char *fmt_pretty = cb_data;
+ const struct ref_format *format = cb_data;
flags = GPG_VERIFY_VERBOSE;
- if (fmt_pretty)
+ if (format->format)
flags = GPG_VERIFY_OMIT_STATUS;
- if (gpg_verify_tag(oid->hash, name, flags))
+ if (gpg_verify_tag(oid, name, flags))
return -1;
- if (fmt_pretty)
- pretty_print_ref(name, oid->hash, fmt_pretty);
+ if (format->format)
+ pretty_print_ref(name, oid->hash, format);
return 0;
}
"Lines starting with '%c' will be kept; you may remove them"
" yourself if you want to.\n");
-/* Parse arg given and add it the ref_sorting array */
-static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
-{
- struct ref_sorting *s;
- int len;
-
- s = xcalloc(1, sizeof(*s));
- s->next = *sorting_tail;
- *sorting_tail = s;
-
- if (*arg == '-') {
- s->reverse = 1;
- arg++;
- }
- if (skip_prefix(arg, "version:", &arg) ||
- skip_prefix(arg, "v:", &arg))
- s->version = 1;
-
- len = strlen(arg);
- s->atom = parse_ref_filter_atom(arg, arg+len);
-
- return 0;
-}
-
static int git_tag_config(const char *var, const char *value, void *cb)
{
int status;
if (!strcmp(var, "tag.sort")) {
if (!value)
return config_error_nonbool(var);
- parse_sorting_string(value, sorting_tail);
+ parse_ref_sorting(sorting_tail, value);
return 0;
}
struct strbuf err = STRBUF_INIT;
struct ref_filter filter;
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
- const char *format = NULL;
+ struct ref_format format = REF_FORMAT_INIT;
int icase = 0;
struct option options[] = {
OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
parse_opt_object_name, (intptr_t) "HEAD"
},
- OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
+ OPT_STRING( 0 , "format", &format.format, N_("format"),
+ N_("format to use for the output")),
OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
OPT_END()
};
cmdmode = 'l';
}
+ if (cmdmode == 'l')
+ setup_auto_pager("tag", 1);
+
if ((create_tag_object || force) && (cmdmode != 0))
usage_with_options(git_tag_usage, options);
run_column_filter(colopts, &copts);
}
filter.name_patterns = argv;
- ret = list_tags(&filter, sorting, format);
+ ret = list_tags(&filter, sorting, &format);
if (column_active(colopts))
stop_column_filter();
return ret;
if (cmdmode == 'd')
return for_each_tag_name(argv, delete_tag, NULL);
if (cmdmode == 'v') {
- if (format)
- verify_ref_format(format);
- return for_each_tag_name(argv, verify_tag, format);
+ if (format.format && verify_ref_format(&format))
+ usage_with_options(git_tag_usage, options);
+ return for_each_tag_name(argv, verify_tag, &format);
}
if (msg.given || msgfile) {
#include "builtin.h"
#include "config.h"
-static char *create_temp_file(unsigned char *sha1)
+static char *create_temp_file(struct object_id *oid)
{
static char path[50];
void *buf;
unsigned long size;
int fd;
- buf = read_sha1_file(sha1, &type, &size);
+ buf = read_sha1_file(oid->hash, &type, &size);
if (!buf || type != OBJ_BLOB)
- die("unable to read blob object %s", sha1_to_hex(sha1));
+ die("unable to read blob object %s", oid_to_hex(oid));
xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
fd = xmkstemp(path);
int cmd_unpack_file(int argc, const char **argv, const char *prefix)
{
- unsigned char sha1[20];
+ struct object_id oid;
if (argc != 2 || !strcmp(argv[1], "-h"))
usage("git unpack-file <sha1>");
- if (get_sha1(argv[1], sha1))
+ if (get_oid(argv[1], &oid))
die("Not a valid object name %s", argv[1]);
git_config(git_default_config, NULL);
- puts(create_temp_file(sha1));
+ puts(create_temp_file(&oid));
return 0;
}
* provided but cannot be converted to a SHA-1, die. flags can
* include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY.
*/
-static int parse_next_sha1(struct strbuf *input, const char **next,
- unsigned char *sha1,
- const char *command, const char *refname,
- int flags)
+static int parse_next_oid(struct strbuf *input, const char **next,
+ struct object_id *oid,
+ const char *command, const char *refname,
+ int flags)
{
struct strbuf arg = STRBUF_INIT;
int ret = 0;
(*next)++;
*next = parse_arg(*next, &arg);
if (arg.len) {
- if (get_sha1(arg.buf, sha1))
+ if (get_oid(arg.buf, oid))
goto invalid;
} else {
/* Without -z, an empty value means all zeros: */
- hashclr(sha1);
+ oidclr(oid);
}
} else {
/* With -z, read the next NUL-terminated line */
*next += arg.len;
if (arg.len) {
- if (get_sha1(arg.buf, sha1))
+ if (get_oid(arg.buf, oid))
goto invalid;
} else if (flags & PARSE_SHA1_ALLOW_EMPTY) {
/* With -z, treat an empty value as all zeros: */
warning("%s %s: missing <newvalue>, treating as zero",
command, refname);
- hashclr(sha1);
+ oidclr(oid);
} else {
/*
* With -z, an empty non-required value means
{
struct strbuf err = STRBUF_INIT;
char *refname;
- unsigned char new_sha1[20];
- unsigned char old_sha1[20];
+ struct object_id new_oid, old_oid;
int have_old;
refname = parse_refname(input, &next);
if (!refname)
die("update: missing <ref>");
- if (parse_next_sha1(input, &next, new_sha1, "update", refname,
- PARSE_SHA1_ALLOW_EMPTY))
+ if (parse_next_oid(input, &next, &new_oid, "update", refname,
+ PARSE_SHA1_ALLOW_EMPTY))
die("update %s: missing <newvalue>", refname);
- have_old = !parse_next_sha1(input, &next, old_sha1, "update", refname,
- PARSE_SHA1_OLD);
+ have_old = !parse_next_oid(input, &next, &old_oid, "update", refname,
+ PARSE_SHA1_OLD);
if (*next != line_termination)
die("update %s: extra input: %s", refname, next);
if (ref_transaction_update(transaction, refname,
- new_sha1, have_old ? old_sha1 : NULL,
+ new_oid.hash, have_old ? old_oid.hash : NULL,
update_flags | create_reflog_flag,
msg, &err))
die("%s", err.buf);
{
struct strbuf err = STRBUF_INIT;
char *refname;
- unsigned char new_sha1[20];
+ struct object_id new_oid;
refname = parse_refname(input, &next);
if (!refname)
die("create: missing <ref>");
- if (parse_next_sha1(input, &next, new_sha1, "create", refname, 0))
+ if (parse_next_oid(input, &next, &new_oid, "create", refname, 0))
die("create %s: missing <newvalue>", refname);
- if (is_null_sha1(new_sha1))
+ if (is_null_oid(&new_oid))
die("create %s: zero <newvalue>", refname);
if (*next != line_termination)
die("create %s: extra input: %s", refname, next);
- if (ref_transaction_create(transaction, refname, new_sha1,
+ if (ref_transaction_create(transaction, refname, new_oid.hash,
update_flags | create_reflog_flag,
msg, &err))
die("%s", err.buf);
{
struct strbuf err = STRBUF_INIT;
char *refname;
- unsigned char old_sha1[20];
+ struct object_id old_oid;
int have_old;
refname = parse_refname(input, &next);
if (!refname)
die("delete: missing <ref>");
- if (parse_next_sha1(input, &next, old_sha1, "delete", refname,
- PARSE_SHA1_OLD)) {
+ if (parse_next_oid(input, &next, &old_oid, "delete", refname,
+ PARSE_SHA1_OLD)) {
have_old = 0;
} else {
- if (is_null_sha1(old_sha1))
+ if (is_null_oid(&old_oid))
die("delete %s: zero <oldvalue>", refname);
have_old = 1;
}
die("delete %s: extra input: %s", refname, next);
if (ref_transaction_delete(transaction, refname,
- have_old ? old_sha1 : NULL,
+ have_old ? old_oid.hash : NULL,
update_flags, msg, &err))
die("%s", err.buf);
{
struct strbuf err = STRBUF_INIT;
char *refname;
- unsigned char old_sha1[20];
+ struct object_id old_oid;
refname = parse_refname(input, &next);
if (!refname)
die("verify: missing <ref>");
- if (parse_next_sha1(input, &next, old_sha1, "verify", refname,
- PARSE_SHA1_OLD))
- hashclr(old_sha1);
+ if (parse_next_oid(input, &next, &old_oid, "verify", refname,
+ PARSE_SHA1_OLD))
+ oidclr(&old_oid);
if (*next != line_termination)
die("verify %s: extra input: %s", refname, next);
- if (ref_transaction_verify(transaction, refname, old_sha1,
+ if (ref_transaction_verify(transaction, refname, old_oid.hash,
update_flags, &err))
die("%s", err.buf);
int cmd_update_ref(int argc, const char **argv, const char *prefix)
{
const char *refname, *oldval;
- unsigned char sha1[20], oldsha1[20];
+ struct object_id oid, oldoid;
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
unsigned int flags = 0;
int create_reflog = 0;
refname = argv[0];
value = argv[1];
oldval = argv[2];
- if (get_sha1(value, sha1))
+ if (get_oid(value, &oid))
die("%s: not a valid SHA1", value);
}
* The empty string implies that the reference
* must not already exist:
*/
- hashclr(oldsha1);
- else if (get_sha1(oldval, oldsha1))
+ oidclr(&oldoid);
+ else if (get_oid(oldval, &oldoid))
die("%s: not a valid old SHA1", oldval);
}
* NULL_SHA1 as "don't care" here:
*/
return delete_ref(msg, refname,
- (oldval && !is_null_sha1(oldsha1)) ? oldsha1 : NULL,
+ (oldval && !is_null_oid(&oldoid)) ? oldoid.hash : NULL,
flags);
else
- return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
+ return update_ref(msg, refname, oid.hash, oldval ? oldoid.hash : NULL,
flags | create_reflog_flag,
UPDATE_REFS_DIE_ON_ERR);
}
{
int i = 1, verbose = 0, had_error = 0;
unsigned flags = 0;
- char *fmt_pretty = NULL;
+ struct ref_format format = REF_FORMAT_INIT;
const struct option verify_tag_options[] = {
OPT__VERBOSE(&verbose, N_("print tag contents")),
OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
- OPT_STRING( 0 , "format", &fmt_pretty, N_("format"), N_("format to use for the output")),
+ OPT_STRING(0, "format", &format.format, N_("format"), N_("format to use for the output")),
OPT_END()
};
if (verbose)
flags |= GPG_VERIFY_VERBOSE;
- if (fmt_pretty) {
- verify_ref_format(fmt_pretty);
+ if (format.format) {
+ if (verify_ref_format(&format))
+ usage_with_options(verify_tag_usage,
+ verify_tag_options);
flags |= GPG_VERIFY_OMIT_STATUS;
}
while (i < argc) {
- unsigned char sha1[20];
+ struct object_id oid;
const char *name = argv[i++];
- if (get_sha1(name, sha1)) {
+
+ if (get_oid(name, &oid)) {
had_error = !!error("tag '%s' not found.", name);
continue;
}
- if (gpg_verify_tag(sha1, name, flags)) {
+ if (gpg_verify_tag(&oid, name, flags)) {
had_error = 1;
continue;
}
- if (fmt_pretty)
- pretty_print_ref(name, sha1, fmt_pretty);
+ if (format.format)
+ pretty_print_ref(name, oid.hash, &format);
}
return had_error;
}
* move 4 and 5 up one place (2 entries)
* 2 = 6 - 3 - 1 = subtree_nr - pos - 1
*/
- memmove(it->down+pos, it->down+pos+1,
- sizeof(struct cache_tree_sub *) *
- (it->subtree_nr - pos - 1));
+ MOVE_ARRAY(it->down + pos, it->down + pos + 1,
+ it->subtree_nr - pos - 1);
it->subtree_nr--;
}
return 1;
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
#define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
#define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
-#define GIT_TOPLEVEL_PREFIX_ENVIRONMENT "GIT_INTERNAL_TOPLEVEL_PREFIX"
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
#define GITATTRIBUTES_FILE ".gitattributes"
#define INFOATTRIBUTES_FILE "info/attributes"
#define ATTRIBUTE_MACRO_PREFIX "[attr]"
+#define GITMODULES_FILE ".gitmodules"
#define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
#define GIT_NOTES_DISPLAY_REF_ENVIRONMENT "GIT_NOTES_DISPLAY_REF"
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
{
- int i;
-
- for (i = 0; i < GIT_SHA1_RAWSZ; i++, sha1++, sha2++) {
- if (*sha1 != *sha2)
- return *sha1 - *sha2;
- }
-
- return 0;
+ return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
}
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
*/
struct strbuf symlink_path;
/*
- * If GET_SHA1_RECORD_PATH is set, this will record path (if any)
+ * If GET_OID_RECORD_PATH is set, this will record path (if any)
* found when resolving the name. The caller is responsible for
* releasing the memory.
*/
char *path;
};
-#define GET_SHA1_QUIETLY 01
-#define GET_SHA1_COMMIT 02
-#define GET_SHA1_COMMITTISH 04
-#define GET_SHA1_TREE 010
-#define GET_SHA1_TREEISH 020
-#define GET_SHA1_BLOB 040
-#define GET_SHA1_FOLLOW_SYMLINKS 0100
-#define GET_SHA1_RECORD_PATH 0200
-#define GET_SHA1_ONLY_TO_DIE 04000
-
-#define GET_SHA1_DISAMBIGUATORS \
- (GET_SHA1_COMMIT | GET_SHA1_COMMITTISH | \
- GET_SHA1_TREE | GET_SHA1_TREEISH | \
- GET_SHA1_BLOB)
-
-extern int get_sha1(const char *str, unsigned char *sha1);
-extern int get_sha1_commit(const char *str, unsigned char *sha1);
-extern int get_sha1_committish(const char *str, unsigned char *sha1);
-extern int get_sha1_tree(const char *str, unsigned char *sha1);
-extern int get_sha1_treeish(const char *str, unsigned char *sha1);
-extern int get_sha1_blob(const char *str, unsigned char *sha1);
-extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
-extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc);
+#define GET_OID_QUIETLY 01
+#define GET_OID_COMMIT 02
+#define GET_OID_COMMITTISH 04
+#define GET_OID_TREE 010
+#define GET_OID_TREEISH 020
+#define GET_OID_BLOB 040
+#define GET_OID_FOLLOW_SYMLINKS 0100
+#define GET_OID_RECORD_PATH 0200
+#define GET_OID_ONLY_TO_DIE 04000
+
+#define GET_OID_DISAMBIGUATORS \
+ (GET_OID_COMMIT | GET_OID_COMMITTISH | \
+ GET_OID_TREE | GET_OID_TREEISH | \
+ GET_OID_BLOB)
extern int get_oid(const char *str, struct object_id *oid);
+extern int get_oid_commit(const char *str, struct object_id *oid);
+extern int get_oid_committish(const char *str, struct object_id *oid);
+extern int get_oid_tree(const char *str, struct object_id *oid);
+extern int get_oid_treeish(const char *str, struct object_id *oid);
+extern int get_oid_blob(const char *str, struct object_id *oid);
+extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
+extern int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc);
+
typedef int each_abbrev_fn(const struct object_id *oid, void *);
extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
struct index_state *istate;
const char *base_dir;
int base_dir_len;
+ struct delayed_checkout *delayed_checkout;
unsigned force:1,
quiet:1,
not_new:1,
#define TEMPORARY_FILENAME_LENGTH 25
extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
+extern void enable_delayed_checkout(struct checkout *state);
+extern int finish_delayed_checkout(struct checkout *state);
struct cache_def {
struct strbuf path;
#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
#define WS_TAB_WIDTH_MASK 077
+/* All WS_* -- when extended, adapt diff.c emit_symbol */
+#define WS_RULE_MASK 07777
extern unsigned whitespace_rule_cfg;
extern unsigned whitespace_rule(const char *);
extern unsigned parse_whitespace_rule(const char *);
return 0;
}
-int git_color_default_config(const char *var, const char *value, void *cb)
-{
- if (git_color_config(var, value, cb) < 0)
- return -1;
-
- return git_default_config(var, value, cb);
-}
-
void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
{
if (*color)
#define GIT_COLOR_BG_BLUE "\033[44m"
#define GIT_COLOR_BG_MAGENTA "\033[45m"
#define GIT_COLOR_BG_CYAN "\033[46m"
+#define GIT_COLOR_FAINT "\033[2m"
+#define GIT_COLOR_FAINT_ITALIC "\033[2;3m"
/* A special value meaning "no color selected" */
#define GIT_COLOR_NIL "NIL"
struct object_id oid;
struct commit *commit;
- if (get_sha1_committish(name, oid.hash))
+ if (get_oid_committish(name, &oid))
return NULL;
commit = lookup_commit_reference(&oid);
if (parse_commit(commit))
commit_graft_prepared = 1;
}
-struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
+struct commit_graft *lookup_commit_graft(const struct object_id *oid)
{
int pos;
prepare_commit_graft();
- pos = commit_graft_pos(sha1);
+ pos = commit_graft_pos(oid->hash);
if (pos < 0)
return NULL;
return commit_graft[pos];
if (pos < 0)
return -1;
if (pos + 1 < commit_graft_nr)
- memmove(commit_graft + pos, commit_graft + pos + 1,
- sizeof(struct commit_graft *)
- * (commit_graft_nr - pos - 1));
+ MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
+ commit_graft_nr - pos - 1);
commit_graft_nr--;
return 0;
}
bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
pptr = &item->parents;
- graft = lookup_commit_graft(item->object.oid.hash);
+ graft = lookup_commit_graft(&item->object.oid);
while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
struct commit *new_parent;
struct object *obj;
struct commit *commit;
struct object_id oid;
- if (get_sha1(name, oid.hash))
+ if (get_oid(name, &oid))
return NULL;
obj = parse_object(&oid);
commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
struct commit_graft *read_graft_line(char *buf, int len);
int register_commit_graft(struct commit_graft *, int);
-struct commit_graft *lookup_commit_graft(const unsigned char *sha1);
+struct commit_graft *lookup_commit_graft(const struct object_id *oid);
extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2);
extern struct commit_list *get_merge_bases_many(struct commit *one, int n, struct commit **twos);
#else
-#define get_be16(p) ( \
- (*((unsigned char *)(p) + 0) << 8) | \
- (*((unsigned char *)(p) + 1) << 0) )
-#define get_be32(p) ( \
- (*((unsigned char *)(p) + 0) << 24) | \
- (*((unsigned char *)(p) + 1) << 16) | \
- (*((unsigned char *)(p) + 2) << 8) | \
- (*((unsigned char *)(p) + 3) << 0) )
-#define put_be32(p, v) do { \
- unsigned int __v = (v); \
- *((unsigned char *)(p) + 0) = __v >> 24; \
- *((unsigned char *)(p) + 1) = __v >> 16; \
- *((unsigned char *)(p) + 2) = __v >> 8; \
- *((unsigned char *)(p) + 3) = __v >> 0; } while (0)
+static inline uint16_t get_be16(const void *ptr)
+{
+ const unsigned char *p = ptr;
+ return (uint16_t)p[0] << 8 |
+ (uint16_t)p[1] << 0;
+}
+
+static inline uint32_t get_be32(const void *ptr)
+{
+ const unsigned char *p = ptr;
+ return (uint32_t)p[0] << 24 |
+ (uint32_t)p[1] << 16 |
+ (uint32_t)p[2] << 8 |
+ (uint32_t)p[3] << 0;
+}
+
+static inline void put_be32(void *ptr, uint32_t value)
+{
+ unsigned char *p = ptr;
+ p[0] = value >> 24;
+ p[1] = value >> 16;
+ p[2] = value >> 8;
+ p[3] = value >> 0;
+}
#endif
va_end(ap);
while ((pos = strstr(str, "%1")) != NULL) {
+ char *oldstr = str;
str = realloc(str, st_add(++str_len, 1));
if (!str) {
+ free(oldstr);
warning_errno("realloc failed");
return;
}
#include "string-list.h"
#include "utf8.h"
#include "dir.h"
+#include "color.h"
struct config_source {
struct config_source *prev;
return ret;
}
-int git_parse_maybe_bool(const char *value)
+static int git_parse_maybe_bool_text(const char *value)
{
if (!value)
return 1;
return -1;
}
-int git_config_maybe_bool(const char *name, const char *value)
+int git_parse_maybe_bool(const char *value)
{
- int v = git_parse_maybe_bool(value);
+ int v = git_parse_maybe_bool_text(value);
if (0 <= v)
return v;
if (git_parse_int(value, &v))
return -1;
}
+int git_config_maybe_bool(const char *name, const char *value)
+{
+ return git_parse_maybe_bool(value);
+}
+
int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
{
- int v = git_parse_maybe_bool(value);
+ int v = git_parse_maybe_bool_text(value);
if (0 <= v) {
*is_bool = 1;
return v;
if (starts_with(var, "advice."))
return git_default_advice_config(var, value);
+ if (git_color_config(var, value, dummy) < 0)
+ return -1;
+
if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
pager_use_color = git_config_bool(var,value);
return 0;
return do_config_from(&top, fn, data);
}
-int git_config_from_blob_sha1(config_fn_t fn,
+int git_config_from_blob_oid(config_fn_t fn,
const char *name,
- const unsigned char *sha1,
+ const struct object_id *oid,
void *data)
{
enum object_type type;
unsigned long size;
int ret;
- buf = read_sha1_file(sha1, &type, &size);
+ buf = read_sha1_file(oid->hash, &type, &size);
if (!buf)
return error("unable to load config blob object '%s'", name);
if (type != OBJ_BLOB) {
const char *name,
void *data)
{
- unsigned char sha1[20];
+ struct object_id oid;
- if (get_sha1(name, sha1) < 0)
+ if (get_oid(name, &oid) < 0)
return error("unable to resolve config blob '%s'", name);
- return git_config_from_blob_sha1(fn, name, sha1, data);
+ return git_config_from_blob_oid(fn, name, &oid, data);
}
const char *git_etc_gitconfig(void)
}
static int config_set_element_cmp(const void *unused_cmp_data,
- const struct config_set_element *e1,
- const struct config_set_element *e2,
+ const void *entry,
+ const void *entry_or_key,
const void *unused_keydata)
{
+ const struct config_set_element *e1 = entry;
+ const struct config_set_element *e2 = entry_or_key;
+
return strcmp(e1->key, e2->key);
}
void git_configset_init(struct config_set *cs)
{
- hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
- NULL, 0);
+ hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0);
cs->hash_initialized = 1;
cs->list.nr = 0;
cs->list.alloc = 0;
{
const char *value;
if (!git_configset_get_value(cs, key, &value)) {
- *dest = git_config_maybe_bool(key, value);
+ *dest = git_parse_maybe_bool(value);
if (*dest == -1)
return -1;
return 0;
return repo_config_get_pathname(the_repository, key, dest);
}
+/*
+ * Note: This function exists solely to maintain backward compatibility with
+ * 'fetch' and 'update_clone' storing configuration in '.gitmodules' and should
+ * NOT be used anywhere else.
+ *
+ * Runs the provided config function on the '.gitmodules' file found in the
+ * working directory.
+ */
+void config_from_gitmodules(config_fn_t fn, void *data)
+{
+ if (the_repository->worktree) {
+ char *file = repo_worktree_path(the_repository, GITMODULES_FILE);
+ git_config_from_file(fn, file, data);
+ free(file);
+ }
+}
+
int git_config_get_expiry(const char *key, const char **output)
{
int ret = git_config_get_string_const(key, output);
extern int git_config_from_file(config_fn_t fn, const char *, void *);
extern int git_config_from_mem(config_fn_t fn, const enum config_origin_type,
const char *name, const char *buf, size_t len, void *data);
-extern int git_config_from_blob_sha1(config_fn_t fn, const char *name,
- const unsigned char *sha1, void *data);
+extern int git_config_from_blob_oid(config_fn_t fn, const char *name,
+ const struct object_id *oid, void *data);
extern void git_config_push_parameter(const char *text);
extern int git_config_from_parameters(config_fn_t fn, void *data);
extern void read_early_config(config_fn_t cb, void *data);
extern int repo_config_get_pathname(struct repository *repo,
const char *key, const char **dest);
+/*
+ * Note: This function exists solely to maintain backward compatibility with
+ * 'fetch' and 'update_clone' storing configuration in '.gitmodules' and should
+ * NOT be used anywhere else.
+ *
+ * Runs the provided config function on the '.gitmodules' file found in the
+ * working directory.
+ */
+extern void config_from_gitmodules(config_fn_t fn, void *data);
+
extern int git_config_get_value(const char *key, const char **value);
extern const struct string_list *git_config_get_value_multi(const char *key);
extern void git_config_clear(void);
- memcpy(dst, src, n * sizeof(T));
+ COPY_ARRAY(dst, src, n);
+@@
+type T;
+T *dst;
+T *src;
+expression n;
+@@
+(
+- memmove(dst, src, (n) * sizeof(*dst));
++ MOVE_ARRAY(dst, src, n);
+|
+- memmove(dst, src, (n) * sizeof(*src));
++ MOVE_ARRAY(dst, src, n);
+|
+- memmove(dst, src, (n) * sizeof(T));
++ MOVE_ARRAY(dst, src, n);
+)
+
@@
type T;
T *ptr;
my $since = '5-years-ago';
my $min_percent = 10;
-my $labels_rx = qr/Signed-off-by|Reviewed-by|Acked-by|Cc/i;
+my $labels_rx = qr/Signed-off-by|Reviewed-by|Acked-by|Cc|Reported-by/i;
my %seen;
sub format_contact {
# Prime rerere database from existing merge commits
me=rerere-train
-USAGE="$me rev-list-args"
+USAGE=$(cat <<-EOF
+usage: $me [--overwrite] <rev-list-args>
+
+ -h, --help show the help
+ -o, --overwrite overwrite any existing rerere cache
+EOF
+)
SUBDIRECTORY_OK=Yes
-OPTIONS_SPEC=
+
+overwrite=0
+
+while test $# -gt 0
+do
+ opt="$1"
+ case "$opt" in
+ -h|--help)
+ echo "$USAGE"
+ exit 0
+ ;;
+ -o|--overwrite)
+ overwrite=1
+ shift
+ break
+ ;;
+ --)
+ shift
+ break
+ ;;
+ *)
+ break
+ ;;
+ esac
+done
+
+# Overwrite or help options are not valid except as first arg
+for opt in "$@"
+do
+ case "$opt" in
+ -h|--help)
+ echo "$USAGE"
+ exit 0
+ ;;
+ -o|--overwrite)
+ echo "$USAGE"
+ exit 0
+ ;;
+ esac
+done
+
. "$(git --exec-path)/git-sh-setup"
require_work_tree
cd_to_toplevel
# Cleanly merges
continue
fi
+ if test $overwrite = 1
+ then
+ git rerere forget .
+ fi
if test -s "$GIT_DIR/MERGE_RR"
then
git show -s --pretty=format:"Learning from %h %s" "$commit"
#define CAP_CLEAN (1u<<0)
#define CAP_SMUDGE (1u<<1)
+#define CAP_DELAY (1u<<2)
struct cmd2process {
struct subprocess_entry subprocess; /* must be the first member! */
static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
{
- int err;
+ static int versions[] = {2, 0};
+ static struct subprocess_capability capabilities[] = {
+ { "clean", CAP_CLEAN },
+ { "smudge", CAP_SMUDGE },
+ { "delay", CAP_DELAY },
+ { NULL, 0 }
+ };
struct cmd2process *entry = (struct cmd2process *)subprocess;
- struct string_list cap_list = STRING_LIST_INIT_NODUP;
- char *cap_buf;
- const char *cap_name;
- struct child_process *process = &subprocess->process;
- const char *cmd = subprocess->cmd;
-
- sigchain_push(SIGPIPE, SIG_IGN);
-
- err = packet_writel(process->in, "git-filter-client", "version=2", NULL);
- if (err)
- goto done;
-
- err = strcmp(packet_read_line(process->out, NULL), "git-filter-server");
- if (err) {
- error("external filter '%s' does not support filter protocol version 2", cmd);
- goto done;
- }
- err = strcmp(packet_read_line(process->out, NULL), "version=2");
- if (err)
- goto done;
- err = packet_read_line(process->out, NULL) != NULL;
- if (err)
- goto done;
-
- err = packet_writel(process->in, "capability=clean", "capability=smudge", NULL);
-
- for (;;) {
- cap_buf = packet_read_line(process->out, NULL);
- if (!cap_buf)
- break;
- string_list_split_in_place(&cap_list, cap_buf, '=', 1);
-
- if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability"))
- continue;
-
- cap_name = cap_list.items[1].string;
- if (!strcmp(cap_name, "clean")) {
- entry->supported_capabilities |= CAP_CLEAN;
- } else if (!strcmp(cap_name, "smudge")) {
- entry->supported_capabilities |= CAP_SMUDGE;
- } else {
- warning(
- "external filter '%s' requested unsupported filter capability '%s'",
- cmd, cap_name
- );
- }
+ return subprocess_handshake(subprocess, "git-filter", versions, NULL,
+ capabilities,
+ &entry->supported_capabilities);
+}
- string_list_clear(&cap_list, 0);
+static void handle_filter_error(const struct strbuf *filter_status,
+ struct cmd2process *entry,
+ const unsigned int wanted_capability) {
+ if (!strcmp(filter_status->buf, "error"))
+ ; /* The filter signaled a problem with the file. */
+ else if (!strcmp(filter_status->buf, "abort") && wanted_capability) {
+ /*
+ * The filter signaled a permanent problem. Don't try to filter
+ * files with the same command for the lifetime of the current
+ * Git process.
+ */
+ entry->supported_capabilities &= ~wanted_capability;
+ } else {
+ /*
+ * Something went wrong with the protocol filter.
+ * Force shutdown and restart if another blob requires filtering.
+ */
+ error("external filter '%s' failed", entry->subprocess.cmd);
+ subprocess_stop(&subprocess_map, &entry->subprocess);
+ free(entry);
}
-
-done:
- sigchain_pop(SIGPIPE);
-
- return err;
}
static int apply_multi_file_filter(const char *path, const char *src, size_t len,
int fd, struct strbuf *dst, const char *cmd,
- const unsigned int wanted_capability)
+ const unsigned int wanted_capability,
+ struct delayed_checkout *dco)
{
int err;
+ int can_delay = 0;
struct cmd2process *entry;
struct child_process *process;
struct strbuf nbuf = STRBUF_INIT;
if (!subprocess_map_initialized) {
subprocess_map_initialized = 1;
- hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
- NULL, 0);
+ hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
entry = NULL;
} else {
entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
}
process = &entry->subprocess.process;
- if (!(wanted_capability & entry->supported_capabilities))
+ if (!(entry->supported_capabilities & wanted_capability))
return 0;
- if (CAP_CLEAN & wanted_capability)
+ if (wanted_capability & CAP_CLEAN)
filter_type = "clean";
- else if (CAP_SMUDGE & wanted_capability)
+ else if (wanted_capability & CAP_SMUDGE)
filter_type = "smudge";
else
die("unexpected filter type");
if (err)
goto done;
+ if ((entry->supported_capabilities & CAP_DELAY) &&
+ dco && dco->state == CE_CAN_DELAY) {
+ can_delay = 1;
+ err = packet_write_fmt_gently(process->in, "can-delay=1\n");
+ if (err)
+ goto done;
+ }
+
err = packet_flush_gently(process->in);
if (err)
goto done;
if (err)
goto done;
- err = strcmp(filter_status.buf, "success");
+ if (can_delay && !strcmp(filter_status.buf, "delayed")) {
+ string_list_insert(&dco->filters, cmd);
+ string_list_insert(&dco->paths, path);
+ } else {
+ /* The filter got the blob and wants to send us a response. */
+ err = strcmp(filter_status.buf, "success");
+ if (err)
+ goto done;
+
+ err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
+ if (err)
+ goto done;
+
+ err = subprocess_read_status(process->out, &filter_status);
+ if (err)
+ goto done;
+
+ err = strcmp(filter_status.buf, "success");
+ }
+
+done:
+ sigchain_pop(SIGPIPE);
+
+ if (err)
+ handle_filter_error(&filter_status, entry, wanted_capability);
+ else
+ strbuf_swap(dst, &nbuf);
+ strbuf_release(&nbuf);
+ return !err;
+}
+
+
+int async_query_available_blobs(const char *cmd, struct string_list *available_paths)
+{
+ int err;
+ char *line;
+ struct cmd2process *entry;
+ struct child_process *process;
+ struct strbuf filter_status = STRBUF_INIT;
+
+ assert(subprocess_map_initialized);
+ entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
+ if (!entry) {
+ error("external filter '%s' is not available anymore although "
+ "not all paths have been filtered", cmd);
+ return 0;
+ }
+ process = &entry->subprocess.process;
+ sigchain_push(SIGPIPE, SIG_IGN);
+
+ err = packet_write_fmt_gently(
+ process->in, "command=list_available_blobs\n");
if (err)
goto done;
- err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
+ err = packet_flush_gently(process->in);
if (err)
goto done;
+ while ((line = packet_read_line(process->out, NULL))) {
+ const char *path;
+ if (skip_prefix(line, "pathname=", &path))
+ string_list_insert(available_paths, xstrdup(path));
+ else
+ ; /* ignore unknown keys */
+ }
+
err = subprocess_read_status(process->out, &filter_status);
if (err)
goto done;
done:
sigchain_pop(SIGPIPE);
- if (err) {
- if (!strcmp(filter_status.buf, "error")) {
- /* The filter signaled a problem with the file. */
- } else if (!strcmp(filter_status.buf, "abort")) {
- /*
- * The filter signaled a permanent problem. Don't try to filter
- * files with the same command for the lifetime of the current
- * Git process.
- */
- entry->supported_capabilities &= ~wanted_capability;
- } else {
- /*
- * Something went wrong with the protocol filter.
- * Force shutdown and restart if another blob requires filtering.
- */
- error("external filter '%s' failed", cmd);
- subprocess_stop(&subprocess_map, &entry->subprocess);
- free(entry);
- }
- } else {
- strbuf_swap(dst, &nbuf);
- }
- strbuf_release(&nbuf);
+ if (err)
+ handle_filter_error(&filter_status, entry, 0);
return !err;
}
static int apply_filter(const char *path, const char *src, size_t len,
int fd, struct strbuf *dst, struct convert_driver *drv,
- const unsigned int wanted_capability)
+ const unsigned int wanted_capability,
+ struct delayed_checkout *dco)
{
const char *cmd = NULL;
if (!dst)
return 1;
- if ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean)
+ if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean)
cmd = drv->clean;
- else if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge)
+ else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge)
cmd = drv->smudge;
if (cmd && *cmd)
return apply_single_file_filter(path, src, len, fd, dst, cmd);
else if (drv->process && *drv->process)
- return apply_multi_file_filter(path, src, len, fd, dst, drv->process, wanted_capability);
+ return apply_multi_file_filter(path, src, len, fd, dst,
+ drv->process, wanted_capability, dco);
return 0;
}
if (!ca.drv->required)
return 0;
- return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN);
+ return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL);
}
const char *get_convert_attr_ascii(const char *path)
convert_attrs(&ca, path);
- ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN);
+ ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL);
if (!ret && ca.drv && ca.drv->required)
die("%s: clean filter '%s' failed", path, ca.drv->name);
assert(ca.drv);
assert(ca.drv->clean || ca.drv->process);
- if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
+ if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL))
die("%s: clean filter '%s' failed", path, ca.drv->name);
crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
static int convert_to_working_tree_internal(const char *path, const char *src,
size_t len, struct strbuf *dst,
- int normalizing)
+ int normalizing, struct delayed_checkout *dco)
{
int ret = 0, ret_filter = 0;
struct conv_attrs ca;
}
}
- ret_filter = apply_filter(path, src, len, -1, dst, ca.drv, CAP_SMUDGE);
+ ret_filter = apply_filter(
+ path, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco);
if (!ret_filter && ca.drv && ca.drv->required)
die("%s: smudge filter %s failed", path, ca.drv->name);
return ret | ret_filter;
}
+int async_convert_to_working_tree(const char *path, const char *src,
+ size_t len, struct strbuf *dst,
+ void *dco)
+{
+ return convert_to_working_tree_internal(path, src, len, dst, 0, dco);
+}
+
int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
{
- return convert_to_working_tree_internal(path, src, len, dst, 0);
+ return convert_to_working_tree_internal(path, src, len, dst, 0, NULL);
}
int renormalize_buffer(const struct index_state *istate, const char *path,
const char *src, size_t len, struct strbuf *dst)
{
- int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
+ int ret = convert_to_working_tree_internal(path, src, len, dst, 1, NULL);
if (ret) {
src = dst->buf;
len = dst->len;
#ifndef CONVERT_H
#define CONVERT_H
+#include "string-list.h"
+
struct index_state;
enum safe_crlf {
#endif
};
+enum ce_delay_state {
+ CE_NO_DELAY = 0,
+ CE_CAN_DELAY = 1,
+ CE_RETRY = 2
+};
+
+struct delayed_checkout {
+ /*
+ * State of the currently processed cache entry. If the state is
+ * CE_CAN_DELAY, then the filter can delay the current cache entry.
+ * If the state is CE_RETRY, then this signals the filter that the
+ * cache entry was requested before.
+ */
+ enum ce_delay_state state;
+ /* List of filter drivers that signaled delayed blobs. */
+ struct string_list filters;
+ /* List of delayed blobs identified by their path. */
+ struct string_list paths;
+};
+
extern enum eol core_eol;
extern const char *get_cached_convert_stats_ascii(const struct index_state *istate,
const char *path);
struct strbuf *dst, enum safe_crlf checksafe);
extern int convert_to_working_tree(const char *path, const char *src,
size_t len, struct strbuf *dst);
+extern int async_convert_to_working_tree(const char *path, const char *src,
+ size_t len, struct strbuf *dst,
+ void *dco);
+extern int async_query_available_blobs(const char *cmd, struct string_list *available_paths);
extern int renormalize_buffer(const struct index_state *istate,
const char *path, const char *src, size_t len,
struct strbuf *dst);
int r;
r = read_in_full(fd, in, sizeof(in));
- if (r == 0)
+ if (r == 0 || (r < 0 && errno == ECONNRESET))
break;
if (r < 0)
die_errno("read error from cache daemon");
#include "userdiff.h"
#include "submodule-config.h"
#include "submodule.h"
+#include "hashmap.h"
#include "ll-merge.h"
#include "string-list.h"
#include "argv-array.h"
static int diff_rename_limit_default = 400;
static int diff_suppress_blank_empty;
static int diff_use_color_default = -1;
+static int diff_color_moved_default;
static int diff_context_default = 3;
static int diff_interhunk_context_default;
static const char *diff_word_regex_cfg;
GIT_COLOR_YELLOW, /* COMMIT */
GIT_COLOR_BG_RED, /* WHITESPACE */
GIT_COLOR_NORMAL, /* FUNCINFO */
+ GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
+ GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
+ GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
+ GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
+ GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
+ GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
+ GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
+ GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
};
static NORETURN void die_want_option(const char *option_name)
return DIFF_WHITESPACE;
if (!strcasecmp(var, "func"))
return DIFF_FUNCINFO;
+ if (!strcasecmp(var, "oldmoved"))
+ return DIFF_FILE_OLD_MOVED;
+ if (!strcasecmp(var, "oldmovedalternative"))
+ return DIFF_FILE_OLD_MOVED_ALT;
+ if (!strcasecmp(var, "oldmoveddimmed"))
+ return DIFF_FILE_OLD_MOVED_DIM;
+ if (!strcasecmp(var, "oldmovedalternativedimmed"))
+ return DIFF_FILE_OLD_MOVED_ALT_DIM;
+ if (!strcasecmp(var, "newmoved"))
+ return DIFF_FILE_NEW_MOVED;
+ if (!strcasecmp(var, "newmovedalternative"))
+ return DIFF_FILE_NEW_MOVED_ALT;
+ if (!strcasecmp(var, "newmoveddimmed"))
+ return DIFF_FILE_NEW_MOVED_DIM;
+ if (!strcasecmp(var, "newmovedalternativedimmed"))
+ return DIFF_FILE_NEW_MOVED_ALT_DIM;
return -1;
}
return 0;
}
+static int parse_color_moved(const char *arg)
+{
+ switch (git_parse_maybe_bool(arg)) {
+ case 0:
+ return COLOR_MOVED_NO;
+ case 1:
+ return COLOR_MOVED_DEFAULT;
+ default:
+ break;
+ }
+
+ if (!strcmp(arg, "no"))
+ return COLOR_MOVED_NO;
+ else if (!strcmp(arg, "plain"))
+ return COLOR_MOVED_PLAIN;
+ else if (!strcmp(arg, "zebra"))
+ return COLOR_MOVED_ZEBRA;
+ else if (!strcmp(arg, "default"))
+ return COLOR_MOVED_DEFAULT;
+ else if (!strcmp(arg, "dimmed_zebra"))
+ return COLOR_MOVED_ZEBRA_DIM;
+ else
+ return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
+}
+
int git_diff_ui_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
diff_use_color_default = git_config_colorbool(var, value);
return 0;
}
+ if (!strcmp(var, "diff.colormoved")) {
+ int cm = parse_color_moved(value);
+ if (cm < 0)
+ return -1;
+ diff_color_moved_default = cm;
+ return 0;
+ }
if (!strcmp(var, "diff.context")) {
diff_context_default = git_config_int(var, value);
if (diff_context_default < 0)
return 0;
}
- if (git_color_config(var, value, cb) < 0)
- return -1;
-
return git_diff_basic_config(var, value, cb);
}
emit_line_0(o, set, reset, line[0], line+1, len-1);
}
-static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
+enum diff_symbol {
+ DIFF_SYMBOL_BINARY_DIFF_HEADER,
+ DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
+ DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
+ DIFF_SYMBOL_BINARY_DIFF_BODY,
+ DIFF_SYMBOL_BINARY_DIFF_FOOTER,
+ DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
+ DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
+ DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
+ DIFF_SYMBOL_STATS_LINE,
+ DIFF_SYMBOL_WORD_DIFF,
+ DIFF_SYMBOL_STAT_SEP,
+ DIFF_SYMBOL_SUMMARY,
+ DIFF_SYMBOL_SUBMODULE_ADD,
+ DIFF_SYMBOL_SUBMODULE_DEL,
+ DIFF_SYMBOL_SUBMODULE_UNTRACKED,
+ DIFF_SYMBOL_SUBMODULE_MODIFIED,
+ DIFF_SYMBOL_SUBMODULE_HEADER,
+ DIFF_SYMBOL_SUBMODULE_ERROR,
+ DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
+ DIFF_SYMBOL_REWRITE_DIFF,
+ DIFF_SYMBOL_BINARY_FILES,
+ DIFF_SYMBOL_HEADER,
+ DIFF_SYMBOL_FILEPAIR_PLUS,
+ DIFF_SYMBOL_FILEPAIR_MINUS,
+ DIFF_SYMBOL_WORDS_PORCELAIN,
+ DIFF_SYMBOL_WORDS,
+ DIFF_SYMBOL_CONTEXT,
+ DIFF_SYMBOL_CONTEXT_INCOMPLETE,
+ DIFF_SYMBOL_PLUS,
+ DIFF_SYMBOL_MINUS,
+ DIFF_SYMBOL_NO_LF_EOF,
+ DIFF_SYMBOL_CONTEXT_FRAGINFO,
+ DIFF_SYMBOL_CONTEXT_MARKER,
+ DIFF_SYMBOL_SEPARATOR
+};
+/*
+ * Flags for content lines:
+ * 0..12 are whitespace rules
+ * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
+ * 16 is marking if the line is blank at EOF
+ */
+#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
+#define DIFF_SYMBOL_MOVED_LINE (1<<17)
+#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
+#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
+#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
+
+/*
+ * This struct is used when we need to buffer the output of the diff output.
+ *
+ * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
+ * into the pre/post image file. This pointer could be a union with the
+ * line pointer. By storing an offset into the file instead of the literal line,
+ * we can decrease the memory footprint for the buffered output. At first we
+ * may want to only have indirection for the content lines, but we could also
+ * enhance the state for emitting prefabricated lines, e.g. the similarity
+ * score line or hunk/file headers would only need to store a number or path
+ * and then the output can be constructed later on depending on state.
+ */
+struct emitted_diff_symbol {
+ const char *line;
+ int len;
+ int flags;
+ enum diff_symbol s;
+};
+#define EMITTED_DIFF_SYMBOL_INIT {NULL}
+
+struct emitted_diff_symbols {
+ struct emitted_diff_symbol *buf;
+ int nr, alloc;
+};
+#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
+
+static void append_emitted_diff_symbol(struct diff_options *o,
+ struct emitted_diff_symbol *e)
{
- if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
- ecbdata->blank_at_eof_in_preimage &&
- ecbdata->blank_at_eof_in_postimage &&
- ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
- ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
- return 0;
- return ws_blank_line(line, len, ecbdata->ws_rule);
+ struct emitted_diff_symbol *f;
+
+ ALLOC_GROW(o->emitted_symbols->buf,
+ o->emitted_symbols->nr + 1,
+ o->emitted_symbols->alloc);
+ f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
+
+ memcpy(f, e, sizeof(struct emitted_diff_symbol));
+ f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
}
-static void emit_line_checked(const char *reset,
- struct emit_callback *ecbdata,
- const char *line, int len,
- enum color_diff color,
- unsigned ws_error_highlight,
- char sign)
+struct moved_entry {
+ struct hashmap_entry ent;
+ const struct emitted_diff_symbol *es;
+ struct moved_entry *next_line;
+};
+
+static int next_byte(const char **cp, const char **endp,
+ const struct diff_options *diffopt)
+{
+ int retval;
+
+ if (*cp > *endp)
+ return -1;
+
+ if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
+ while (*cp < *endp && isspace(**cp))
+ (*cp)++;
+ /*
+ * After skipping a couple of whitespaces, we still have to
+ * account for one space.
+ */
+ return (int)' ';
+ }
+
+ if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
+ while (*cp < *endp && isspace(**cp))
+ (*cp)++;
+ /* return the first non-ws character via the usual below */
+ }
+
+ retval = (unsigned char)(**cp);
+ (*cp)++;
+ return retval;
+}
+
+static int moved_entry_cmp(const struct diff_options *diffopt,
+ const struct moved_entry *a,
+ const struct moved_entry *b,
+ const void *keydata)
+{
+ const char *ap = a->es->line, *ae = a->es->line + a->es->len;
+ const char *bp = b->es->line, *be = b->es->line + b->es->len;
+
+ if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
+ return a->es->len != b->es->len || memcmp(ap, bp, a->es->len);
+
+ if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
+ while (ae > ap && isspace(*ae))
+ ae--;
+ while (be > bp && isspace(*be))
+ be--;
+ }
+
+ while (1) {
+ int ca, cb;
+ ca = next_byte(&ap, &ae, diffopt);
+ cb = next_byte(&bp, &be, diffopt);
+ if (ca != cb)
+ return 1;
+ if (ca < 0)
+ return 0;
+ }
+}
+
+static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
+{
+ if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
+ static struct strbuf sb = STRBUF_INIT;
+ const char *ap = es->line, *ae = es->line + es->len;
+ int c;
+
+ strbuf_reset(&sb);
+ while (ae > ap && isspace(*ae))
+ ae--;
+ while ((c = next_byte(&ap, &ae, o)) > 0)
+ strbuf_addch(&sb, c);
+
+ return memhash(sb.buf, sb.len);
+ } else {
+ return memhash(es->line, es->len);
+ }
+}
+
+static struct moved_entry *prepare_entry(struct diff_options *o,
+ int line_no)
+{
+ struct moved_entry *ret = xmalloc(sizeof(*ret));
+ struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
+
+ ret->ent.hash = get_string_hash(l, o);
+ ret->es = l;
+ ret->next_line = NULL;
+
+ return ret;
+}
+
+static void add_lines_to_move_detection(struct diff_options *o,
+ struct hashmap *add_lines,
+ struct hashmap *del_lines)
+{
+ struct moved_entry *prev_line = NULL;
+
+ int n;
+ for (n = 0; n < o->emitted_symbols->nr; n++) {
+ struct hashmap *hm;
+ struct moved_entry *key;
+
+ switch (o->emitted_symbols->buf[n].s) {
+ case DIFF_SYMBOL_PLUS:
+ hm = add_lines;
+ break;
+ case DIFF_SYMBOL_MINUS:
+ hm = del_lines;
+ break;
+ default:
+ prev_line = NULL;
+ continue;
+ }
+
+ key = prepare_entry(o, n);
+ if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
+ prev_line->next_line = key;
+
+ hashmap_add(hm, key);
+ prev_line = key;
+ }
+}
+
+static int shrink_potential_moved_blocks(struct moved_entry **pmb,
+ int pmb_nr)
+{
+ int lp, rp;
+
+ /* Shrink the set of potential block to the remaining running */
+ for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
+ while (lp < pmb_nr && pmb[lp])
+ lp++;
+ /* lp points at the first NULL now */
+
+ while (rp > -1 && !pmb[rp])
+ rp--;
+ /* rp points at the last non-NULL */
+
+ if (lp < pmb_nr && rp > -1 && lp < rp) {
+ pmb[lp] = pmb[rp];
+ pmb[rp] = NULL;
+ rp--;
+ lp++;
+ }
+ }
+
+ /* Remember the number of running sets */
+ return rp + 1;
+}
+
+/* Find blocks of moved code, delegate actual coloring decision to helper */
+static void mark_color_as_moved(struct diff_options *o,
+ struct hashmap *add_lines,
+ struct hashmap *del_lines)
+{
+ struct moved_entry **pmb = NULL; /* potentially moved blocks */
+ int pmb_nr = 0, pmb_alloc = 0;
+ int n, flipped_block = 1, block_length = 0;
+
+
+ for (n = 0; n < o->emitted_symbols->nr; n++) {
+ struct hashmap *hm = NULL;
+ struct moved_entry *key;
+ struct moved_entry *match = NULL;
+ struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
+ int i;
+
+ switch (l->s) {
+ case DIFF_SYMBOL_PLUS:
+ hm = del_lines;
+ key = prepare_entry(o, n);
+ match = hashmap_get(hm, key, o);
+ free(key);
+ break;
+ case DIFF_SYMBOL_MINUS:
+ hm = add_lines;
+ key = prepare_entry(o, n);
+ match = hashmap_get(hm, key, o);
+ free(key);
+ break;
+ default:
+ flipped_block = 1;
+ }
+
+ if (!match) {
+ if (block_length < COLOR_MOVED_MIN_BLOCK_LENGTH &&
+ o->color_moved != COLOR_MOVED_PLAIN) {
+ for (i = 0; i < block_length + 1; i++) {
+ l = &o->emitted_symbols->buf[n - i];
+ l->flags &= ~DIFF_SYMBOL_MOVED_LINE;
+ }
+ }
+ pmb_nr = 0;
+ block_length = 0;
+ continue;
+ }
+
+ l->flags |= DIFF_SYMBOL_MOVED_LINE;
+ block_length++;
+
+ if (o->color_moved == COLOR_MOVED_PLAIN)
+ continue;
+
+ /* Check any potential block runs, advance each or nullify */
+ for (i = 0; i < pmb_nr; i++) {
+ struct moved_entry *p = pmb[i];
+ struct moved_entry *pnext = (p && p->next_line) ?
+ p->next_line : NULL;
+ if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
+ pmb[i] = p->next_line;
+ } else {
+ pmb[i] = NULL;
+ }
+ }
+
+ pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
+
+ if (pmb_nr == 0) {
+ /*
+ * The current line is the start of a new block.
+ * Setup the set of potential blocks.
+ */
+ for (; match; match = hashmap_get_next(hm, match)) {
+ ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
+ pmb[pmb_nr++] = match;
+ }
+
+ flipped_block = (flipped_block + 1) % 2;
+ }
+
+ if (flipped_block)
+ l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
+ }
+
+ free(pmb);
+}
+
+#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
+ (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
+static void dim_moved_lines(struct diff_options *o)
+{
+ int n;
+ for (n = 0; n < o->emitted_symbols->nr; n++) {
+ struct emitted_diff_symbol *prev = (n != 0) ?
+ &o->emitted_symbols->buf[n - 1] : NULL;
+ struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
+ struct emitted_diff_symbol *next =
+ (n < o->emitted_symbols->nr - 1) ?
+ &o->emitted_symbols->buf[n + 1] : NULL;
+
+ /* Not a plus or minus line? */
+ if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
+ continue;
+
+ /* Not a moved line? */
+ if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
+ continue;
+
+ /*
+ * If prev or next are not a plus or minus line,
+ * pretend they don't exist
+ */
+ if (prev && prev->s != DIFF_SYMBOL_PLUS &&
+ prev->s != DIFF_SYMBOL_MINUS)
+ prev = NULL;
+ if (next && next->s != DIFF_SYMBOL_PLUS &&
+ next->s != DIFF_SYMBOL_MINUS)
+ next = NULL;
+
+ /* Inside a block? */
+ if ((prev &&
+ (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
+ (next &&
+ (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
+ l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
+ continue;
+ }
+
+ /* Check if we are at an interesting bound: */
+ if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
+ (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
+ continue;
+ if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
+ (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
+ continue;
+
+ /*
+ * The boundary to prev and next are not interesting,
+ * so this line is not interesting as a whole
+ */
+ l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
+ }
+}
+
+static void emit_line_ws_markup(struct diff_options *o,
+ const char *set, const char *reset,
+ const char *line, int len, char sign,
+ unsigned ws_rule, int blank_at_eof)
{
- const char *set = diff_get_color(ecbdata->color_diff, color);
const char *ws = NULL;
- if (ecbdata->opt->ws_error_highlight & ws_error_highlight) {
- ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
+ if (o->ws_error_highlight & ws_rule) {
+ ws = diff_get_color_opt(o, DIFF_WHITESPACE);
if (!*ws)
ws = NULL;
}
if (!ws)
- emit_line_0(ecbdata->opt, set, reset, sign, line, len);
- else if (sign == '+' && new_blank_line_at_eof(ecbdata, line, len))
+ emit_line_0(o, set, reset, sign, line, len);
+ else if (blank_at_eof)
/* Blank line at EOF - paint '+' as well */
- emit_line_0(ecbdata->opt, ws, reset, sign, line, len);
+ emit_line_0(o, ws, reset, sign, line, len);
else {
/* Emit just the prefix, then the rest. */
- emit_line_0(ecbdata->opt, set, reset, sign, "", 0);
- ws_check_emit(line, len, ecbdata->ws_rule,
- ecbdata->opt->file, set, reset, ws);
+ emit_line_0(o, set, reset, sign, "", 0);
+ ws_check_emit(line, len, ws_rule,
+ o->file, set, reset, ws);
}
}
+static void emit_diff_symbol_from_struct(struct diff_options *o,
+ struct emitted_diff_symbol *eds)
+{
+ static const char *nneof = " No newline at end of file\n";
+ const char *context, *reset, *set, *meta, *fraginfo;
+ struct strbuf sb = STRBUF_INIT;
+
+ enum diff_symbol s = eds->s;
+ const char *line = eds->line;
+ int len = eds->len;
+ unsigned flags = eds->flags;
+
+ switch (s) {
+ case DIFF_SYMBOL_NO_LF_EOF:
+ context = diff_get_color_opt(o, DIFF_CONTEXT);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ putc('\n', o->file);
+ emit_line_0(o, context, reset, '\\',
+ nneof, strlen(nneof));
+ break;
+ case DIFF_SYMBOL_SUBMODULE_HEADER:
+ case DIFF_SYMBOL_SUBMODULE_ERROR:
+ case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
+ case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
+ case DIFF_SYMBOL_SUMMARY:
+ case DIFF_SYMBOL_STATS_LINE:
+ case DIFF_SYMBOL_BINARY_DIFF_BODY:
+ case DIFF_SYMBOL_CONTEXT_FRAGINFO:
+ emit_line(o, "", "", line, len);
+ break;
+ case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
+ case DIFF_SYMBOL_CONTEXT_MARKER:
+ context = diff_get_color_opt(o, DIFF_CONTEXT);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line(o, context, reset, line, len);
+ break;
+ case DIFF_SYMBOL_SEPARATOR:
+ fprintf(o->file, "%s%c",
+ diff_line_prefix(o),
+ o->line_termination);
+ break;
+ case DIFF_SYMBOL_CONTEXT:
+ set = diff_get_color_opt(o, DIFF_CONTEXT);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line_ws_markup(o, set, reset, line, len, ' ',
+ flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
+ break;
+ case DIFF_SYMBOL_PLUS:
+ switch (flags & (DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_ALT |
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
+ case DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_ALT |
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+ set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
+ break;
+ case DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_ALT:
+ set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
+ break;
+ case DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+ set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
+ break;
+ case DIFF_SYMBOL_MOVED_LINE:
+ set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
+ break;
+ default:
+ set = diff_get_color_opt(o, DIFF_FILE_NEW);
+ }
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line_ws_markup(o, set, reset, line, len, '+',
+ flags & DIFF_SYMBOL_CONTENT_WS_MASK,
+ flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
+ break;
+ case DIFF_SYMBOL_MINUS:
+ switch (flags & (DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_ALT |
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
+ case DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_ALT |
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+ set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
+ break;
+ case DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_ALT:
+ set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
+ break;
+ case DIFF_SYMBOL_MOVED_LINE |
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+ set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
+ break;
+ case DIFF_SYMBOL_MOVED_LINE:
+ set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
+ break;
+ default:
+ set = diff_get_color_opt(o, DIFF_FILE_OLD);
+ }
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line_ws_markup(o, set, reset, line, len, '-',
+ flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
+ break;
+ case DIFF_SYMBOL_WORDS_PORCELAIN:
+ context = diff_get_color_opt(o, DIFF_CONTEXT);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line(o, context, reset, line, len);
+ fputs("~\n", o->file);
+ break;
+ case DIFF_SYMBOL_WORDS:
+ context = diff_get_color_opt(o, DIFF_CONTEXT);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ /*
+ * Skip the prefix character, if any. With
+ * diff_suppress_blank_empty, there may be
+ * none.
+ */
+ if (line[0] != '\n') {
+ line++;
+ len--;
+ }
+ emit_line(o, context, reset, line, len);
+ break;
+ case DIFF_SYMBOL_FILEPAIR_PLUS:
+ meta = diff_get_color_opt(o, DIFF_METAINFO);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
+ line, reset,
+ strchr(line, ' ') ? "\t" : "");
+ break;
+ case DIFF_SYMBOL_FILEPAIR_MINUS:
+ meta = diff_get_color_opt(o, DIFF_METAINFO);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
+ line, reset,
+ strchr(line, ' ') ? "\t" : "");
+ break;
+ case DIFF_SYMBOL_BINARY_FILES:
+ case DIFF_SYMBOL_HEADER:
+ fprintf(o->file, "%s", line);
+ break;
+ case DIFF_SYMBOL_BINARY_DIFF_HEADER:
+ fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
+ break;
+ case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
+ fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
+ break;
+ case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
+ fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
+ break;
+ case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
+ fputs(diff_line_prefix(o), o->file);
+ fputc('\n', o->file);
+ break;
+ case DIFF_SYMBOL_REWRITE_DIFF:
+ fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line(o, fraginfo, reset, line, len);
+ break;
+ case DIFF_SYMBOL_SUBMODULE_ADD:
+ set = diff_get_color_opt(o, DIFF_FILE_NEW);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line(o, set, reset, line, len);
+ break;
+ case DIFF_SYMBOL_SUBMODULE_DEL:
+ set = diff_get_color_opt(o, DIFF_FILE_OLD);
+ reset = diff_get_color_opt(o, DIFF_RESET);
+ emit_line(o, set, reset, line, len);
+ break;
+ case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
+ fprintf(o->file, "%sSubmodule %s contains untracked content\n",
+ diff_line_prefix(o), line);
+ break;
+ case DIFF_SYMBOL_SUBMODULE_MODIFIED:
+ fprintf(o->file, "%sSubmodule %s contains modified content\n",
+ diff_line_prefix(o), line);
+ break;
+ case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
+ emit_line(o, "", "", " 0 files changed\n",
+ strlen(" 0 files changed\n"));
+ break;
+ case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
+ emit_line(o, "", "", " ...\n", strlen(" ...\n"));
+ break;
+ case DIFF_SYMBOL_WORD_DIFF:
+ fprintf(o->file, "%.*s", len, line);
+ break;
+ case DIFF_SYMBOL_STAT_SEP:
+ fputs(o->stat_sep, o->file);
+ break;
+ default:
+ die("BUG: unknown diff symbol");
+ }
+ strbuf_release(&sb);
+}
+
+static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
+ const char *line, int len, unsigned flags)
+{
+ struct emitted_diff_symbol e = {line, len, flags, s};
+
+ if (o->emitted_symbols)
+ append_emitted_diff_symbol(o, &e);
+ else
+ emit_diff_symbol_from_struct(o, &e);
+}
+
+void diff_emit_submodule_del(struct diff_options *o, const char *line)
+{
+ emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
+}
+
+void diff_emit_submodule_add(struct diff_options *o, const char *line)
+{
+ emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
+}
+
+void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
+{
+ emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
+ path, strlen(path), 0);
+}
+
+void diff_emit_submodule_modified(struct diff_options *o, const char *path)
+{
+ emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
+ path, strlen(path), 0);
+}
+
+void diff_emit_submodule_header(struct diff_options *o, const char *header)
+{
+ emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
+ header, strlen(header), 0);
+}
+
+void diff_emit_submodule_error(struct diff_options *o, const char *err)
+{
+ emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
+}
+
+void diff_emit_submodule_pipethrough(struct diff_options *o,
+ const char *line, int len)
+{
+ emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
+}
+
+static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
+{
+ if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
+ ecbdata->blank_at_eof_in_preimage &&
+ ecbdata->blank_at_eof_in_postimage &&
+ ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
+ ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
+ return 0;
+ return ws_blank_line(line, len, ecbdata->ws_rule);
+}
+
static void emit_add_line(const char *reset,
struct emit_callback *ecbdata,
const char *line, int len)
{
- emit_line_checked(reset, ecbdata, line, len,
- DIFF_FILE_NEW, WSEH_NEW, '+');
+ unsigned flags = WSEH_NEW | ecbdata->ws_rule;
+ if (new_blank_line_at_eof(ecbdata, line, len))
+ flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
+
+ emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
}
static void emit_del_line(const char *reset,
struct emit_callback *ecbdata,
const char *line, int len)
{
- emit_line_checked(reset, ecbdata, line, len,
- DIFF_FILE_OLD, WSEH_OLD, '-');
+ unsigned flags = WSEH_OLD | ecbdata->ws_rule;
+ emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
}
static void emit_context_line(const char *reset,
struct emit_callback *ecbdata,
const char *line, int len)
{
- emit_line_checked(reset, ecbdata, line, len,
- DIFF_CONTEXT, WSEH_CONTEXT, ' ');
+ unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
+ emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
}
static void emit_hunk_header(struct emit_callback *ecbdata,
if (len < 10 ||
memcmp(line, atat, 2) ||
!(ep = memmem(line + 2, len - 2, atat, 2))) {
- emit_line(ecbdata->opt, context, reset, line, len);
+ emit_diff_symbol(ecbdata->opt,
+ DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
return;
}
ep += 2; /* skip over @@ */
}
strbuf_add(&msgbuf, line + len, org_len - len);
- emit_line(ecbdata->opt, "", "", msgbuf.buf, msgbuf.len);
+ strbuf_complete_line(&msgbuf);
+ emit_diff_symbol(ecbdata->opt,
+ DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
strbuf_release(&msgbuf);
}
}
}
-static void print_line_count(FILE *file, int count)
+static void add_line_count(struct strbuf *out, int count)
{
switch (count) {
case 0:
- fprintf(file, "0,0");
+ strbuf_addstr(out, "0,0");
break;
case 1:
- fprintf(file, "1");
+ strbuf_addstr(out, "1");
break;
default:
- fprintf(file, "1,%d", count);
+ strbuf_addf(out, "1,%d", count);
break;
}
}
int prefix, const char *data, int size)
{
const char *endp = NULL;
- static const char *nneof = " No newline at end of file\n";
const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
while (0 < size) {
size -= len;
data += len;
}
- if (!endp) {
- const char *context = diff_get_color(ecb->color_diff,
- DIFF_CONTEXT);
- putc('\n', ecb->opt->file);
- emit_line_0(ecb->opt, context, reset, '\\',
- nneof, strlen(nneof));
- }
+ if (!endp)
+ emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
}
static void emit_rewrite_diff(const char *name_a,
struct diff_options *o)
{
int lc_a, lc_b;
- const char *name_a_tab, *name_b_tab;
- const char *metainfo = diff_get_color(o->use_color, DIFF_METAINFO);
- const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
- const char *reset = diff_get_color(o->use_color, DIFF_RESET);
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
const char *a_prefix, *b_prefix;
char *data_one, *data_two;
size_t size_one, size_two;
struct emit_callback ecbdata;
- const char *line_prefix = diff_line_prefix(o);
+ struct strbuf out = STRBUF_INIT;
if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
a_prefix = o->b_prefix;
name_a += (*name_a == '/');
name_b += (*name_b == '/');
- name_a_tab = strchr(name_a, ' ') ? "\t" : "";
- name_b_tab = strchr(name_b, ' ') ? "\t" : "";
strbuf_reset(&a_name);
strbuf_reset(&b_name);
lc_a = count_lines(data_one, size_one);
lc_b = count_lines(data_two, size_two);
- fprintf(o->file,
- "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
- line_prefix, metainfo, a_name.buf, name_a_tab, reset,
- line_prefix, metainfo, b_name.buf, name_b_tab, reset,
- line_prefix, fraginfo);
+
+ emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
+ a_name.buf, a_name.len, 0);
+ emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
+ b_name.buf, b_name.len, 0);
+
+ strbuf_addstr(&out, "@@ -");
if (!o->irreversible_delete)
- print_line_count(o->file, lc_a);
+ add_line_count(&out, lc_a);
else
- fprintf(o->file, "?,?");
- fprintf(o->file, " +");
- print_line_count(o->file, lc_b);
- fprintf(o->file, " @@%s\n", reset);
+ strbuf_addstr(&out, "?,?");
+ strbuf_addstr(&out, " +");
+ add_line_count(&out, lc_b);
+ strbuf_addstr(&out, " @@\n");
+ emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
+ strbuf_release(&out);
+
if (lc_a && !o->irreversible_delete)
emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
if (lc_b)
struct diff_words_style *style;
};
-static int fn_out_diff_words_write_helper(FILE *fp,
+static int fn_out_diff_words_write_helper(struct diff_options *o,
struct diff_words_style_elem *st_el,
const char *newline,
- size_t count, const char *buf,
- const char *line_prefix)
+ size_t count, const char *buf)
{
int print = 0;
+ struct strbuf sb = STRBUF_INIT;
while (count) {
char *p = memchr(buf, '\n', count);
if (print)
- fputs(line_prefix, fp);
+ strbuf_addstr(&sb, diff_line_prefix(o));
+
if (p != buf) {
- if (st_el->color && fputs(st_el->color, fp) < 0)
- return -1;
- if (fputs(st_el->prefix, fp) < 0 ||
- fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
- fputs(st_el->suffix, fp) < 0)
- return -1;
- if (st_el->color && *st_el->color
- && fputs(GIT_COLOR_RESET, fp) < 0)
- return -1;
+ const char *reset = st_el->color && *st_el->color ?
+ GIT_COLOR_RESET : NULL;
+ if (st_el->color && *st_el->color)
+ strbuf_addstr(&sb, st_el->color);
+ strbuf_addstr(&sb, st_el->prefix);
+ strbuf_add(&sb, buf, p ? p - buf : count);
+ strbuf_addstr(&sb, st_el->suffix);
+ if (reset)
+ strbuf_addstr(&sb, reset);
}
if (!p)
- return 0;
- if (fputs(newline, fp) < 0)
- return -1;
+ goto out;
+
+ strbuf_addstr(&sb, newline);
count -= p + 1 - buf;
buf = p + 1;
print = 1;
+ if (count) {
+ emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
+ sb.buf, sb.len, 0);
+ strbuf_reset(&sb);
+ }
}
+
+out:
+ if (sb.len)
+ emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
+ sb.buf, sb.len, 0);
+ strbuf_release(&sb);
return 0;
}
fputs(line_prefix, diff_words->opt->file);
}
if (diff_words->current_plus != plus_begin) {
- fn_out_diff_words_write_helper(diff_words->opt->file,
+ fn_out_diff_words_write_helper(diff_words->opt,
&style->ctx, style->newline,
plus_begin - diff_words->current_plus,
- diff_words->current_plus, line_prefix);
- if (*(plus_begin - 1) == '\n')
- fputs(line_prefix, diff_words->opt->file);
+ diff_words->current_plus);
}
if (minus_begin != minus_end) {
- fn_out_diff_words_write_helper(diff_words->opt->file,
+ fn_out_diff_words_write_helper(diff_words->opt,
&style->old, style->newline,
- minus_end - minus_begin, minus_begin,
- line_prefix);
+ minus_end - minus_begin, minus_begin);
}
if (plus_begin != plus_end) {
- fn_out_diff_words_write_helper(diff_words->opt->file,
+ fn_out_diff_words_write_helper(diff_words->opt,
&style->new, style->newline,
- plus_end - plus_begin, plus_begin,
- line_prefix);
+ plus_end - plus_begin, plus_begin);
}
diff_words->current_plus = plus_end;
/* special case: only removal */
if (!diff_words->plus.text.size) {
- fputs(line_prefix, diff_words->opt->file);
- fn_out_diff_words_write_helper(diff_words->opt->file,
+ emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
+ line_prefix, strlen(line_prefix), 0);
+ fn_out_diff_words_write_helper(diff_words->opt,
&style->old, style->newline,
diff_words->minus.text.size,
- diff_words->minus.text.ptr, line_prefix);
+ diff_words->minus.text.ptr);
diff_words->minus.text.size = 0;
return;
}
if (diff_words->current_plus != diff_words->plus.text.ptr +
diff_words->plus.text.size) {
if (color_words_output_graph_prefix(diff_words))
- fputs(line_prefix, diff_words->opt->file);
- fn_out_diff_words_write_helper(diff_words->opt->file,
+ emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
+ line_prefix, strlen(line_prefix), 0);
+ fn_out_diff_words_write_helper(diff_words->opt,
&style->ctx, style->newline,
diff_words->plus.text.ptr + diff_words->plus.text.size
- - diff_words->current_plus, diff_words->current_plus,
- line_prefix);
+ - diff_words->current_plus, diff_words->current_plus);
}
diff_words->minus.text.size = diff_words->plus.text.size = 0;
}
/* In "color-words" mode, show word-diff of words accumulated in the buffer */
static void diff_words_flush(struct emit_callback *ecbdata)
{
+ struct diff_options *wo = ecbdata->diff_words->opt;
+
if (ecbdata->diff_words->minus.text.size ||
ecbdata->diff_words->plus.text.size)
diff_words_show(ecbdata->diff_words);
+
+ if (wo->emitted_symbols) {
+ struct diff_options *o = ecbdata->opt;
+ struct emitted_diff_symbols *wol = wo->emitted_symbols;
+ int i;
+
+ /*
+ * NEEDSWORK:
+ * Instead of appending each, concat all words to a line?
+ */
+ for (i = 0; i < wol->nr; i++)
+ append_emitted_diff_symbol(o, &wol->buf[i]);
+
+ for (i = 0; i < wol->nr; i++)
+ free((void *)wol->buf[i].line);
+
+ wol->nr = 0;
+ }
}
static void diff_filespec_load_driver(struct diff_filespec *one)
xcalloc(1, sizeof(struct diff_words_data));
ecbdata->diff_words->type = o->word_diff;
ecbdata->diff_words->opt = o;
+
+ if (orig_opts->emitted_symbols)
+ o->emitted_symbols =
+ xcalloc(1, sizeof(struct emitted_diff_symbols));
+
if (!o->word_regex)
o->word_regex = userdiff_word_regex(one);
if (!o->word_regex)
{
if (ecbdata->diff_words) {
diff_words_flush(ecbdata);
+ free (ecbdata->diff_words->opt->emitted_symbols);
free (ecbdata->diff_words->opt);
free (ecbdata->diff_words->minus.text.ptr);
free (ecbdata->diff_words->minus.orig);
static void fn_out_consume(void *priv, char *line, unsigned long len)
{
struct emit_callback *ecbdata = priv;
- const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
- const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
struct diff_options *o = ecbdata->opt;
- const char *line_prefix = diff_line_prefix(o);
o->found_changes = 1;
if (ecbdata->header) {
- fprintf(o->file, "%s", ecbdata->header->buf);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+ ecbdata->header->buf, ecbdata->header->len, 0);
strbuf_reset(ecbdata->header);
ecbdata->header = NULL;
}
if (ecbdata->label_path[0]) {
- const char *name_a_tab, *name_b_tab;
-
- name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
- name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
-
- fprintf(o->file, "%s%s--- %s%s%s\n",
- line_prefix, meta, ecbdata->label_path[0], reset, name_a_tab);
- fprintf(o->file, "%s%s+++ %s%s%s\n",
- line_prefix, meta, ecbdata->label_path[1], reset, name_b_tab);
+ emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
+ ecbdata->label_path[0],
+ strlen(ecbdata->label_path[0]), 0);
+ emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
+ ecbdata->label_path[1],
+ strlen(ecbdata->label_path[1]), 0);
ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
}
len = sane_truncate_line(ecbdata, line, len);
find_lno(line, ecbdata);
emit_hunk_header(ecbdata, line, len);
- if (line[len-1] != '\n')
- putc('\n', o->file);
return;
}
if (ecbdata->diff_words) {
+ enum diff_symbol s =
+ ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
+ DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
if (line[0] == '-') {
diff_words_append(line, len,
&ecbdata->diff_words->minus);
return;
}
diff_words_flush(ecbdata);
- if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
- emit_line(o, context, reset, line, len);
- fputs("~\n", o->file);
- } else {
- /*
- * Skip the prefix character, if any. With
- * diff_suppress_blank_empty, there may be
- * none.
- */
- if (line[0] != '\n') {
- line++;
- len--;
- }
- emit_line(o, context, reset, line, len);
- }
+ emit_diff_symbol(o, s, line, len, 0);
return;
}
default:
/* incomplete line at the end */
ecbdata->lno_in_preimage++;
- emit_line(o, diff_get_color(ecbdata->color_diff, DIFF_CONTEXT),
- reset, line, len);
+ emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
+ line, len, 0);
break;
}
}
return 1 + (it * (width - 1) / max_change);
}
-static void show_name(FILE *file,
- const char *prefix, const char *name, int len)
-{
- fprintf(file, " %s%-*s |", prefix, len, name);
-}
-
-static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
+static void show_graph(struct strbuf *out, char ch, int cnt,
+ const char *set, const char *reset)
{
if (cnt <= 0)
return;
- fprintf(file, "%s", set);
- while (cnt--)
- putc(ch, file);
- fprintf(file, "%s", reset);
+ strbuf_addstr(out, set);
+ strbuf_addchars(out, ch, cnt);
+ strbuf_addstr(out, reset);
}
static void fill_print_name(struct diffstat_file *file)
file->print_name = pname;
}
-int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
+static void print_stat_summary_inserts_deletes(struct diff_options *options,
+ int files, int insertions, int deletions)
{
struct strbuf sb = STRBUF_INIT;
- int ret;
if (!files) {
assert(insertions == 0 && deletions == 0);
- return fprintf(fp, "%s\n", " 0 files changed");
+ emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
+ NULL, 0, 0);
+ return;
}
strbuf_addf(&sb,
deletions);
}
strbuf_addch(&sb, '\n');
- ret = fputs(sb.buf, fp);
+ emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
+ sb.buf, sb.len, 0);
strbuf_release(&sb);
- return ret;
+}
+
+void print_stat_summary(FILE *fp, int files,
+ int insertions, int deletions)
+{
+ struct diff_options o;
+ memset(&o, 0, sizeof(o));
+ o.file = fp;
+
+ print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
}
static void show_stats(struct diffstat_t *data, struct diff_options *options)
int total_files = data->nr, count;
int width, name_width, graph_width, number_width = 0, bin_width = 0;
const char *reset, *add_c, *del_c;
- const char *line_prefix = "";
int extra_shown = 0;
+ const char *line_prefix = diff_line_prefix(options);
+ struct strbuf out = STRBUF_INIT;
if (data->nr == 0)
return;
- line_prefix = diff_line_prefix(options);
count = options->stat_count ? options->stat_count : data->nr;
reset = diff_get_color_opt(options, DIFF_RESET);
}
if (file->is_binary) {
- fprintf(options->file, "%s", line_prefix);
- show_name(options->file, prefix, name, len);
- fprintf(options->file, " %*s", number_width, "Bin");
+ strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+ strbuf_addf(&out, " %*s", number_width, "Bin");
if (!added && !deleted) {
- putc('\n', options->file);
+ strbuf_addch(&out, '\n');
+ emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+ out.buf, out.len, 0);
+ strbuf_reset(&out);
continue;
}
- fprintf(options->file, " %s%"PRIuMAX"%s",
+ strbuf_addf(&out, " %s%"PRIuMAX"%s",
del_c, deleted, reset);
- fprintf(options->file, " -> ");
- fprintf(options->file, "%s%"PRIuMAX"%s",
+ strbuf_addstr(&out, " -> ");
+ strbuf_addf(&out, "%s%"PRIuMAX"%s",
add_c, added, reset);
- fprintf(options->file, " bytes");
- fprintf(options->file, "\n");
+ strbuf_addstr(&out, " bytes\n");
+ emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+ out.buf, out.len, 0);
+ strbuf_reset(&out);
continue;
}
else if (file->is_unmerged) {
- fprintf(options->file, "%s", line_prefix);
- show_name(options->file, prefix, name, len);
- fprintf(options->file, " Unmerged\n");
+ strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+ strbuf_addstr(&out, " Unmerged\n");
+ emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+ out.buf, out.len, 0);
+ strbuf_reset(&out);
continue;
}
add = total - del;
}
}
- fprintf(options->file, "%s", line_prefix);
- show_name(options->file, prefix, name, len);
- fprintf(options->file, " %*"PRIuMAX"%s",
+ strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+ strbuf_addf(&out, " %*"PRIuMAX"%s",
number_width, added + deleted,
added + deleted ? " " : "");
- show_graph(options->file, '+', add, add_c, reset);
- show_graph(options->file, '-', del, del_c, reset);
- fprintf(options->file, "\n");
+ show_graph(&out, '+', add, add_c, reset);
+ show_graph(&out, '-', del, del_c, reset);
+ strbuf_addch(&out, '\n');
+ emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+ out.buf, out.len, 0);
+ strbuf_reset(&out);
}
for (i = 0; i < data->nr; i++) {
if (i < count)
continue;
if (!extra_shown)
- fprintf(options->file, "%s ...\n", line_prefix);
+ emit_diff_symbol(options,
+ DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
+ NULL, 0, 0);
extra_shown = 1;
}
- fprintf(options->file, "%s", line_prefix);
- print_stat_summary(options->file, total_files, adds, dels);
+
+ print_stat_summary_inserts_deletes(options, total_files, adds, dels);
}
static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
for (i = 0; i < data->nr; i++) {
int added = data->files[i]->added;
- int deleted= data->files[i]->deleted;
+ int deleted = data->files[i]->deleted;
if (data->files[i]->is_unmerged ||
(!data->files[i]->is_interesting && (added + deleted == 0))) {
dels += deleted;
}
}
- fprintf(options->file, "%s", diff_line_prefix(options));
- print_stat_summary(options->file, total_files, adds, dels);
+ print_stat_summary_inserts_deletes(options, total_files, adds, dels);
}
static void show_numstat(struct diffstat_t *data, struct diff_options *options)
return deflated;
}
-static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
- const char *prefix)
+static void emit_binary_diff_body(struct diff_options *o,
+ mmfile_t *one, mmfile_t *two)
{
void *cp;
void *delta;
}
if (delta && delta_size < deflate_size) {
- fprintf(file, "%sdelta %lu\n", prefix, orig_size);
+ char *s = xstrfmt("%lu", orig_size);
+ emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
+ s, strlen(s), 0);
+ free(s);
free(deflated);
data = delta;
data_size = delta_size;
- }
- else {
- fprintf(file, "%sliteral %lu\n", prefix, two->size);
+ } else {
+ char *s = xstrfmt("%lu", two->size);
+ emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
+ s, strlen(s), 0);
+ free(s);
free(delta);
data = deflated;
data_size = deflate_size;
/* emit data encoded in base85 */
cp = data;
while (data_size) {
+ int len;
int bytes = (52 < data_size) ? 52 : data_size;
- char line[70];
+ char line[71];
data_size -= bytes;
if (bytes <= 26)
line[0] = bytes + 'A' - 1;
line[0] = bytes - 26 + 'a' - 1;
encode_85(line + 1, cp, bytes);
cp = (char *) cp + bytes;
- fprintf(file, "%s", prefix);
- fputs(line, file);
- fputc('\n', file);
+
+ len = strlen(line);
+ line[len++] = '\n';
+ line[len] = '\0';
+
+ emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
+ line, len, 0);
}
- fprintf(file, "%s\n", prefix);
+ emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
free(data);
}
-static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two,
- const char *prefix)
+static void emit_binary_diff(struct diff_options *o,
+ mmfile_t *one, mmfile_t *two)
{
- fprintf(file, "%sGIT binary patch\n", prefix);
- emit_binary_diff_body(file, one, two, prefix);
- emit_binary_diff_body(file, two, one, prefix);
+ emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
+ emit_binary_diff_body(o, one, two);
+ emit_binary_diff_body(o, two, one);
}
int diff_filespec_is_binary(struct diff_filespec *one)
if (o->submodule_format == DIFF_SUBMODULE_LOG &&
(!one->mode || S_ISGITLINK(one->mode)) &&
(!two->mode || S_ISGITLINK(two->mode))) {
- const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
- const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
- show_submodule_summary(o->file, one->path ? one->path : two->path,
- line_prefix,
+ show_submodule_summary(o, one->path ? one->path : two->path,
&one->oid, &two->oid,
- two->dirty_submodule,
- meta, del, add, reset);
+ two->dirty_submodule);
return;
} else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
(!one->mode || S_ISGITLINK(one->mode)) &&
(!two->mode || S_ISGITLINK(two->mode))) {
- const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
- const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
- show_submodule_inline_diff(o->file, one->path ? one->path : two->path,
- line_prefix,
+ show_submodule_inline_diff(o, one->path ? one->path : two->path,
&one->oid, &two->oid,
- two->dirty_submodule,
- meta, del, add, reset, o);
+ two->dirty_submodule);
return;
}
if (complete_rewrite &&
(textconv_one || !diff_filespec_is_binary(one)) &&
(textconv_two || !diff_filespec_is_binary(two))) {
- fprintf(o->file, "%s", header.buf);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+ header.buf, header.len, 0);
strbuf_reset(&header);
emit_rewrite_diff(name_a, name_b, one, two,
textconv_one, textconv_two, o);
}
if (o->irreversible_delete && lbl[1][0] == '/') {
- fprintf(o->file, "%s", header.buf);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
+ header.len, 0);
strbuf_reset(&header);
goto free_ab_and_return;
} else if (!DIFF_OPT_TST(o, TEXT) &&
( (!textconv_one && diff_filespec_is_binary(one)) ||
(!textconv_two && diff_filespec_is_binary(two)) )) {
+ struct strbuf sb = STRBUF_INIT;
if (!one->data && !two->data &&
S_ISREG(one->mode) && S_ISREG(two->mode) &&
!DIFF_OPT_TST(o, BINARY)) {
if (!oidcmp(&one->oid, &two->oid)) {
if (must_show_header)
- fprintf(o->file, "%s", header.buf);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+ header.buf, header.len,
+ 0);
goto free_ab_and_return;
}
- fprintf(o->file, "%s", header.buf);
- fprintf(o->file, "%sBinary files %s and %s differ\n",
- line_prefix, lbl[0], lbl[1]);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+ header.buf, header.len, 0);
+ strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
+ diff_line_prefix(o), lbl[0], lbl[1]);
+ emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
+ sb.buf, sb.len, 0);
+ strbuf_release(&sb);
goto free_ab_and_return;
}
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
if (mf1.size == mf2.size &&
!memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
if (must_show_header)
- fprintf(o->file, "%s", header.buf);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+ header.buf, header.len, 0);
goto free_ab_and_return;
}
- fprintf(o->file, "%s", header.buf);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
strbuf_reset(&header);
if (DIFF_OPT_TST(o, BINARY))
- emit_binary_diff(o->file, &mf1, &mf2, line_prefix);
- else
- fprintf(o->file, "%sBinary files %s and %s differ\n",
- line_prefix, lbl[0], lbl[1]);
+ emit_binary_diff(o, &mf1, &mf2);
+ else {
+ strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
+ diff_line_prefix(o), lbl[0], lbl[1]);
+ emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
+ sb.buf, sb.len, 0);
+ strbuf_release(&sb);
+ }
o->found_changes = 1;
} else {
/* Crazy xdl interfaces.. */
const struct userdiff_funcname *pe;
if (must_show_header) {
- fprintf(o->file, "%s", header.buf);
+ emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+ header.buf, header.len, 0);
strbuf_reset(&header);
}
const char *other;
const char *attr_path;
- name = p->one->path;
- other = (strcmp(name, p->two->path) ? p->two->path : NULL);
+ name = one->path;
+ other = (strcmp(name, two->path) ? two->path : NULL);
attr_path = name;
if (o->prefix_length)
strip_prefix(o->prefix_length, &name, &other);
options->a_prefix = "a/";
options->b_prefix = "b/";
}
+
+ options->color_moved = diff_color_moved_default;
}
void diff_setup_done(struct diff_options *options)
if (DIFF_OPT_TST(options, FOLLOW_RENAMES) && options->pathspec.nr != 1)
die(_("--follow requires exactly one pathspec"));
+
+ if (!options->use_color || external_diff())
+ options->color_moved = 0;
}
static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
}
else if (!strcmp(arg, "--no-color"))
options->use_color = 0;
- else if (!strcmp(arg, "--color-words")) {
+ else if (!strcmp(arg, "--color-moved")) {
+ if (diff_color_moved_default)
+ options->color_moved = diff_color_moved_default;
+ if (options->color_moved == COLOR_MOVED_NO)
+ options->color_moved = COLOR_MOVED_DEFAULT;
+ } else if (!strcmp(arg, "--no-color-moved"))
+ options->color_moved = COLOR_MOVED_NO;
+ else if (skip_prefix(arg, "--color-moved=", &arg)) {
+ int cm = parse_color_moved(arg);
+ if (cm < 0)
+ die("bad --color-moved argument: %s", arg);
+ options->color_moved = cm;
+ } else if (!strcmp(arg, "--color-words")) {
options->use_color = 1;
options->word_diff = DIFF_WORDS_COLOR;
}
}
}
-static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
+static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
{
+ struct strbuf sb = STRBUF_INIT;
if (fs->mode)
- fprintf(file, " %s mode %06o ", newdelete, fs->mode);
+ strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
else
- fprintf(file, " %s ", newdelete);
- write_name_quoted(fs->path, file, '\n');
-}
+ strbuf_addf(&sb, " %s ", newdelete);
+ quote_c_style(fs->path, &sb, NULL, 0);
+ strbuf_addch(&sb, '\n');
+ emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
+ sb.buf, sb.len, 0);
+ strbuf_release(&sb);
+}
-static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name,
- const char *line_prefix)
+static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
+ int show_name)
{
if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
- fprintf(file, "%s mode change %06o => %06o%c", line_prefix, p->one->mode,
- p->two->mode, show_name ? ' ' : '\n');
+ struct strbuf sb = STRBUF_INIT;
+ strbuf_addf(&sb, " mode change %06o => %06o",
+ p->one->mode, p->two->mode);
if (show_name) {
- write_name_quoted(p->two->path, file, '\n');
+ strbuf_addch(&sb, ' ');
+ quote_c_style(p->two->path, &sb, NULL, 0);
}
+ emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
+ sb.buf, sb.len, 0);
+ strbuf_release(&sb);
}
}
-static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p,
- const char *line_prefix)
+static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
+ struct diff_filepair *p)
{
+ struct strbuf sb = STRBUF_INIT;
char *names = pprint_rename(p->one->path, p->two->path);
-
- fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
+ strbuf_addf(&sb, " %s %s (%d%%)\n",
+ renamecopy, names, similarity_index(p));
free(names);
- show_mode_change(file, p, 0, line_prefix);
+ emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
+ sb.buf, sb.len, 0);
+ show_mode_change(opt, p, 0);
}
static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
{
- FILE *file = opt->file;
- const char *line_prefix = diff_line_prefix(opt);
-
switch(p->status) {
case DIFF_STATUS_DELETED:
- fputs(line_prefix, file);
- show_file_mode_name(file, "delete", p->one);
+ show_file_mode_name(opt, "delete", p->one);
break;
case DIFF_STATUS_ADDED:
- fputs(line_prefix, file);
- show_file_mode_name(file, "create", p->two);
+ show_file_mode_name(opt, "create", p->two);
break;
case DIFF_STATUS_COPIED:
- fputs(line_prefix, file);
- show_rename_copy(file, "copy", p, line_prefix);
+ show_rename_copy(opt, "copy", p);
break;
case DIFF_STATUS_RENAMED:
- fputs(line_prefix, file);
- show_rename_copy(file, "rename", p, line_prefix);
+ show_rename_copy(opt, "rename", p);
break;
default:
if (p->score) {
- fprintf(file, "%s rewrite ", line_prefix);
- write_name_quoted(p->two->path, file, ' ');
- fprintf(file, "(%d%%)\n", similarity_index(p));
+ struct strbuf sb = STRBUF_INIT;
+ strbuf_addstr(&sb, " rewrite ");
+ quote_c_style(p->two->path, &sb, NULL, 0);
+ strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
+ emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
+ sb.buf, sb.len, 0);
}
- show_mode_change(file, p, !p->score, line_prefix);
+ show_mode_change(opt, p, !p->score);
break;
}
}
warning(_(rename_limit_advice), varname, needed);
}
+static void diff_flush_patch_all_file_pairs(struct diff_options *o)
+{
+ int i;
+ static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
+ struct diff_queue_struct *q = &diff_queued_diff;
+
+ if (WSEH_NEW & WS_RULE_MASK)
+ die("BUG: WS rules bit mask overlaps with diff symbol flags");
+
+ if (o->color_moved)
+ o->emitted_symbols = &esm;
+
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ if (check_pair_status(p))
+ diff_flush_patch(p, o);
+ }
+
+ if (o->emitted_symbols) {
+ if (o->color_moved) {
+ struct hashmap add_lines, del_lines;
+
+ hashmap_init(&del_lines,
+ (hashmap_cmp_fn)moved_entry_cmp, o, 0);
+ hashmap_init(&add_lines,
+ (hashmap_cmp_fn)moved_entry_cmp, o, 0);
+
+ add_lines_to_move_detection(o, &add_lines, &del_lines);
+ mark_color_as_moved(o, &add_lines, &del_lines);
+ if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
+ dim_moved_lines(o);
+
+ hashmap_free(&add_lines, 0);
+ hashmap_free(&del_lines, 0);
+ }
+
+ for (i = 0; i < esm.nr; i++)
+ emit_diff_symbol_from_struct(o, &esm.buf[i]);
+
+ for (i = 0; i < esm.nr; i++)
+ free((void *)esm.buf[i].line);
+ }
+ esm.nr = 0;
+}
+
void diff_flush(struct diff_options *options)
{
struct diff_queue_struct *q = &diff_queued_diff;
fclose(options->file);
options->file = xfopen("/dev/null", "w");
options->close_file = 1;
+ options->color_moved = 0;
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if (check_pair_status(p))
if (output_format & DIFF_FORMAT_PATCH) {
if (separator) {
- fprintf(options->file, "%s%c",
- diff_line_prefix(options),
- options->line_termination);
- if (options->stat_sep) {
+ emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
+ if (options->stat_sep)
/* attach patch instead of inline */
- fputs(options->stat_sep, options->file);
- }
+ emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
+ NULL, 0, 0);
}
- for (i = 0; i < q->nr; i++) {
- struct diff_filepair *p = q->queue[i];
- if (check_pair_status(p))
- diff_flush_patch(p, options);
- }
+ diff_flush_patch_all_file_pairs(options);
}
if (output_format & DIFF_FORMAT_CALLBACK)
int abbrev;
int ita_invisible_in_index;
/* white-space error highlighting */
-#define WSEH_NEW 1
-#define WSEH_CONTEXT 2
-#define WSEH_OLD 4
+#define WSEH_NEW (1<<12)
+#define WSEH_CONTEXT (1<<13)
+#define WSEH_OLD (1<<14)
unsigned ws_error_highlight;
const char *prefix;
int prefix_length;
void *output_prefix_data;
int diff_path_counter;
+
+ struct emitted_diff_symbols *emitted_symbols;
+ enum {
+ COLOR_MOVED_NO = 0,
+ COLOR_MOVED_PLAIN = 1,
+ COLOR_MOVED_ZEBRA = 2,
+ COLOR_MOVED_ZEBRA_DIM = 3,
+ } color_moved;
+ #define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA
+ #define COLOR_MOVED_MIN_BLOCK_LENGTH 3
};
+void diff_emit_submodule_del(struct diff_options *o, const char *line);
+void diff_emit_submodule_add(struct diff_options *o, const char *line);
+void diff_emit_submodule_untracked(struct diff_options *o, const char *path);
+void diff_emit_submodule_modified(struct diff_options *o, const char *path);
+void diff_emit_submodule_header(struct diff_options *o, const char *header);
+void diff_emit_submodule_error(struct diff_options *o, const char *err);
+void diff_emit_submodule_pipethrough(struct diff_options *o,
+ const char *line, int len);
+
enum color_diff {
DIFF_RESET = 0,
DIFF_CONTEXT = 1,
DIFF_FILE_NEW = 5,
DIFF_COMMIT = 6,
DIFF_WHITESPACE = 7,
- DIFF_FUNCINFO = 8
+ DIFF_FUNCINFO = 8,
+ DIFF_FILE_OLD_MOVED = 9,
+ DIFF_FILE_OLD_MOVED_ALT = 10,
+ DIFF_FILE_OLD_MOVED_DIM = 11,
+ DIFF_FILE_OLD_MOVED_ALT_DIM = 12,
+ DIFF_FILE_NEW_MOVED = 13,
+ DIFF_FILE_NEW_MOVED_ALT = 14,
+ DIFF_FILE_NEW_MOVED_DIM = 15,
+ DIFF_FILE_NEW_MOVED_ALT_DIM = 16
};
const char *diff_get_color(int diff_use_color, enum color_diff ix);
#define diff_get_color_opt(o, ix) \
extern long parse_algorithm_value(const char *value);
-extern int print_stat_summary(FILE *fp, int files,
- int insertions, int deletions);
+extern void print_stat_summary(FILE *fp, int files,
+ int insertions, int deletions);
extern void setup_diff_pager(struct diff_options *);
#endif /* DIFF_H */
char exclude_per_dir[FLEX_ARRAY];
};
-#define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1)
+#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)
+#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)
struct write_data {
int index; /* number of written untracked_cache_dir */
const unsigned char *end;
};
-static void stat_data_from_disk(struct stat_data *to, const struct stat_data *from)
+static void stat_data_from_disk(struct stat_data *to, const unsigned char *data)
{
- to->sd_ctime.sec = get_be32(&from->sd_ctime.sec);
- to->sd_ctime.nsec = get_be32(&from->sd_ctime.nsec);
- to->sd_mtime.sec = get_be32(&from->sd_mtime.sec);
- to->sd_mtime.nsec = get_be32(&from->sd_mtime.nsec);
- to->sd_dev = get_be32(&from->sd_dev);
- to->sd_ino = get_be32(&from->sd_ino);
- to->sd_uid = get_be32(&from->sd_uid);
- to->sd_gid = get_be32(&from->sd_gid);
- to->sd_size = get_be32(&from->sd_size);
+ memcpy(to, data, sizeof(*to));
+ to->sd_ctime.sec = ntohl(to->sd_ctime.sec);
+ to->sd_ctime.nsec = ntohl(to->sd_ctime.nsec);
+ to->sd_mtime.sec = ntohl(to->sd_mtime.sec);
+ to->sd_mtime.nsec = ntohl(to->sd_mtime.nsec);
+ to->sd_dev = ntohl(to->sd_dev);
+ to->sd_ino = ntohl(to->sd_ino);
+ to->sd_uid = ntohl(to->sd_uid);
+ to->sd_gid = ntohl(to->sd_gid);
+ to->sd_size = ntohl(to->sd_size);
}
static int read_one_dir(struct untracked_cache_dir **untracked_,
rd->data = rd->end + 1;
return;
}
- stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data);
+ stat_data_from_disk(&ud->stat_data, rd->data);
rd->data += sizeof(struct stat_data);
ud->valid = 1;
}
}
static void load_sha1_stat(struct sha1_stat *sha1_stat,
- const struct stat_data *stat,
+ const unsigned char *data,
const unsigned char *sha1)
{
- stat_data_from_disk(&sha1_stat->stat, stat);
+ stat_data_from_disk(&sha1_stat->stat, data);
hashcpy(sha1_stat->sha1, sha1);
sha1_stat->valid = 1;
}
struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)
{
- const struct ondisk_untracked_cache *ouc;
struct untracked_cache *uc;
struct read_data rd;
const unsigned char *next = data, *end = (const unsigned char *)data + sz;
const char *ident;
int ident_len, len;
+ const char *exclude_per_dir;
if (sz <= 1 || end[-1] != '\0')
return NULL;
ident = (const char *)next;
next += ident_len;
- ouc = (const struct ondisk_untracked_cache *)next;
if (next + ouc_size(0) > end)
return NULL;
uc = xcalloc(1, sizeof(*uc));
strbuf_init(&uc->ident, ident_len);
strbuf_add(&uc->ident, ident, ident_len);
- load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,
- ouc->info_exclude_sha1);
- load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat,
- ouc->excludes_file_sha1);
- uc->dir_flags = get_be32(&ouc->dir_flags);
- uc->exclude_per_dir = xstrdup(ouc->exclude_per_dir);
+ load_sha1_stat(&uc->ss_info_exclude,
+ next + ouc_offset(info_exclude_stat),
+ next + ouc_offset(info_exclude_sha1));
+ load_sha1_stat(&uc->ss_excludes_file,
+ next + ouc_offset(excludes_file_stat),
+ next + ouc_offset(excludes_file_sha1));
+ uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
+ exclude_per_dir = (const char *)next + ouc_offset(exclude_per_dir);
+ uc->exclude_per_dir = xstrdup(exclude_per_dir);
/* NUL after exclude_per_dir is covered by sizeof(*ouc) */
- next += ouc_size(strlen(ouc->exclude_per_dir));
+ next += ouc_size(strlen(exclude_per_dir));
if (next >= end)
goto done2;
return result;
}
+void enable_delayed_checkout(struct checkout *state)
+{
+ if (!state->delayed_checkout) {
+ state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout));
+ state->delayed_checkout->state = CE_CAN_DELAY;
+ string_list_init(&state->delayed_checkout->filters, 0);
+ string_list_init(&state->delayed_checkout->paths, 0);
+ }
+}
+
+static int remove_available_paths(struct string_list_item *item, void *cb_data)
+{
+ struct string_list *available_paths = cb_data;
+ struct string_list_item *available;
+
+ available = string_list_lookup(available_paths, item->string);
+ if (available)
+ available->util = (void *)item->string;
+ return !available;
+}
+
+int finish_delayed_checkout(struct checkout *state)
+{
+ int errs = 0;
+ struct string_list_item *filter, *path;
+ struct delayed_checkout *dco = state->delayed_checkout;
+
+ if (!state->delayed_checkout)
+ return errs;
+
+ dco->state = CE_RETRY;
+ while (dco->filters.nr > 0) {
+ for_each_string_list_item(filter, &dco->filters) {
+ struct string_list available_paths = STRING_LIST_INIT_NODUP;
+
+ if (!async_query_available_blobs(filter->string, &available_paths)) {
+ /* Filter reported an error */
+ errs = 1;
+ filter->string = "";
+ continue;
+ }
+ if (available_paths.nr <= 0) {
+ /*
+ * Filter responded with no entries. That means
+ * the filter is done and we can remove the
+ * filter from the list (see
+ * "string_list_remove_empty_items" call below).
+ */
+ filter->string = "";
+ continue;
+ }
+
+ /*
+ * In dco->paths we store a list of all delayed paths.
+ * The filter just send us a list of available paths.
+ * Remove them from the list.
+ */
+ filter_string_list(&dco->paths, 0,
+ &remove_available_paths, &available_paths);
+
+ for_each_string_list_item(path, &available_paths) {
+ struct cache_entry* ce;
+
+ if (!path->util) {
+ error("external filter '%s' signaled that '%s' "
+ "is now available although it has not been "
+ "delayed earlier",
+ filter->string, path->string);
+ errs |= 1;
+
+ /*
+ * Do not ask the filter for available blobs,
+ * again, as the filter is likely buggy.
+ */
+ filter->string = "";
+ continue;
+ }
+ ce = index_file_exists(state->istate, path->string,
+ strlen(path->string), 0);
+ errs |= (ce ? checkout_entry(ce, state, NULL) : 1);
+ }
+ }
+ string_list_remove_empty_items(&dco->filters, 0);
+ }
+ string_list_clear(&dco->filters, 0);
+
+ /* At this point we should not have any delayed paths anymore. */
+ errs |= dco->paths.nr;
+ for_each_string_list_item(path, &dco->paths) {
+ error("'%s' was not filtered properly", path->string);
+ }
+ string_list_clear(&dco->paths, 0);
+
+ free(dco);
+ state->delayed_checkout = NULL;
+
+ return errs;
+}
+
static int write_entry(struct cache_entry *ce,
char *path, const struct checkout *state, int to_tempfile)
{
/*
* Convert from git internal format to working tree format
*/
- if (ce_mode_s_ifmt == S_IFREG &&
- convert_to_working_tree(ce->name, new, size, &buf)) {
- free(new);
- new = strbuf_detach(&buf, &newsize);
- size = newsize;
+ if (ce_mode_s_ifmt == S_IFREG) {
+ struct delayed_checkout *dco = state->delayed_checkout;
+ if (dco && dco->state != CE_NO_DELAY) {
+ /* Do not send the blob in case of a retry. */
+ if (dco->state == CE_RETRY) {
+ new = NULL;
+ size = 0;
+ }
+ ret = async_convert_to_working_tree(
+ ce->name, new, size, &buf, dco);
+ if (ret && string_list_has_string(&dco->paths, ce->name)) {
+ free(new);
+ goto finish;
+ }
+ } else
+ ret = convert_to_working_tree(
+ ce->name, new, size, &buf);
+
+ if (ret) {
+ free(new);
+ new = strbuf_detach(&buf, &newsize);
+ size = newsize;
+ }
+ /*
+ * No "else" here as errors from convert are OK at this
+ * point. If the error would have been fatal (e.g.
+ * filter is required), then we would have died already.
+ */
}
fd = open_output_fd(path, ce, to_tempfile);
buffer += 41;
parent_line_count++;
}
- graft = lookup_commit_graft(commit->object.oid.hash);
+ graft = lookup_commit_graft(&commit->object.oid);
parent_count = commit_list_count(commit->parents);
if (graft) {
if (graft->nr_parent == -1 && !parent_count)
memcpy(dst, src, st_mult(size, n));
}
+#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
+ BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
+static inline void move_array(void *dst, const void *src, size_t n, size_t size)
+{
+ if (n)
+ memmove(dst, src, st_mult(size, n));
+}
+
/*
* These functions help you allocate structs with flex arrays, and copy
* the data directly into the array. For example, if you had:
endif
endif
ifneq (,$(findstring MINGW,$(uname_S)))
+ifeq ($(shell expr "$(uname_R)" : '1\.'),2)
NO_MSGFMT=1
+endif
GITGUI_WINDOWS_WRAPPER := YesPlease
GITGUI_RELATIVE := 1
endif
-label [mc "Recent Repositories"]
}
+ if {[set lenrecent [llength $sorted_recent]] < $maxrecent} {
+ set lenrecent $maxrecent
+ }
+
${NS}::label $w_body.space
${NS}::label $w_body.recentlabel \
-anchor w \
-background [get_bg_color $w_body.recentlabel] \
-wrap none \
-width 50 \
- -height $maxrecent
+ -height $lenrecent
$w_recentlist tag conf link \
-foreground blue \
-underline 1
proc _get_recentrepos {} {
set recent [list]
- foreach p [get_config gui.recentrepo] {
+ foreach p [lsort -unique [get_config gui.recentrepo]] {
if {[_is_git [file join $p .git]]} {
lappend recent $p
} else {
_unset_recentrepo $p
}
}
- return [lsort $recent]
+ return $recent
}
proc _unset_recentrepo {p} {
regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
- git config --global --unset gui.recentrepo "^$p\$"
+ catch {git config --global --unset-all gui.recentrepo "^$p\$"}
load_config 1
}
set i [lsearch $recent $path]
if {$i >= 0} {
_unset_recentrepo $path
- set recent [lreplace $recent $i $i]
}
- lappend recent $path
git config --global --add gui.recentrepo $path
load_config 1
+ set recent [get_config gui.recentrepo]
if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
set maxrecent 10
while {[llength $recent] > $maxrecent} {
_unset_recentrepo [lindex $recent 0]
- set recent [lrange $recent 1 end]
+ set recent [get_config gui.recentrepo]
}
}
if test $? -ne 0
then
gettextln "Simple merge did not work, trying automatic merge."
- git-merge-index -o git-merge-one-file -a ||
+ git merge-index -o git-merge-one-file -a ||
OCTOPUS_FAILURE=1
next=$(git write-tree 2>/dev/null)
fi
;;
esac
- src1=$(git-unpack-file $2)
- src2=$(git-unpack-file $3)
+ src1=$(git unpack-file $2)
+ src2=$(git unpack-file $3)
case "$1" in
'')
echo "Added $4 in both, but differently."
- orig=$(git-unpack-file e69de29bb2d1d6434b8b29ae775ad8c2e48c5391)
+ orig=$(git unpack-file e69de29bb2d1d6434b8b29ae775ad8c2e48c5391)
;;
*)
echo "Auto-merging $4"
- orig=$(git-unpack-file $1)
+ orig=$(git unpack-file $1)
;;
esac
exit 0
else
echo "Simple merge failed, trying Automatic merge."
- if git-merge-index -o git-merge-one-file -a
+ if git merge-index -o git-merge-one-file -a
then
exit 0
else
# itself well to recording empty patches. fortunately, cherry-pick
# makes this easy
git cherry-pick ${gpg_sign_opt:+"$gpg_sign_opt"} --allow-empty \
- --right-only "$revisions" \
+ $allow_rerere_autoupdate --right-only "$revisions" \
${restrict_revision+^$restrict_revision}
ret=$?
else
git format-patch -k --stdout --full-index --cherry-pick --right-only \
--src-prefix=a/ --dst-prefix=b/ --no-renames --no-cover-letter \
+ $git_format_patch_opt \
"$revisions" ${restrict_revision+^$restrict_revision} \
>"$GIT_DIR/rebased-patches"
ret=$?
fi
git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" \
+ $allow_rerere_autoupdate \
${gpg_sign_opt:+"$gpg_sign_opt"} <"$GIT_DIR/rebased-patches"
ret=$?
test -d "$rewritten" &&
pick_one_preserving_merges "$@" && return
- output eval git cherry-pick \
+ output eval git cherry-pick $allow_rerere_autoupdate \
${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
"$strategy_args" $empty_args $ff "$@"
merge_args="--no-log --no-ff"
if ! do_with_author output eval \
'git merge ${gpg_sign_opt:+"$gpg_sign_opt"} \
- $merge_args $strategy_args -m "$msg_content" $new_parents'
+ $allow_rerere_autoupdate $merge_args \
+ $strategy_args -m "$msg_content" $new_parents'
then
printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
die_with_patch $sha1 "$(eval_gettext "Error redoing merge \$sha1")"
echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
;;
*)
- output eval git cherry-pick \
+ output eval git cherry-pick $allow_rerere_autoupdate \
${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
"$strategy_args" "$@" ||
die_with_patch $sha1 "$(eval_gettext "Could not pick \$sha1")"
'
ok_to_skip_pre_rebase=
resolvemsg="
-$(gettext 'When you have resolved this problem, run "git rebase --continue".
-If you prefer to skip this patch, run "git rebase --skip" instead.
-To check out the original branch and stop rebasing, run "git rebase --abort".')
+$(gettext 'Resolve all conflicts manually, mark them as resolved with
+"git add/rm <conflicted_files>", then run "git rebase --continue".
+You can instead skip this commit: run "git rebase --skip".
+To abort and get back to the state before "git rebase", run "git rebase --abort".')
"
unset onto
unset restrict_revision
autostash="$(git config --bool rebase.autostash || echo false)"
fork_point=auto
git_am_opt=
+git_format_patch_opt=
rebase_root=
force_rebase=
allow_rerere_autoupdate=
state_dir="$apply_dir"
fi
+if test -t 2 && test -z "$GIT_QUIET"
+then
+ git_format_patch_opt="$git_format_patch_opt --progress"
+fi
+
if test -z "$rebase_root"
then
case "$#" in
if test -z "$patch_mode"
then
+ test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
+ if test -n "$untracked"
+ then
+ git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
+ fi
+
if test $# != 0
then
git reset -q -- "$@"
else
git reset --hard -q
fi
- test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
- if test -n "$untracked"
- then
- git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
- fi
if test "$keep_index" = "t" && test -n "$i_tree"
then
if test -n "$u_tree"
then
- GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
+ GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
rm -f "$TMPindex" ||
die "$(gettext "Could not restore untracked files from stash entry")"
test $status != A && test $ignore_config = all && continue
fi
# Also show added or modified modules which are checked out
- GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
+ GIT_DIR="$sm_path/.git" git rev-parse --git-dir >/dev/null 2>&1 &&
printf '%s\n' "$sm_path"
done
)
missing_dst=
test $mod_src = 160000 &&
- ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
+ ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_src^0 >/dev/null &&
missing_src=t
test $mod_dst = 160000 &&
- ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
+ ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_dst^0 >/dev/null &&
missing_dst=t
display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
}
}
+void setup_auto_pager(const char *cmd, int def)
+{
+ if (use_pager != -1 || pager_in_use())
+ return;
+ use_pager = check_pager_config(cmd);
+ if (use_pager == -1)
+ use_pager = def;
+ commit_pager_choice();
+}
+
static int handle_options(const char ***argv, int *argc, int *envchanged)
{
const char **orig_argv = *argv;
*/
#define NEED_WORK_TREE (1<<3)
#define SUPPORT_SUPER_PREFIX (1<<4)
+#define DELAY_PAGER_CONFIG (1<<5)
struct cmd_struct {
const char *cmd;
prefix = setup_git_directory_gently(&nongit_ok);
}
- if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY))
+ if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) &&
+ !(p->option & DELAY_PAGER_CONFIG))
use_pager = check_pager_config(p->cmd);
if (use_pager == -1 && p->option & USE_PAGER)
use_pager = 1;
{ "fsck-objects", cmd_fsck, RUN_SETUP },
{ "gc", cmd_gc, RUN_SETUP },
{ "get-tar-commit-id", cmd_get_tar_commit_id },
- { "grep", cmd_grep, RUN_SETUP_GENTLY | SUPPORT_SUPER_PREFIX },
+ { "grep", cmd_grep, RUN_SETUP_GENTLY },
{ "hash-object", cmd_hash_object },
{ "help", cmd_help },
{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
{ "stripspace", cmd_stripspace },
{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX},
{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
- { "tag", cmd_tag, RUN_SETUP },
+ { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },
{ "unpack-file", cmd_unpack_file, RUN_SETUP },
{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
{ "update-index", cmd_update_index, RUN_SETUP },
if (get_super_prefix())
die("%s doesn't support --super-prefix", argv[0]);
- if (use_pager == -1)
+ if (use_pager == -1 && !is_builtin(argv[0]))
use_pager = check_pager_config(argv[0]);
commit_pager_choice();
return if (m!^[/.]$!);
# only directories can be git repositories
return unless (-d $_);
+ # need search permission
+ return unless (-x $_);
# don't traverse too deep (Find is super slow on os x)
# $project_maxdepth excludes depth of $projectroot
if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
case GREP_SOURCE_FILE:
gs->identifier = xstrdup(identifier);
break;
- case GREP_SOURCE_SUBMODULE:
- if (!identifier) {
- gs->identifier = NULL;
- break;
- }
- /*
- * FALL THROUGH
- * If the identifier is non-NULL (in the submodule case) it
- * will be a SHA1 that needs to be copied.
- */
case GREP_SOURCE_OID:
gs->identifier = oiddup(identifier);
break;
switch (gs->type) {
case GREP_SOURCE_FILE:
case GREP_SOURCE_OID:
- case GREP_SOURCE_SUBMODULE:
FREE_AND_NULL(gs->buf);
gs->size = 0;
break;
return grep_source_load_oid(gs);
case GREP_SOURCE_BUF:
return gs->buf ? 0 : -1;
- case GREP_SOURCE_SUBMODULE:
- break;
}
die("BUG: invalid grep_source type to load");
}
GREP_SOURCE_OID,
GREP_SOURCE_FILE,
GREP_SOURCE_BUF,
- GREP_SOURCE_SUBMODULE,
} type;
void *identifier;
if (!strcmp("http.sslversion", var))
return git_config_string(&ssl_version, var, value);
if (!strcmp("http.sslcert", var))
- return git_config_string(&ssl_cert, var, value);
+ return git_config_pathname(&ssl_cert, var, value);
#if LIBCURL_VERSION_NUM >= 0x070903
if (!strcmp("http.sslkey", var))
- return git_config_string(&ssl_key, var, value);
+ return git_config_pathname(&ssl_key, var, value);
#endif
#if LIBCURL_VERSION_NUM >= 0x070908
if (!strcmp("http.sslcapath", var))
const char *name,
char **repo_abbrev)
{
- unsigned char sha1[20];
+ struct object_id oid;
char *buf;
unsigned long size;
enum object_type type;
if (!name)
return 0;
- if (get_sha1(name, sha1) < 0)
+ if (get_oid(name, &oid) < 0)
return 0;
- buf = read_sha1_file(sha1, &type, &size);
+ buf = read_sha1_file(oid.hash, &type, &size);
if (!buf)
return error("unable to read mailmap object at %s", name);
if (type != OBJ_BLOB)
};
static int dir_entry_cmp(const void *unused_cmp_data,
- const struct dir_entry *e1,
- const struct dir_entry *e2,
- const char *name)
+ const void *entry,
+ const void *entry_or_key,
+ const void *keydata)
{
+ const struct dir_entry *e1 = entry;
+ const struct dir_entry *e2 = entry_or_key;
+ const char *name = keydata;
+
return e1->namelen != e2->namelen || strncasecmp(e1->name,
name ? name : e2->name, e1->namelen);
}
}
static int cache_entry_cmp(const void *unused_cmp_data,
- const struct cache_entry *ce1,
- const struct cache_entry *ce2,
+ const void *entry,
+ const void *entry_or_key,
const void *remove)
{
+ const struct cache_entry *ce1 = entry;
+ const struct cache_entry *ce2 = entry_or_key;
/*
* For remove_name_hash, find the exact entry (pointer equality); for
* index_file_exists, find all entries with matching hash code and
{
if (istate->name_hash_initialized)
return;
- hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
- NULL, istate->cache_nr);
- hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
- NULL, istate->cache_nr);
+ hashmap_init(&istate->name_hash, cache_entry_cmp, NULL, istate->cache_nr);
+ hashmap_init(&istate->dir_hash, dir_entry_cmp, NULL, istate->cache_nr);
if (lookup_lazy_params(istate)) {
hashmap_disallow_rehash(&istate->dir_hash, 1);
else {
*occupied = 0;
if (insert_new && i < len) {
- memmove(list + i + 1, list + i,
- (len - i) * sizeof(struct notes_merge_pair));
+ MOVE_ARRAY(list + i + 1, list + i, len - i);
memset(list + i, 0, sizeof(struct notes_merge_pair));
}
}
unsigned char type;
struct leaf_node *l;
- buf = fill_tree_descriptor(&desc, subtree->val_oid.hash);
+ buf = fill_tree_descriptor(&desc, &subtree->val_oid);
if (!buf)
die("Could not read %s for notes-index",
oid_to_hex(&subtree->val_oid));
t->dirty = 0;
if (flags & NOTES_INIT_EMPTY || !notes_ref ||
- get_sha1_treeish(notes_ref, object_oid.hash))
+ get_oid_treeish(notes_ref, &object_oid))
return;
if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, object_oid.hash))
die("Cannot use notes ref %s", notes_ref);
struct object *obj = o;
obj->parsed = 0;
- obj->used = 0;
obj->flags = 0;
hashcpy(obj->oid.hash, sha1);
* http-push.c: 16-----19
* commit.c: 16-----19
* sha1_name.c: 20
+ * builtin/fsck.c: 0--3
*/
#define FLAG_BITS 27
*/
struct object {
unsigned parsed : 1;
- unsigned used : 1;
unsigned type : TYPE_BITS;
unsigned flags : FLAG_BITS;
struct object_id oid;
const char *cmd;
if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
- int b = git_config_maybe_bool(var, value);
+ int b = git_parse_maybe_bool(value);
if (b >= 0)
data->want = b;
else {
* the side of safety. The actual value being negative does not have
* any significance; only that it is non-zero matters.
*/
-static int patch_id_cmp(struct diff_options *opt,
- struct patch_id *a,
- struct patch_id *b,
+static int patch_id_cmp(const void *cmpfn_data,
+ const void *entry,
+ const void *entry_or_key,
const void *unused_keydata)
{
+ /* NEEDSWORK: const correctness? */
+ struct diff_options *opt = (void *)cmpfn_data;
+ struct patch_id *a = (void *)entry;
+ struct patch_id *b = (void *)entry_or_key;
+
if (is_null_oid(&a->patch_id) &&
commit_patch_id(a->commit, opt, &a->patch_id, 0))
return error("Could not get patch ID for %s",
ids->diffopts.detect_rename = 0;
DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
diff_setup_done(&ids->diffopts);
- hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
- &ids->diffopts, 256);
+ hashmap_init(&ids->patches, patch_id_cmp, &ids->diffopts, 256);
return 0;
}
{
struct pathspec_item *item;
const char *entry = argv ? *argv : NULL;
- int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
+ int i, n, prefixlen, nr_exclude = 0;
memset(pathspec, 0, sizeof(*pathspec));
}
n = 0;
- warn_empty_string = 1;
while (argv[n]) {
- if (*argv[n] == '\0' && warn_empty_string) {
- warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
- "please use . instead if you meant to match all paths"));
- warn_empty_string = 0;
- }
+ if (*argv[n] == '\0')
+ die("empty string is not a valid pathspec. "
+ "please use . instead if you meant to match all paths");
n++;
}
=cut
sub get_tz_offset {
- # some systmes don't handle or mishandle %z, so be creative.
+ # some systems don't handle or mishandle %z, so be creative.
my $t = shift || time;
my $gm = timegm(localtime($t));
my $sign = qw( + + - )[ $gm <=> $t ];
delete $ENV{TZ};
}
- my $our_TZ = get_tz_offset();
+ my $our_TZ = get_tz_offset($epoch_in_UTC);
# This converts $epoch_in_UTC into our local timezone.
my ($sec, $min, $hour, $mday, $mon, $year,
return status;
}
-int packet_writel(int fd, const char *line, ...)
-{
- va_list args;
- int err;
- va_start(args, line);
- for (;;) {
- if (!line)
- break;
- if (strlen(line) > LARGE_PACKET_DATA_MAX)
- return -1;
- err = packet_write_fmt_gently(fd, "%s\n", line);
- if (err)
- return err;
- line = va_arg(args, const char*);
- }
- va_end(args);
- return packet_flush_gently(fd);
-}
-
static int packet_write_gently(const int fd_out, const char *buf, size_t size)
{
static char packet_write_buffer[LARGE_PACKET_MAX];
void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
int packet_flush_gently(int fd);
int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
-LAST_ARG_MUST_BE_NULL
-int packet_writel(int fd, const char *line, ...);
int write_packetized_from_fd(int fd_in, int fd_out);
int write_packetized_from_buf(const char *src_in, size_t len, int fd_out);
struct format_commit_context *c)
{
const char *rest = placeholder;
+ const char *basic_color = NULL;
if (placeholder[1] == '(') {
const char *begin = placeholder + 2;
if (!end)
return 0;
+
if (skip_prefix(begin, "auto,", &begin)) {
if (!want_color(c->pretty_ctx->color))
return end - placeholder + 1;
+ } else if (skip_prefix(begin, "always,", &begin)) {
+ /* nothing to do; we do not respect want_color at all */
+ } else {
+ /* the default is the same as "auto" */
+ if (!want_color(c->pretty_ctx->color))
+ return end - placeholder + 1;
}
+
if (color_parse_mem(begin, end - begin, color) < 0)
die(_("unable to parse --pretty format"));
strbuf_addstr(sb, color);
return end - placeholder + 1;
}
+
+ /*
+ * We handle things like "%C(red)" above; for historical reasons, there
+ * are a few colors that can be specified without parentheses (and
+ * they cannot support things like "auto" or "always" at all).
+ */
if (skip_prefix(placeholder + 1, "red", &rest))
- strbuf_addstr(sb, GIT_COLOR_RED);
+ basic_color = GIT_COLOR_RED;
else if (skip_prefix(placeholder + 1, "green", &rest))
- strbuf_addstr(sb, GIT_COLOR_GREEN);
+ basic_color = GIT_COLOR_GREEN;
else if (skip_prefix(placeholder + 1, "blue", &rest))
- strbuf_addstr(sb, GIT_COLOR_BLUE);
+ basic_color = GIT_COLOR_BLUE;
else if (skip_prefix(placeholder + 1, "reset", &rest))
- strbuf_addstr(sb, GIT_COLOR_RESET);
+ basic_color = GIT_COLOR_RESET;
+
+ if (basic_color && want_color(c->pretty_ctx->color))
+ strbuf_addstr(sb, basic_color);
+
return rest - placeholder;
}
istate->cache_nr--;
if (pos >= istate->cache_nr)
return 0;
- memmove(istate->cache + pos,
- istate->cache + pos + 1,
- (istate->cache_nr - pos) * sizeof(struct cache_entry *));
+ MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
+ istate->cache_nr - pos);
return 1;
}
} u;
} *used_atom;
static int used_atom_cnt, need_tagged, need_symref;
-static int need_color_reset_at_eol;
-static void color_atom_parser(struct used_atom *atom, const char *color_value)
+static void color_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *color_value)
{
if (!color_value)
die(_("expected format: %%(color:<color>)"));
if (color_parse(color_value, atom->u.color) < 0)
die(_("unrecognized color: %%(color:%s)"), color_value);
+ /*
+ * We check this after we've parsed the color, which lets us complain
+ * about syntactically bogus color names even if they won't be used.
+ */
+ if (!want_color(format->use_color))
+ color_parse("", atom->u.color);
}
static void refname_atom_parser_internal(struct refname_atom *atom,
die(_("unrecognized %%(%s) argument: %s"), name, arg);
}
-static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
+static void remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
struct string_list params = STRING_LIST_INIT_DUP;
int i;
string_list_clear(¶ms, 0);
}
-static void body_atom_parser(struct used_atom *atom, const char *arg)
+static void body_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
if (arg)
die(_("%%(body) does not take arguments"));
atom->u.contents.option = C_BODY_DEP;
}
-static void subject_atom_parser(struct used_atom *atom, const char *arg)
+static void subject_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
if (arg)
die(_("%%(subject) does not take arguments"));
atom->u.contents.option = C_SUB;
}
-static void trailers_atom_parser(struct used_atom *atom, const char *arg)
+static void trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
if (arg)
die(_("%%(trailers) does not take arguments"));
atom->u.contents.option = C_TRAILERS;
}
-static void contents_atom_parser(struct used_atom *atom, const char *arg)
+static void contents_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
if (!arg)
atom->u.contents.option = C_BARE;
die(_("unrecognized %%(contents) argument: %s"), arg);
}
-static void objectname_atom_parser(struct used_atom *atom, const char *arg)
+static void objectname_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
if (!arg)
atom->u.objectname.option = O_FULL;
die(_("unrecognized %%(objectname) argument: %s"), arg);
}
-static void refname_atom_parser(struct used_atom *atom, const char *arg)
+static void refname_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
refname_atom_parser_internal(&atom->u.refname, arg, atom->name);
}
return -1;
}
-static void align_atom_parser(struct used_atom *atom, const char *arg)
+static void align_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
struct align *align = &atom->u.align;
struct string_list params = STRING_LIST_INIT_DUP;
string_list_clear(¶ms, 0);
}
-static void if_atom_parser(struct used_atom *atom, const char *arg)
+static void if_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
if (!arg) {
atom->u.if_then_else.cmp_status = COMPARE_NONE;
}
}
-static void head_atom_parser(struct used_atom *atom, const char *arg)
+static void head_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
{
struct object_id unused;
static struct {
const char *name;
cmp_type cmp_type;
- void (*parser)(struct used_atom *atom, const char *arg);
+ void (*parser)(const struct ref_format *format, struct used_atom *atom, const char *arg);
} valid_atom[] = {
{ "refname" , FIELD_STR, refname_atom_parser },
{ "objecttype" },
/*
* Used to parse format string and sort specifiers
*/
-int parse_ref_filter_atom(const char *atom, const char *ep)
+static int parse_ref_filter_atom(const struct ref_format *format,
+ const char *atom, const char *ep)
{
const char *sp;
const char *arg;
arg = used_atom[at].name + (arg - atom) + 1;
memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
if (valid_atom[i].parser)
- valid_atom[i].parser(&used_atom[at], arg);
+ valid_atom[i].parser(format, &used_atom[at], arg);
if (*atom == '*')
need_tagged = 1;
if (!strcmp(valid_atom[i].name, "symref"))
* Make sure the format string is well formed, and parse out
* the used atoms.
*/
-int verify_ref_format(const char *format)
+int verify_ref_format(struct ref_format *format)
{
const char *cp, *sp;
- need_color_reset_at_eol = 0;
- for (cp = format; *cp && (sp = find_next(cp)); ) {
+ format->need_color_reset_at_eol = 0;
+ for (cp = format->format; *cp && (sp = find_next(cp)); ) {
const char *color, *ep = strchr(sp, ')');
int at;
if (!ep)
return error(_("malformed format string %s"), sp);
/* sp points at "%(" and ep points at the closing ")" */
- at = parse_ref_filter_atom(sp + 2, ep);
+ at = parse_ref_filter_atom(format, sp + 2, ep);
cp = ep + 1;
if (skip_prefix(used_atom[at].name, "color:", &color))
- need_color_reset_at_eol = !!strcmp(color, "reset");
+ format->need_color_reset_at_eol = !!strcmp(color, "reset");
}
+ if (format->need_color_reset_at_eol && !want_color(format->use_color))
+ format->need_color_reset_at_eol = 0;
return 0;
}
}
}
-void format_ref_array_item(struct ref_array_item *info, const char *format,
- int quote_style, struct strbuf *final_buf)
+void format_ref_array_item(struct ref_array_item *info,
+ const struct ref_format *format,
+ struct strbuf *final_buf)
{
const char *cp, *sp, *ep;
struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
- state.quote_style = quote_style;
+ state.quote_style = format->quote_style;
push_stack_element(&state.stack);
- for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
+ for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
struct atom_value *atomv;
ep = strchr(sp, ')');
if (cp < sp)
append_literal(cp, sp, &state);
- get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
+ get_ref_atom_value(info,
+ parse_ref_filter_atom(format, sp + 2, ep),
+ &atomv);
atomv->handler(atomv, &state);
}
if (*cp) {
sp = cp + strlen(cp);
append_literal(cp, sp, &state);
}
- if (need_color_reset_at_eol) {
+ if (format->need_color_reset_at_eol) {
struct atom_value resetv;
- char color[COLOR_MAXLEN] = "";
-
- if (color_parse("reset", color) < 0)
- die("BUG: couldn't parse 'reset' as a color");
- resetv.s = color;
+ resetv.s = GIT_COLOR_RESET;
append_atom(&resetv, &state);
}
if (state.stack->prev)
pop_stack_element(&state.stack);
}
-void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
+void show_ref_array_item(struct ref_array_item *info,
+ const struct ref_format *format)
{
struct strbuf final_buf = STRBUF_INIT;
- format_ref_array_item(info, format, quote_style, &final_buf);
+ format_ref_array_item(info, format, &final_buf);
fwrite(final_buf.buf, 1, final_buf.len, stdout);
strbuf_release(&final_buf);
putchar('\n');
}
void pretty_print_ref(const char *name, const unsigned char *sha1,
- const char *format)
+ const struct ref_format *format)
{
struct ref_array_item *ref_item;
ref_item = new_ref_array_item(name, sha1, 0);
ref_item->kind = ref_kind_from_refname(name);
- show_ref_array_item(ref_item, format, 0);
+ show_ref_array_item(ref_item, format);
free_array_item(ref_item);
}
+static int parse_sorting_atom(const char *atom)
+{
+ /*
+ * This parses an atom using a dummy ref_format, since we don't
+ * actually care about the formatting details.
+ */
+ struct ref_format dummy = REF_FORMAT_INIT;
+ const char *end = atom + strlen(atom);
+ return parse_ref_filter_atom(&dummy, atom, end);
+}
+
/* If no sorting option is given, use refname to sort as default */
struct ref_sorting *ref_default_sorting(void)
{
struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
sorting->next = NULL;
- sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
+ sorting->atom = parse_sorting_atom(cstr_name);
return sorting;
}
-int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
+void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
{
- struct ref_sorting **sorting_tail = opt->value;
struct ref_sorting *s;
- int len;
-
- if (!arg) /* should --no-sort void the list ? */
- return -1;
s = xcalloc(1, sizeof(*s));
s->next = *sorting_tail;
if (skip_prefix(arg, "version:", &arg) ||
skip_prefix(arg, "v:", &arg))
s->version = 1;
- len = strlen(arg);
- s->atom = parse_ref_filter_atom(arg, arg+len);
+ s->atom = parse_sorting_atom(arg);
+}
+
+int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
+{
+ if (!arg) /* should --no-sort void the list ? */
+ return -1;
+ parse_ref_sorting(opt->value, arg);
return 0;
}
verbose;
};
+struct ref_format {
+ /*
+ * Set these to define the format; make sure you call
+ * verify_ref_format() afterwards to finalize.
+ */
+ const char *format;
+ int quote_style;
+ int use_color;
+
+ /* Internal state to ref-filter */
+ int need_color_reset_at_eol;
+};
+
+#define REF_FORMAT_INIT { NULL, 0, -1 }
+
/* Macros for checking --merged and --no-merged options */
#define _OPT_MERGED_NO_MERGED(option, filter, h) \
{ OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
/* Clear all memory allocated to ref_array */
void ref_array_clear(struct ref_array *array);
-/* Parse format string and sort specifiers */
-int parse_ref_filter_atom(const char *atom, const char *ep);
/* Used to verify if the given format is correct and to parse out the used atoms */
-int verify_ref_format(const char *format);
+int verify_ref_format(struct ref_format *format);
/* Sort the given ref_array as per the ref_sorting provided */
void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
/* Based on the given format and quote_style, fill the strbuf */
-void format_ref_array_item(struct ref_array_item *info, const char *format,
- int quote_style, struct strbuf *final_buf);
+void format_ref_array_item(struct ref_array_item *info,
+ const struct ref_format *format,
+ struct strbuf *final_buf);
/* Print the ref using the given format and quote_style */
-void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style);
+void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
+/* Parse a single sort specifier and add it to the list */
+void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
/* Callback function for parsing the sort option */
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
/* Default sort option based on refname */
* name must be a fully qualified refname.
*/
void pretty_print_ref(const char *name, const unsigned char *sha1,
- const char *format);
+ const struct ref_format *format);
#endif /* REF_FILTER_H */
return -1;
}
-struct commit_info_lifo {
- struct commit_info {
- struct commit *commit;
- void *util;
- } *items;
- int nr, alloc;
-};
-
-static struct commit_info *get_commit_info(struct commit *commit,
- struct commit_info_lifo *lifo, int pop)
-{
- int i;
- for (i = 0; i < lifo->nr; i++)
- if (lifo->items[i].commit == commit) {
- struct commit_info *result = &lifo->items[i];
- if (pop) {
- if (i + 1 < lifo->nr)
- memmove(lifo->items + i,
- lifo->items + i + 1,
- (lifo->nr - i) *
- sizeof(struct commit_info));
- lifo->nr--;
- }
- return result;
- }
- return NULL;
-}
-
-static void add_commit_info(struct commit *commit, void *util,
- struct commit_info_lifo *lifo)
-{
- struct commit_info *info;
- ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
- info = lifo->items + lifo->nr;
- info->commit = commit;
- info->util = util;
- lifo->nr++;
-}
-
struct commit_reflog {
int recno;
enum selector_type {
};
struct reflog_walk_info {
- struct commit_info_lifo reflogs;
+ struct commit_reflog **logs;
+ size_t nr, alloc;
struct string_list complete_reflogs;
struct commit_reflog *last_commit_reflog;
};
commit_reflog->selector = selector;
commit_reflog->reflogs = reflogs;
- add_commit_info(commit, commit_reflog, &info->reflogs);
- return 0;
-}
+ ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
+ info->logs[info->nr++] = commit_reflog;
-void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
-{
- struct commit_info *commit_info =
- get_commit_info(commit, &info->reflogs, 0);
- struct commit_reflog *commit_reflog;
- struct object *logobj;
- struct reflog_info *reflog;
-
- info->last_commit_reflog = NULL;
- if (!commit_info)
- return;
-
- commit_reflog = commit_info->util;
- if (commit_reflog->recno < 0) {
- commit->parents = NULL;
- return;
- }
- info->last_commit_reflog = commit_reflog;
-
- do {
- reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
- commit_reflog->recno--;
- logobj = parse_object(&reflog->ooid);
- } while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
-
- if (!logobj && commit_reflog->recno >= 0 && is_null_oid(&reflog->ooid)) {
- /* a root commit, but there are still more entries to show */
- reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
- logobj = parse_object(&reflog->noid);
- if (!logobj)
- logobj = parse_object(&reflog->ooid);
- }
-
- if (!logobj || logobj->type != OBJ_COMMIT) {
- commit_info->commit = NULL;
- commit->parents = NULL;
- return;
- }
- commit_info->commit = (struct commit *)logobj;
-
- commit->parents = xcalloc(1, sizeof(struct commit_list));
- commit->parents->item = commit_info->commit;
+ return 0;
}
void get_reflog_selector(struct strbuf *sb,
return info->email;
}
+timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
+{
+ struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
+ struct reflog_info *info;
+
+ if (!commit_reflog)
+ return 0;
+
+ info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
+ return info->timestamp;
+}
+
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
const struct date_mode *dmode, int force_date)
{
strbuf_release(&selector);
}
}
+
+int reflog_walk_empty(struct reflog_walk_info *info)
+{
+ return !info || !info->nr;
+}
+
+static struct commit *next_reflog_commit(struct commit_reflog *log)
+{
+ for (; log->recno >= 0; log->recno--) {
+ struct reflog_info *entry = &log->reflogs->items[log->recno];
+ struct object *obj = parse_object(&entry->noid);
+
+ if (obj && obj->type == OBJ_COMMIT)
+ return (struct commit *)obj;
+ }
+ return NULL;
+}
+
+static timestamp_t log_timestamp(struct commit_reflog *log)
+{
+ return log->reflogs->items[log->recno].timestamp;
+}
+
+struct commit *next_reflog_entry(struct reflog_walk_info *walk)
+{
+ struct commit_reflog *best = NULL;
+ struct commit *best_commit = NULL;
+ size_t i;
+
+ for (i = 0; i < walk->nr; i++) {
+ struct commit_reflog *log = walk->logs[i];
+ struct commit *commit = next_reflog_commit(log);
+
+ if (!commit)
+ continue;
+
+ if (!best || log_timestamp(log) > log_timestamp(best)) {
+ best = log;
+ best_commit = commit;
+ }
+ }
+
+ if (best) {
+ best->recno--;
+ walk->last_commit_reflog = best;
+ return best_commit;
+ }
+
+ return NULL;
+}
extern void init_reflog_walk(struct reflog_walk_info **info);
extern int add_reflog_for_walk(struct reflog_walk_info *info,
struct commit *commit, const char *name);
-extern void fake_reflog_parent(struct reflog_walk_info *info,
- struct commit *commit);
extern void show_reflog_message(struct reflog_walk_info *info, int,
const struct date_mode *, int force_date);
extern void get_reflog_message(struct strbuf *sb,
struct reflog_walk_info *reflog_info);
extern const char *get_reflog_ident(struct reflog_walk_info *reflog_info);
+extern timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);
extern void get_reflog_selector(struct strbuf *sb,
struct reflog_walk_info *reflog_info,
const struct date_mode *dmode, int force_date,
int shorten);
+extern int reflog_walk_empty(struct reflog_walk_info *walk);
+
+struct commit *next_reflog_entry(struct reflog_walk_info *reflog_info);
+
#endif
return 1;
}
+/*
+ * Return true if refname, which has the specified oid and flags, can
+ * be resolved to an object in the database. If the referred-to object
+ * does not exist, emit a warning and return false.
+ */
+int ref_resolves_to_object(const char *refname,
+ const struct object_id *oid,
+ unsigned int flags)
+{
+ if (flags & REF_ISBROKEN)
+ return 0;
+ if (!has_sha1_file(oid->hash)) {
+ error("%s does not point to a valid object!", refname);
+ return 0;
+ }
+ return 1;
+}
+
char *refs_resolve_refdup(struct ref_store *refs,
const char *refname, int resolve_flags,
unsigned char *sha1, int *flags)
for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
if (!cb.reccnt) {
- if (flags & GET_SHA1_QUIETLY)
+ if (flags & GET_OID_QUIETLY)
exit(128);
else
die("Log for %s is empty.", refname);
const char *match = hide_refs->items[i].string;
const char *subject;
int neg = 0;
- int len;
+ const char *p;
if (*match == '!') {
neg = 1;
}
/* refname can be NULL when namespaces are used. */
- if (!subject || !starts_with(subject, match))
- continue;
- len = strlen(match);
- if (!subject[len] || subject[len] == '/')
+ if (subject &&
+ skip_prefix(subject, match, &p) &&
+ (!*p || *p == '/'))
return !neg;
}
return 0;
#include "../refs.h"
#include "refs-internal.h"
#include "ref-cache.h"
+#include "packed-backend.h"
#include "../iterator.h"
#include "../dir-iterator.h"
#include "../lockfile.h"
struct object_id old_oid;
};
-/*
- * Return true if refname, which has the specified oid and flags, can
- * be resolved to an object in the database. If the referred-to object
- * does not exist, emit a warning and return false.
- */
-static int ref_resolves_to_object(const char *refname,
- const struct object_id *oid,
- unsigned int flags)
-{
- if (flags & REF_ISBROKEN)
- return 0;
- if (!has_sha1_file(oid->hash)) {
- error("%s does not point to a valid object!", refname);
- return 0;
- }
- return 1;
-}
-
-struct packed_ref_cache {
- struct ref_cache *cache;
-
- /*
- * Count of references to the data structure in this instance,
- * including the pointer from files_ref_store::packed if any.
- * The data will not be freed as long as the reference count
- * is nonzero.
- */
- unsigned int referrers;
-
- /* The metadata from when this packed-refs cache was read */
- struct stat_validity validity;
-};
-
/*
* Future: need to be in "struct repository"
* when doing a full libification.
char *gitdir;
char *gitcommondir;
- char *packed_refs_path;
struct ref_cache *loose;
- struct packed_ref_cache *packed;
- /*
- * Lock used for the "packed-refs" file. Note that this (and
- * thus the enclosing `files_ref_store`) must not be freed.
- */
- struct lock_file packed_refs_lock;
+ struct ref_store *packed_ref_store;
};
-/*
- * Increment the reference count of *packed_refs.
- */
-static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
-{
- packed_refs->referrers++;
-}
-
-/*
- * Decrease the reference count of *packed_refs. If it goes to zero,
- * free *packed_refs and return true; otherwise return false.
- */
-static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
-{
- if (!--packed_refs->referrers) {
- free_ref_cache(packed_refs->cache);
- stat_validity_clear(&packed_refs->validity);
- free(packed_refs);
- return 1;
- } else {
- return 0;
- }
-}
-
-static void clear_packed_ref_cache(struct files_ref_store *refs)
-{
- if (refs->packed) {
- struct packed_ref_cache *packed_refs = refs->packed;
-
- if (is_lock_file_locked(&refs->packed_refs_lock))
- die("BUG: packed-ref cache cleared while locked");
- refs->packed = NULL;
- release_packed_ref_cache(packed_refs);
- }
-}
-
static void clear_loose_ref_cache(struct files_ref_store *refs)
{
if (refs->loose) {
get_common_dir_noenv(&sb, gitdir);
refs->gitcommondir = strbuf_detach(&sb, NULL);
strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
- refs->packed_refs_path = strbuf_detach(&sb, NULL);
+ refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
+ strbuf_release(&sb);
return ref_store;
}
return refs;
}
-/* The length of a peeled reference line in packed-refs, including EOL: */
-#define PEELED_LINE_LENGTH 42
-
-/*
- * The packed-refs header line that we write out. Perhaps other
- * traits will be added later. The trailing space is required.
- */
-static const char PACKED_REFS_HEADER[] =
- "# pack-refs with: peeled fully-peeled \n";
-
-/*
- * Parse one line from a packed-refs file. Write the SHA1 to sha1.
- * Return a pointer to the refname within the line (null-terminated),
- * or NULL if there was a problem.
- */
-static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
-{
- const char *ref;
-
- if (parse_oid_hex(line->buf, oid, &ref) < 0)
- return NULL;
- if (!isspace(*ref++))
- return NULL;
-
- if (isspace(*ref))
- return NULL;
-
- if (line->buf[line->len - 1] != '\n')
- return NULL;
- line->buf[--line->len] = 0;
-
- return ref;
-}
-
-/*
- * Read from `packed_refs_file` into a newly-allocated
- * `packed_ref_cache` and return it. The return value will already
- * have its reference count incremented.
- *
- * A comment line of the form "# pack-refs with: " may contain zero or
- * more traits. We interpret the traits as follows:
- *
- * No traits:
- *
- * Probably no references are peeled. But if the file contains a
- * peeled value for a reference, we will use it.
- *
- * peeled:
- *
- * References under "refs/tags/", if they *can* be peeled, *are*
- * peeled in this file. References outside of "refs/tags/" are
- * probably not peeled even if they could have been, but if we find
- * a peeled value for such a reference we will use it.
- *
- * fully-peeled:
- *
- * All references in the file that can be peeled are peeled.
- * Inversely (and this is more important), any references in the
- * file for which no peeled value is recorded is not peelable. This
- * trait should typically be written alongside "peeled" for
- * compatibility with older clients, but we do not require it
- * (i.e., "peeled" is a no-op if "fully-peeled" is set).
- */
-static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
-{
- FILE *f;
- struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
- struct ref_entry *last = NULL;
- struct strbuf line = STRBUF_INIT;
- enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
- struct ref_dir *dir;
-
- acquire_packed_ref_cache(packed_refs);
- packed_refs->cache = create_ref_cache(NULL, NULL);
- packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
-
- f = fopen(packed_refs_file, "r");
- if (!f) {
- if (errno == ENOENT) {
- /*
- * This is OK; it just means that no
- * "packed-refs" file has been written yet,
- * which is equivalent to it being empty.
- */
- return packed_refs;
- } else {
- die_errno("couldn't read %s", packed_refs_file);
- }
- }
-
- stat_validity_update(&packed_refs->validity, fileno(f));
-
- dir = get_ref_dir(packed_refs->cache->root);
- while (strbuf_getwholeline(&line, f, '\n') != EOF) {
- struct object_id oid;
- const char *refname;
- const char *traits;
-
- if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
- if (strstr(traits, " fully-peeled "))
- peeled = PEELED_FULLY;
- else if (strstr(traits, " peeled "))
- peeled = PEELED_TAGS;
- /* perhaps other traits later as well */
- continue;
- }
-
- refname = parse_ref_line(&line, &oid);
- if (refname) {
- int flag = REF_ISPACKED;
-
- if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
- if (!refname_is_safe(refname))
- die("packed refname is dangerous: %s", refname);
- oidclr(&oid);
- flag |= REF_BAD_NAME | REF_ISBROKEN;
- }
- last = create_ref_entry(refname, &oid, flag);
- if (peeled == PEELED_FULLY ||
- (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
- last->flag |= REF_KNOWS_PEELED;
- add_ref_entry(dir, last);
- continue;
- }
- if (last &&
- line.buf[0] == '^' &&
- line.len == PEELED_LINE_LENGTH &&
- line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
- !get_oid_hex(line.buf + 1, &oid)) {
- oidcpy(&last->u.value.peeled, &oid);
- /*
- * Regardless of what the file header said,
- * we definitely know the value of *this*
- * reference:
- */
- last->flag |= REF_KNOWS_PEELED;
- }
- }
-
- fclose(f);
- strbuf_release(&line);
-
- return packed_refs;
-}
-
-static const char *files_packed_refs_path(struct files_ref_store *refs)
-{
- return refs->packed_refs_path;
-}
-
static void files_reflog_path(struct files_ref_store *refs,
struct strbuf *sb,
const char *refname)
}
}
-/*
- * Check that the packed refs cache (if any) still reflects the
- * contents of the file. If not, clear the cache.
- */
-static void validate_packed_ref_cache(struct files_ref_store *refs)
-{
- if (refs->packed &&
- !stat_validity_check(&refs->packed->validity,
- files_packed_refs_path(refs)))
- clear_packed_ref_cache(refs);
-}
-
-/*
- * Get the packed_ref_cache for the specified files_ref_store,
- * creating and populating it if it hasn't been read before or if the
- * file has been changed (according to its `validity` field) since it
- * was last read. On the other hand, if we hold the lock, then assume
- * that the file hasn't been changed out from under us, so skip the
- * extra `stat()` call in `stat_validity_check()`.
- */
-static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
-{
- const char *packed_refs_file = files_packed_refs_path(refs);
-
- if (!is_lock_file_locked(&refs->packed_refs_lock))
- validate_packed_ref_cache(refs);
-
- if (!refs->packed)
- refs->packed = read_packed_refs(packed_refs_file);
-
- return refs->packed;
-}
-
-static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
-{
- return get_ref_dir(packed_ref_cache->cache->root);
-}
-
-static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
-{
- return get_packed_ref_dir(get_packed_ref_cache(refs));
-}
-
-/*
- * Add a reference to the in-memory packed reference cache. This may
- * only be called while the packed-refs file is locked (see
- * lock_packed_refs()). To actually write the packed-refs file, call
- * commit_packed_refs().
- */
-static void add_packed_ref(struct files_ref_store *refs,
- const char *refname, const struct object_id *oid)
-{
- struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
-
- if (!is_lock_file_locked(&refs->packed_refs_lock))
- die("BUG: packed refs not locked");
-
- if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
- die("Reference has invalid format: '%s'", refname);
-
- add_ref_entry(get_packed_ref_dir(packed_ref_cache),
- create_ref_entry(refname, oid, REF_ISPACKED));
-}
-
/*
* Read the loose references from the namespace dirname into dir
* (without recursing). dirname must end with '/'. dir must be the
return refs->loose;
}
-/*
- * Return the ref_entry for the given refname from the packed
- * references. If it does not exist, return NULL.
- */
-static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
- const char *refname)
-{
- return find_ref_entry(get_packed_refs(refs), refname);
-}
-
-/*
- * A loose ref file doesn't exist; check for a packed ref.
- */
-static int resolve_packed_ref(struct files_ref_store *refs,
- const char *refname,
- unsigned char *sha1, unsigned int *flags)
-{
- struct ref_entry *entry;
-
- /*
- * The loose reference file does not exist; check for a packed
- * reference.
- */
- entry = get_packed_ref(refs, refname);
- if (entry) {
- hashcpy(sha1, entry->u.value.oid.hash);
- *flags |= REF_ISPACKED;
- return 0;
- }
- /* refname is not a packed reference. */
- return -1;
-}
-
static int files_read_raw_ref(struct ref_store *ref_store,
const char *refname, unsigned char *sha1,
struct strbuf *referent, unsigned int *type)
if (lstat(path, &st) < 0) {
if (errno != ENOENT)
goto out;
- if (resolve_packed_ref(refs, refname, sha1, type)) {
+ if (refs_read_raw_ref(refs->packed_ref_store, refname,
+ sha1, referent, type)) {
errno = ENOENT;
goto out;
}
* ref is supposed to be, there could still be a
* packed ref:
*/
- if (resolve_packed_ref(refs, refname, sha1, type)) {
+ if (refs_read_raw_ref(refs->packed_ref_store, refname,
+ sha1, referent, type)) {
errno = EISDIR;
goto out;
}
/*
* If the ref did not exist and we are creating it,
- * make sure there is no existing ref that conflicts
- * with refname:
+ * make sure there is no existing packed ref that
+ * conflicts with refname:
*/
if (refs_verify_refname_available(
- &refs->base, refname,
+ refs->packed_ref_store, refname,
extras, skip, err))
goto error_return;
}
* be expensive and (b) loose references anyway usually do not
* have REF_KNOWS_PEELED.
*/
- if (flag & REF_ISPACKED) {
- struct ref_entry *r = get_packed_ref(refs, refname);
- if (r) {
- if (peel_entry(r, 0))
- return -1;
- hashcpy(sha1, r->u.value.peeled.hash);
- return 0;
- }
- }
+ if (flag & REF_ISPACKED &&
+ !refs_peel_ref(refs->packed_ref_store, refname, sha1))
+ return 0;
return peel_object(base, sha1);
}
struct files_ref_iterator {
struct ref_iterator base;
- struct packed_ref_cache *packed_ref_cache;
struct ref_iterator *iter0;
unsigned int flags;
};
if (iter->iter0)
ok = ref_iterator_abort(iter->iter0);
- release_packed_ref_cache(iter->packed_ref_cache);
base_ref_iterator_free(ref_iterator);
return ok;
}
* (If they've already been read, that's OK; we only need to
* guarantee that they're read before the packed refs, not
* *how much* before.) After that, we call
- * get_packed_ref_cache(), which internally checks whether the
- * packed-ref cache is up to date with what is on disk, and
- * re-reads it if not.
+ * packed_ref_iterator_begin(), which internally checks
+ * whether the packed-ref cache is up to date with what is on
+ * disk, and re-reads it if not.
*/
loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
prefix, 1);
- iter->packed_ref_cache = get_packed_ref_cache(refs);
- acquire_packed_ref_cache(iter->packed_ref_cache);
- packed_iter = cache_ref_iterator_begin(iter->packed_ref_cache->cache,
- prefix, 0);
+ /*
+ * The packed-refs file might contain broken references, for
+ * example an old version of a reference that points at an
+ * object that has since been garbage-collected. This is OK as
+ * long as there is a corresponding loose reference that
+ * overrides it, and we don't want to emit an error message in
+ * this case. So ask the packed_ref_store for all of its
+ * references, and (if needed) do our own check for broken
+ * ones in files_ref_iterator_advance(), after we have merged
+ * the packed and loose references.
+ */
+ packed_iter = refs_ref_iterator_begin(
+ refs->packed_ref_store, prefix, 0,
+ DO_FOR_EACH_INCLUDE_BROKEN);
iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
iter->flags = flags;
* our refname.
*/
if (is_null_oid(&lock->old_oid) &&
- refs_verify_refname_available(&refs->base, refname,
+ refs_verify_refname_available(refs->packed_ref_store, refname,
extras, skip, err)) {
last_errno = ENOTDIR;
goto error_return;
return lock;
}
-/*
- * Write an entry to the packed-refs file for the specified refname.
- * If peeled is non-NULL, write it as the entry's peeled value.
- */
-static void write_packed_entry(FILE *fh, const char *refname,
- const unsigned char *sha1,
- const unsigned char *peeled)
-{
- fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
- if (peeled)
- fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
-}
-
-/*
- * Lock the packed-refs file for writing. Flags is passed to
- * hold_lock_file_for_update(). Return 0 on success. On errors, set
- * errno appropriately and return a nonzero value.
- */
-static int lock_packed_refs(struct files_ref_store *refs, int flags)
-{
- static int timeout_configured = 0;
- static int timeout_value = 1000;
- struct packed_ref_cache *packed_ref_cache;
-
- files_assert_main_repository(refs, "lock_packed_refs");
-
- if (!timeout_configured) {
- git_config_get_int("core.packedrefstimeout", &timeout_value);
- timeout_configured = 1;
- }
-
- if (hold_lock_file_for_update_timeout(
- &refs->packed_refs_lock, files_packed_refs_path(refs),
- flags, timeout_value) < 0)
- return -1;
-
- /*
- * Now that we hold the `packed-refs` lock, make sure that our
- * cache matches the current version of the file. Normally
- * `get_packed_ref_cache()` does that for us, but that
- * function assumes that when the file is locked, any existing
- * cache is still valid. We've just locked the file, but it
- * might have changed the moment *before* we locked it.
- */
- validate_packed_ref_cache(refs);
-
- packed_ref_cache = get_packed_ref_cache(refs);
- /* Increment the reference count to prevent it from being freed: */
- acquire_packed_ref_cache(packed_ref_cache);
- return 0;
-}
-
-/*
- * Write the current version of the packed refs cache from memory to
- * disk. The packed-refs file must already be locked for writing (see
- * lock_packed_refs()). Return zero on success. On errors, set errno
- * and return a nonzero value
- */
-static int commit_packed_refs(struct files_ref_store *refs)
-{
- struct packed_ref_cache *packed_ref_cache =
- get_packed_ref_cache(refs);
- int ok, error = 0;
- int save_errno = 0;
- FILE *out;
- struct ref_iterator *iter;
-
- files_assert_main_repository(refs, "commit_packed_refs");
-
- if (!is_lock_file_locked(&refs->packed_refs_lock))
- die("BUG: packed-refs not locked");
-
- out = fdopen_lock_file(&refs->packed_refs_lock, "w");
- if (!out)
- die_errno("unable to fdopen packed-refs descriptor");
-
- fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
-
- iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
- while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
- struct object_id peeled;
- int peel_error = ref_iterator_peel(iter, &peeled);
-
- write_packed_entry(out, iter->refname, iter->oid->hash,
- peel_error ? NULL : peeled.hash);
- }
-
- if (ok != ITER_DONE)
- die("error while iterating over references");
-
- if (commit_lock_file(&refs->packed_refs_lock)) {
- save_errno = errno;
- error = -1;
- }
- release_packed_ref_cache(packed_ref_cache);
- errno = save_errno;
- return error;
-}
-
-/*
- * Rollback the lockfile for the packed-refs file, and discard the
- * in-memory packed reference cache. (The packed-refs file will be
- * read anew if it is needed again after this function is called.)
- */
-static void rollback_packed_refs(struct files_ref_store *refs)
-{
- struct packed_ref_cache *packed_ref_cache =
- get_packed_ref_cache(refs);
-
- files_assert_main_repository(refs, "rollback_packed_refs");
-
- if (!is_lock_file_locked(&refs->packed_refs_lock))
- die("BUG: packed-refs not locked");
- rollback_lock_file(&refs->packed_refs_lock);
- release_packed_ref_cache(packed_ref_cache);
- clear_packed_ref_cache(refs);
-}
-
struct ref_to_prune {
struct ref_to_prune *next;
unsigned char sha1[20];
files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
"pack_refs");
struct ref_iterator *iter;
- struct ref_dir *packed_refs;
int ok;
struct ref_to_prune *refs_to_prune = NULL;
+ struct strbuf err = STRBUF_INIT;
- lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
- packed_refs = get_packed_refs(refs);
+ packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
* in the packed ref cache. If the reference should be
* pruned, also add it to refs_to_prune.
*/
- struct ref_entry *packed_entry;
-
if (!should_pack_ref(iter->refname, iter->oid, iter->flags,
flags))
continue;
* we don't copy the peeled status, because we want it
* to be re-peeled.
*/
- packed_entry = find_ref_entry(packed_refs, iter->refname);
- if (packed_entry) {
- /* Overwrite existing packed entry with info from loose entry */
- packed_entry->flag = REF_ISPACKED;
- oidcpy(&packed_entry->u.value.oid, iter->oid);
- } else {
- packed_entry = create_ref_entry(iter->refname, iter->oid,
- REF_ISPACKED);
- add_ref_entry(packed_refs, packed_entry);
- }
- oidclr(&packed_entry->u.value.peeled);
+ add_packed_ref(refs->packed_ref_store, iter->refname, iter->oid);
/* Schedule the loose reference for pruning if requested. */
if ((flags & PACK_REFS_PRUNE)) {
if (ok != ITER_DONE)
die("error while iterating over references");
- if (commit_packed_refs(refs))
- die_errno("unable to overwrite old ref-pack file");
+ if (commit_packed_refs(refs->packed_ref_store, &err))
+ die("unable to overwrite old ref-pack file: %s", err.buf);
+ packed_refs_unlock(refs->packed_ref_store);
prune_refs(refs, refs_to_prune);
+ strbuf_release(&err);
return 0;
}
-/*
- * Rewrite the packed-refs file, omitting any refs listed in
- * 'refnames'. On error, leave packed-refs unchanged, write an error
- * message to 'err', and return a nonzero value.
- *
- * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
- */
-static int repack_without_refs(struct files_ref_store *refs,
- struct string_list *refnames, struct strbuf *err)
-{
- struct ref_dir *packed;
- struct string_list_item *refname;
- int ret, needs_repacking = 0, removed = 0;
-
- files_assert_main_repository(refs, "repack_without_refs");
- assert(err);
-
- /* Look for a packed ref */
- for_each_string_list_item(refname, refnames) {
- if (get_packed_ref(refs, refname->string)) {
- needs_repacking = 1;
- break;
- }
- }
-
- /* Avoid locking if we have nothing to do */
- if (!needs_repacking)
- return 0; /* no refname exists in packed refs */
-
- if (lock_packed_refs(refs, 0)) {
- unable_to_lock_message(files_packed_refs_path(refs), errno, err);
- return -1;
- }
- packed = get_packed_refs(refs);
-
- /* Remove refnames from the cache */
- for_each_string_list_item(refname, refnames)
- if (remove_entry_from_dir(packed, refname->string) != -1)
- removed = 1;
- if (!removed) {
- /*
- * All packed entries disappeared while we were
- * acquiring the lock.
- */
- rollback_packed_refs(refs);
- return 0;
- }
-
- /* Write what remains */
- ret = commit_packed_refs(refs);
- if (ret)
- strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
- strerror(errno));
- return ret;
-}
-
static int files_delete_refs(struct ref_store *ref_store, const char *msg,
struct string_list *refnames, unsigned int flags)
{
if (!refnames->nr)
return 0;
- result = repack_without_refs(refs, refnames, &err);
- if (result) {
- /*
- * If we failed to rewrite the packed-refs file, then
- * it is unsafe to try to remove loose refs, because
- * doing so might expose an obsolete packed value for
- * a reference that might even point at an object that
- * has been garbage collected.
- */
- if (refnames->nr == 1)
- error(_("could not delete reference %s: %s"),
- refnames->items[0].string, err.buf);
- else
- error(_("could not delete references: %s"), err.buf);
+ if (packed_refs_lock(refs->packed_ref_store, 0, &err))
+ goto error;
- goto out;
+ if (repack_without_refs(refs->packed_ref_store, refnames, &err)) {
+ packed_refs_unlock(refs->packed_ref_store);
+ goto error;
}
+ packed_refs_unlock(refs->packed_ref_store);
+
for (i = 0; i < refnames->nr; i++) {
const char *refname = refnames->items[i].string;
result |= error(_("could not remove reference %s"), refname);
}
-out:
strbuf_release(&err);
return result;
+
+error:
+ /*
+ * If we failed to rewrite the packed-refs file, then it is
+ * unsafe to try to remove loose refs, because doing so might
+ * expose an obsolete packed value for a reference that might
+ * even point at an object that has been garbage collected.
+ */
+ if (refnames->nr == 1)
+ error(_("could not delete reference %s: %s"),
+ refnames->items[0].string, err.buf);
+ else
+ error(_("could not delete references: %s"), err.buf);
+
+ strbuf_release(&err);
+ return -1;
}
/*
}
}
- if (repack_without_refs(refs, &refs_to_delete, err)) {
+ if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
ret = TRANSACTION_GENERIC_ERROR;
goto cleanup;
}
+ if (repack_without_refs(refs->packed_ref_store, &refs_to_delete, err)) {
+ ret = TRANSACTION_GENERIC_ERROR;
+ packed_refs_unlock(refs->packed_ref_store);
+ goto cleanup;
+ }
+
+ packed_refs_unlock(refs->packed_ref_store);
+
/* Delete the reflogs of any references that were deleted: */
for_each_string_list_item(ref_to_delete, &refs_to_delete) {
strbuf_reset(&sb);
}
}
- if (lock_packed_refs(refs, 0)) {
- strbuf_addf(err, "unable to lock packed-refs file: %s",
- strerror(errno));
+ if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
ret = TRANSACTION_GENERIC_ERROR;
goto cleanup;
}
if ((update->flags & REF_HAVE_NEW) &&
!is_null_oid(&update->new_oid))
- add_packed_ref(refs, update->refname,
+ add_packed_ref(refs->packed_ref_store, update->refname,
&update->new_oid);
}
- if (commit_packed_refs(refs)) {
- strbuf_addf(err, "unable to commit packed-refs file: %s",
- strerror(errno));
+ if (commit_packed_refs(refs->packed_ref_store, err)) {
ret = TRANSACTION_GENERIC_ERROR;
goto cleanup;
}
cleanup:
+ packed_refs_unlock(refs->packed_ref_store);
transaction->state = REF_TRANSACTION_CLOSED;
string_list_clear(&affected_refnames, 0);
return ret;
--- /dev/null
+#include "../cache.h"
+#include "../config.h"
+#include "../refs.h"
+#include "refs-internal.h"
+#include "ref-cache.h"
+#include "packed-backend.h"
+#include "../iterator.h"
+#include "../lockfile.h"
+
+struct packed_ref_cache {
+ struct ref_cache *cache;
+
+ /*
+ * Count of references to the data structure in this instance,
+ * including the pointer from files_ref_store::packed if any.
+ * The data will not be freed as long as the reference count
+ * is nonzero.
+ */
+ unsigned int referrers;
+
+ /* The metadata from when this packed-refs cache was read */
+ struct stat_validity validity;
+};
+
+/*
+ * Increment the reference count of *packed_refs.
+ */
+static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
+{
+ packed_refs->referrers++;
+}
+
+/*
+ * Decrease the reference count of *packed_refs. If it goes to zero,
+ * free *packed_refs and return true; otherwise return false.
+ */
+static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
+{
+ if (!--packed_refs->referrers) {
+ free_ref_cache(packed_refs->cache);
+ stat_validity_clear(&packed_refs->validity);
+ free(packed_refs);
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+/*
+ * A container for `packed-refs`-related data. It is not (yet) a
+ * `ref_store`.
+ */
+struct packed_ref_store {
+ struct ref_store base;
+
+ unsigned int store_flags;
+
+ /* The path of the "packed-refs" file: */
+ char *path;
+
+ /*
+ * A cache of the values read from the `packed-refs` file, if
+ * it might still be current; otherwise, NULL.
+ */
+ struct packed_ref_cache *cache;
+
+ /*
+ * Lock used for the "packed-refs" file. Note that this (and
+ * thus the enclosing `packed_ref_store`) must not be freed.
+ */
+ struct lock_file lock;
+
+ /*
+ * Temporary file used when rewriting new contents to the
+ * "packed-refs" file. Note that this (and thus the enclosing
+ * `packed_ref_store`) must not be freed.
+ */
+ struct tempfile tempfile;
+};
+
+struct ref_store *packed_ref_store_create(const char *path,
+ unsigned int store_flags)
+{
+ struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
+ struct ref_store *ref_store = (struct ref_store *)refs;
+
+ base_ref_store_init(ref_store, &refs_be_packed);
+ refs->store_flags = store_flags;
+
+ refs->path = xstrdup(path);
+ return ref_store;
+}
+
+/*
+ * Die if refs is not the main ref store. caller is used in any
+ * necessary error messages.
+ */
+static void packed_assert_main_repository(struct packed_ref_store *refs,
+ const char *caller)
+{
+ if (refs->store_flags & REF_STORE_MAIN)
+ return;
+
+ die("BUG: operation %s only allowed for main ref store", caller);
+}
+
+/*
+ * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
+ * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
+ * support at least the flags specified in `required_flags`. `caller`
+ * is used in any necessary error messages.
+ */
+static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
+ unsigned int required_flags,
+ const char *caller)
+{
+ struct packed_ref_store *refs;
+
+ if (ref_store->be != &refs_be_packed)
+ die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
+ ref_store->be->name, caller);
+
+ refs = (struct packed_ref_store *)ref_store;
+
+ if ((refs->store_flags & required_flags) != required_flags)
+ die("BUG: unallowed operation (%s), requires %x, has %x\n",
+ caller, required_flags, refs->store_flags);
+
+ return refs;
+}
+
+static void clear_packed_ref_cache(struct packed_ref_store *refs)
+{
+ if (refs->cache) {
+ struct packed_ref_cache *cache = refs->cache;
+
+ refs->cache = NULL;
+ release_packed_ref_cache(cache);
+ }
+}
+
+/* The length of a peeled reference line in packed-refs, including EOL: */
+#define PEELED_LINE_LENGTH 42
+
+/*
+ * Parse one line from a packed-refs file. Write the SHA1 to sha1.
+ * Return a pointer to the refname within the line (null-terminated),
+ * or NULL if there was a problem.
+ */
+static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
+{
+ const char *ref;
+
+ if (parse_oid_hex(line->buf, oid, &ref) < 0)
+ return NULL;
+ if (!isspace(*ref++))
+ return NULL;
+
+ if (isspace(*ref))
+ return NULL;
+
+ if (line->buf[line->len - 1] != '\n')
+ return NULL;
+ line->buf[--line->len] = 0;
+
+ return ref;
+}
+
+/*
+ * Read from `packed_refs_file` into a newly-allocated
+ * `packed_ref_cache` and return it. The return value will already
+ * have its reference count incremented.
+ *
+ * A comment line of the form "# pack-refs with: " may contain zero or
+ * more traits. We interpret the traits as follows:
+ *
+ * No traits:
+ *
+ * Probably no references are peeled. But if the file contains a
+ * peeled value for a reference, we will use it.
+ *
+ * peeled:
+ *
+ * References under "refs/tags/", if they *can* be peeled, *are*
+ * peeled in this file. References outside of "refs/tags/" are
+ * probably not peeled even if they could have been, but if we find
+ * a peeled value for such a reference we will use it.
+ *
+ * fully-peeled:
+ *
+ * All references in the file that can be peeled are peeled.
+ * Inversely (and this is more important), any references in the
+ * file for which no peeled value is recorded is not peelable. This
+ * trait should typically be written alongside "peeled" for
+ * compatibility with older clients, but we do not require it
+ * (i.e., "peeled" is a no-op if "fully-peeled" is set).
+ */
+static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
+{
+ FILE *f;
+ struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
+ struct ref_entry *last = NULL;
+ struct strbuf line = STRBUF_INIT;
+ enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
+ struct ref_dir *dir;
+
+ acquire_packed_ref_cache(packed_refs);
+ packed_refs->cache = create_ref_cache(NULL, NULL);
+ packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
+
+ f = fopen(packed_refs_file, "r");
+ if (!f) {
+ if (errno == ENOENT) {
+ /*
+ * This is OK; it just means that no
+ * "packed-refs" file has been written yet,
+ * which is equivalent to it being empty.
+ */
+ return packed_refs;
+ } else {
+ die_errno("couldn't read %s", packed_refs_file);
+ }
+ }
+
+ stat_validity_update(&packed_refs->validity, fileno(f));
+
+ dir = get_ref_dir(packed_refs->cache->root);
+ while (strbuf_getwholeline(&line, f, '\n') != EOF) {
+ struct object_id oid;
+ const char *refname;
+ const char *traits;
+
+ if (!line.len || line.buf[line.len - 1] != '\n')
+ die("unterminated line in %s: %s", packed_refs_file, line.buf);
+
+ if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
+ if (strstr(traits, " fully-peeled "))
+ peeled = PEELED_FULLY;
+ else if (strstr(traits, " peeled "))
+ peeled = PEELED_TAGS;
+ /* perhaps other traits later as well */
+ continue;
+ }
+
+ refname = parse_ref_line(&line, &oid);
+ if (refname) {
+ int flag = REF_ISPACKED;
+
+ if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
+ if (!refname_is_safe(refname))
+ die("packed refname is dangerous: %s", refname);
+ oidclr(&oid);
+ flag |= REF_BAD_NAME | REF_ISBROKEN;
+ }
+ last = create_ref_entry(refname, &oid, flag);
+ if (peeled == PEELED_FULLY ||
+ (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
+ last->flag |= REF_KNOWS_PEELED;
+ add_ref_entry(dir, last);
+ } else if (last &&
+ line.buf[0] == '^' &&
+ line.len == PEELED_LINE_LENGTH &&
+ line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
+ !get_oid_hex(line.buf + 1, &oid)) {
+ oidcpy(&last->u.value.peeled, &oid);
+ /*
+ * Regardless of what the file header said,
+ * we definitely know the value of *this*
+ * reference:
+ */
+ last->flag |= REF_KNOWS_PEELED;
+ } else {
+ strbuf_setlen(&line, line.len - 1);
+ die("unexpected line in %s: %s", packed_refs_file, line.buf);
+ }
+ }
+
+ fclose(f);
+ strbuf_release(&line);
+
+ return packed_refs;
+}
+
+/*
+ * Check that the packed refs cache (if any) still reflects the
+ * contents of the file. If not, clear the cache.
+ */
+static void validate_packed_ref_cache(struct packed_ref_store *refs)
+{
+ if (refs->cache &&
+ !stat_validity_check(&refs->cache->validity, refs->path))
+ clear_packed_ref_cache(refs);
+}
+
+/*
+ * Get the packed_ref_cache for the specified packed_ref_store,
+ * creating and populating it if it hasn't been read before or if the
+ * file has been changed (according to its `validity` field) since it
+ * was last read. On the other hand, if we hold the lock, then assume
+ * that the file hasn't been changed out from under us, so skip the
+ * extra `stat()` call in `stat_validity_check()`.
+ */
+static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
+{
+ if (!is_lock_file_locked(&refs->lock))
+ validate_packed_ref_cache(refs);
+
+ if (!refs->cache)
+ refs->cache = read_packed_refs(refs->path);
+
+ return refs->cache;
+}
+
+static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
+{
+ return get_ref_dir(packed_ref_cache->cache->root);
+}
+
+static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
+{
+ return get_packed_ref_dir(get_packed_ref_cache(refs));
+}
+
+/*
+ * Add or overwrite a reference in the in-memory packed reference
+ * cache. This may only be called while the packed-refs file is locked
+ * (see packed_refs_lock()). To actually write the packed-refs file,
+ * call commit_packed_refs().
+ */
+void add_packed_ref(struct ref_store *ref_store,
+ const char *refname, const struct object_id *oid)
+{
+ struct packed_ref_store *refs =
+ packed_downcast(ref_store, REF_STORE_WRITE,
+ "add_packed_ref");
+ struct ref_dir *packed_refs;
+ struct ref_entry *packed_entry;
+
+ if (!is_lock_file_locked(&refs->lock))
+ die("BUG: packed refs not locked");
+
+ if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
+ die("Reference has invalid format: '%s'", refname);
+
+ packed_refs = get_packed_refs(refs);
+ packed_entry = find_ref_entry(packed_refs, refname);
+ if (packed_entry) {
+ /* Overwrite the existing entry: */
+ oidcpy(&packed_entry->u.value.oid, oid);
+ packed_entry->flag = REF_ISPACKED;
+ oidclr(&packed_entry->u.value.peeled);
+ } else {
+ packed_entry = create_ref_entry(refname, oid, REF_ISPACKED);
+ add_ref_entry(packed_refs, packed_entry);
+ }
+}
+
+/*
+ * Return the ref_entry for the given refname from the packed
+ * references. If it does not exist, return NULL.
+ */
+static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
+ const char *refname)
+{
+ return find_ref_entry(get_packed_refs(refs), refname);
+}
+
+static int packed_read_raw_ref(struct ref_store *ref_store,
+ const char *refname, unsigned char *sha1,
+ struct strbuf *referent, unsigned int *type)
+{
+ struct packed_ref_store *refs =
+ packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
+
+ struct ref_entry *entry;
+
+ *type = 0;
+
+ entry = get_packed_ref(refs, refname);
+ if (!entry) {
+ errno = ENOENT;
+ return -1;
+ }
+
+ hashcpy(sha1, entry->u.value.oid.hash);
+ *type = REF_ISPACKED;
+ return 0;
+}
+
+static int packed_peel_ref(struct ref_store *ref_store,
+ const char *refname, unsigned char *sha1)
+{
+ struct packed_ref_store *refs =
+ packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
+ "peel_ref");
+ struct ref_entry *r = get_packed_ref(refs, refname);
+
+ if (!r || peel_entry(r, 0))
+ return -1;
+
+ hashcpy(sha1, r->u.value.peeled.hash);
+ return 0;
+}
+
+struct packed_ref_iterator {
+ struct ref_iterator base;
+
+ struct packed_ref_cache *cache;
+ struct ref_iterator *iter0;
+ unsigned int flags;
+};
+
+static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
+{
+ struct packed_ref_iterator *iter =
+ (struct packed_ref_iterator *)ref_iterator;
+ int ok;
+
+ while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
+ if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
+ ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
+ continue;
+
+ if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
+ !ref_resolves_to_object(iter->iter0->refname,
+ iter->iter0->oid,
+ iter->iter0->flags))
+ continue;
+
+ iter->base.refname = iter->iter0->refname;
+ iter->base.oid = iter->iter0->oid;
+ iter->base.flags = iter->iter0->flags;
+ return ITER_OK;
+ }
+
+ iter->iter0 = NULL;
+ if (ref_iterator_abort(ref_iterator) != ITER_DONE)
+ ok = ITER_ERROR;
+
+ return ok;
+}
+
+static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
+ struct object_id *peeled)
+{
+ struct packed_ref_iterator *iter =
+ (struct packed_ref_iterator *)ref_iterator;
+
+ return ref_iterator_peel(iter->iter0, peeled);
+}
+
+static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
+{
+ struct packed_ref_iterator *iter =
+ (struct packed_ref_iterator *)ref_iterator;
+ int ok = ITER_DONE;
+
+ if (iter->iter0)
+ ok = ref_iterator_abort(iter->iter0);
+
+ release_packed_ref_cache(iter->cache);
+ base_ref_iterator_free(ref_iterator);
+ return ok;
+}
+
+static struct ref_iterator_vtable packed_ref_iterator_vtable = {
+ packed_ref_iterator_advance,
+ packed_ref_iterator_peel,
+ packed_ref_iterator_abort
+};
+
+static struct ref_iterator *packed_ref_iterator_begin(
+ struct ref_store *ref_store,
+ const char *prefix, unsigned int flags)
+{
+ struct packed_ref_store *refs;
+ struct packed_ref_iterator *iter;
+ struct ref_iterator *ref_iterator;
+ unsigned int required_flags = REF_STORE_READ;
+
+ if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
+ required_flags |= REF_STORE_ODB;
+ refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
+
+ iter = xcalloc(1, sizeof(*iter));
+ ref_iterator = &iter->base;
+ base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);
+
+ /*
+ * Note that get_packed_ref_cache() internally checks whether
+ * the packed-ref cache is up to date with what is on disk,
+ * and re-reads it if not.
+ */
+
+ iter->cache = get_packed_ref_cache(refs);
+ acquire_packed_ref_cache(iter->cache);
+ iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
+
+ iter->flags = flags;
+
+ return ref_iterator;
+}
+
+/*
+ * Write an entry to the packed-refs file for the specified refname.
+ * If peeled is non-NULL, write it as the entry's peeled value. On
+ * error, return a nonzero value and leave errno set at the value left
+ * by the failing call to `fprintf()`.
+ */
+static int write_packed_entry(FILE *fh, const char *refname,
+ const unsigned char *sha1,
+ const unsigned char *peeled)
+{
+ if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
+ (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
+ return -1;
+
+ return 0;
+}
+
+int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
+{
+ struct packed_ref_store *refs =
+ packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
+ "packed_refs_lock");
+ static int timeout_configured = 0;
+ static int timeout_value = 1000;
+ struct packed_ref_cache *packed_ref_cache;
+
+ if (!timeout_configured) {
+ git_config_get_int("core.packedrefstimeout", &timeout_value);
+ timeout_configured = 1;
+ }
+
+ /*
+ * Note that we close the lockfile immediately because we
+ * don't write new content to it, but rather to a separate
+ * tempfile.
+ */
+ if (hold_lock_file_for_update_timeout(
+ &refs->lock,
+ refs->path,
+ flags, timeout_value) < 0) {
+ unable_to_lock_message(refs->path, errno, err);
+ return -1;
+ }
+
+ if (close_lock_file(&refs->lock)) {
+ strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
+ return -1;
+ }
+
+ /*
+ * Now that we hold the `packed-refs` lock, make sure that our
+ * cache matches the current version of the file. Normally
+ * `get_packed_ref_cache()` does that for us, but that
+ * function assumes that when the file is locked, any existing
+ * cache is still valid. We've just locked the file, but it
+ * might have changed the moment *before* we locked it.
+ */
+ validate_packed_ref_cache(refs);
+
+ packed_ref_cache = get_packed_ref_cache(refs);
+ /* Increment the reference count to prevent it from being freed: */
+ acquire_packed_ref_cache(packed_ref_cache);
+ return 0;
+}
+
+void packed_refs_unlock(struct ref_store *ref_store)
+{
+ struct packed_ref_store *refs = packed_downcast(
+ ref_store,
+ REF_STORE_READ | REF_STORE_WRITE,
+ "packed_refs_unlock");
+
+ if (!is_lock_file_locked(&refs->lock))
+ die("BUG: packed_refs_unlock() called when not locked");
+ rollback_lock_file(&refs->lock);
+ release_packed_ref_cache(refs->cache);
+}
+
+int packed_refs_is_locked(struct ref_store *ref_store)
+{
+ struct packed_ref_store *refs = packed_downcast(
+ ref_store,
+ REF_STORE_READ | REF_STORE_WRITE,
+ "packed_refs_is_locked");
+
+ return is_lock_file_locked(&refs->lock);
+}
+
+/*
+ * The packed-refs header line that we write out. Perhaps other
+ * traits will be added later. The trailing space is required.
+ */
+static const char PACKED_REFS_HEADER[] =
+ "# pack-refs with: peeled fully-peeled \n";
+
+/*
+ * Write the current version of the packed refs cache from memory to
+ * disk. The packed-refs file must already be locked for writing (see
+ * packed_refs_lock()). Return zero on success. On errors, rollback
+ * the lockfile, write an error message to `err`, and return a nonzero
+ * value.
+ */
+int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
+{
+ struct packed_ref_store *refs =
+ packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
+ "commit_packed_refs");
+ struct packed_ref_cache *packed_ref_cache =
+ get_packed_ref_cache(refs);
+ int ok;
+ int ret = -1;
+ struct strbuf sb = STRBUF_INIT;
+ FILE *out;
+ struct ref_iterator *iter;
+ char *packed_refs_path;
+
+ if (!is_lock_file_locked(&refs->lock))
+ die("BUG: commit_packed_refs() called when unlocked");
+
+ /*
+ * If packed-refs is a symlink, we want to overwrite the
+ * symlinked-to file, not the symlink itself. Also, put the
+ * staging file next to it:
+ */
+ packed_refs_path = get_locked_file_path(&refs->lock);
+ strbuf_addf(&sb, "%s.new", packed_refs_path);
+ if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
+ strbuf_addf(err, "unable to create file %s: %s",
+ sb.buf, strerror(errno));
+ strbuf_release(&sb);
+ goto out;
+ }
+ strbuf_release(&sb);
+
+ out = fdopen_tempfile(&refs->tempfile, "w");
+ if (!out) {
+ strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
+ strerror(errno));
+ goto error;
+ }
+
+ if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) {
+ strbuf_addf(err, "error writing to %s: %s",
+ get_tempfile_path(&refs->tempfile), strerror(errno));
+ goto error;
+ }
+
+ iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
+ while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
+ struct object_id peeled;
+ int peel_error = ref_iterator_peel(iter, &peeled);
+
+ if (write_packed_entry(out, iter->refname, iter->oid->hash,
+ peel_error ? NULL : peeled.hash)) {
+ strbuf_addf(err, "error writing to %s: %s",
+ get_tempfile_path(&refs->tempfile),
+ strerror(errno));
+ ref_iterator_abort(iter);
+ goto error;
+ }
+ }
+
+ if (ok != ITER_DONE) {
+ strbuf_addf(err, "unable to rewrite packed-refs file: "
+ "error iterating over old contents");
+ goto error;
+ }
+
+ if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
+ strbuf_addf(err, "error replacing %s: %s",
+ refs->path, strerror(errno));
+ goto out;
+ }
+
+ ret = 0;
+ goto out;
+
+error:
+ delete_tempfile(&refs->tempfile);
+
+out:
+ free(packed_refs_path);
+ return ret;
+}
+
+/*
+ * Rewrite the packed-refs file, omitting any refs listed in
+ * 'refnames'. On error, leave packed-refs unchanged, write an error
+ * message to 'err', and return a nonzero value. The packed refs lock
+ * must be held when calling this function; it will still be held when
+ * the function returns.
+ *
+ * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
+ */
+int repack_without_refs(struct ref_store *ref_store,
+ struct string_list *refnames, struct strbuf *err)
+{
+ struct packed_ref_store *refs =
+ packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
+ "repack_without_refs");
+ struct ref_dir *packed;
+ struct string_list_item *refname;
+ int needs_repacking = 0, removed = 0;
+
+ packed_assert_main_repository(refs, "repack_without_refs");
+ assert(err);
+
+ if (!is_lock_file_locked(&refs->lock))
+ die("BUG: repack_without_refs called without holding lock");
+
+ /* Look for a packed ref */
+ for_each_string_list_item(refname, refnames) {
+ if (get_packed_ref(refs, refname->string)) {
+ needs_repacking = 1;
+ break;
+ }
+ }
+
+ /* Avoid locking if we have nothing to do */
+ if (!needs_repacking)
+ return 0; /* no refname exists in packed refs */
+
+ packed = get_packed_refs(refs);
+
+ /* Remove refnames from the cache */
+ for_each_string_list_item(refname, refnames)
+ if (remove_entry_from_dir(packed, refname->string) != -1)
+ removed = 1;
+ if (!removed) {
+ /*
+ * All packed entries disappeared while we were
+ * acquiring the lock.
+ */
+ clear_packed_ref_cache(refs);
+ return 0;
+ }
+
+ /* Write what remains */
+ return commit_packed_refs(&refs->base, err);
+}
+
+static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
+{
+ /* Nothing to do. */
+ return 0;
+}
+
+static int packed_transaction_prepare(struct ref_store *ref_store,
+ struct ref_transaction *transaction,
+ struct strbuf *err)
+{
+ die("BUG: not implemented yet");
+}
+
+static int packed_transaction_abort(struct ref_store *ref_store,
+ struct ref_transaction *transaction,
+ struct strbuf *err)
+{
+ die("BUG: not implemented yet");
+}
+
+static int packed_transaction_finish(struct ref_store *ref_store,
+ struct ref_transaction *transaction,
+ struct strbuf *err)
+{
+ die("BUG: not implemented yet");
+}
+
+static int packed_initial_transaction_commit(struct ref_store *ref_store,
+ struct ref_transaction *transaction,
+ struct strbuf *err)
+{
+ return ref_transaction_commit(transaction, err);
+}
+
+static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
+ struct string_list *refnames, unsigned int flags)
+{
+ die("BUG: not implemented yet");
+}
+
+static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
+{
+ /*
+ * Packed refs are already packed. It might be that loose refs
+ * are packed *into* a packed refs store, but that is done by
+ * updating the packed references via a transaction.
+ */
+ return 0;
+}
+
+static int packed_create_symref(struct ref_store *ref_store,
+ const char *refname, const char *target,
+ const char *logmsg)
+{
+ die("BUG: packed reference store does not support symrefs");
+}
+
+static int packed_rename_ref(struct ref_store *ref_store,
+ const char *oldrefname, const char *newrefname,
+ const char *logmsg)
+{
+ die("BUG: packed reference store does not support renaming references");
+}
+
+static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
+{
+ return empty_ref_iterator_begin();
+}
+
+static int packed_for_each_reflog_ent(struct ref_store *ref_store,
+ const char *refname,
+ each_reflog_ent_fn fn, void *cb_data)
+{
+ return 0;
+}
+
+static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
+ const char *refname,
+ each_reflog_ent_fn fn,
+ void *cb_data)
+{
+ return 0;
+}
+
+static int packed_reflog_exists(struct ref_store *ref_store,
+ const char *refname)
+{
+ return 0;
+}
+
+static int packed_create_reflog(struct ref_store *ref_store,
+ const char *refname, int force_create,
+ struct strbuf *err)
+{
+ die("BUG: packed reference store does not support reflogs");
+}
+
+static int packed_delete_reflog(struct ref_store *ref_store,
+ const char *refname)
+{
+ return 0;
+}
+
+static int packed_reflog_expire(struct ref_store *ref_store,
+ const char *refname, const unsigned char *sha1,
+ unsigned int flags,
+ reflog_expiry_prepare_fn prepare_fn,
+ reflog_expiry_should_prune_fn should_prune_fn,
+ reflog_expiry_cleanup_fn cleanup_fn,
+ void *policy_cb_data)
+{
+ return 0;
+}
+
+struct ref_storage_be refs_be_packed = {
+ NULL,
+ "packed",
+ packed_ref_store_create,
+ packed_init_db,
+ packed_transaction_prepare,
+ packed_transaction_finish,
+ packed_transaction_abort,
+ packed_initial_transaction_commit,
+
+ packed_pack_refs,
+ packed_peel_ref,
+ packed_create_symref,
+ packed_delete_refs,
+ packed_rename_ref,
+
+ packed_ref_iterator_begin,
+ packed_read_raw_ref,
+
+ packed_reflog_iterator_begin,
+ packed_for_each_reflog_ent,
+ packed_for_each_reflog_ent_reverse,
+ packed_reflog_exists,
+ packed_create_reflog,
+ packed_delete_reflog,
+ packed_reflog_expire
+};
--- /dev/null
+#ifndef REFS_PACKED_BACKEND_H
+#define REFS_PACKED_BACKEND_H
+
+struct ref_store *packed_ref_store_create(const char *path,
+ unsigned int store_flags);
+
+/*
+ * Lock the packed-refs file for writing. Flags is passed to
+ * hold_lock_file_for_update(). Return 0 on success. On errors, write
+ * an error message to `err` and return a nonzero value.
+ */
+int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err);
+
+void packed_refs_unlock(struct ref_store *ref_store);
+int packed_refs_is_locked(struct ref_store *ref_store);
+
+void add_packed_ref(struct ref_store *ref_store,
+ const char *refname, const struct object_id *oid);
+
+int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err);
+
+int repack_without_refs(struct ref_store *ref_store,
+ struct string_list *refnames, struct strbuf *err);
+
+#endif /* REFS_PACKED_BACKEND_H */
*/
int refname_is_safe(const char *refname);
+/*
+ * Helper function: return true if refname, which has the specified
+ * oid and flags, can be resolved to an object in the database. If the
+ * referred-to object does not exist, emit a warning and return false.
+ */
+int ref_resolves_to_object(const char *refname,
+ const struct object_id *oid,
+ unsigned int flags);
+
enum peel_status {
/* object was peeled successfully: */
PEEL_PEELED = 0,
};
extern struct ref_storage_be refs_be_files;
+extern struct ref_storage_be refs_be_packed;
/*
* A representation of the reference store for the main repository or
};
static int remotes_hash_cmp(const void *unused_cmp_data,
- const struct remote *a,
- const struct remote *b,
- const struct remotes_hash_key *key)
+ const void *entry,
+ const void *entry_or_key,
+ const void *keydata)
{
+ const struct remote *a = entry;
+ const struct remote *b = entry_or_key;
+ const struct remotes_hash_key *key = keydata;
+
if (key)
return strncmp(a->name, key->str, key->len) || a->name[key->len];
else
static inline void init_remotes_hash(void)
{
if (!remotes_hash.cmpfn)
- hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, NULL, 0);
+ hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
}
static struct remote *make_remote(const char *name, int len)
return 0;
}
- if (get_sha1(name, oid.hash))
+ if (get_oid(name, &oid))
return -1;
if (match) {
if (!*colon)
entry->use_tracking = 1;
else if (!colon[1])
- hashclr(entry->expect);
- else if (get_sha1(colon + 1, entry->expect))
+ oidclr(&entry->expect);
+ else if (get_oid(colon + 1, &entry->expect))
return error("cannot parse expected object name '%s'", colon + 1);
return 0;
}
continue;
ref->expect_old_sha1 = 1;
if (!entry->use_tracking)
- hashcpy(ref->old_oid_expect.hash, cas->entry[i].expect);
+ oidcpy(&ref->old_oid_expect, &entry->expect);
else if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
oidclr(&ref->old_oid_expect);
return;
struct push_cas_option {
unsigned use_tracking_for_rest:1;
struct push_cas {
- unsigned char expect[20];
+ struct object_id expect;
unsigned use_tracking:1;
char *refname;
} *entry;
#include "submodule-config.h"
/* The main repository */
-static struct repository the_repo;
+static struct repository the_repo = {
+ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &the_index, 0, 0
+};
struct repository *the_repository = &the_repo;
static char *git_path_from_env(const char *envvar, const char *git_dir,
struct strbuf worktree = STRBUF_INIT;
int ret = 0;
- sub = submodule_from_cache(superproject, null_sha1, path);
+ sub = submodule_from_cache(superproject, &null_oid, path);
if (!sub) {
ret = -1;
goto out;
{
if (!repo->index)
repo->index = xcalloc(1, sizeof(*repo->index));
- else
- discard_index(repo->index);
return read_index_from(repo->index, repo->index_file);
}
const char *path);
extern void repo_clear(struct repository *repo);
+/*
+ * Populates the repository's index from its index_file, an index struct will
+ * be allocated if needed.
+ *
+ * Return the number of index entries in the populated index or a value less
+ * than zero if an error occured. If the repository's index has already been
+ * populated then the number of entries will simply be returned.
+ */
extern int repo_read_index(struct repository *repo);
#endif /* REPOSITORY_H */
if (revs->reflog_info && obj->type == OBJ_COMMIT) {
struct strbuf buf = STRBUF_INIT;
int len = interpret_branch_name(name, 0, &buf, 0);
- int st;
if (0 < len && name[len] && buf.len)
strbuf_addstr(&buf, name + len);
- st = add_reflog_for_walk(revs->reflog_info,
- (struct commit *)obj,
- buf.buf[0] ? buf.buf: name);
+ add_reflog_for_walk(revs->reflog_info,
+ (struct commit *)obj,
+ buf.buf[0] ? buf.buf: name);
strbuf_release(&buf);
- if (st)
- return;
+ return; /* do not add the commit itself */
}
add_object_array_with_path(obj, name, &revs->pending, mode, path);
}
{
cb->all_revs = revs;
cb->all_flags = flags;
+ revs->rev_input_given = 1;
}
void clear_ref_exclusion(struct string_list **ref_excludes_p)
flags ^= UNINTERESTING | BOTTOM;
arg++;
}
- if (get_sha1_committish(arg, oid.hash))
+ if (get_oid_committish(arg, &oid))
return 0;
while (1) {
it = get_reference(revs, arg, &oid, 0);
unsigned int a_flags, b_flags;
int symmetric = 0;
unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
- unsigned int oc_flags = GET_SHA1_COMMITTISH | GET_SHA1_RECORD_PATH;
+ unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
a_name = arg;
if (!*a_name)
if (!*b_name)
b_name = "HEAD";
- if (get_sha1_with_context(a_name, oc_flags, a_oid.hash, a_oc) ||
- get_sha1_with_context(b_name, oc_flags, b_oid.hash, b_oc))
+ if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
+ get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
return -1;
if (!cant_be_filename) {
int local_flags;
const char *arg = arg_;
int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
- unsigned get_sha1_flags = GET_SHA1_RECORD_PATH;
+ unsigned get_sha1_flags = GET_OID_RECORD_PATH;
flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
}
if (revarg_opt & REVARG_COMMITTISH)
- get_sha1_flags |= GET_SHA1_COMMITTISH;
+ get_sha1_flags |= GET_OID_COMMITTISH;
- if (get_sha1_with_context(arg, get_sha1_flags, oid.hash, &oc))
+ if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
return revs->ignore_missing ? 0 : -1;
if (!cant_be_filename)
verify_non_filename(revs->prefix, arg);
opt->tweak(revs, opt);
if (revs->show_merge)
prepare_show_merge(revs);
- if (revs->def && !revs->pending.nr && !got_rev_arg) {
+ if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
struct object_id oid;
struct object *object;
struct object_context oc;
- if (get_sha1_with_context(revs->def, 0, oid.hash, &oc))
+ if (get_oid_with_context(revs->def, 0, &oid, &oc))
diagnose_missing_default(revs->def);
object = get_reference(revs, revs->def, &oid, 0);
add_pending_object_with_mode(revs, object, revs->def, oc.mode);
if (revs->reverse && revs->reflog_info)
die("cannot combine --reverse with --walk-reflogs");
+ if (revs->reflog_info && revs->limited)
+ die("cannot combine --walk-reflogs with history-limiting options");
if (revs->rewrite_parents && revs->children.name)
die("cannot combine --parents and --children");
return (revs->rewrite_parents || revs->children.name);
}
+/*
+ * Return a timestamp to be used for --since/--until comparisons for this
+ * commit, based on the revision options.
+ */
+static timestamp_t comparison_date(const struct rev_info *revs,
+ struct commit *commit)
+{
+ return revs->reflog_info ?
+ get_reflog_timestamp(revs->reflog_info) :
+ commit->date;
+}
+
enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
{
if (commit->object.flags & SHOWN)
return commit_show;
if (commit->object.flags & UNINTERESTING)
return commit_ignore;
- if (revs->min_age != -1 && (commit->date > revs->min_age))
- return commit_ignore;
+ if (revs->min_age != -1 &&
+ comparison_date(revs, commit) > revs->min_age)
+ return commit_ignore;
if (revs->min_parents || (revs->max_parents >= 0)) {
int n = commit_list_count(commit->parents);
if ((n < revs->min_parents) ||
static struct commit *get_revision_1(struct rev_info *revs)
{
- if (!revs->commits)
- return NULL;
+ while (1) {
+ struct commit *commit;
- do {
- struct commit *commit = pop_commit(&revs->commits);
+ if (revs->reflog_info)
+ commit = next_reflog_entry(revs->reflog_info);
+ else
+ commit = pop_commit(&revs->commits);
- if (revs->reflog_info) {
- save_parents(revs, commit);
- fake_reflog_parent(revs->reflog_info, commit);
+ if (!commit)
+ return NULL;
+
+ if (revs->reflog_info)
commit->object.flags &= ~(ADDED | SEEN | SHOWN);
- }
/*
* If we haven't done the list limiting, we need to look at
*/
if (!revs->limited) {
if (revs->max_age != -1 &&
- (commit->date < revs->max_age))
+ comparison_date(revs, commit) < revs->max_age)
continue;
- if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
+
+ if (revs->reflog_info)
+ try_to_simplify_commit(revs, commit);
+ else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
if (!revs->ignore_missing_links)
die("Failed to traverse parents of commit %s",
oid_to_hex(&commit->object.oid));
track_linear(revs, commit);
return commit;
}
- } while (revs->commits);
- return NULL;
+ }
}
/*
const char *def;
struct pathspec prune_data;
+ /*
+ * Whether the arguments parsed by setup_revisions() included any
+ * "input" revisions that might still have yielded an empty pending
+ * list (e.g., patterns like "--all" or "--glob").
+ */
+ int rev_input_given;
+
/* topo-sort */
enum rev_sort_order sort_order;
if (start_command(&pp->children[i].process)) {
code = pp->start_failure(&pp->children[i].err,
pp->data,
- &pp->children[i].data);
+ pp->children[i].data);
strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
strbuf_reset(&pp->children[i].err);
if (code)
code = pp->task_finished(code,
&pp->children[i].err, pp->data,
- &pp->children[i].data);
+ pp->children[i].data);
if (code)
result = code;
static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash")
static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
+static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
static inline int is_rebase_i(const struct replay_opts *opts)
{
static int is_original_commit_empty(struct commit *commit)
{
- const unsigned char *ptree_sha1;
+ const struct object_id *ptree_oid;
if (parse_commit(commit))
return error(_("could not parse commit %s\n"),
if (parse_commit(parent))
return error(_("could not parse parent commit %s\n"),
oid_to_hex(&parent->object.oid));
- ptree_sha1 = parent->tree->object.oid.hash;
+ ptree_oid = &parent->tree->object.oid;
} else {
- ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */
+ ptree_oid = &empty_tree_oid; /* commit is root */
}
- return !hashcmp(ptree_sha1, commit->tree->object.oid.hash);
+ return !oidcmp(ptree_oid, &commit->tree->object.oid);
}
/*
static void flush_rewritten_pending(void) {
struct strbuf buf = STRBUF_INIT;
- unsigned char newsha1[20];
+ struct object_id newoid;
FILE *out;
- if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), 82) > 0 &&
- !get_sha1("HEAD", newsha1) &&
+ if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 &&
+ !get_oid("HEAD", &newoid) &&
(out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
char *bol = buf.buf, *eol;
while (*bol) {
eol = strchrnul(bol, '\n');
fprintf(out, "%.*s %s\n", (int)(eol - bol),
- bol, sha1_to_hex(newsha1));
+ bol, oid_to_hex(&newoid));
if (!*eol)
break;
bol = eol + 1;
else if (!strcmp(key, "options.strategy-option")) {
ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
opts->xopts[opts->xopts_nr++] = xstrdup(value);
- } else
+ } else if (!strcmp(key, "options.allow-rerere-auto"))
+ opts->allow_rerere_auto =
+ git_config_bool_or_int(key, value, &error_flag) ?
+ RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
+ else
return error(_("invalid key: %s"), key);
if (!error_flag)
free(opts->gpg_sign);
opts->gpg_sign = xstrdup(buf.buf + 2);
}
+ strbuf_reset(&buf);
+ }
+
+ if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), 1)) {
+ if (!strcmp(buf.buf, "--rerere-autoupdate"))
+ opts->allow_rerere_auto = RERERE_AUTOUPDATE;
+ else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
+ opts->allow_rerere_auto = RERERE_NOAUTOUPDATE;
+ strbuf_reset(&buf);
}
if (file_exists(rebase_path_verbose()))
return !oidcmp(&actual_head, &expected_head);
}
-static int reset_for_rollback(const unsigned char *sha1)
+static int reset_for_rollback(const struct object_id *oid)
{
const char *argv[4]; /* reset --merge <arg> + NULL */
argv[0] = "reset";
argv[1] = "--merge";
- argv[2] = sha1_to_hex(sha1);
+ argv[2] = oid_to_hex(oid);
argv[3] = NULL;
return run_command_v_opt(argv, RUN_GIT_CMD);
}
static int rollback_single_pick(void)
{
- unsigned char head_sha1[20];
+ struct object_id head_oid;
if (!file_exists(git_path_cherry_pick_head()) &&
!file_exists(git_path_revert_head()))
return error(_("no cherry-pick or revert in progress"));
- if (read_ref_full("HEAD", 0, head_sha1, NULL))
+ if (read_ref_full("HEAD", 0, head_oid.hash, NULL))
return error(_("cannot resolve HEAD"));
- if (is_null_sha1(head_sha1))
+ if (is_null_oid(&head_oid))
return error(_("cannot abort from a branch yet to be born"));
- return reset_for_rollback(head_sha1);
+ return reset_for_rollback(&head_oid);
}
int sequencer_rollback(struct replay_opts *opts)
{
FILE *f;
- unsigned char sha1[20];
+ struct object_id oid;
struct strbuf buf = STRBUF_INIT;
+ const char *p;
f = fopen(git_path_head_file(), "r");
if (!f && errno == ENOENT) {
goto fail;
}
fclose(f);
- if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') {
+ if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') {
error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
git_path_head_file());
goto fail;
}
- if (is_null_sha1(sha1)) {
+ if (is_null_oid(&oid)) {
error(_("cannot abort from a branch yet to be born"));
goto fail;
}
warning(_("You seem to have moved HEAD. "
"Not rewinding, check your HEAD!"));
} else
- if (reset_for_rollback(sha1))
+ if (reset_for_rollback(&oid))
goto fail;
strbuf_release(&buf);
return sequencer_remove_state(opts);
"options.strategy-option",
opts->xopts[i], "^$", 0);
}
+ if (opts->allow_rerere_auto)
+ res |= git_config_set_in_file_gently(opts_file, "options.allow-rerere-auto",
+ opts->allow_rerere_auto == RERERE_AUTOUPDATE ?
+ "true" : "false");
return res;
}
static int intend_to_amend(void)
{
- unsigned char head[20];
+ struct object_id head;
char *p;
- if (get_sha1("HEAD", head))
+ if (get_oid("HEAD", &head))
return error(_("cannot read HEAD"));
- p = sha1_to_hex(head);
+ p = oid_to_hex(&head);
return write_message(p, strlen(p), rebase_path_amend(), 1);
}
if (read_oneliner(&head_ref, rebase_path_head_name(), 0) &&
starts_with(head_ref.buf, "refs/")) {
const char *msg;
- unsigned char head[20], orig[20];
+ struct object_id head, orig;
int res;
- if (get_sha1("HEAD", head)) {
+ if (get_oid("HEAD", &head)) {
res = error(_("cannot read HEAD"));
cleanup_head_ref:
strbuf_release(&head_ref);
return res;
}
if (!read_oneliner(&buf, rebase_path_orig_head(), 0) ||
- get_sha1_hex(buf.buf, orig)) {
+ get_oid_hex(buf.buf, &orig)) {
res = error(_("could not read orig-head"));
goto cleanup_head_ref;
}
}
msg = reflog_message(opts, "finish", "%s onto %s",
head_ref.buf, buf.buf);
- if (update_ref(msg, head_ref.buf, head, orig,
+ if (update_ref(msg, head_ref.buf, head.hash, orig.hash,
REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) {
res = error(_("could not update %s"),
head_ref.buf);
log_tree_opt.disable_stdin = 1;
if (read_oneliner(&buf, rebase_path_orig_head(), 0) &&
- !get_sha1(buf.buf, orig.hash) &&
- !get_sha1("HEAD", head.hash)) {
+ !get_oid(buf.buf, &orig) &&
+ !get_oid("HEAD", &head)) {
diff_tree_oid(&orig, &head, "",
&log_tree_opt.diffopt);
log_tree_diff_flush(&log_tree_opt);
if (file_exists(rebase_path_amend())) {
struct strbuf rev = STRBUF_INIT;
- unsigned char head[20], to_amend[20];
+ struct object_id head, to_amend;
- if (get_sha1("HEAD", head))
+ if (get_oid("HEAD", &head))
return error(_("cannot amend non-existing commit"));
if (!read_oneliner(&rev, rebase_path_amend(), 0))
return error(_("invalid file: '%s'"), rebase_path_amend());
- if (get_sha1_hex(rev.buf, to_amend))
+ if (get_oid_hex(rev.buf, &to_amend))
return error(_("invalid contents: '%s'"),
rebase_path_amend());
- if (hashcmp(head, to_amend))
+ if (oidcmp(&head, &to_amend))
return error(_("\nYou have uncommitted changes in your "
"working tree. Please, commit them\n"
"first and then run 'git rebase "
struct object_id oid;
if (read_oneliner(&buf, rebase_path_stopped_sha(), 1) &&
- !get_sha1_committish(buf.buf, oid.hash))
+ !get_oid_committish(buf.buf, &oid))
record_in_rewritten(&oid, peek_command(&todo_list, 0));
strbuf_release(&buf);
}
{
static struct strbuf cwd = STRBUF_INIT;
struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
- const char *prefix, *env_prefix;
+ const char *prefix;
/*
* We may have read an incomplete configuration before
die("BUG: unhandled setup_git_directory_1() result");
}
- /*
- * NEEDSWORK: This was a hack in order to get ls-files and grep to have
- * properly formated output when recursing submodules. Once ls-files
- * and grep have been changed to perform this recursing in-process this
- * needs to be removed.
- */
- env_prefix = getenv(GIT_TOPLEVEL_PREFIX_ENVIRONMENT);
- if (env_prefix)
- prefix = env_prefix;
-
if (prefix)
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
else
} while (lo < hi);
return -lo-1;
}
-
-/*
- * Conventional binary search loop looks like this:
- *
- * unsigned lo, hi;
- * do {
- * unsigned mi = (lo + hi) / 2;
- * int cmp = "entry pointed at by mi" minus "target";
- * if (!cmp)
- * return (mi is the wanted one)
- * if (cmp > 0)
- * hi = mi; "mi is larger than target"
- * else
- * lo = mi+1; "mi is smaller than target"
- * } while (lo < hi);
- *
- * The invariants are:
- *
- * - When entering the loop, lo points at a slot that is never
- * above the target (it could be at the target), hi points at a
- * slot that is guaranteed to be above the target (it can never
- * be at the target).
- *
- * - We find a point 'mi' between lo and hi (mi could be the same
- * as lo, but never can be as same as hi), and check if it hits
- * the target. There are three cases:
- *
- * - if it is a hit, we are happy.
- *
- * - if it is strictly higher than the target, we set it to hi,
- * and repeat the search.
- *
- * - if it is strictly lower than the target, we update lo to
- * one slot after it, because we allow lo to be at the target.
- *
- * If the loop exits, there is no matching entry.
- *
- * When choosing 'mi', we do not have to take the "middle" but
- * anywhere in between lo and hi, as long as lo <= mi < hi is
- * satisfied. When we somehow know that the distance between the
- * target and lo is much shorter than the target and hi, we could
- * pick mi that is much closer to lo than the midway.
- *
- * Now, we can take advantage of the fact that SHA-1 is a good hash
- * function, and as long as there are enough entries in the table, we
- * can expect uniform distribution. An entry that begins with for
- * example "deadbeef..." is much likely to appear much later than in
- * the midway of the table. It can reasonably be expected to be near
- * 87% (222/256) from the top of the table.
- *
- * However, we do not want to pick "mi" too precisely. If the entry at
- * the 87% in the above example turns out to be higher than the target
- * we are looking for, we would end up narrowing the search space down
- * only by 13%, instead of 50% we would get if we did a simple binary
- * search. So we would want to hedge our bets by being less aggressive.
- *
- * The table at "table" holds at least "nr" entries of "elem_size"
- * bytes each. Each entry has the SHA-1 key at "key_offset". The
- * table is sorted by the SHA-1 key of the entries. The caller wants
- * to find the entry with "key", and knows that the entry at "lo" is
- * not higher than the entry it is looking for, and that the entry at
- * "hi" is higher than the entry it is looking for.
- */
-int sha1_entry_pos(const void *table,
- size_t elem_size,
- size_t key_offset,
- unsigned lo, unsigned hi, unsigned nr,
- const unsigned char *key)
-{
- const unsigned char *base = table;
- const unsigned char *hi_key, *lo_key;
- unsigned ofs_0;
- static int debug_lookup = -1;
-
- if (debug_lookup < 0)
- debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
-
- if (!nr || lo >= hi)
- return -1;
-
- if (nr == hi)
- hi_key = NULL;
- else
- hi_key = base + elem_size * hi + key_offset;
- lo_key = base + elem_size * lo + key_offset;
-
- ofs_0 = 0;
- do {
- int cmp;
- unsigned ofs, mi, range;
- unsigned lov, hiv, kyv;
- const unsigned char *mi_key;
-
- range = hi - lo;
- if (hi_key) {
- for (ofs = ofs_0; ofs < 20; ofs++)
- if (lo_key[ofs] != hi_key[ofs])
- break;
- ofs_0 = ofs;
- /*
- * byte 0 thru (ofs-1) are the same between
- * lo and hi; ofs is the first byte that is
- * different.
- *
- * If ofs==20, then no bytes are different,
- * meaning we have entries with duplicate
- * keys. We know that we are in a solid run
- * of this entry (because the entries are
- * sorted, and our lo and hi are the same,
- * there can be nothing but this single key
- * in between). So we can stop the search.
- * Either one of these entries is it (and
- * we do not care which), or we do not have
- * it.
- *
- * Furthermore, we know that one of our
- * endpoints must be the edge of the run of
- * duplicates. For example, given this
- * sequence:
- *
- * idx 0 1 2 3 4 5
- * key A C C C C D
- *
- * If we are searching for "B", we might
- * hit the duplicate run at lo=1, hi=3
- * (e.g., by first mi=3, then mi=0). But we
- * can never have lo > 1, because B < C.
- * That is, if our key is less than the
- * run, we know that "lo" is the edge, but
- * we can say nothing of "hi". Similarly,
- * if our key is greater than the run, we
- * know that "hi" is the edge, but we can
- * say nothing of "lo".
- *
- * Therefore if we do not find it, we also
- * know where it would go if it did exist:
- * just on the far side of the edge that we
- * know about.
- */
- if (ofs == 20) {
- mi = lo;
- mi_key = base + elem_size * mi + key_offset;
- cmp = memcmp(mi_key, key, 20);
- if (!cmp)
- return mi;
- if (cmp < 0)
- return -1 - hi;
- else
- return -1 - lo;
- }
-
- hiv = hi_key[ofs_0];
- if (ofs_0 < 19)
- hiv = (hiv << 8) | hi_key[ofs_0+1];
- } else {
- hiv = 256;
- if (ofs_0 < 19)
- hiv <<= 8;
- }
- lov = lo_key[ofs_0];
- kyv = key[ofs_0];
- if (ofs_0 < 19) {
- lov = (lov << 8) | lo_key[ofs_0+1];
- kyv = (kyv << 8) | key[ofs_0+1];
- }
- assert(lov < hiv);
-
- if (kyv < lov)
- return -1 - lo;
- if (hiv < kyv)
- return -1 - hi;
-
- /*
- * Even if we know the target is much closer to 'hi'
- * than 'lo', if we pick too precisely and overshoot
- * (e.g. when we know 'mi' is closer to 'hi' than to
- * 'lo', pick 'mi' that is higher than the target), we
- * end up narrowing the search space by a smaller
- * amount (i.e. the distance between 'mi' and 'hi')
- * than what we would have (i.e. about half of 'lo'
- * and 'hi'). Hedge our bets to pick 'mi' less
- * aggressively, i.e. make 'mi' a bit closer to the
- * middle than we would otherwise pick.
- */
- kyv = (kyv * 6 + lov + hiv) / 8;
- if (lov < hiv - 1) {
- if (kyv == lov)
- kyv++;
- else if (kyv == hiv)
- kyv--;
- }
- mi = (range - 1) * (kyv - lov) / (hiv - lov) + lo;
-
- if (debug_lookup) {
- printf("lo %u hi %u rg %u mi %u ", lo, hi, range, mi);
- printf("ofs %u lov %x, hiv %x, kyv %x\n",
- ofs_0, lov, hiv, kyv);
- }
- if (!(lo <= mi && mi < hi))
- die("assertion failure lo %u mi %u hi %u %s",
- lo, mi, hi, sha1_to_hex(key));
-
- mi_key = base + elem_size * mi + key_offset;
- cmp = memcmp(mi_key + ofs_0, key + ofs_0, 20 - ofs_0);
- if (!cmp)
- return mi;
- if (cmp > 0) {
- hi = mi;
- hi_key = mi_key;
- } else {
- lo = mi + 1;
- lo_key = mi_key + elem_size;
- }
- } while (lo < hi);
- return -lo-1;
-}
hashclr(oi->delta_base_sha1);
}
+ oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED :
+ OI_PACKED;
+
out:
unuse_pack(&w_curs);
return type;
error("bad packed object CRC for %s",
sha1_to_hex(sha1));
mark_bad_packed_object(p, sha1);
- unuse_pack(&w_curs);
- return NULL;
+ data = NULL;
+ goto out;
}
}
if (final_size)
*final_size = size;
+out:
unuse_pack(&w_curs);
if (delta_stack != small_delta_stack)
const uint32_t *level1_ofs = p->index_data;
const unsigned char *index = p->index_data;
unsigned hi, lo, stride;
- static int use_lookup = -1;
static int debug_lookup = -1;
if (debug_lookup < 0)
printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
- if (use_lookup < 0)
- use_lookup = !!getenv("GIT_USE_LOOKUP");
- if (use_lookup) {
- int pos = sha1_entry_pos(index, stride, 0,
- lo, hi, p->num_objects, sha1);
- if (pos < 0)
- return 0;
- return nth_packed_object_offset(p, pos);
- }
-
- do {
+ while (lo < hi) {
unsigned mi = (lo + hi) / 2;
int cmp = hashcmp(index + mi * stride, sha1);
hi = mi;
else
lo = mi+1;
- } while (lo < hi);
+ }
return 0;
}
if (oi->sizep == &size_scratch)
oi->sizep = NULL;
strbuf_release(&hdrbuf);
+ oi->whence = OI_LOOSE;
return (status < 0) ? status : 0;
}
if (!find_pack_entry(real, &e)) {
/* Most likely it's a loose object. */
- if (!sha1_loose_object_info(real, oi, flags)) {
- oi->whence = OI_LOOSE;
+ if (!sha1_loose_object_info(real, oi, flags))
return 0;
- }
/* Not a loose object; someone else may have just packed it. */
if (flags & OBJECT_INFO_QUICK) {
if (rtype < 0) {
mark_bad_packed_object(e.p, real);
return sha1_object_info_extended(real, oi, 0);
- } else if (in_delta_base_cache(e.p, e.offset)) {
- oi->whence = OI_DBCACHED;
- } else {
- oi->whence = OI_PACKED;
+ } else if (oi->whence == OI_PACKED) {
oi->u.packed.offset = e.offset;
oi->u.packed.pack = e.p;
oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
return type;
}
-static void *read_packed_sha1(const unsigned char *sha1,
- enum object_type *type, unsigned long *size)
-{
- struct pack_entry e;
- void *data;
-
- if (!find_pack_entry(sha1, &e))
- return NULL;
- data = cache_or_unpack_entry(e.p, e.offset, size, type);
- if (!data) {
- /*
- * We're probably in deep shit, but let's try to fetch
- * the required object anyway from another pack or loose.
- * This should happen only in the presence of a corrupted
- * pack, and is better than failing outright.
- */
- error("failed to read object %s at offset %"PRIuMAX" from %s",
- sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name);
- mark_bad_packed_object(e.p, sha1);
- data = read_object(sha1, type, size);
- }
- return data;
-}
-
int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
unsigned char *sha1)
{
if (has_loose_object(sha1))
return 0;
- buf = read_packed_sha1(sha1, &type, &len);
+ buf = read_object(sha1, &type, &len);
if (!buf)
return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
#include "dir.h"
#include "sha1-array.h"
-static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
+static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
typedef int (*disambiguate_hint_fn)(const struct object_id *, void *);
#define SHORT_NAME_AMBIGUOUS (-2)
static int finish_object_disambiguation(struct disambiguate_state *ds,
- unsigned char *sha1)
+ struct object_id *oid)
{
if (ds->ambiguous)
return SHORT_NAME_AMBIGUOUS;
if (!ds->candidate_ok)
return SHORT_NAME_AMBIGUOUS;
- hashcpy(sha1, ds->candidate.hash);
+ oidcpy(oid, &ds->candidate);
return 0;
}
return 0;
}
-static int get_short_sha1(const char *name, int len, unsigned char *sha1,
+static int get_short_oid(const char *name, int len, struct object_id *oid,
unsigned flags)
{
int status;
struct disambiguate_state ds;
- int quietly = !!(flags & GET_SHA1_QUIETLY);
+ int quietly = !!(flags & GET_OID_QUIETLY);
if (init_object_disambiguation(name, len, &ds) < 0)
return -1;
- if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS))
- die("BUG: multiple get_short_sha1 disambiguator flags");
+ if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
+ die("BUG: multiple get_short_oid disambiguator flags");
- if (flags & GET_SHA1_COMMIT)
+ if (flags & GET_OID_COMMIT)
ds.fn = disambiguate_commit_only;
- else if (flags & GET_SHA1_COMMITTISH)
+ else if (flags & GET_OID_COMMITTISH)
ds.fn = disambiguate_committish_only;
- else if (flags & GET_SHA1_TREE)
+ else if (flags & GET_OID_TREE)
ds.fn = disambiguate_tree_only;
- else if (flags & GET_SHA1_TREEISH)
+ else if (flags & GET_OID_TREEISH)
ds.fn = disambiguate_treeish_only;
- else if (flags & GET_SHA1_BLOB)
+ else if (flags & GET_OID_BLOB)
ds.fn = disambiguate_blob_only;
else
ds.fn = default_disambiguate_hint;
find_short_object_filename(&ds);
find_short_packed_object(&ds);
- status = finish_object_disambiguation(&ds, sha1);
+ status = finish_object_disambiguation(&ds, oid);
if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
error(_("short SHA1 %s is ambiguous"), ds.hex_pfx);
}
sha1_to_hex_r(hex, sha1);
- if (len == 40 || !len)
- return 40;
+ if (len == GIT_SHA1_HEXSZ || !len)
+ return GIT_SHA1_HEXSZ;
exists = has_sha1_file(sha1);
- while (len < 40) {
- unsigned char sha1_ret[20];
- status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
+ while (len < GIT_SHA1_HEXSZ) {
+ struct object_id oid_ret;
+ status = get_short_oid(hex, len, &oid_ret, GET_OID_QUIETLY);
if (exists
? !status
: status == SHORT_NAME_NOT_FOUND) {
return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
}
-static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
+static int get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags);
static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
-static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
+static int get_oid_basic(const char *str, int len, struct object_id *oid,
unsigned int flags)
{
static const char *warn_msg = "refname '%.*s' is ambiguous.";
"where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
"examine these refs and maybe delete them. Turn this message off by\n"
"running \"git config advice.objectNameWarning false\"");
- unsigned char tmp_sha1[20];
+ struct object_id tmp_oid;
char *real_ref = NULL;
int refs_found = 0;
int at, reflog_len, nth_prior = 0;
- if (len == 40 && !get_sha1_hex(str, sha1)) {
+ if (len == GIT_SHA1_HEXSZ && !get_oid_hex(str, oid)) {
if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
- refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
+ refs_found = dwim_ref(str, len, tmp_oid.hash, &real_ref);
if (refs_found > 0) {
warning(warn_msg, len, str);
if (advice_object_name_warning)
int detached;
if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
- detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1));
+ detached = (buf.len == GIT_SHA1_HEXSZ && !get_oid_hex(buf.buf, oid));
strbuf_release(&buf);
if (detached)
return 0;
if (!len && reflog_len)
/* allow "@{...}" to mean the current branch reflog */
- refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
+ refs_found = dwim_ref("HEAD", 4, oid->hash, &real_ref);
else if (reflog_len)
- refs_found = dwim_log(str, len, sha1, &real_ref);
+ refs_found = dwim_log(str, len, oid->hash, &real_ref);
else
- refs_found = dwim_ref(str, len, sha1, &real_ref);
+ refs_found = dwim_ref(str, len, oid->hash, &real_ref);
if (!refs_found)
return -1;
- if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&
+ if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
(refs_found > 1 ||
- !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
+ !get_short_oid(str, len, &tmp_oid, GET_OID_QUIETLY)))
warning(warn_msg, len, str);
if (reflog_len) {
return -1;
}
}
- if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL,
+ if (read_ref_at(real_ref, flags, at_time, nth, oid->hash, NULL,
&co_time, &co_tz, &co_cnt)) {
if (!len) {
if (starts_with(real_ref, "refs/heads/")) {
}
}
if (at_time) {
- if (!(flags & GET_SHA1_QUIETLY)) {
+ if (!(flags & GET_OID_QUIETLY)) {
warning("Log for '%.*s' only goes "
"back to %s.", len, str,
show_date(co_time, co_tz, DATE_MODE(RFC2822)));
}
} else {
- if (flags & GET_SHA1_QUIETLY) {
+ if (flags & GET_OID_QUIETLY) {
exit(128);
}
die("Log for '%.*s' only has %d entries.",
}
static int get_parent(const char *name, int len,
- unsigned char *result, int idx)
+ struct object_id *result, int idx)
{
struct object_id oid;
- int ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
+ int ret = get_oid_1(name, len, &oid, GET_OID_COMMITTISH);
struct commit *commit;
struct commit_list *p;
if (parse_commit(commit))
return -1;
if (!idx) {
- hashcpy(result, commit->object.oid.hash);
+ oidcpy(result, &commit->object.oid);
return 0;
}
p = commit->parents;
while (p) {
if (!--idx) {
- hashcpy(result, p->item->object.oid.hash);
+ oidcpy(result, &p->item->object.oid);
return 0;
}
p = p->next;
}
static int get_nth_ancestor(const char *name, int len,
- unsigned char *result, int generation)
+ struct object_id *result, int generation)
{
struct object_id oid;
struct commit *commit;
int ret;
- ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
+ ret = get_oid_1(name, len, &oid, GET_OID_COMMITTISH);
if (ret)
return ret;
commit = lookup_commit_reference(&oid);
return -1;
commit = commit->parents->item;
}
- hashcpy(result, commit->object.oid.hash);
+ oidcpy(result, &commit->object.oid);
return 0;
}
}
}
-static int peel_onion(const char *name, int len, unsigned char *sha1,
+static int peel_onion(const char *name, int len, struct object_id *oid,
unsigned lookup_flags)
{
struct object_id outer;
else
return -1;
- lookup_flags &= ~GET_SHA1_DISAMBIGUATORS;
+ lookup_flags &= ~GET_OID_DISAMBIGUATORS;
if (expected_type == OBJ_COMMIT)
- lookup_flags |= GET_SHA1_COMMITTISH;
+ lookup_flags |= GET_OID_COMMITTISH;
else if (expected_type == OBJ_TREE)
- lookup_flags |= GET_SHA1_TREEISH;
+ lookup_flags |= GET_OID_TREEISH;
- if (get_sha1_1(name, sp - name - 2, outer.hash, lookup_flags))
+ if (get_oid_1(name, sp - name - 2, &outer, lookup_flags))
return -1;
o = parse_object(&outer);
o = deref_tag(o, name, sp - name - 2);
if (!o || (!o->parsed && !parse_object(&o->oid)))
return -1;
- hashcpy(sha1, o->oid.hash);
+ oidcpy(oid, &o->oid);
return 0;
}
if (!o)
return -1;
- hashcpy(sha1, o->oid.hash);
+ oidcpy(oid, &o->oid);
if (sp[0] == '/') {
/* "$commit^{/foo}" */
char *prefix;
prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
commit_list_insert((struct commit *)o, &list);
- ret = get_sha1_oneline(prefix, sha1, list);
+ ret = get_oid_oneline(prefix, oid, list);
free(prefix);
return ret;
}
return 0;
}
-static int get_describe_name(const char *name, int len, unsigned char *sha1)
+static int get_describe_name(const char *name, int len, struct object_id *oid)
{
const char *cp;
- unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT;
+ unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
for (cp = name + len - 1; name + 2 <= cp; cp--) {
char ch = *cp;
if (ch == 'g' && cp[-1] == '-') {
cp++;
len -= cp - name;
- return get_short_sha1(cp, len, sha1, flags);
+ return get_short_oid(cp, len, oid, flags);
}
}
}
return -1;
}
-static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags)
+static int get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags)
{
int ret, has_suffix;
const char *cp;
if (!num && len1 == len - 1)
num = 1;
if (has_suffix == '^')
- return get_parent(name, len1, sha1, num);
+ return get_parent(name, len1, oid, num);
/* else if (has_suffix == '~') -- goes without saying */
- return get_nth_ancestor(name, len1, sha1, num);
+ return get_nth_ancestor(name, len1, oid, num);
}
- ret = peel_onion(name, len, sha1, lookup_flags);
+ ret = peel_onion(name, len, oid, lookup_flags);
if (!ret)
return 0;
- ret = get_sha1_basic(name, len, sha1, lookup_flags);
+ ret = get_oid_basic(name, len, oid, lookup_flags);
if (!ret)
return 0;
/* It could be describe output that is "SOMETHING-gXXXX" */
- ret = get_describe_name(name, len, sha1);
+ ret = get_describe_name(name, len, oid);
if (!ret)
return 0;
- return get_short_sha1(name, len, sha1, lookup_flags);
+ return get_short_oid(name, len, oid, lookup_flags);
}
/*
return 0;
}
-static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
+static int get_oid_oneline(const char *prefix, struct object_id *oid,
struct commit_list *list)
{
struct commit_list *backup = NULL, *l;
unuse_commit_buffer(commit, buf);
if (matches) {
- hashcpy(sha1, commit->object.oid.hash);
+ oidcpy(oid, &commit->object.oid);
found = 1;
break;
}
struct strbuf sb;
strbuf_init(&sb, dots - name);
strbuf_add(&sb, name, dots - name);
- st = get_sha1_committish(sb.buf, oid_tmp.hash);
+ st = get_oid_committish(sb.buf, &oid_tmp);
strbuf_release(&sb);
}
if (st)
if (!one)
return -1;
- if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash))
+ if (get_oid_committish(dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
return -1;
two = lookup_commit_reference_gently(&oid_tmp, 0);
if (!two)
}
/*
- * This is like "get_sha1_basic()", except it allows "sha1 expressions",
+ * This is like "get_oid_basic()", except it allows "object ID expressions",
* notably "xyz^" for "parent of xyz"
*/
-int get_sha1(const char *name, unsigned char *sha1)
-{
- struct object_context unused;
- return get_sha1_with_context(name, 0, sha1, &unused);
-}
-
-/*
- * This is like "get_sha1()", but for struct object_id.
- */
int get_oid(const char *name, struct object_id *oid)
{
- return get_sha1(name, oid->hash);
+ struct object_context unused;
+ return get_oid_with_context(name, 0, oid, &unused);
}
* commit-ish. It is merely to give a hint to the disambiguation
* machinery.
*/
-int get_sha1_committish(const char *name, unsigned char *sha1)
+int get_oid_committish(const char *name, struct object_id *oid)
{
struct object_context unused;
- return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
- sha1, &unused);
+ return get_oid_with_context(name, GET_OID_COMMITTISH,
+ oid, &unused);
}
-int get_sha1_treeish(const char *name, unsigned char *sha1)
+int get_oid_treeish(const char *name, struct object_id *oid)
{
struct object_context unused;
- return get_sha1_with_context(name, GET_SHA1_TREEISH,
- sha1, &unused);
+ return get_oid_with_context(name, GET_OID_TREEISH,
+ oid, &unused);
}
-int get_sha1_commit(const char *name, unsigned char *sha1)
+int get_oid_commit(const char *name, struct object_id *oid)
{
struct object_context unused;
- return get_sha1_with_context(name, GET_SHA1_COMMIT,
- sha1, &unused);
+ return get_oid_with_context(name, GET_OID_COMMIT,
+ oid, &unused);
}
-int get_sha1_tree(const char *name, unsigned char *sha1)
+int get_oid_tree(const char *name, struct object_id *oid)
{
struct object_context unused;
- return get_sha1_with_context(name, GET_SHA1_TREE,
- sha1, &unused);
+ return get_oid_with_context(name, GET_OID_TREE,
+ oid, &unused);
}
-int get_sha1_blob(const char *name, unsigned char *sha1)
+int get_oid_blob(const char *name, struct object_id *oid)
{
struct object_context unused;
- return get_sha1_with_context(name, GET_SHA1_BLOB,
- sha1, &unused);
+ return get_oid_with_context(name, GET_OID_BLOB,
+ oid, &unused);
}
/* Must be called only when object_name:filename doesn't exist. */
-static void diagnose_invalid_sha1_path(const char *prefix,
- const char *filename,
- const unsigned char *tree_sha1,
- const char *object_name,
- int object_name_len)
+static void diagnose_invalid_oid_path(const char *prefix,
+ const char *filename,
+ const struct object_id *tree_oid,
+ const char *object_name,
+ int object_name_len)
{
- unsigned char sha1[20];
+ struct object_id oid;
unsigned mode;
if (!prefix)
if (is_missing_file_error(errno)) {
char *fullname = xstrfmt("%s%s", prefix, filename);
- if (!get_tree_entry(tree_sha1, fullname,
- sha1, &mode)) {
+ if (!get_tree_entry(tree_oid->hash, fullname,
+ oid.hash, &mode)) {
die("Path '%s' exists, but not '%s'.\n"
"Did you mean '%.*s:%s' aka '%.*s:./%s'?",
fullname,
rel);
}
-static int get_sha1_with_context_1(const char *name,
- unsigned flags,
- const char *prefix,
- unsigned char *sha1,
- struct object_context *oc)
+static int get_oid_with_context_1(const char *name,
+ unsigned flags,
+ const char *prefix,
+ struct object_id *oid,
+ struct object_context *oc)
{
int ret, bracket_depth;
int namelen = strlen(name);
const char *cp;
- int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
+ int only_to_die = flags & GET_OID_ONLY_TO_DIE;
if (only_to_die)
- flags |= GET_SHA1_QUIETLY;
+ flags |= GET_OID_QUIETLY;
memset(oc, 0, sizeof(*oc));
oc->mode = S_IFINVALID;
strbuf_init(&oc->symlink_path, 0);
- ret = get_sha1_1(name, namelen, sha1, flags);
+ ret = get_oid_1(name, namelen, oid, flags);
if (!ret)
return ret;
/*
for_each_ref(handle_one_ref, &list);
commit_list_sort_by_date(&list);
- return get_sha1_oneline(name + 2, sha1, list);
+ return get_oid_oneline(name + 2, oid, list);
}
if (namelen < 3 ||
name[2] != ':' ||
namelen = strlen(cp);
}
- if (flags & GET_SHA1_RECORD_PATH)
+ if (flags & GET_OID_RECORD_PATH)
oc->path = xstrdup(cp);
if (!active_cache)
memcmp(ce->name, cp, namelen))
break;
if (ce_stage(ce) == stage) {
- hashcpy(sha1, ce->oid.hash);
+ oidcpy(oid, &ce->oid);
oc->mode = ce->ce_mode;
free(new_path);
return 0;
break;
}
if (*cp == ':') {
- unsigned char tree_sha1[20];
+ struct object_id tree_oid;
int len = cp - name;
unsigned sub_flags = flags;
- sub_flags &= ~GET_SHA1_DISAMBIGUATORS;
- sub_flags |= GET_SHA1_TREEISH;
+ sub_flags &= ~GET_OID_DISAMBIGUATORS;
+ sub_flags |= GET_OID_TREEISH;
- if (!get_sha1_1(name, len, tree_sha1, sub_flags)) {
+ if (!get_oid_1(name, len, &tree_oid, sub_flags)) {
const char *filename = cp+1;
char *new_filename = NULL;
new_filename = resolve_relative_path(filename);
if (new_filename)
filename = new_filename;
- if (flags & GET_SHA1_FOLLOW_SYMLINKS) {
- ret = get_tree_entry_follow_symlinks(tree_sha1,
- filename, sha1, &oc->symlink_path,
+ if (flags & GET_OID_FOLLOW_SYMLINKS) {
+ ret = get_tree_entry_follow_symlinks(tree_oid.hash,
+ filename, oid->hash, &oc->symlink_path,
&oc->mode);
} else {
- ret = get_tree_entry(tree_sha1, filename,
- sha1, &oc->mode);
+ ret = get_tree_entry(tree_oid.hash, filename,
+ oid->hash, &oc->mode);
if (ret && only_to_die) {
- diagnose_invalid_sha1_path(prefix,
+ diagnose_invalid_oid_path(prefix,
filename,
- tree_sha1,
+ &tree_oid,
name, len);
}
}
- hashcpy(oc->tree, tree_sha1);
- if (flags & GET_SHA1_RECORD_PATH)
+ hashcpy(oc->tree, tree_oid.hash);
+ if (flags & GET_OID_RECORD_PATH)
oc->path = xstrdup(filename);
free(new_filename);
void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
{
struct object_context oc;
- unsigned char sha1[20];
- get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
+ struct object_id oid;
+ get_oid_with_context_1(name, GET_OID_ONLY_TO_DIE, prefix, &oid, &oc);
}
-int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc)
+int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc)
{
- if (flags & GET_SHA1_FOLLOW_SYMLINKS && flags & GET_SHA1_ONLY_TO_DIE)
+ if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
die("BUG: incompatible flags for get_sha1_with_context");
- return get_sha1_with_context_1(str, flags, NULL, sha1, oc);
+ return get_oid_with_context_1(str, flags, NULL, oid, oc);
}
cur_depth++;
if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
(is_repository_shallow() && !commit->parents &&
- (graft = lookup_commit_graft(commit->object.oid.hash)) != NULL &&
+ (graft = lookup_commit_graft(&commit->object.oid)) != NULL &&
graft->nr_parent < 0)) {
commit_list_insert(commit, &result);
commit->object.flags |= shallow_flag;
for (i = 0; i < sa->nr; i++) {
if (has_object_file(sa->oid + i)) {
struct commit_graft *graft;
- graft = lookup_commit_graft(sa->oid[i].hash);
+ graft = lookup_commit_graft(&sa->oid[i]);
if (graft && graft->nr_parent < 0)
continue;
info->ours[info->nr_ours++] = i;
/* Translate slopbuf to NULL, as we cannot call realloc on it */
if (!sb->alloc)
sb->buf = NULL;
+ errno = 0;
r = getdelim(&sb->buf, &sb->alloc, term, fp);
if (r > 0) {
};
extern char strbuf_slopbuf[];
-#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
+#define STRBUF_INIT { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
/**
* Life Cycle Functions
ALLOC_GROW(list->items, list->nr+1, list->alloc);
if (index < list->nr)
- memmove(list->items + index + 1, list->items + index,
- (list->nr - index)
- * sizeof(struct string_list_item));
+ MOVE_ARRAY(list->items + index + 1, list->items + index,
+ list->nr - index);
list->items[index].string = list->strdup_strings ?
xstrdup(string) : (char *)string;
list->items[index].util = NULL;
free(list->items[i].util);
list->nr--;
- memmove(list->items + i, list->items + i + 1,
- (list->nr - i) * sizeof(struct string_list_item));
+ MOVE_ARRAY(list->items + i, list->items + i + 1, list->nr - i);
}
}
#include "pkt-line.h"
int cmd2process_cmp(const void *unused_cmp_data,
- const struct subprocess_entry *e1,
- const struct subprocess_entry *e2,
+ const void *entry,
+ const void *entry_or_key,
const void *unused_keydata)
{
+ const struct subprocess_entry *e1 = entry;
+ const struct subprocess_entry *e2 = entry_or_key;
+
return strcmp(e1->cmd, e2->cmd);
}
hashmap_add(hashmap, entry);
return 0;
}
+
+static int handshake_version(struct child_process *process,
+ const char *welcome_prefix, int *versions,
+ int *chosen_version)
+{
+ int version_scratch;
+ int i;
+ char *line;
+ const char *p;
+
+ if (!chosen_version)
+ chosen_version = &version_scratch;
+
+ if (packet_write_fmt_gently(process->in, "%s-client\n",
+ welcome_prefix))
+ return error("Could not write client identification");
+ for (i = 0; versions[i]; i++) {
+ if (packet_write_fmt_gently(process->in, "version=%d\n",
+ versions[i]))
+ return error("Could not write requested version");
+ }
+ if (packet_flush_gently(process->in))
+ return error("Could not write flush packet");
+
+ if (!(line = packet_read_line(process->out, NULL)) ||
+ !skip_prefix(line, welcome_prefix, &p) ||
+ strcmp(p, "-server"))
+ return error("Unexpected line '%s', expected %s-server",
+ line ? line : "<flush packet>", welcome_prefix);
+ if (!(line = packet_read_line(process->out, NULL)) ||
+ !skip_prefix(line, "version=", &p) ||
+ strtol_i(p, 10, chosen_version))
+ return error("Unexpected line '%s', expected version",
+ line ? line : "<flush packet>");
+ if ((line = packet_read_line(process->out, NULL)))
+ return error("Unexpected line '%s', expected flush", line);
+
+ /* Check to make sure that the version received is supported */
+ for (i = 0; versions[i]; i++) {
+ if (versions[i] == *chosen_version)
+ break;
+ }
+ if (!versions[i])
+ return error("Version %d not supported", *chosen_version);
+
+ return 0;
+}
+
+static int handshake_capabilities(struct child_process *process,
+ struct subprocess_capability *capabilities,
+ unsigned int *supported_capabilities)
+{
+ int i;
+ char *line;
+
+ for (i = 0; capabilities[i].name; i++) {
+ if (packet_write_fmt_gently(process->in, "capability=%s\n",
+ capabilities[i].name))
+ return error("Could not write requested capability");
+ }
+ if (packet_flush_gently(process->in))
+ return error("Could not write flush packet");
+
+ while ((line = packet_read_line(process->out, NULL))) {
+ const char *p;
+ if (!skip_prefix(line, "capability=", &p))
+ continue;
+
+ for (i = 0;
+ capabilities[i].name && strcmp(p, capabilities[i].name);
+ i++)
+ ;
+ if (capabilities[i].name) {
+ if (supported_capabilities)
+ *supported_capabilities |= capabilities[i].flag;
+ } else {
+ warning("external filter requested unsupported filter capability '%s'",
+ p);
+ }
+ }
+
+ return 0;
+}
+
+int subprocess_handshake(struct subprocess_entry *entry,
+ const char *welcome_prefix,
+ int *versions,
+ int *chosen_version,
+ struct subprocess_capability *capabilities,
+ unsigned int *supported_capabilities)
+{
+ int retval;
+ struct child_process *process = &entry->process;
+
+ sigchain_push(SIGPIPE, SIG_IGN);
+
+ retval = handshake_version(process, welcome_prefix, versions,
+ chosen_version) ||
+ handshake_capabilities(process, capabilities,
+ supported_capabilities);
+
+ sigchain_pop(SIGPIPE);
+ return retval;
+}
#include "run-command.h"
/*
- * Generic implementation of background process infrastructure.
- * See: Documentation/technical/api-sub-process.txt
+ * The sub-process API makes it possible to run background sub-processes
+ * for the entire lifetime of a Git invocation. If Git needs to communicate
+ * with an external process multiple times, then this can reduces the process
+ * invocation overhead. Git and the sub-process communicate through stdin and
+ * stdout.
+ *
+ * The sub-processes are kept in a hashmap by command name and looked up
+ * via the subprocess_find_entry function. If an existing instance can not
+ * be found then a new process should be created and started. When the
+ * parent git command terminates, all sub-processes are also terminated.
+ *
+ * This API is based on the run-command API.
*/
/* data structures */
+/* Members should not be accessed directly. */
struct subprocess_entry {
struct hashmap_entry ent; /* must be the first member! */
const char *cmd;
struct child_process process;
};
+struct subprocess_capability {
+ const char *name;
+
+ /*
+ * subprocess_handshake will "|=" this value to supported_capabilities
+ * if the server reports that it supports this capability.
+ */
+ unsigned int flag;
+};
+
/* subprocess functions */
+/* Function to test two subprocess hashmap entries for equality. */
extern int cmd2process_cmp(const void *unused_cmp_data,
- const struct subprocess_entry *e1,
- const struct subprocess_entry *e2,
+ const void *e1,
+ const void *e2,
const void *unused_keydata);
+/*
+ * User-supplied function to initialize the sub-process. This is
+ * typically used to negotiate the interface version and capabilities.
+ */
typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
+
+/* Start a subprocess and add it to the subprocess hashmap. */
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
subprocess_start_fn startfn);
+/* Kill a subprocess and remove it from the subprocess hashmap. */
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry);
+/* Find a subprocess in the subprocess hashmap. */
struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd);
/* subprocess helper functions */
+/* Get the underlying `struct child_process` from a subprocess. */
static inline struct child_process *subprocess_get_child_process(
struct subprocess_entry *entry)
{
return &entry->process;
}
+/*
+ * Perform the version and capability negotiation as described in the "Long
+ * Running Filter Process" section of the gitattributes documentation using the
+ * given requested versions and capabilities. The "versions" and "capabilities"
+ * parameters are arrays terminated by a 0 or blank struct.
+ *
+ * This function is typically called when a subprocess is started (as part of
+ * the "startfn" passed to subprocess_start).
+ */
+int subprocess_handshake(struct subprocess_entry *entry,
+ const char *welcome_prefix,
+ int *versions,
+ int *chosen_version,
+ struct subprocess_capability *capabilities,
+ unsigned int *supported_capabilities);
+
/*
* Helper function that will read packets looking for "status=<foo>"
* key/value pairs and return the value from the last "status" packet
};
static int config_path_cmp(const void *unused_cmp_data,
- const struct submodule_entry *a,
- const struct submodule_entry *b,
+ const void *entry,
+ const void *entry_or_key,
const void *unused_keydata)
{
+ const struct submodule_entry *a = entry;
+ const struct submodule_entry *b = entry_or_key;
+
return strcmp(a->config->path, b->config->path) ||
hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
}
static int config_name_cmp(const void *unused_cmp_data,
- const struct submodule_entry *a,
- const struct submodule_entry *b,
+ const void *entry,
+ const void *entry_or_key,
const void *unused_keydata)
{
+ const struct submodule_entry *a = entry;
+ const struct submodule_entry *b = entry_or_key;
+
return strcmp(a->config->name, b->config->name) ||
hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
}
static void submodule_cache_init(struct submodule_cache *cache)
{
- hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
- hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
+ hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
+ hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
cache->initialized = 1;
}
static int parse_fetch_recurse(const char *opt, const char *arg,
int die_on_error)
{
- switch (git_config_maybe_bool(opt, arg)) {
+ switch (git_parse_maybe_bool(arg)) {
case 1:
return RECURSE_SUBMODULES_ON;
case 0:
}
}
+int parse_submodule_fetchjobs(const char *var, const char *value)
+{
+ int fetchjobs = git_config_int(var, value);
+ if (fetchjobs < 0)
+ die(_("negative values not allowed for submodule.fetchjobs"));
+ return fetchjobs;
+}
+
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
{
return parse_fetch_recurse(opt, arg, 1);
static int parse_update_recurse(const char *opt, const char *arg,
int die_on_error)
{
- switch (git_config_maybe_bool(opt, arg)) {
+ switch (git_parse_maybe_bool(arg)) {
case 1:
return RECURSE_SUBMODULES_ON;
case 0:
static int parse_push_recurse(const char *opt, const char *arg,
int die_on_error)
{
- switch (git_config_maybe_bool(opt, arg)) {
+ switch (git_parse_maybe_bool(arg)) {
case 1:
/* There's no simple "on" value when pushing */
if (die_on_error)
return ret;
}
-int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
- unsigned char *gitmodules_sha1,
+int gitmodule_oid_from_commit(const struct object_id *treeish_name,
+ struct object_id *gitmodules_oid,
struct strbuf *rev)
{
int ret = 0;
- if (is_null_sha1(treeish_name)) {
- hashclr(gitmodules_sha1);
+ if (is_null_oid(treeish_name)) {
+ oidclr(gitmodules_oid);
return 1;
}
- strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
- if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
+ strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
+ if (get_oid(rev->buf, gitmodules_oid) >= 0)
ret = 1;
return ret;
* revisions.
*/
static const struct submodule *config_from(struct submodule_cache *cache,
- const unsigned char *treeish_name, const char *key,
+ const struct object_id *treeish_name, const char *key,
enum lookup_type lookup_type)
{
struct strbuf rev = STRBUF_INIT;
unsigned long config_size;
char *config = NULL;
- unsigned char sha1[20];
+ struct object_id oid;
enum object_type type;
const struct submodule *submodule = NULL;
struct parse_config_parameter parameter;
return entry->config;
}
- if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
+ if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
goto out;
switch (lookup_type) {
case lookup_name:
- submodule = cache_lookup_name(cache, sha1, key);
+ submodule = cache_lookup_name(cache, oid.hash, key);
break;
case lookup_path:
- submodule = cache_lookup_path(cache, sha1, key);
+ submodule = cache_lookup_path(cache, oid.hash, key);
break;
}
if (submodule)
goto out;
- config = read_sha1_file(sha1, &type, &config_size);
+ config = read_sha1_file(oid.hash, &type, &config_size);
if (!config || type != OBJ_BLOB)
goto out;
/* fill the submodule config into the cache */
parameter.cache = cache;
- parameter.treeish_name = treeish_name;
- parameter.gitmodules_sha1 = sha1;
+ parameter.treeish_name = treeish_name->hash;
+ parameter.gitmodules_sha1 = oid.hash;
parameter.overwrite = 0;
git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
config, config_size, ¶meter);
switch (lookup_type) {
case lookup_name:
- return cache_lookup_name(cache, sha1, key);
+ return cache_lookup_name(cache, oid.hash, key);
case lookup_path:
- return cache_lookup_path(cache, sha1, key);
+ return cache_lookup_path(cache, oid.hash, key);
default:
return NULL;
}
return submodule_config_option(the_repository, var, value);
}
-const struct submodule *submodule_from_name(const unsigned char *treeish_name,
+const struct submodule *submodule_from_name(const struct object_id *treeish_name,
const char *name)
{
submodule_cache_check_init(the_repository);
return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
}
-const struct submodule *submodule_from_path(const unsigned char *treeish_name,
+const struct submodule *submodule_from_path(const struct object_id *treeish_name,
const char *path)
{
submodule_cache_check_init(the_repository);
}
const struct submodule *submodule_from_cache(struct repository *repo,
- const unsigned char *treeish_name,
+ const struct object_id *treeish_name,
const char *key)
{
submodule_cache_check_init(repo);
extern void submodule_cache_free(struct submodule_cache *cache);
+extern int parse_submodule_fetchjobs(const char *var, const char *value);
extern int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
struct option;
extern int option_fetch_parse_recurse_submodules(const struct option *opt,
extern int submodule_config_option(struct repository *repo,
const char *var, const char *value);
extern const struct submodule *submodule_from_name(
- const unsigned char *commit_or_tree, const char *name);
+ const struct object_id *commit_or_tree, const char *name);
extern const struct submodule *submodule_from_path(
- const unsigned char *commit_or_tree, const char *path);
+ const struct object_id *commit_or_tree, const char *path);
extern const struct submodule *submodule_from_cache(struct repository *repo,
- const unsigned char *treeish_name,
+ const struct object_id *treeish_name,
const char *key);
-extern int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
- unsigned char *gitmodules_sha1,
- struct strbuf *rev);
+extern int gitmodule_oid_from_commit(const struct object_id *commit_oid,
+ struct object_id *gitmodules_oid,
+ struct strbuf *rev);
extern void submodule_free(void);
#endif /* SUBMODULE_CONFIG_H */
#include "worktree.h"
#include "parse-options.h"
-static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
-static int parallel_jobs = 1;
static struct string_list changed_submodule_paths = STRING_LIST_INIT_DUP;
static int initialized_fetch_ref_tips;
static struct oid_array ref_tips_before_fetch;
static struct oid_array ref_tips_after_fetch;
/*
- * The following flag is set if the .gitmodules file is unmerged. We then
- * disable recursion for all submodules where .git/config doesn't have a
- * matching config entry because we can't guess what might be configured in
- * .gitmodules unless the user resolves the conflict. When a command line
- * option is given (which always overrides configuration) this flag will be
- * ignored.
+ * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
+ * will be disabled because we can't guess what might be configured in
+ * .gitmodules unless the user resolves the conflict.
*/
-static int gitmodules_is_unmerged;
+int is_gitmodules_unmerged(const struct index_state *istate)
+{
+ int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
+ if (pos < 0) { /* .gitmodules not found or isn't merged */
+ pos = -1 - pos;
+ if (istate->cache_nr > pos) { /* there is a .gitmodules */
+ const struct cache_entry *ce = istate->cache[pos];
+ if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
+ !strcmp(ce->name, GITMODULES_FILE))
+ return 1;
+ }
+ }
+
+ return 0;
+}
/*
- * This flag is set if the .gitmodules file had unstaged modifications on
- * startup. This must be checked before allowing modifications to the
- * .gitmodules file with the intention to stage them later, because when
- * continuing we would stage the modifications the user didn't stage herself
- * too. That might change in a future version when we learn to stage the
- * changes we do ourselves without staging any previous modifications.
+ * Check if the .gitmodules file has unstaged modifications. This must be
+ * checked before allowing modifications to the .gitmodules file with the
+ * intention to stage them later, because when continuing we would stage the
+ * modifications the user didn't stage herself too. That might change in a
+ * future version when we learn to stage the changes we do ourselves without
+ * staging any previous modifications.
*/
-static int gitmodules_is_modified;
-
-int is_staging_gitmodules_ok(void)
+int is_staging_gitmodules_ok(const struct index_state *istate)
{
- return !gitmodules_is_modified;
+ int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
+
+ if ((pos >= 0) && (pos < istate->cache_nr)) {
+ struct stat st;
+ if (lstat(GITMODULES_FILE, &st) == 0 &&
+ ce_match_stat(istate->cache[pos], &st, 0) & DATA_CHANGED)
+ return 0;
+ }
+
+ return 1;
}
/*
struct strbuf entry = STRBUF_INIT;
const struct submodule *submodule;
- if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
+ if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
return -1;
- if (gitmodules_is_unmerged)
+ if (is_gitmodules_unmerged(&the_index))
die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
- submodule = submodule_from_path(null_sha1, oldpath);
+ submodule = submodule_from_path(&null_oid, oldpath);
if (!submodule || !submodule->name) {
warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
return -1;
strbuf_addstr(&entry, "submodule.");
strbuf_addstr(&entry, submodule->name);
strbuf_addstr(&entry, ".path");
- if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
+ if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
/* Maybe the user already did that, don't error out here */
warning(_("Could not update .gitmodules entry %s"), entry.buf);
strbuf_release(&entry);
struct strbuf sect = STRBUF_INIT;
const struct submodule *submodule;
- if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
+ if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
return -1;
- if (gitmodules_is_unmerged)
+ if (is_gitmodules_unmerged(&the_index))
die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
- submodule = submodule_from_path(null_sha1, path);
+ submodule = submodule_from_path(&null_oid, path);
if (!submodule || !submodule->name) {
warning(_("Could not find section in .gitmodules where path=%s"), path);
return -1;
}
strbuf_addstr(§, "submodule.");
strbuf_addstr(§, submodule->name);
- if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
+ if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
/* Maybe the user already did that, don't error out here */
warning(_("Could not remove .gitmodules entry for %s"), path);
strbuf_release(§);
void stage_updated_gitmodules(void)
{
- if (add_file_to_cache(".gitmodules", 0))
+ if (add_file_to_cache(GITMODULES_FILE, 0))
die(_("staging updated .gitmodules failed"));
}
void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
const char *path)
{
- const struct submodule *submodule = submodule_from_path(null_sha1, path);
+ const struct submodule *submodule = submodule_from_path(&null_oid, path);
if (submodule) {
if (submodule->ignore)
handle_ignore_submodules_arg(diffopt, submodule->ignore);
- else if (gitmodules_is_unmerged)
+ else if (is_gitmodules_unmerged(&the_index))
DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
}
}
/* For loading from the .gitmodules file. */
static int git_modules_config(const char *var, const char *value, void *cb)
{
- if (!strcmp(var, "submodule.fetchjobs")) {
- parallel_jobs = git_config_int(var, value);
- if (parallel_jobs < 0)
- die(_("negative values not allowed for submodule.fetchJobs"));
- return 0;
- } else if (starts_with(var, "submodule."))
+ if (starts_with(var, "submodule."))
return parse_submodule_config_option(var, value);
- else if (!strcmp(var, "fetch.recursesubmodules")) {
- config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
- return 0;
- }
return 0;
}
git_config(submodule_config, NULL);
}
-void gitmodules_config(void)
-{
- const char *work_tree = get_git_work_tree();
- if (work_tree) {
- struct strbuf gitmodules_path = STRBUF_INIT;
- int pos;
- strbuf_addstr(&gitmodules_path, work_tree);
- strbuf_addstr(&gitmodules_path, "/.gitmodules");
- if (read_cache() < 0)
- die("index file corrupt");
- pos = cache_name_pos(".gitmodules", 11);
- if (pos < 0) { /* .gitmodules not found or isn't merged */
- pos = -1 - pos;
- if (active_nr > pos) { /* there is a .gitmodules */
- const struct cache_entry *ce = active_cache[pos];
- if (ce_namelen(ce) == 11 &&
- !memcmp(ce->name, ".gitmodules", 11))
- gitmodules_is_unmerged = 1;
- }
- } else if (pos < active_nr) {
- struct stat st;
- if (lstat(".gitmodules", &st) == 0 &&
- ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
- gitmodules_is_modified = 1;
- }
-
- if (!gitmodules_is_unmerged)
- git_config_from_file(git_modules_config,
- gitmodules_path.buf, NULL);
- strbuf_release(&gitmodules_path);
- }
-}
-
static int gitmodules_cb(const char *var, const char *value, void *data)
{
struct repository *repo = data;
void repo_read_gitmodules(struct repository *repo)
{
- char *gitmodules_path = repo_worktree_path(repo, ".gitmodules");
+ if (repo->worktree) {
+ char *gitmodules;
+
+ if (repo_read_index(repo) < 0)
+ return;
+
+ gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
- git_config_from_file(gitmodules_cb, gitmodules_path, repo);
- free(gitmodules_path);
+ if (!is_gitmodules_unmerged(repo->index))
+ git_config_from_file(gitmodules_cb, gitmodules, repo);
+
+ free(gitmodules);
+ }
}
-void gitmodules_config_sha1(const unsigned char *commit_sha1)
+void gitmodules_config(void)
+{
+ repo_read_gitmodules(the_repository);
+}
+
+void gitmodules_config_oid(const struct object_id *commit_oid)
{
struct strbuf rev = STRBUF_INIT;
- unsigned char sha1[20];
+ struct object_id oid;
- if (gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) {
- git_config_from_blob_sha1(git_modules_config, rev.buf,
- sha1, NULL);
+ if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
+ git_config_from_blob_oid(submodule_config, rev.buf,
+ &oid, NULL);
}
strbuf_release(&rev);
}
const struct string_list *sl;
const struct submodule *module;
- module = submodule_from_cache(repo, null_sha1, path);
+ module = submodule_from_cache(repo, &null_oid, path);
/* early return if there isn't a path->module mapping */
if (!module)
return prepare_revision_walk(rev);
}
-static void print_submodule_summary(struct rev_info *rev, FILE *f,
- const char *line_prefix,
- const char *del, const char *add, const char *reset)
+static void print_submodule_summary(struct rev_info *rev, struct diff_options *o)
{
static const char format[] = " %m %s";
struct strbuf sb = STRBUF_INIT;
ctx.date_mode = rev->date_mode;
ctx.output_encoding = get_log_output_encoding();
strbuf_setlen(&sb, 0);
- strbuf_addstr(&sb, line_prefix);
- if (commit->object.flags & SYMMETRIC_LEFT) {
- if (del)
- strbuf_addstr(&sb, del);
- }
- else if (add)
- strbuf_addstr(&sb, add);
format_commit_message(commit, format, &sb, &ctx);
- if (reset)
- strbuf_addstr(&sb, reset);
strbuf_addch(&sb, '\n');
- fprintf(f, "%s", sb.buf);
+ if (commit->object.flags & SYMMETRIC_LEFT)
+ diff_emit_submodule_del(o, sb.buf);
+ else
+ diff_emit_submodule_add(o, sb.buf);
}
strbuf_release(&sb);
}
* attempt to lookup both the left and right commits and put them into the
* left and right pointers.
*/
-static void show_submodule_header(FILE *f, const char *path,
- const char *line_prefix,
+static void show_submodule_header(struct diff_options *o, const char *path,
struct object_id *one, struct object_id *two,
- unsigned dirty_submodule, const char *meta,
- const char *reset,
+ unsigned dirty_submodule,
struct commit **left, struct commit **right,
struct commit_list **merge_bases)
{
int fast_forward = 0, fast_backward = 0;
if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
- fprintf(f, "%sSubmodule %s contains untracked content\n",
- line_prefix, path);
+ diff_emit_submodule_untracked(o, path);
+
if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
- fprintf(f, "%sSubmodule %s contains modified content\n",
- line_prefix, path);
+ diff_emit_submodule_modified(o, path);
if (is_null_oid(one))
message = "(new submodule)";
}
output_header:
- strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
+ strbuf_addf(&sb, "Submodule %s ", path);
strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
if (message)
- strbuf_addf(&sb, " %s%s\n", message, reset);
+ strbuf_addf(&sb, " %s\n", message);
else
- strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
- fwrite(sb.buf, sb.len, 1, f);
+ strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
+ diff_emit_submodule_header(o, sb.buf);
strbuf_release(&sb);
}
-void show_submodule_summary(FILE *f, const char *path,
- const char *line_prefix,
+void show_submodule_summary(struct diff_options *o, const char *path,
struct object_id *one, struct object_id *two,
- unsigned dirty_submodule, const char *meta,
- const char *del, const char *add, const char *reset)
+ unsigned dirty_submodule)
{
struct rev_info rev;
struct commit *left = NULL, *right = NULL;
struct commit_list *merge_bases = NULL;
- show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
- meta, reset, &left, &right, &merge_bases);
+ show_submodule_header(o, path, one, two, dirty_submodule,
+ &left, &right, &merge_bases);
/*
* If we don't have both a left and a right pointer, there is no
/* Treat revision walker failure the same as missing commits */
if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
- fprintf(f, "%s(revision walker failed)\n", line_prefix);
+ diff_emit_submodule_error(o, "(revision walker failed)\n");
goto out;
}
- print_submodule_summary(&rev, f, line_prefix, del, add, reset);
+ print_submodule_summary(&rev, o);
out:
if (merge_bases)
clear_commit_marks(right, ~0);
}
-void show_submodule_inline_diff(FILE *f, const char *path,
- const char *line_prefix,
+void show_submodule_inline_diff(struct diff_options *o, const char *path,
struct object_id *one, struct object_id *two,
- unsigned dirty_submodule, const char *meta,
- const char *del, const char *add, const char *reset,
- const struct diff_options *o)
+ unsigned dirty_submodule)
{
const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
struct commit *left = NULL, *right = NULL;
struct commit_list *merge_bases = NULL;
- struct strbuf submodule_dir = STRBUF_INIT;
struct child_process cp = CHILD_PROCESS_INIT;
+ struct strbuf sb = STRBUF_INIT;
- show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
- meta, reset, &left, &right, &merge_bases);
+ show_submodule_header(o, path, one, two, dirty_submodule,
+ &left, &right, &merge_bases);
/* We need a valid left and right commit to display a difference */
if (!(left || is_null_oid(one)) ||
if (right)
new = two;
- fflush(f);
cp.git_cmd = 1;
cp.dir = path;
- cp.out = dup(fileno(f));
+ cp.out = -1;
cp.no_stdin = 1;
/* TODO: other options may need to be passed here. */
argv_array_pushl(&cp.args, "diff", "--submodule=diff", NULL);
+ argv_array_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
+ "always" : "never");
- argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
o->b_prefix, path);
argv_array_push(&cp.args, oid_to_hex(new));
prepare_submodule_repo_env(&cp.env_array);
- if (run_command(&cp))
- fprintf(f, "(diff failed)\n");
+ if (start_command(&cp))
+ diff_emit_submodule_error(o, "(diff failed)\n");
+
+ while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
+ diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
+
+ if (finish_command(&cp))
+ diff_emit_submodule_error(o, "(diff failed)\n");
done:
- strbuf_release(&submodule_dir);
+ strbuf_release(&sb);
if (merge_bases)
free_commit_list(merge_bases);
if (left)
clear_commit_marks(right, ~0);
}
-void set_config_fetch_recurse_submodules(int value)
-{
- config_fetch_recurse_submodules = value;
-}
-
int should_update_submodules(void)
{
return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
if (!should_update_submodules())
return NULL;
- return submodule_from_path(null_sha1, ce->name);
+ return submodule_from_path(&null_oid, ce->name);
}
static struct oid_array *submodule_commits(struct string_list *submodules,
* Perform a check in the submodule to see if the remote and refspec work.
* Die if the submodule can't be pushed.
*/
-static void submodule_push_check(const char *path, const struct remote *remote,
+static void submodule_push_check(const char *path, const char *head,
+ const struct remote *remote,
const char **refspec, int refspec_nr)
{
struct child_process cp = CHILD_PROCESS_INIT;
argv_array_push(&cp.args, "submodule--helper");
argv_array_push(&cp.args, "push-check");
+ argv_array_push(&cp.args, head);
argv_array_push(&cp.args, remote->name);
for (i = 0; i < refspec_nr; i++)
* won't be propagated due to the remote being unconfigured (e.g. a URL
* instead of a remote name).
*/
- if (remote->origin != REMOTE_UNCONFIGURED)
+ if (remote->origin != REMOTE_UNCONFIGURED) {
+ char *head;
+ struct object_id head_oid;
+
+ head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
+ if (!head)
+ die(_("Failed to resolve HEAD as a valid ref."));
+
for (i = 0; i < needs_pushing.nr; i++)
submodule_push_check(needs_pushing.items[i].string,
- remote, refspec, refspec_nr);
+ head, remote,
+ refspec, refspec_nr);
+ free(head);
+ }
/* Actually push the submodules */
for (i = 0; i < needs_pushing.nr; i++) {
const char *work_tree;
const char *prefix;
int command_line_option;
+ int default_option;
int quiet;
int result;
};
-#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
+#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0, 0}
static int get_next_submodule(struct child_process *cp,
struct strbuf *err, void *data, void **task_cb)
if (!S_ISGITLINK(ce->ce_mode))
continue;
- submodule = submodule_from_path(null_sha1, ce->name);
+ submodule = submodule_from_path(&null_oid, ce->name);
if (!submodule)
- submodule = submodule_from_name(null_sha1, ce->name);
+ submodule = submodule_from_name(&null_oid, ce->name);
default_argv = "yes";
if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
default_argv = "on-demand";
}
} else {
- if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
- gitmodules_is_unmerged)
+ if (spf->default_option == RECURSE_SUBMODULES_OFF)
continue;
- if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
+ if (spf->default_option == RECURSE_SUBMODULES_ON_DEMAND) {
if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
continue;
default_argv = "on-demand";
int fetch_populated_submodules(const struct argv_array *options,
const char *prefix, int command_line_option,
+ int default_option,
int quiet, int max_parallel_jobs)
{
int i;
spf.work_tree = get_git_work_tree();
spf.command_line_option = command_line_option;
+ spf.default_option = default_option;
spf.quiet = quiet;
spf.prefix = prefix;
argv_array_push(&spf.args, "--recurse-submodules-default");
/* default value, "--submodule-prefix" and its value are added later */
- if (max_parallel_jobs < 0)
- max_parallel_jobs = parallel_jobs;
-
calculate_changed_submodule_paths();
run_processes_parallel(max_parallel_jobs,
get_next_submodule,
if (old && !is_submodule_populated_gently(path, error_code_ptr))
return 0;
- sub = submodule_from_path(null_sha1, path);
+ sub = submodule_from_path(&null_oid, path);
if (!sub)
die("BUG: could not get submodule information for '%s'", path);
return 0;
}
-int parallel_submodules(void)
-{
- return parallel_jobs;
-}
-
/*
* Embeds a single submodules git directory into the superprojects git dir,
* non recursively.
real_old_git_dir = real_pathdup(old_git_dir, 1);
- sub = submodule_from_path(null_sha1, path);
+ sub = submodule_from_path(&null_oid, path);
if (!sub)
die(_("could not lookup name for submodule '%s'"), path);
* superproject did not rewrite the git file links yet,
* fix it now.
*/
- sub = submodule_from_path(null_sha1, path);
+ sub = submodule_from_path(&null_oid, path);
if (!sub)
die(_("could not lookup name for submodule '%s'"), path);
connect_work_tree_and_git_dir(path,
}
if (!is_git_directory(buf->buf)) {
gitmodules_config();
- sub = submodule_from_path(null_sha1, submodule);
+ sub = submodule_from_path(&null_oid, submodule);
if (!sub) {
ret = -1;
goto cleanup;
};
#define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL}
-extern int is_staging_gitmodules_ok(void);
+extern int is_gitmodules_unmerged(const struct index_state *istate);
+extern int is_staging_gitmodules_ok(const struct index_state *istate);
extern int update_path_in_gitmodules(const char *oldpath, const char *newpath);
extern int remove_path_from_gitmodules(const char *path);
extern void stage_updated_gitmodules(void);
void load_submodule_cache(void);
extern void gitmodules_config(void);
extern void repo_read_gitmodules(struct repository *repo);
-extern void gitmodules_config_sha1(const unsigned char *commit_sha1);
+extern void gitmodules_config_oid(const struct object_id *commit_oid);
extern int is_submodule_active(struct repository *repo, const char *path);
/*
* Determine if a submodule has been populated at a given 'path' by checking if
struct submodule_update_strategy *dst);
extern const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);
extern void handle_ignore_submodules_arg(struct diff_options *, const char *);
-extern void show_submodule_summary(FILE *f, const char *path,
- const char *line_prefix,
+extern void show_submodule_summary(struct diff_options *o, const char *path,
struct object_id *one, struct object_id *two,
- unsigned dirty_submodule, const char *meta,
- const char *del, const char *add, const char *reset);
-extern void show_submodule_inline_diff(FILE *f, const char *path,
- const char *line_prefix,
+ unsigned dirty_submodule);
+extern void show_submodule_inline_diff(struct diff_options *o, const char *path,
struct object_id *one, struct object_id *two,
- unsigned dirty_submodule, const char *meta,
- const char *del, const char *add, const char *reset,
- const struct diff_options *opt);
-extern void set_config_fetch_recurse_submodules(int value);
+ unsigned dirty_submodule);
/* Check if we want to update any submodule.*/
extern int should_update_submodules(void);
/*
extern void check_for_new_submodule_commits(struct object_id *oid);
extern int fetch_populated_submodules(const struct argv_array *options,
const char *prefix, int command_line_option,
+ int default_option,
int quiet, int max_parallel_jobs);
extern unsigned is_submodule_modified(const char *path, int ignore_untracked);
extern int submodule_uses_gitfile(const char *path);
const struct string_list *push_options,
int dry_run);
extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
-extern int parallel_submodules(void);
/*
* Given a submodule path (as in the index), return the repository
* path of that submodule in 'buf'. Return -1 on error or when the
return e->key + strlen(e->key) + 1;
}
-static int test_entry_cmp(const void *unused_cmp_data,
- const struct test_entry *e1,
- const struct test_entry *e2,
- const char* key)
+static int test_entry_cmp(const void *cmp_data,
+ const void *entry,
+ const void *entry_or_key,
+ const void *keydata)
{
- return strcmp(e1->key, key ? key : e2->key);
-}
-
-static int test_entry_cmp_icase(const void *unused_cmp_data,
- const struct test_entry *e1,
- const struct test_entry *e2,
- const char* key)
-{
- return strcasecmp(e1->key, key ? key : e2->key);
+ const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
+ const struct test_entry *e1 = entry;
+ const struct test_entry *e2 = entry_or_key;
+ const char *key = keydata;
+
+ if (ignore_case)
+ return strcasecmp(e1->key, key ? key : e2->key);
+ else
+ return strcmp(e1->key, key ? key : e2->key);
}
static struct test_entry *alloc_test_entry(int hash, char *key, int klen,
if (method & TEST_ADD) {
/* test adding to the map */
for (j = 0; j < rounds; j++) {
- hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
- NULL, 0);
+ hashmap_init(&map, test_entry_cmp, NULL, 0);
/* add entries */
for (i = 0; i < TEST_SIZE; i++) {
}
} else {
/* test map lookups */
- hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
+ hashmap_init(&map, test_entry_cmp, NULL, 0);
/* fill the map (sparsely if specified) */
j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
/* init hash map */
icase = argc > 1 && !strcmp("ignorecase", argv[1]);
- hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
- : test_entry_cmp), NULL, 0);
+ hashmap_init(&map, test_entry_cmp, &icase, 0);
/* process commands from stdin */
while (fgets(line, sizeof(line), stdin)) {
const char *alternative; /* output: ... or this. */
};
+/*
+ * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
+ * have const parameters.
+ */
+static char *posix_basename(char *path)
+{
+ return basename(path);
+}
+
+static char *posix_dirname(char *path)
+{
+ return dirname(path);
+}
+
static int test_function(struct test_data *data, char *(*func)(char *input),
const char *funcname)
{
}
if (argc == 2 && !strcmp(argv[1], "basename"))
- return test_function(basename_data, basename, argv[1]);
+ return test_function(basename_data, posix_basename, argv[1]);
if (argc == 2 && !strcmp(argv[1], "dirname"))
- return test_function(dirname_data, dirname, argv[1]);
+ return test_function(dirname_data, posix_dirname, argv[1]);
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
argv[1] ? argv[1] : "(there was none)");
git_config(git_test_config, NULL);
while (*arg) {
- unsigned char commit_sha1[20];
+ struct object_id commit_oid;
const struct submodule *submodule;
const char *commit;
const char *path_or_name;
path_or_name = arg[1];
if (commit[0] == '\0')
- hashclr(commit_sha1);
- else if (get_sha1(commit, commit_sha1) < 0)
+ oidclr(&commit_oid);
+ else if (get_oid(commit, &commit_oid) < 0)
die_usage(argc, argv, "Commit not found.");
if (lookup_name) {
- submodule = submodule_from_name(commit_sha1, path_or_name);
+ submodule = submodule_from_name(&commit_oid, path_or_name);
} else
- submodule = submodule_from_path(commit_sha1, path_or_name);
+ submodule = submodule_from_path(&commit_oid, path_or_name);
if (!submodule)
die_usage(argc, argv, "Submodule not found.");
chmod 0700 ./gpghome &&
GNUPGHOME="$(pwd)/gpghome" &&
export GNUPGHOME &&
+ (gpgconf --kill gpg-agent 2>&1 >/dev/null || : ) &&
gpg --homedir "${GNUPGHOME}" 2>/dev/null --import \
"$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
gpg --homedir "${GNUPGHOME}" 2>/dev/null --import-ownertrust \
test_path_is_dir realgitdir/refs
'
-test_expect_success 'init in long base path' '
+test_lazy_prereq GETCWD_IGNORES_PERMS '
+ base=GETCWD_TEST_BASE_DIR &&
+ mkdir -p $base/dir &&
+ chmod 100 $base ||
+ error "bug in test script: cannot prepare $base"
+
+ (cd $base/dir && /bin/pwd -P)
+ status=$?
+
+ chmod 700 $base &&
+ rm -rf $base ||
+ error "bug in test script: cannot clean $base"
+ return $status
+'
+
+check_long_base_path () {
# exceed initial buffer size of strbuf_getcwd()
component=123456789abcdef &&
test_when_finished "chmod 0700 $component; rm -rf $component" &&
p31=$component/$component &&
p127=$p31/$p31/$p31/$p31 &&
mkdir -p $p127 &&
- chmod 0111 $component &&
+ if test $# = 1
+ then
+ chmod $1 $component
+ fi &&
(
cd $p127 &&
git init newdir
)
+}
+
+test_expect_success 'init in long base path' '
+ check_long_base_path
+'
+
+test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' '
+ check_long_base_path 0111
'
test_expect_success 're-init on .git file' '
}
filter_git () {
- rm -f rot13-filter.log &&
+ rm -f *.log &&
git "$@"
}
for FILE in "$expect" "$actual"
do
sort "$FILE" | uniq -c |
- sed -e "s/^ *[0-9][0-9]*[ ]*IN: /x IN: /" >"$FILE.tmp" &&
- mv "$FILE.tmp" "$FILE" || return
+ sed -e "s/^ *[0-9][0-9]*[ ]*IN: /x IN: /" >"$FILE.tmp"
done &&
- test_cmp "$expect" "$actual"
+ test_cmp "$expect.tmp" "$actual.tmp" &&
+ rm "$expect.tmp" "$actual.tmp"
}
# Compare two files but exclude all `clean` invocations because Git can
actual=$2
for FILE in "$expect" "$actual"
do
- grep -v "IN: clean" "$FILE" >"$FILE.tmp" &&
- mv "$FILE.tmp" "$FILE"
+ grep -v "IN: clean" "$FILE" >"$FILE.tmp"
done &&
- test_cmp "$expect" "$actual"
+ test_cmp "$expect.tmp" "$actual.tmp" &&
+ rm "$expect.tmp" "$actual.tmp"
}
# Check that the contents of two files are equal and that their rot13 version
'
test_expect_success PERL 'required process filter should filter data' '
- test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
test_config_global filter.protocol.required true &&
rm -rf repo &&
mkdir repo &&
IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
STOP
EOF
- test_cmp_count expected.log rot13-filter.log &&
+ test_cmp_count expected.log debug.log &&
git commit -m "test commit 2" &&
rm -f test2.r "testsubdir/test3 '\''sq'\'',\$x=.r" &&
IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log &&
+ test_cmp_exclude_clean expected.log debug.log &&
filter_git checkout --quiet --no-progress empty-branch &&
cat >expected.log <<-EOF &&
IN: clean test.r $S [OK] -- OUT: $S . [OK]
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log &&
+ test_cmp_exclude_clean expected.log debug.log &&
filter_git checkout --quiet --no-progress master &&
cat >expected.log <<-EOF &&
IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log &&
+ test_cmp_exclude_clean expected.log debug.log &&
test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
test_expect_success PERL 'required process filter takes precedence' '
test_config_global filter.protocol.clean false &&
- test_config_global filter.protocol.process "rot13-filter.pl clean" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean" &&
test_config_global filter.protocol.required true &&
rm -rf repo &&
mkdir repo &&
IN: clean test.r $S [OK] -- OUT: $S . [OK]
STOP
EOF
- test_cmp_count expected.log rot13-filter.log
+ test_cmp_count expected.log debug.log
)
'
test_expect_success PERL 'required process filter should be used only for "clean" operation only' '
- test_config_global filter.protocol.process "rot13-filter.pl clean" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean" &&
rm -rf repo &&
mkdir repo &&
(
IN: clean test.r $S [OK] -- OUT: $S . [OK]
STOP
EOF
- test_cmp_count expected.log rot13-filter.log &&
+ test_cmp_count expected.log debug.log &&
rm test.r &&
init handshake complete
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log
+ test_cmp_exclude_clean expected.log debug.log
)
'
test_expect_success PERL 'required process filter should process multiple packets' '
- test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
test_config_global filter.protocol.required true &&
rm -rf repo &&
IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
STOP
EOF
- test_cmp_count expected.log rot13-filter.log &&
+ test_cmp_count expected.log debug.log &&
rm -f *.file &&
IN: smudge 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log &&
+ test_cmp_exclude_clean expected.log debug.log &&
for FILE in *.file
do
'
test_expect_success PERL 'required process filter with clean error should fail' '
- test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
test_config_global filter.protocol.required true &&
rm -rf repo &&
mkdir repo &&
'
test_expect_success PERL 'process filter should restart after unexpected write failure' '
- test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
rm -rf repo &&
mkdir repo &&
(
git add . &&
rm -f *.r &&
- rm -f rot13-filter.log &&
+ rm -f debug.log &&
git checkout --quiet --no-progress . 2>git-stderr.log &&
grep "smudge write error at" git-stderr.log &&
cat >expected.log <<-EOF &&
START
init handshake complete
- IN: smudge smudge-write-fail.r $SF [OK] -- OUT: $SF [WRITE FAIL]
+ IN: smudge smudge-write-fail.r $SF [OK] -- [WRITE FAIL]
START
init handshake complete
IN: smudge test.r $S [OK] -- OUT: $S . [OK]
IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log &&
+ test_cmp_exclude_clean expected.log debug.log &&
test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
'
test_expect_success PERL 'process filter should not be restarted if it signals an error' '
- test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
rm -rf repo &&
mkdir repo &&
(
cat >expected.log <<-EOF &&
START
init handshake complete
- IN: smudge error.r $SE [OK] -- OUT: 0 [ERROR]
+ IN: smudge error.r $SE [OK] -- [ERROR]
IN: smudge test.r $S [OK] -- OUT: $S . [OK]
IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log &&
+ test_cmp_exclude_clean expected.log debug.log &&
test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
'
test_expect_success PERL 'process filter abort stops processing of all further files' '
- test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
+ test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
rm -rf repo &&
mkdir repo &&
(
cat >expected.log <<-EOF &&
START
init handshake complete
- IN: smudge abort.r $SA [OK] -- OUT: 0 [ABORT]
+ IN: smudge abort.r $SA [OK] -- [ABORT]
STOP
EOF
- test_cmp_exclude_clean expected.log rot13-filter.log &&
+ test_cmp_exclude_clean expected.log debug.log &&
test_cmp "$TEST_ROOT/test.o" test.r &&
test_cmp "$TEST_ROOT/test2.o" test2.r &&
cp "$TEST_ROOT/test.o" test.r &&
test_must_fail git add . 2>git-stderr.log &&
- grep "does not support filter protocol version" git-stderr.log
+ grep "expected git-filter-server" git-stderr.log
)
'
+test_expect_success PERL 'delayed checkout in process filter' '
+ test_config_global filter.a.process "rot13-filter.pl a.log clean smudge delay" &&
+ test_config_global filter.a.required true &&
+ test_config_global filter.b.process "rot13-filter.pl b.log clean smudge delay" &&
+ test_config_global filter.b.required true &&
+
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo "*.a filter=a" >.gitattributes &&
+ echo "*.b filter=b" >>.gitattributes &&
+ cp "$TEST_ROOT/test.o" test.a &&
+ cp "$TEST_ROOT/test.o" test-delay10.a &&
+ cp "$TEST_ROOT/test.o" test-delay11.a &&
+ cp "$TEST_ROOT/test.o" test-delay20.a &&
+ cp "$TEST_ROOT/test.o" test-delay10.b &&
+ git add . &&
+ git commit -m "test commit"
+ ) &&
+
+ S=$(file_size "$TEST_ROOT/test.o") &&
+ cat >a.exp <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.a $S [OK] -- OUT: $S . [OK]
+ IN: smudge test-delay10.a $S [OK] -- [DELAYED]
+ IN: smudge test-delay11.a $S [OK] -- [DELAYED]
+ IN: smudge test-delay20.a $S [OK] -- [DELAYED]
+ IN: list_available_blobs test-delay10.a test-delay11.a [OK]
+ IN: smudge test-delay10.a 0 [OK] -- OUT: $S . [OK]
+ IN: smudge test-delay11.a 0 [OK] -- OUT: $S . [OK]
+ IN: list_available_blobs test-delay20.a [OK]
+ IN: smudge test-delay20.a 0 [OK] -- OUT: $S . [OK]
+ IN: list_available_blobs [OK]
+ STOP
+ EOF
+ cat >b.exp <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test-delay10.b $S [OK] -- [DELAYED]
+ IN: list_available_blobs test-delay10.b [OK]
+ IN: smudge test-delay10.b 0 [OK] -- OUT: $S . [OK]
+ IN: list_available_blobs [OK]
+ STOP
+ EOF
+
+ rm -rf repo-cloned &&
+ filter_git clone repo repo-cloned &&
+ test_cmp_count a.exp repo-cloned/a.log &&
+ test_cmp_count b.exp repo-cloned/b.log &&
+
+ (
+ cd repo-cloned &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b &&
+
+ rm *.a *.b &&
+ filter_git checkout . &&
+ test_cmp_count ../a.exp a.log &&
+ test_cmp_count ../b.exp b.log &&
+
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b
+ )
+'
+
+test_expect_success PERL 'missing file in delayed checkout' '
+ test_config_global filter.bug.process "rot13-filter.pl bug.log clean smudge delay" &&
+ test_config_global filter.bug.required true &&
+
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo "*.a filter=bug" >.gitattributes &&
+ cp "$TEST_ROOT/test.o" missing-delay.a
+ git add . &&
+ git commit -m "test commit"
+ ) &&
+
+ rm -rf repo-cloned &&
+ test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
+ cat git-stderr.log &&
+ grep "error: .missing-delay\.a. was not filtered properly" git-stderr.log
+'
+
+test_expect_success PERL 'invalid file in delayed checkout' '
+ test_config_global filter.bug.process "rot13-filter.pl bug.log clean smudge delay" &&
+ test_config_global filter.bug.required true &&
+
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo "*.a filter=bug" >.gitattributes &&
+ cp "$TEST_ROOT/test.o" invalid-delay.a &&
+ cp "$TEST_ROOT/test.o" unfiltered
+ git add . &&
+ git commit -m "test commit"
+ ) &&
+
+ rm -rf repo-cloned &&
+ test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
+ grep "error: external filter .* signaled that .unfiltered. is now available although it has not been delayed earlier" git-stderr.log
+'
+
test_done
# Example implementation for the Git filter protocol version 2
# See Documentation/gitattributes.txt, section "Filter Protocol"
#
-# The script takes the list of supported protocol capabilities as
-# arguments ("clean", "smudge", etc).
+# The first argument defines a debug log file that the script write to.
+# All remaining arguments define a list of supported protocol
+# capabilities ("clean", "smudge", etc).
#
# This implementation supports special test cases:
# (1) If data with the pathname "clean-write-fail.r" is processed with
# operation then the filter signals that it cannot or does not want
# to process the file and any file after that is processed with the
# same command.
+# (5) If data with a pathname that is a key in the DELAY hash is
+# requested (e.g. "test-delay10.a") then the filter responds with
+# a "delay" status and sets the "requested" field in the DELAY hash.
+# The filter will signal the availability of this object after
+# "count" (field in DELAY hash) "list_available_blobs" commands.
+# (6) If data with the pathname "missing-delay.a" is processed that the
+# filter will drop the path from the "list_available_blobs" response.
+# (7) If data with the pathname "invalid-delay.a" is processed that the
+# filter will add the path "unfiltered" which was not delayed before
+# to the "list_available_blobs" response.
#
use strict;
use IO::File;
my $MAX_PACKET_CONTENT_SIZE = 65516;
+my $log_file = shift @ARGV;
my @capabilities = @ARGV;
-open my $debug, ">>", "rot13-filter.log" or die "cannot open log file: $!";
+open my $debug, ">>", $log_file or die "cannot open log file: $!";
+
+my %DELAY = (
+ 'test-delay10.a' => { "requested" => 0, "count" => 1 },
+ 'test-delay11.a' => { "requested" => 0, "count" => 1 },
+ 'test-delay20.a' => { "requested" => 0, "count" => 2 },
+ 'test-delay10.b' => { "requested" => 0, "count" => 1 },
+ 'missing-delay.a' => { "requested" => 0, "count" => 1 },
+ 'invalid-delay.a' => { "requested" => 0, "count" => 1 },
+);
sub rot13 {
my $str = shift;
sub packet_txt_read {
my ( $res, $buf ) = packet_bin_read();
- unless ( $buf =~ s/\n$// ) {
+ unless ( $buf eq '' or $buf =~ s/\n$// ) {
die "A non-binary line MUST be terminated by an LF.";
}
return ( $res, $buf );
( packet_txt_read() eq ( 0, "capability=clean" ) ) || die "bad capability";
( packet_txt_read() eq ( 0, "capability=smudge" ) ) || die "bad capability";
+( packet_txt_read() eq ( 0, "capability=delay" ) ) || die "bad capability";
( packet_bin_read() eq ( 1, "" ) ) || die "bad capability end";
foreach (@capabilities) {
$debug->flush();
while (1) {
- my ($command) = packet_txt_read() =~ /^command=(.+)$/;
+ my ( $command ) = packet_txt_read() =~ /^command=(.+)$/;
print $debug "IN: $command";
$debug->flush();
- my ($pathname) = packet_txt_read() =~ /^pathname=(.+)$/;
- print $debug " $pathname";
- $debug->flush();
-
- if ( $pathname eq "" ) {
- die "bad pathname '$pathname'";
- }
+ if ( $command eq "list_available_blobs" ) {
+ # Flush
+ packet_bin_read();
- # Flush
- packet_bin_read();
-
- my $input = "";
- {
- binmode(STDIN);
- my $buffer;
- my $done = 0;
- while ( !$done ) {
- ( $done, $buffer ) = packet_bin_read();
- $input .= $buffer;
+ foreach my $pathname ( sort keys %DELAY ) {
+ if ( $DELAY{$pathname}{"requested"} >= 1 ) {
+ $DELAY{$pathname}{"count"} = $DELAY{$pathname}{"count"} - 1;
+ if ( $pathname eq "invalid-delay.a" ) {
+ # Send Git a pathname that was not delayed earlier
+ packet_txt_write("pathname=unfiltered");
+ }
+ if ( $pathname eq "missing-delay.a" ) {
+ # Do not signal Git that this file is available
+ } elsif ( $DELAY{$pathname}{"count"} == 0 ) {
+ print $debug " $pathname";
+ packet_txt_write("pathname=$pathname");
+ }
+ }
}
- print $debug " " . length($input) . " [OK] -- ";
- $debug->flush();
- }
-
- my $output;
- if ( $pathname eq "error.r" or $pathname eq "abort.r" ) {
- $output = "";
- }
- elsif ( $command eq "clean" and grep( /^clean$/, @capabilities ) ) {
- $output = rot13($input);
- }
- elsif ( $command eq "smudge" and grep( /^smudge$/, @capabilities ) ) {
- $output = rot13($input);
- }
- else {
- die "bad command '$command'";
- }
- print $debug "OUT: " . length($output) . " ";
- $debug->flush();
-
- if ( $pathname eq "error.r" ) {
- print $debug "[ERROR]\n";
- $debug->flush();
- packet_txt_write("status=error");
packet_flush();
- }
- elsif ( $pathname eq "abort.r" ) {
- print $debug "[ABORT]\n";
+
+ print $debug " [OK]\n";
$debug->flush();
- packet_txt_write("status=abort");
+ packet_txt_write("status=success");
packet_flush();
}
else {
- packet_txt_write("status=success");
- packet_flush();
+ my ( $pathname ) = packet_txt_read() =~ /^pathname=(.+)$/;
+ print $debug " $pathname";
+ $debug->flush();
- if ( $pathname eq "${command}-write-fail.r" ) {
- print $debug "[WRITE FAIL]\n";
+ if ( $pathname eq "" ) {
+ die "bad pathname '$pathname'";
+ }
+
+ # Read until flush
+ my ( $done, $buffer ) = packet_txt_read();
+ while ( $buffer ne '' ) {
+ if ( $buffer eq "can-delay=1" ) {
+ if ( exists $DELAY{$pathname} and $DELAY{$pathname}{"requested"} == 0 ) {
+ $DELAY{$pathname}{"requested"} = 1;
+ }
+ } else {
+ die "Unknown message '$buffer'";
+ }
+
+ ( $done, $buffer ) = packet_txt_read();
+ }
+
+ my $input = "";
+ {
+ binmode(STDIN);
+ my $buffer;
+ my $done = 0;
+ while ( !$done ) {
+ ( $done, $buffer ) = packet_bin_read();
+ $input .= $buffer;
+ }
+ print $debug " " . length($input) . " [OK] -- ";
$debug->flush();
- die "${command} write error";
}
- while ( length($output) > 0 ) {
- my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );
- packet_bin_write($packet);
- # dots represent the number of packets
- print $debug ".";
- if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {
- $output = substr( $output, $MAX_PACKET_CONTENT_SIZE );
+ my $output;
+ if ( exists $DELAY{$pathname} and exists $DELAY{$pathname}{"output"} ) {
+ $output = $DELAY{$pathname}{"output"}
+ }
+ elsif ( $pathname eq "error.r" or $pathname eq "abort.r" ) {
+ $output = "";
+ }
+ elsif ( $command eq "clean" and grep( /^clean$/, @capabilities ) ) {
+ $output = rot13($input);
+ }
+ elsif ( $command eq "smudge" and grep( /^smudge$/, @capabilities ) ) {
+ $output = rot13($input);
+ }
+ else {
+ die "bad command '$command'";
+ }
+
+ if ( $pathname eq "error.r" ) {
+ print $debug "[ERROR]\n";
+ $debug->flush();
+ packet_txt_write("status=error");
+ packet_flush();
+ }
+ elsif ( $pathname eq "abort.r" ) {
+ print $debug "[ABORT]\n";
+ $debug->flush();
+ packet_txt_write("status=abort");
+ packet_flush();
+ }
+ elsif ( $command eq "smudge" and
+ exists $DELAY{$pathname} and
+ $DELAY{$pathname}{"requested"} == 1
+ ) {
+ print $debug "[DELAYED]\n";
+ $debug->flush();
+ packet_txt_write("status=delayed");
+ packet_flush();
+ $DELAY{$pathname}{"requested"} = 2;
+ $DELAY{$pathname}{"output"} = $output;
+ }
+ else {
+ packet_txt_write("status=success");
+ packet_flush();
+
+ if ( $pathname eq "${command}-write-fail.r" ) {
+ print $debug "[WRITE FAIL]\n";
+ $debug->flush();
+ die "${command} write error";
}
- else {
- $output = "";
+
+ print $debug "OUT: " . length($output) . " ";
+ $debug->flush();
+
+ while ( length($output) > 0 ) {
+ my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );
+ packet_bin_write($packet);
+ # dots represent the number of packets
+ print $debug ".";
+ if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {
+ $output = substr( $output, $MAX_PACKET_CONTENT_SIZE );
+ }
+ else {
+ $output = "";
+ }
}
+ packet_flush();
+ print $debug " [OK]\n";
+ $debug->flush();
+ packet_flush();
}
- packet_flush();
- print $debug " [OK]\n";
- $debug->flush();
- packet_flush();
}
}
echo >.gitattributes &&
git checkout -b master &&
git add .gitattributes &&
- git commit -m "add .gitattributes" "" &&
+ git commit -m "add .gitattributes" . &&
printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\nLINETWO\nLINETHREE" >LF &&
printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\r\nLINETHREE" >CRLF &&
printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\r\nLINETWO\nLINETHREE" >CRLF_mix_LF &&
+++ /dev/null
-#!/bin/sh
-#
-# Copyright (c) 2005 Johannes Schindelin
-#
-
-test_description='A simple turial in the form of a test case'
-
-. ./test-lib.sh
-
-test_expect_success 'blob' '
- echo "Hello World" > hello &&
- echo "Silly example" > example &&
-
- git update-index --add hello example &&
-
- test blob = "$(git cat-file -t 557db03)"
-'
-
-test_expect_success 'blob 557db03' '
- test "Hello World" = "$(git cat-file blob 557db03)"
-'
-
-echo "It's a new day for git" >>hello
-cat > diff.expect << EOF
-diff --git a/hello b/hello
-index 557db03..263414f 100644
---- a/hello
-+++ b/hello
-@@ -1 +1,2 @@
- Hello World
-+It's a new day for git
-EOF
-
-test_expect_success 'git diff-files -p' '
- git diff-files -p > diff.output &&
- test_cmp diff.expect diff.output
-'
-
-test_expect_success 'git diff' '
- git diff > diff.output &&
- test_cmp diff.expect diff.output
-'
-
-test_expect_success 'tree' '
- tree=$(git write-tree 2>/dev/null) &&
- test 8988da15d077d4829fc51d8544c097def6644dbb = $tree
-'
-
-test_expect_success 'git diff-index -p HEAD' '
- test_tick &&
- tree=$(git write-tree) &&
- commit=$(echo "Initial commit" | git commit-tree $tree) &&
- git update-ref HEAD $commit &&
- git diff-index -p HEAD > diff.output &&
- test_cmp diff.expect diff.output
-'
-
-test_expect_success 'git diff HEAD' '
- git diff HEAD > diff.output &&
- test_cmp diff.expect diff.output
-'
-
-cat > whatchanged.expect << EOF
-commit VARIABLE
-Author: VARIABLE
-Date: VARIABLE
-
- Initial commit
-
-diff --git a/example b/example
-new file mode 100644
-index 0000000..f24c74a
---- /dev/null
-+++ b/example
-@@ -0,0 +1 @@
-+Silly example
-diff --git a/hello b/hello
-new file mode 100644
-index 0000000..557db03
---- /dev/null
-+++ b/hello
-@@ -0,0 +1 @@
-+Hello World
-EOF
-
-test_expect_success 'git whatchanged -p --root' '
- git whatchanged -p --root |
- sed -e "1s/^\(.\{7\}\).\{40\}/\1VARIABLE/" \
- -e "2,3s/^\(.\{8\}\).*$/\1VARIABLE/" \
- > whatchanged.output &&
- test_cmp whatchanged.expect whatchanged.output
-'
-
-test_expect_success 'git tag my-first-tag' '
- git tag my-first-tag &&
- test_cmp .git/refs/heads/master .git/refs/tags/my-first-tag
-'
-
-test_expect_success 'git checkout -b mybranch' '
- git checkout -b mybranch &&
- test_cmp .git/refs/heads/master .git/refs/heads/mybranch
-'
-
-cat > branch.expect <<EOF
- master
-* mybranch
-EOF
-
-test_expect_success 'git branch' '
- git branch > branch.output &&
- test_cmp branch.expect branch.output
-'
-
-test_expect_success 'git resolve now fails' '
- git checkout mybranch &&
- echo "Work, work, work" >>hello &&
- test_tick &&
- git commit -m "Some work." -i hello &&
-
- git checkout master &&
-
- echo "Play, play, play" >>hello &&
- echo "Lots of fun" >>example &&
- test_tick &&
- git commit -m "Some fun." -i hello example &&
-
- test_must_fail git merge -m "Merge work in mybranch" mybranch
-'
-
-cat > hello << EOF
-Hello World
-It's a new day for git
-Play, play, play
-Work, work, work
-EOF
-
-cat > show-branch.expect << EOF
-* [master] Merge work in mybranch
- ! [mybranch] Some work.
---
-- [master] Merge work in mybranch
-*+ [mybranch] Some work.
-* [master^] Some fun.
-EOF
-
-test_expect_success 'git show-branch' '
- test_tick &&
- git commit -m "Merge work in mybranch" -i hello &&
- git show-branch --topo-order --more=1 master mybranch \
- > show-branch.output &&
- test_cmp show-branch.expect show-branch.output
-'
-
-cat > resolve.expect << EOF
-Updating VARIABLE..VARIABLE
-FASTFORWARD (no commit created; -m option ignored)
- example | 1 +
- hello | 1 +
- 2 files changed, 2 insertions(+)
-EOF
-
-test_expect_success 'git resolve' '
- git checkout mybranch &&
- git merge -m "Merge upstream changes." master |
- sed -e "1s/[0-9a-f]\{7\}/VARIABLE/g" \
- -e "s/^Fast[- ]forward /FASTFORWARD /" >resolve.output
-'
-
-test_expect_success 'git resolve output' '
- test_i18ncmp resolve.expect resolve.output
-'
-
-cat > show-branch2.expect << EOF
-! [master] Merge work in mybranch
- * [mybranch] Merge work in mybranch
---
--- [master] Merge work in mybranch
-EOF
-
-test_expect_success 'git show-branch (part 2)' '
- git show-branch --topo-order master mybranch > show-branch2.output &&
- test_cmp show-branch2.expect show-branch2.output
-'
-
-cat > show-branch3.expect << EOF
-! [master] Merge work in mybranch
- * [mybranch] Merge work in mybranch
---
--- [master] Merge work in mybranch
-+* [master^2] Some work.
-+* [master^] Some fun.
-EOF
-
-test_expect_success 'git show-branch (part 3)' '
- git show-branch --topo-order --more=2 master mybranch \
- > show-branch3.output &&
- test_cmp show-branch3.expect show-branch3.output
-'
-
-test_expect_success 'rewind to "Some fun." and "Some work."' '
- git checkout mybranch &&
- git reset --hard master^2 &&
- git checkout master &&
- git reset --hard master^
-'
-
-cat > show-branch4.expect << EOF
-* [master] Some fun.
- ! [mybranch] Some work.
---
-* [master] Some fun.
- + [mybranch] Some work.
-*+ [master^] Initial commit
-EOF
-
-test_expect_success 'git show-branch (part 4)' '
- git show-branch --topo-order > show-branch4.output &&
- test_cmp show-branch4.expect show-branch4.output
-'
-
-test_expect_success 'manual merge' '
- mb=$(git merge-base HEAD mybranch) &&
- git name-rev --name-only --tags $mb > name-rev.output &&
- test "my-first-tag" = $(cat name-rev.output) &&
-
- git read-tree -m -u $mb HEAD mybranch
-'
-
-cat > ls-files.expect << EOF
-100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
-100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
-100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
-100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
-EOF
-
-test_expect_success 'git ls-files --stage' '
- git ls-files --stage > ls-files.output &&
- test_cmp ls-files.expect ls-files.output
-'
-
-cat > ls-files-unmerged.expect << EOF
-100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
-100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
-100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
-EOF
-
-test_expect_success 'git ls-files --unmerged' '
- git ls-files --unmerged > ls-files-unmerged.output &&
- test_cmp ls-files-unmerged.expect ls-files-unmerged.output
-'
-
-test_expect_success 'git-merge-index' '
- test_must_fail git merge-index git-merge-one-file hello
-'
-
-test_expect_success 'git ls-files --stage (part 2)' '
- git ls-files --stage > ls-files.output2 &&
- test_cmp ls-files.expect ls-files.output2
-'
-
-test_expect_success 'git repack' 'git repack'
-test_expect_success 'git prune-packed' 'git prune-packed'
-test_expect_success '-> only packed objects' '
- git prune && # Remove conflict marked blobs
- test $(find .git/objects/[0-9a-f][0-9a-f] -type f -print 2>/dev/null | wc -l) = 0
-'
-
-test_done
--- /dev/null
+#!/bin/sh
+
+test_description='packed-refs entries are covered by loose refs'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_tick &&
+ git commit --allow-empty -m one &&
+ one=$(git rev-parse HEAD) &&
+ git for-each-ref >actual &&
+ echo "$one commit refs/heads/master" >expect &&
+ test_cmp expect actual &&
+
+ git pack-refs --all &&
+ git for-each-ref >actual &&
+ echo "$one commit refs/heads/master" >expect &&
+ test_cmp expect actual &&
+
+ git checkout --orphan another &&
+ test_tick &&
+ git commit --allow-empty -m two &&
+ two=$(git rev-parse HEAD) &&
+ git checkout -B master &&
+ git branch -D another &&
+
+ git for-each-ref >actual &&
+ echo "$two commit refs/heads/master" >expect &&
+ test_cmp expect actual &&
+
+ git reflog expire --expire=now --all &&
+ git prune &&
+ git tag -m v1.0 v1.0 master
+'
+
+test_expect_success 'no error from stale entry in packed-refs' '
+ git describe master >actual 2>&1 &&
+ echo "v1.0" >expect &&
+ test_cmp expect actual
+'
+
+test_done
! git reflog exists refs/heads/nonexistent
'
-# The behavior with two reflogs is buggy and the output is in flux; for now
-# we're just checking that the program works at all without segfaulting.
-test_expect_success 'showing multiple reflogs works' '
- git log -g HEAD HEAD >actual
-'
-
-test_expect_success 'showing multiple reflogs with an old date' '
- git log -g HEAD@{1979-01-01} HEAD >actual
-'
-
test_done
--- /dev/null
+#!/bin/sh
+
+test_description='various tests of reflog walk (log -g) behavior'
+. ./test-lib.sh
+
+test_expect_success 'set up some reflog entries' '
+ test_commit one &&
+ test_commit two &&
+ git checkout -b side HEAD^ &&
+ test_commit three &&
+ git merge --no-commit master &&
+ echo evil-merge-content >>one.t &&
+ test_tick &&
+ git commit --no-edit -a
+'
+
+do_walk () {
+ git log -g --format="%gd %gs" "$@"
+}
+
+sq="'"
+test_expect_success 'set up expected reflog' '
+ cat >expect.all <<-EOF
+ HEAD@{0} commit (merge): Merge branch ${sq}master${sq} into side
+ HEAD@{1} commit: three
+ HEAD@{2} checkout: moving from master to side
+ HEAD@{3} commit: two
+ HEAD@{4} commit (initial): one
+ EOF
+'
+
+test_expect_success 'reflog walk shows expected logs' '
+ do_walk >actual &&
+ test_cmp expect.all actual
+'
+
+test_expect_success 'reflog can limit with --no-merges' '
+ grep -v merge expect.all >expect &&
+ do_walk --no-merges >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reflog can limit with pathspecs' '
+ grep two expect.all >expect &&
+ do_walk -- two.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pathspec limiting handles merges' '
+ # we pick up:
+ # - the initial commit of one
+ # - the checkout back to commit one
+ # - the evil merge which touched one
+ sed -n "1p;3p;5p" expect.all >expect &&
+ do_walk -- one.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--parents shows true parents' '
+ # convert newlines to spaces
+ echo $(git rev-parse HEAD HEAD^1 HEAD^2) >expect &&
+ git rev-list -g --parents -1 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'walking multiple reflogs shows all' '
+ # We expect to see all entries for all reflogs, but interleaved by
+ # date, with order on the command line breaking ties. We
+ # can use "sort" on the separate lists to generate this,
+ # but note two tricks:
+ #
+ # 1. We use "{" as the delimiter, which lets us skip to the reflog
+ # date specifier as our second field, and then our "-n" numeric
+ # sort ignores the bits after the timestamp.
+ #
+ # 2. POSIX leaves undefined whether this is a stable sort or not. So
+ # we use "-k 1" to ensure that we see HEAD before master before
+ # side when breaking ties.
+ {
+ do_walk --date=unix HEAD &&
+ do_walk --date=unix side &&
+ do_walk --date=unix master
+ } >expect.raw &&
+ sort -t "{" -k 2nr -k 1 <expect.raw >expect &&
+ do_walk --date=unix HEAD master side >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'date-limiting does not interfere with other logs' '
+ do_walk HEAD@{1979-01-01} HEAD >actual &&
+ test_cmp expect.all actual
+'
+
+test_expect_success 'min/max age uses entry date to limit' '
+ # Flip between commits one and two so each ref update actually
+ # does something (and does not get optimized out). We know
+ # that the timestamps of those commits will be before our "min".
+
+ git update-ref -m before refs/heads/minmax one &&
+
+ test_tick &&
+ min=$test_tick &&
+ git update-ref -m min refs/heads/minmax two &&
+
+ test_tick &&
+ max=$test_tick &&
+ git update-ref -m max refs/heads/minmax one &&
+
+ test_tick &&
+ git update-ref -m after refs/heads/minmax two &&
+
+ cat >expect <<-\EOF &&
+ max
+ min
+ EOF
+ git log -g --since=$min --until=$max --format=%gs minmax >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'walk prefers reflog to ref tip' '
+ head=$(git rev-parse HEAD) &&
+ one=$(git rev-parse one) &&
+ ident="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" &&
+ echo "$head $one $ident broken reflog entry" >>.git/logs/HEAD &&
+
+ echo $one >expect &&
+ git log -g --format=%H -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list -g complains when there are no reflogs' '
+ test_must_fail git rev-list -g
+'
+
+test_done
! grep corrupt out
'
+test_expect_success 'fsck fails on corrupt packfile' '
+ hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
+ pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
+
+ # Corrupt the first byte of the first object. (It contains 3 type bits,
+ # at least one of which is not zero, so setting the first byte to 0 is
+ # sufficient.)
+ chmod a+w .git/objects/pack/pack-$pack.pack &&
+ printf '\0' | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
+
+ test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
+ remove_object $hsh &&
+ test_must_fail git fsck 2>out &&
+ test_i18ngrep "checksum mismatch" out
+'
+
test_expect_success 'fsck finds problems in duplicate loose objects' '
rm -rf broken-duplicate &&
git init broken-duplicate &&
oid=$(git rev-parse HEAD) &&
cat >expect <<-EOF &&
HEAD@{0} $oid $msg
- HEAD@{1} $oid $msg
HEAD@{2} $oid checkout: moving from foo to baz
EOF
- git log -g --format="%gd %H %gs" -3 HEAD >actual &&
+ git log -g --format="%gd %H %gs" -2 HEAD >actual &&
test_cmp expect actual
'
test_description='git branch display tests'
. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
test_expect_success 'make commits' '
echo content >file &&
test_i18ncmp expect actual
'
+test_expect_success "set up color tests" '
+ echo "<RED>master<RESET>" >expect.color &&
+ echo "master" >expect.bare &&
+ color_args="--format=%(color:red)%(refname:short) --list master"
+'
+
+test_expect_success '%(color) omitted without tty' '
+ TERM=vt100 git branch $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.bare actual
+'
+
+test_expect_success TTY '%(color) present with tty' '
+ test_terminal env TERM=vt100 git branch $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success 'color.branch=always overrides auto-color' '
+ git -c color.branch=always branch $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success '--color overrides auto-color' '
+ git branch --color $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
test_done
test_must_fail git branch foo/bar/baz/lots/of/extra/components
'
+test_expect_success 'reject packed-refs with unterminated line' '
+ cp .git/packed-refs .git/packed-refs.bak &&
+ test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+ printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
+ echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
+ test_must_fail git for-each-ref >out 2>err &&
+ test_cmp expected_err err
+'
+
+test_expect_success 'reject packed-refs containing junk' '
+ cp .git/packed-refs .git/packed-refs.bak &&
+ test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+ printf "%s\n" "bogus content" >>.git/packed-refs &&
+ echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
+ test_must_fail git for-each-ref >out 2>err &&
+ test_cmp expected_err err
+'
+
+test_expect_success 'reject packed-refs with a short SHA-1' '
+ cp .git/packed-refs .git/packed-refs.bak &&
+ test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+ printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
+ printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
+ test_must_fail git for-each-ref >out 2>err &&
+ test_cmp expected_err err
+'
+
test_expect_success 'timeout if packed-refs.lock exists' '
LOCK=.git/packed-refs.lock &&
>"$LOCK" &&
git -c core.packedrefstimeout=3000 pack-refs --all --prune
'
+test_expect_success SYMLINKS 'pack symlinked packed-refs' '
+ # First make sure that symlinking works when reading:
+ git update-ref refs/heads/loosy refs/heads/master &&
+ git for-each-ref >all-refs-before &&
+ mv .git/packed-refs .git/my-deviant-packed-refs &&
+ ln -s my-deviant-packed-refs .git/packed-refs &&
+ git for-each-ref >all-refs-linked &&
+ test_cmp all-refs-before all-refs-linked &&
+ git pack-refs --all --prune &&
+ git for-each-ref >all-refs-packed &&
+ test_cmp all-refs-before all-refs-packed &&
+ test -h .git/packed-refs &&
+ test "$(readlink .git/packed-refs)" = "my-deviant-packed-refs"
+'
+
test_done
git rebase --continue
'
-test_expect_success 'non-interactive rebase --continue with rerere enabled' '
- test_config rerere.enabled true &&
- test_when_finished "test_might_fail git rebase --abort" &&
- git reset --hard commit-new-file-F2-on-topic-branch &&
- git checkout master &&
- rm -fr .git/rebase-* &&
-
- test_must_fail git rebase --onto master master topic &&
- echo "Resolved" >F2 &&
- git add F2 &&
- cp F2 F2.expected &&
- git rebase --continue &&
-
- git reset --hard commit-new-file-F2-on-topic-branch &&
- git checkout master &&
- test_must_fail git rebase --onto master master topic &&
- test_cmp F2.expected F2
-'
-
test_expect_success 'rebase --continue can not be used with other options' '
test_must_fail git rebase -v --continue &&
test_must_fail git rebase --continue -v
test -f funny.was.run
'
-test_expect_success 'rebase --continue remembers --rerere-autoupdate' '
+test_expect_success 'setup rerere database' '
rm -fr .git/rebase-* &&
git reset --hard commit-new-file-F3-on-topic-branch &&
git checkout master &&
test_commit "commit-new-file-F3" F3 3 &&
- git config rerere.enabled true &&
+ test_config rerere.enabled true &&
test_must_fail git rebase -m master topic &&
echo "Resolved" >F2 &&
+ cp F2 expected-F2 &&
git add F2 &&
test_must_fail git rebase --continue &&
echo "Resolved" >F3 &&
+ cp F3 expected-F3 &&
git add F3 &&
git rebase --continue &&
- git reset --hard topic@{1} &&
- test_must_fail git rebase -m --rerere-autoupdate master &&
- test "$(cat F2)" = "Resolved" &&
- test_must_fail git rebase --continue &&
- test "$(cat F3)" = "Resolved" &&
- git rebase --continue
+ git reset --hard topic@{1}
'
+prepare () {
+ rm -fr .git/rebase-* &&
+ git reset --hard commit-new-file-F3-on-topic-branch &&
+ git checkout master &&
+ test_config rerere.enabled true
+}
+
+test_rerere_autoupdate () {
+ action=$1 &&
+ test_expect_success "rebase $action --continue remembers --rerere-autoupdate" '
+ prepare &&
+ test_must_fail git rebase $action --rerere-autoupdate master topic &&
+ test_cmp expected-F2 F2 &&
+ git diff-files --quiet &&
+ test_must_fail git rebase --continue &&
+ test_cmp expected-F3 F3 &&
+ git diff-files --quiet &&
+ git rebase --continue
+ '
+
+ test_expect_success "rebase $action --continue honors rerere.autoUpdate" '
+ prepare &&
+ test_config rerere.autoupdate true &&
+ test_must_fail git rebase $action master topic &&
+ test_cmp expected-F2 F2 &&
+ git diff-files --quiet &&
+ test_must_fail git rebase --continue &&
+ test_cmp expected-F3 F3 &&
+ git diff-files --quiet &&
+ git rebase --continue
+ '
+
+ test_expect_success "rebase $action --continue remembers --no-rerere-autoupdate" '
+ prepare &&
+ test_config rerere.autoupdate true &&
+ test_must_fail git rebase $action --no-rerere-autoupdate master topic &&
+ test_cmp expected-F2 F2 &&
+ test_must_fail git diff-files --quiet &&
+ git add F2 &&
+ test_must_fail git rebase --continue &&
+ test_cmp expected-F3 F3 &&
+ test_must_fail git diff-files --quiet &&
+ git add F3 &&
+ git rebase --continue
+ '
+}
+
+test_rerere_autoupdate
+test_rerere_autoupdate -m
+GIT_SEQUENCE_EDITOR=: && export GIT_SEQUENCE_EDITOR
+test_rerere_autoupdate -i
+test_rerere_autoupdate --preserve-merges
+
test_done
. ./test-lib.sh
test_expect_success setup '
- echo foo >foo &&
- git add foo && test_tick && git commit -q -m 1 &&
- echo foo-master >foo &&
- git add foo && test_tick && git commit -q -m 2 &&
-
- git checkout -b dev HEAD^ &&
- echo foo-dev >foo &&
- git add foo && test_tick && git commit -q -m 3 &&
+ test_commit foo &&
+ test_commit foo-master foo &&
+ test_commit bar-master bar &&
+
+ git checkout -b dev foo &&
+ test_commit foo-dev foo &&
+ test_commit bar-dev bar &&
git config rerere.enabled true
'
'
test_expect_success 'fixup' '
- echo foo-dev >foo &&
- git add foo && test_tick && git commit -q -m 4 &&
- git reset --hard HEAD^ &&
- echo foo-dev >expect
+ echo foo-resolved >foo &&
+ echo bar-resolved >bar &&
+ git commit -am resolved &&
+ cp foo foo-expect &&
+ cp bar bar-expect &&
+ git reset --hard HEAD^
'
-test_expect_success 'cherry-pick conflict' '
- test_must_fail git cherry-pick master &&
- test_cmp expect foo
+test_expect_success 'cherry-pick conflict with --rerere-autoupdate' '
+ test_must_fail git cherry-pick --rerere-autoupdate foo..bar-master &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ test_must_fail git cherry-pick --continue &&
+ test_cmp bar-expect bar &&
+ git diff-files --quiet &&
+ git cherry-pick --continue &&
+ git reset --hard bar-dev
+'
+
+test_expect_success 'cherry-pick conflict repsects rerere.autoUpdate' '
+ test_config rerere.autoUpdate true &&
+ test_must_fail git cherry-pick foo..bar-master &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ test_must_fail git cherry-pick --continue &&
+ test_cmp bar-expect bar &&
+ git diff-files --quiet &&
+ git cherry-pick --continue &&
+ git reset --hard bar-dev
+'
+
+test_expect_success 'cherry-pick conflict with --no-rerere-autoupdate' '
+ test_config rerere.autoUpdate true &&
+ test_must_fail git cherry-pick --no-rerere-autoupdate foo..bar-master &&
+ test_cmp foo-expect foo &&
+ test_must_fail git diff-files --quiet &&
+ git add foo &&
+ test_must_fail git cherry-pick --continue &&
+ test_cmp bar-expect bar &&
+ test_must_fail git diff-files --quiet &&
+ git add bar &&
+ git cherry-pick --continue &&
+ git reset --hard bar-dev
+'
+
+test_expect_success 'cherry-pick --continue rejects --rerere-autoupdate' '
+ test_must_fail git cherry-pick --rerere-autoupdate foo..bar-master &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ test_must_fail git cherry-pick --continue --rerere-autoupdate >actual 2>&1 &&
+ echo "fatal: cherry-pick: --rerere-autoupdate cannot be used with --continue" >expect &&
+ test_i18ncmp expect actual &&
+ test_must_fail git cherry-pick --continue --no-rerere-autoupdate >actual 2>&1 &&
+ echo "fatal: cherry-pick: --no-rerere-autoupdate cannot be used with --continue" >expect &&
+ test_i18ncmp expect actual &&
+ git cherry-pick --abort
'
-test_expect_success 'reconfigure' '
- git config rerere.enabled false &&
- git reset --hard
+test_expect_success 'cherry-pick --rerere-autoupdate more than once' '
+ test_must_fail git cherry-pick --rerere-autoupdate --rerere-autoupdate foo..bar-master &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ git cherry-pick --abort &&
+ test_must_fail git cherry-pick --rerere-autoupdate --no-rerere-autoupdate --rerere-autoupdate foo..bar-master &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ git cherry-pick --abort &&
+ test_must_fail git cherry-pick --rerere-autoupdate --no-rerere-autoupdate foo..bar-master &&
+ test_must_fail git diff-files --quiet &&
+ git cherry-pick --abort
'
test_expect_success 'cherry-pick conflict without rerere' '
+ test_config rerere.enabled false &&
test_must_fail git cherry-pick master &&
test_must_fail test_cmp expect foo
'
test_i18ncmp expect actual
'
-test_expect_success 'rm empty string should invoke warning' '
- git rm -rf "" 2>output &&
- test_i18ngrep "warning: empty strings" output
+test_expect_success 'rm empty string should fail' '
+ test_must_fail git rm -rf ""
'
test_done
test_i18ncmp expect.err actual.err
'
-test_expect_success 'git add empty string should invoke warning' '
- git add "" 2>output &&
- test_i18ngrep "warning: empty strings" output
+test_expect_success 'git add empty string should fail' '
+ test_must_fail git add ""
'
test_expect_success 'git add --chmod=[+-]x stages correctly' '
test_expect_success 'git add --chmod=[+-]x changes index with already added file' '
rm -f foo3 xfoo3 &&
+ git reset --hard &&
echo foo >foo3 &&
git add foo3 &&
git add --chmod=+x foo3 &&
test_path_is_file bar
'
+cat > .gitignore <<EOF
+ignored
+ignored.d/*
+EOF
+
+test_expect_success 'stash previously ignored file' '
+ git reset HEAD &&
+ git add .gitignore &&
+ git commit -m "Add .gitignore" &&
+ >ignored.d/foo &&
+ echo "!ignored.d/foo" >> .gitignore &&
+ git stash save --include-untracked &&
+ test_path_is_missing ignored.d/foo &&
+ git stash pop &&
+ test_path_is_file ignored.d/foo
+'
+
test_done
'
+test_expect_success 'detect moved code, complete file' '
+ git reset --hard &&
+ cat <<-\EOF >test.c &&
+ #include<stdio.h>
+ main()
+ {
+ printf("Hello World");
+ }
+ EOF
+ git add test.c &&
+ git commit -m "add main function" &&
+ git mv test.c main.c &&
+ test_config color.diff.oldMoved "normal red" &&
+ test_config color.diff.newMoved "normal green" &&
+ git diff HEAD --color-moved=zebra --no-renames | test_decode_color >actual &&
+ cat >expected <<-\EOF &&
+ <BOLD>diff --git a/main.c b/main.c<RESET>
+ <BOLD>new file mode 100644<RESET>
+ <BOLD>index 0000000..a986c57<RESET>
+ <BOLD>--- /dev/null<RESET>
+ <BOLD>+++ b/main.c<RESET>
+ <CYAN>@@ -0,0 +1,5 @@<RESET>
+ <BGREEN>+<RESET><BGREEN>#include<stdio.h><RESET>
+ <BGREEN>+<RESET><BGREEN>main()<RESET>
+ <BGREEN>+<RESET><BGREEN>{<RESET>
+ <BGREEN>+<RESET><BGREEN>printf("Hello World");<RESET>
+ <BGREEN>+<RESET><BGREEN>}<RESET>
+ <BOLD>diff --git a/test.c b/test.c<RESET>
+ <BOLD>deleted file mode 100644<RESET>
+ <BOLD>index a986c57..0000000<RESET>
+ <BOLD>--- a/test.c<RESET>
+ <BOLD>+++ /dev/null<RESET>
+ <CYAN>@@ -1,5 +0,0 @@<RESET>
+ <BRED>-#include<stdio.h><RESET>
+ <BRED>-main()<RESET>
+ <BRED>-{<RESET>
+ <BRED>-printf("Hello World");<RESET>
+ <BRED>-}<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'detect malicious moved code, inside file' '
+ test_config color.diff.oldMoved "normal red" &&
+ test_config color.diff.newMoved "normal green" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ git reset --hard &&
+ cat <<-\EOF >main.c &&
+ #include<stdio.h>
+ int stuff()
+ {
+ printf("Hello ");
+ printf("World\n");
+ }
+
+ int secure_foo(struct user *u)
+ {
+ if (!u->is_allowed_foo)
+ return;
+ foo(u);
+ }
+
+ int main()
+ {
+ foo();
+ }
+ EOF
+ cat <<-\EOF >test.c &&
+ #include<stdio.h>
+ int bar()
+ {
+ printf("Hello World, but different\n");
+ }
+
+ int another_function()
+ {
+ bar();
+ }
+ EOF
+ git add main.c test.c &&
+ git commit -m "add main and test file" &&
+ cat <<-\EOF >main.c &&
+ #include<stdio.h>
+ int stuff()
+ {
+ printf("Hello ");
+ printf("World\n");
+ }
+
+ int main()
+ {
+ foo();
+ }
+ EOF
+ cat <<-\EOF >test.c &&
+ #include<stdio.h>
+ int bar()
+ {
+ printf("Hello World, but different\n");
+ }
+
+ int secure_foo(struct user *u)
+ {
+ foo(u);
+ if (!u->is_allowed_foo)
+ return;
+ }
+
+ int another_function()
+ {
+ bar();
+ }
+ EOF
+ git diff HEAD --no-renames --color-moved=zebra| test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/main.c b/main.c<RESET>
+ <BOLD>index 27a619c..7cf9336 100644<RESET>
+ <BOLD>--- a/main.c<RESET>
+ <BOLD>+++ b/main.c<RESET>
+ <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
+ printf("World\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BRED>-int secure_foo(struct user *u)<RESET>
+ <BRED>-{<RESET>
+ <BLUE>-if (!u->is_allowed_foo)<RESET>
+ <BLUE>-return;<RESET>
+ <BRED>-foo(u);<RESET>
+ <BLUE>-}<RESET>
+ <BLUE>-<RESET>
+ int main()<RESET>
+ {<RESET>
+ foo();<RESET>
+ <BOLD>diff --git a/test.c b/test.c<RESET>
+ <BOLD>index 1dc1d85..2bedec9 100644<RESET>
+ <BOLD>--- a/test.c<RESET>
+ <BOLD>+++ b/test.c<RESET>
+ <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
+ printf("Hello World, but different\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
+ <BGREEN>+<RESET><BGREEN>{<RESET>
+ <YELLOW>+<RESET><YELLOW>foo(u);<RESET>
+ <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
+ <BGREEN>+<RESET><BGREEN>return;<RESET>
+ <YELLOW>+<RESET><YELLOW>}<RESET>
+ <YELLOW>+<RESET>
+ int another_function()<RESET>
+ {<RESET>
+ bar();<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'plain moved code, inside file' '
+ test_config color.diff.oldMoved "normal red" &&
+ test_config color.diff.newMoved "normal green" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ # needs previous test as setup
+ git diff HEAD --no-renames --color-moved=plain| test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/main.c b/main.c<RESET>
+ <BOLD>index 27a619c..7cf9336 100644<RESET>
+ <BOLD>--- a/main.c<RESET>
+ <BOLD>+++ b/main.c<RESET>
+ <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
+ printf("World\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BRED>-int secure_foo(struct user *u)<RESET>
+ <BRED>-{<RESET>
+ <BRED>-if (!u->is_allowed_foo)<RESET>
+ <BRED>-return;<RESET>
+ <BRED>-foo(u);<RESET>
+ <BRED>-}<RESET>
+ <BRED>-<RESET>
+ int main()<RESET>
+ {<RESET>
+ foo();<RESET>
+ <BOLD>diff --git a/test.c b/test.c<RESET>
+ <BOLD>index 1dc1d85..2bedec9 100644<RESET>
+ <BOLD>--- a/test.c<RESET>
+ <BOLD>+++ b/test.c<RESET>
+ <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
+ printf("Hello World, but different\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
+ <BGREEN>+<RESET><BGREEN>{<RESET>
+ <BGREEN>+<RESET><BGREEN>foo(u);<RESET>
+ <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
+ <BGREEN>+<RESET><BGREEN>return;<RESET>
+ <BGREEN>+<RESET><BGREEN>}<RESET>
+ <BGREEN>+<RESET>
+ int another_function()<RESET>
+ {<RESET>
+ bar();<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'detect permutations inside moved code -- dimmed_zebra' '
+ git reset --hard &&
+ cat <<-\EOF >lines.txt &&
+ line 1
+ line 2
+ line 3
+ line 4
+ line 5
+ line 6
+ line 7
+ line 8
+ line 9
+ line 10
+ line 11
+ line 12
+ line 13
+ line 14
+ line 15
+ line 16
+ EOF
+ git add lines.txt &&
+ git commit -m "add poetry" &&
+ cat <<-\EOF >lines.txt &&
+ line 4
+ line 5
+ line 6
+ line 7
+ line 8
+ line 9
+ line 1
+ line 2
+ line 3
+ line 14
+ line 15
+ line 16
+ line 10
+ line 11
+ line 12
+ line 13
+ EOF
+ test_config color.diff.oldMoved "magenta" &&
+ test_config color.diff.newMoved "cyan" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ test_config color.diff.oldMovedDimmed "normal magenta" &&
+ test_config color.diff.newMovedDimmed "normal cyan" &&
+ test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
+ test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
+ git diff HEAD --no-renames --color-moved=dimmed_zebra| test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>index 47ea9c3..ba96a38 100644<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
+ <BMAGENTA>-line 1<RESET>
+ <BMAGENTA>-line 2<RESET>
+ <BMAGENTA>-line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ line 6<RESET>
+ line 7<RESET>
+ line 8<RESET>
+ line 9<RESET>
+ <BCYAN>+<RESET><BCYAN>line 1<RESET>
+ <BCYAN>+<RESET><BCYAN>line 2<RESET>
+ <CYAN>+<RESET><CYAN>line 3<RESET>
+ <YELLOW>+<RESET><YELLOW>line 14<RESET>
+ <BYELLOW>+<RESET><BYELLOW>line 15<RESET>
+ <BYELLOW>+<RESET><BYELLOW>line 16<RESET>
+ line 10<RESET>
+ line 11<RESET>
+ line 12<RESET>
+ line 13<RESET>
+ <BMAGENTA>-line 14<RESET>
+ <BMAGENTA>-line 15<RESET>
+ <BMAGENTA>-line 16<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'cmd option assumes configured colored-moved' '
+ test_config color.diff.oldMoved "magenta" &&
+ test_config color.diff.newMoved "cyan" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ test_config color.diff.oldMovedDimmed "normal magenta" &&
+ test_config color.diff.newMovedDimmed "normal cyan" &&
+ test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
+ test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
+ test_config diff.colorMoved zebra &&
+ git diff HEAD --no-renames --color-moved| test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>index 47ea9c3..ba96a38 100644<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
+ <MAGENTA>-line 1<RESET>
+ <MAGENTA>-line 2<RESET>
+ <MAGENTA>-line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ line 6<RESET>
+ line 7<RESET>
+ line 8<RESET>
+ line 9<RESET>
+ <CYAN>+<RESET><CYAN>line 1<RESET>
+ <CYAN>+<RESET><CYAN>line 2<RESET>
+ <CYAN>+<RESET><CYAN>line 3<RESET>
+ <YELLOW>+<RESET><YELLOW>line 14<RESET>
+ <YELLOW>+<RESET><YELLOW>line 15<RESET>
+ <YELLOW>+<RESET><YELLOW>line 16<RESET>
+ line 10<RESET>
+ line 11<RESET>
+ line 12<RESET>
+ line 13<RESET>
+ <MAGENTA>-line 14<RESET>
+ <MAGENTA>-line 15<RESET>
+ <MAGENTA>-line 16<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'no effect from --color-moved with --word-diff' '
+ cat <<-\EOF >text.txt &&
+ Lorem Ipsum is simply dummy text of the printing and typesetting industry.
+ EOF
+ git add text.txt &&
+ git commit -a -m "clean state" &&
+ cat <<-\EOF >text.txt &&
+ simply Lorem Ipsum dummy is text of the typesetting and printing industry.
+ EOF
+ git diff --color-moved --word-diff >actual &&
+ git diff --word-diff >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'move detection ignoring whitespace ' '
+ git reset --hard &&
+ cat <<\EOF >lines.txt &&
+line 1
+line 2
+line 3
+line 4
+line 5
+line 6
+line 7
+EOF
+ git add lines.txt &&
+ git commit -m "add poetry" &&
+ cat <<\EOF >lines.txt &&
+ line 5
+ line 6
+ line 7
+line 1
+line 2
+line 3
+line 4
+EOF
+ test_config color.diff.oldMoved "magenta" &&
+ test_config color.diff.newMoved "cyan" &&
+ git diff HEAD --no-renames --color-moved| test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>index 734156d..eb89ead 100644<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,7 +1,7 @@<RESET>
+ <GREEN>+<RESET> <GREEN>line 5<RESET>
+ <GREEN>+<RESET> <GREEN>line 6<RESET>
+ <GREEN>+<RESET> <GREEN>line 7<RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ <RED>-line 5<RESET>
+ <RED>-line 6<RESET>
+ <RED>-line 7<RESET>
+ EOF
+ test_cmp expected actual &&
+
+ git diff HEAD --no-renames -w --color-moved| test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>index 734156d..eb89ead 100644<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,7 +1,7 @@<RESET>
+ <CYAN>+<RESET> <CYAN>line 5<RESET>
+ <CYAN>+<RESET> <CYAN>line 6<RESET>
+ <CYAN>+<RESET> <CYAN>line 7<RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ <MAGENTA>-line 5<RESET>
+ <MAGENTA>-line 6<RESET>
+ <MAGENTA>-line 7<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'move detection with submodules' '
+ test_create_repo bananas &&
+ echo ripe >bananas/recipe &&
+ git -C bananas add recipe &&
+ test_commit fruit &&
+ test_commit -C bananas recipe &&
+ git submodule add ./bananas &&
+ git add bananas &&
+ git commit -a -m "bananas are like a heavy library?" &&
+ echo foul >bananas/recipe &&
+ echo ripe >fruit.t &&
+
+ git diff --submodule=diff --color-moved >actual &&
+
+ # no move detection as the moved line is across repository boundaries.
+ test_decode_color <actual >decoded_actual &&
+ ! grep BGREEN decoded_actual &&
+ ! grep BRED decoded_actual &&
+
+ # nor did we mess with it another way
+ git diff --submodule=diff | test_decode_color >expect &&
+ test_cmp expect decoded_actual
+'
+
test_done
test_tick &&
git commit -m "A 4k file"
'
+
+# OpenBSD only supports up to 255 repetitions, so repeat twice for 64*64=4096.
test_expect_success '-G matches' '
- git diff --name-only -G "^0{4096}$" HEAD^ >out &&
+ git diff --name-only -G "^(0{64}){64}$" HEAD^ >out &&
test 4096-zeroes.txt = "$(cat out)"
'
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio
dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te
feugait nulla facilisi.
+
+ Reported-by: A N Other <a.n.other@example.com>
EOF
cat >failmail <<-\EOF &&
echo world >>file &&
git add file &&
test_tick &&
- git commit -s -F msg &&
+ git commit -F msg &&
git tag second &&
git format-patch --stdout first >patch1 &&
echo "Date: $GIT_AUTHOR_DATE" &&
echo &&
sed -e "1,2d" msg &&
- echo &&
- echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" &&
echo "---" &&
git diff-tree --no-commit-id --stat -p second
} >patch1-stgit.eml &&
echo "# Parent $_z40" &&
cat msg &&
echo &&
- echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" &&
- echo &&
git diff-tree --no-commit-id -p second
} >patch1-hg.eml &&
git reset --hard &&
git checkout -b master2 first &&
git am --signoff <patch2 &&
- printf "%s\n" "$signoff" >expected &&
- echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >>expected &&
- git cat-file commit HEAD^ | grep "Signed-off-by:" >actual &&
- test_cmp expected actual &&
- echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >expected &&
- git cat-file commit HEAD | grep "Signed-off-by:" >actual &&
- test_cmp expected actual
+ {
+ printf "third\n\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" &&
+ cat msg &&
+ printf "Signed-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL"
+ } >expected-log &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
'
test_expect_success 'am stays in branch' '
'
test_expect_success 'am --signoff does not add Signed-off-by: line if already there' '
- git format-patch --stdout HEAD^ >patch3 &&
- sed -e "/^Subject/ s,\[PATCH,Re: Re: Re: & 1/5 v2] [foo," patch3 >patch4 &&
- rm -fr .git/rebase-apply &&
- git reset --hard &&
- git checkout HEAD^ &&
- git am --signoff patch4 &&
- git cat-file commit HEAD >actual &&
- test $(grep -c "^Signed-off-by:" actual) -eq 1
+ git format-patch --stdout first >patch3 &&
+ git reset --hard first &&
+ git am --signoff <patch3 &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
+'
+
+test_expect_success 'am --signoff adds Signed-off-by: if another author is preset' '
+ NAME="A N Other" &&
+ EMAIL="a.n.other@example.com" &&
+ {
+ printf "third\n\nSigned-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL" &&
+ cat msg &&
+ printf "Signed-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL"
+ } >expected-log &&
+ git reset --hard first &&
+ GIT_COMMITTER_NAME="$NAME" GIT_COMMITTER_EMAIL="$EMAIL" \
+ git am --signoff <patch3 &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
+'
+
+test_expect_success 'am --signoff duplicates Signed-off-by: if it is not the last one' '
+ NAME="A N Other" &&
+ EMAIL="a.n.other@example.com" &&
+ {
+ printf "third\n\nSigned-off-by: %s <%s>\n\
+Signed-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" &&
+ cat msg &&
+ printf "Signed-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\
+Signed-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL"
+ } >expected-log &&
+ git format-patch --stdout first >patch3 &&
+ git reset --hard first &&
+ git am --signoff <patch3 &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
'
test_expect_success 'am without --keep removes Re: and [PATCH] stuff' '
+ git format-patch --stdout HEAD^ >tmp &&
+ sed -e "/^Subject/ s,\[PATCH,Re: Re: Re: & 1/5 v2] [foo," tmp >patch4 &&
+ git reset --hard HEAD^ &&
+ git am <patch4 &&
git rev-parse HEAD >expected &&
git rev-parse master2 >actual &&
test_cmp expected actual
test_i18ngrep broken stderr
'
+test_expect_success 'log does not default to HEAD when rev input is given' '
+ >expect &&
+ git log --branches=does-not-exist >actual &&
+ test_cmp expect actual
+'
+
test_expect_success 'set up --source tests' '
git checkout --orphan source-a &&
test_commit one &&
. ./test-lib.sh
-get_color ()
-{
- git config --get-color no.such.slot "$1"
-}
-
test_expect_success setup '
git config diff.color.commit yellow &&
git config color.decorate.branch green &&
git config color.decorate.stash magenta &&
git config color.decorate.HEAD cyan &&
- c_reset=$(get_color reset) &&
+ c_reset="<RESET>" &&
- c_commit=$(get_color yellow) &&
- c_branch=$(get_color green) &&
- c_remoteBranch=$(get_color red) &&
- c_tag=$(get_color "reverse bold yellow") &&
- c_stash=$(get_color magenta) &&
- c_HEAD=$(get_color cyan) &&
+ c_commit="<YELLOW>" &&
+ c_branch="<GREEN>" &&
+ c_remoteBranch="<RED>" &&
+ c_tag="<BOLD;REVERSE;YELLOW>" &&
+ c_stash="<MAGENTA>" &&
+ c_HEAD="<CYAN>" &&
test_commit A &&
git clone . other &&
# to this test since it does not contain any decoration, hence --first-parent
test_expect_success 'Commit Decorations Colored Correctly' '
git log --first-parent --abbrev=10 --all --decorate --oneline --color=always |
- sed "s/[0-9a-f]\{10,10\}/COMMIT_ID/" >out &&
+ sed "s/[0-9a-f]\{10,10\}/COMMIT_ID/" |
+ test_decode_color >out &&
test_cmp expected out
'
EOF
'
-test_expect_success 'lookup in duplicated pack (binary search)' '
+test_expect_success 'lookup in duplicated pack' '
git cat-file --batch-check <input >actual &&
test_cmp expect actual
'
-test_expect_success 'lookup in duplicated pack (GIT_USE_LOOKUP)' '
- (
- GIT_USE_LOOKUP=1 &&
- export GIT_USE_LOOKUP &&
- git cat-file --batch-check <input >actual
- ) &&
- test_cmp expect actual
-'
-
test_expect_success 'index-pack can reject packs with duplicates' '
clear_packs &&
create_pack dups.pack 2 &&
test_tick &&
git commit -m "Create conflict" seq.txt &&
test_must_fail git pull --rebase . seq 2>err >out &&
- test_i18ngrep "When you have resolved this problem" out
+ test_i18ngrep "Resolve all conflicts manually" out
'
test_expect_success 'failed --rebase shows advice' '
git checkout -f -b fails-to-rebase HEAD^ &&
test_commit v2-without-cr file "2" file2-lf &&
test_must_fail git pull --rebase . diverging 2>err >out &&
- test_i18ngrep "When you have resolved this problem" out
+ test_i18ngrep "Resolve all conflicts manually" out
'
test_expect_success '--rebase fails with multiple branches' '
# Fails when refspec includes an object id
test_must_fail git -C work push --recurse-submodules=on-demand origin \
"$(git -C work rev-parse branch2):refs/heads/branch2" &&
- # Fails when refspec includes 'HEAD' as it is unsupported at this time
+ # Fails when refspec includes HEAD and parent and submodule do not
+ # have the same named branch checked out
test_must_fail git -C work push --recurse-submodules=on-demand origin \
HEAD:refs/heads/branch2 &&
test_cmp expected_pub actual_pub
'
+test_expect_success 'push propagating HEAD refspec to a submodule' '
+ git -C work/gar/bage checkout branch2 &&
+ > work/gar/bage/junk12 &&
+ git -C work/gar/bage add junk12 &&
+ git -C work/gar/bage commit -m "Twelfth junk" &&
+
+ git -C work checkout branch2 &&
+ git -C work add gar/bage &&
+ git -C work commit -m "updating gar/bage in branch2" &&
+
+ # Passes since the superproject and submodules HEAD are both on branch2
+ git -C work push --recurse-submodules=on-demand origin \
+ HEAD:refs/heads/branch2 &&
+
+ git -C submodule.git rev-parse branch2 >actual_submodule &&
+ git -C pub.git rev-parse branch2 >actual_pub &&
+ git -C work/gar/bage rev-parse branch2 >expected_submodule &&
+ git -C work rev-parse branch2 >expected_pub &&
+ test_cmp expected_submodule actual_submodule &&
+ test_cmp expected_pub actual_pub
+'
+
test_done
test_i18ngrep "the receiving end does not support" err
'
+test_expect_success 'push --signed=1 is accepted' '
+ prepare_dst &&
+ mkdir -p dst/.git/hooks &&
+ test_must_fail git push --signed=1 dst noop ff +noff 2>err &&
+ test_i18ngrep "the receiving end does not support" err
+'
+
test_expect_success GPG 'no certificate for a signed push with no update' '
prepare_dst &&
mkdir -p dst/.git/hooks &&
}
# Feed to --format to provide predictable colored sequences.
+BASIC_COLOR='%Credfoo%Creset'
+COLOR='%C(red)foo%C(reset)'
AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
+ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
has_color () {
- printf '\033[31mfoo\033[m\n' >expect &&
- test_cmp expect "$1"
+ test_decode_color <"$1" >decoded &&
+ echo "<RED>foo<RESET>" >expect &&
+ test_cmp expect decoded
}
has_no_color () {
EOF
-test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
-commit $head2
-\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
-commit $head1
-\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
-EOF
-
-test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
-commit $head2
-\e[1;31;43mfoo\e[m
-commit $head1
-\e[1;31;43mfoo\e[m
-EOF
-
-test_expect_success '%C(auto,...) does not enable color by default' '
- git log --format=$AUTO_COLOR -1 >actual &&
- has_no_color actual
+test_expect_success 'basic colors' '
+ cat >expect <<-EOF &&
+ commit $head2
+ <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
+ EOF
+ format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
+ git rev-list --color --format="$format" -1 master >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
'
-test_expect_success '%C(auto,...) enables colors for color.diff' '
- git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
- has_color actual
+test_expect_success 'advanced colors' '
+ cat >expect <<-EOF &&
+ commit $head2
+ <BOLD;RED;BYELLOW>foo<RESET>
+ EOF
+ format="%C(red yellow bold)foo%C(reset)" &&
+ git rev-list --color --format="$format" -1 master >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
'
-test_expect_success '%C(auto,...) enables colors for color.ui' '
- git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
- has_color actual
-'
+for spec in \
+ "%Cred:$BASIC_COLOR" \
+ "%C(...):$COLOR" \
+ "%C(auto,...):$AUTO_COLOR"
+do
+ desc=${spec%%:*}
+ color=${spec#*:}
+ test_expect_success "$desc does not enable color by default" '
+ git log --format=$color -1 >actual &&
+ has_no_color actual
+ '
-test_expect_success '%C(auto,...) respects --color' '
- git log --format=$AUTO_COLOR -1 --color >actual &&
- has_color actual
-'
+ test_expect_success "$desc enables colors for color.diff" '
+ git -c color.diff=always log --format=$color -1 >actual &&
+ has_color actual
+ '
-test_expect_success '%C(auto,...) respects --no-color' '
- git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
- has_no_color actual
-'
+ test_expect_success "$desc enables colors for color.ui" '
+ git -c color.ui=always log --format=$color -1 >actual &&
+ has_color actual
+ '
-test_expect_success TTY '%C(auto,...) respects --color=auto (stdout is tty)' '
- test_terminal env TERM=vt100 \
- git log --format=$AUTO_COLOR -1 --color=auto >actual &&
- has_color actual
-'
+ test_expect_success "$desc respects --color" '
+ git log --format=$color -1 --color >actual &&
+ has_color actual
+ '
-test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
- (
- TERM=vt100 && export TERM &&
- git log --format=$AUTO_COLOR -1 --color=auto >actual &&
+ test_expect_success "$desc respects --no-color" '
+ git -c color.ui=always log --format=$color -1 --no-color >actual &&
has_no_color actual
- )
+ '
+
+ test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
+ test_terminal env TERM=vt100 \
+ git log --format=$color -1 --color=auto >actual &&
+ has_color actual
+ '
+
+ test_expect_success "$desc respects --color=auto (stdout not tty)" '
+ (
+ TERM=vt100 && export TERM &&
+ git log --format=$color -1 --color=auto >actual &&
+ has_no_color actual
+ )
+ '
+done
+
+test_expect_success '%C(always,...) enables color even without tty' '
+ git log --format=$ALWAYS_COLOR -1 >actual &&
+ has_color actual
'
test_expect_success '%C(auto) respects --color' '
- git log --color --format="%C(auto)%H" -1 >actual &&
- printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
+ git log --color --format="%C(auto)%H" -1 >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
test_cmp expect actual
'
test_cmp expect actual
'
+test_expect_success 'rev-list %C(auto,...) respects --color' '
+ git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
+ -1 HEAD >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ cat >expect <<-EOF &&
+ commit $(git rev-parse HEAD)
+ <GREEN>foo<RESET>
+ EOF
+ test_cmp expect actual
+'
+
iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
Test printing of complex bodies
compare rev-list "--exclude=refs/remotes/* --exclude=refs/tags/* --all" --branches
'
-
-# "git rev-list<ENTER>" is likely to be a bug in the calling script and may
-# deserve an error message, but do cases where set of refs programmatically
-# given using globbing and/or --stdin need to fail with the same error, or
-# are we better off reporting a success with no output? The following few
-# tests document the current behaviour to remind us that we might want to
-# think about this issue.
-
-test_expect_failure 'rev-list may want to succeed with empty output on no input (1)' '
+test_expect_failure 'rev-list should succeed with empty output on empty stdin' '
>expect &&
git rev-list --stdin <expect >actual &&
test_cmp expect actual
'
-test_expect_failure 'rev-list may want to succeed with empty output on no input (2)' '
+test_expect_success 'rev-list should succeed with empty output with all refs excluded' '
>expect &&
git rev-list --exclude=* --all >actual &&
test_cmp expect actual
'
-test_expect_failure 'rev-list may want to succeed with empty output on no input (3)' '
+test_expect_success 'rev-list should succeed with empty output with empty --all' '
(
test_create_repo empty &&
cd empty &&
)
'
+test_expect_success 'rev-list should succeed with empty output with empty glob' '
+ >expect &&
+ git rev-list --glob=does-not-match-anything >actual &&
+ test_cmp expect actual
+'
+
test_expect_success 'shortlog accepts --glob/--tags/--remotes' '
compare shortlog "subspace/one subspace/two" --branches=subspace &&
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-gpg.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
# Mon Jul 3 23:18:43 2006 +0000
datestamp=1151968723
test_must_fail git for-each-ref --format="%(refname:INVALID)"
'
-get_color ()
-{
- git config --get-color no.such.slot "$1"
-}
+test_expect_success 'set up color tests' '
+ cat >expected.color <<-EOF &&
+ $(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
+ $(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
+ $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
+ $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
+ EOF
+ sed "s/<[^>]*>//g" <expected.color >expected.bare &&
+ color_format="%(objectname:short) %(color:green)%(refname:short)"
+'
-cat >expected <<EOF
-$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
-$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
-$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
-$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
-EOF
+test_expect_success TTY '%(color) shows color with a tty' '
+ test_terminal env TERM=vt100 \
+ git for-each-ref --format="$color_format" >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expected.color actual
+'
-test_expect_success 'Check %(color:...) ' '
- git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
- test_cmp expected actual
+test_expect_success '%(color) does not show color without tty' '
+ TERM=vt100 git for-each-ref --format="$color_format" >actual &&
+ test_cmp expected.bare actual
+'
+
+test_expect_success 'color.ui=always can override tty check' '
+ git -c color.ui=always for-each-ref --format="$color_format" >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expected.color actual
'
cat >expected <<\EOF
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-gpg.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
# creating and listing lightweight tags:
run_with_limited_stack git tag --contains HEAD >actual &&
test_cmp expect actual &&
run_with_limited_stack git tag --no-contains HEAD >actual &&
- test_line_count ">" 10 actual
+ test_line_count "-gt" 10 actual
'
test_expect_success '--format should list tags as per format given' '
test_cmp expect actual
'
+test_expect_success "set up color tests" '
+ echo "<RED>v1.0<RESET>" >expect.color &&
+ echo "v1.0" >expect.bare &&
+ color_args="--format=%(color:red)%(refname:short) --list v1.0"
+'
+
+test_expect_success '%(color) omitted without tty' '
+ TERM=vt100 git tag $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.bare actual
+'
+
+test_expect_success TTY '%(color) present with tty' '
+ test_terminal env TERM=vt100 git tag $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success 'color.ui=always overrides auto-color' '
+ git -c color.ui=always tag $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
test_expect_success 'setup --merged test tags' '
git tag mergetest-1 HEAD~2 &&
git tag mergetest-2 HEAD~1 &&
}
'
+test_expect_success TTY 'git tag -l defaults to paging' '
+ rm -f paginated.out &&
+ test_terminal git tag -l &&
+ test -e paginated.out
+'
+
+test_expect_success TTY 'git tag -l respects pager.tag' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false tag -l &&
+ ! test -e paginated.out
+'
+
+test_expect_success TTY 'git tag -l respects --no-pager' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag --no-pager tag -l &&
+ ! test -e paginated.out
+'
+
+test_expect_success TTY 'git tag with no args defaults to paging' '
+ # no args implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git tag &&
+ test -e paginated.out
+'
+
+test_expect_success TTY 'git tag with no args respects pager.tag' '
+ # no args implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false tag &&
+ ! test -e paginated.out
+'
+
+test_expect_success TTY 'git tag --contains defaults to paging' '
+ # --contains implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git tag --contains &&
+ test -e paginated.out
+'
+
+test_expect_success TTY 'git tag --contains respects pager.tag' '
+ # --contains implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false tag --contains &&
+ ! test -e paginated.out
+'
+
+test_expect_success TTY 'git tag -a defaults to not paging' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git tag -am message newtag &&
+ ! test -e paginated.out
+'
+
+test_expect_success TTY 'git tag -a ignores pager.tag' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag tag -am message newtag &&
+ ! test -e paginated.out
+'
+
+test_expect_success TTY 'git tag -a respects --paginate' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git --paginate tag -am message newtag &&
+ test -e paginated.out
+'
+
+test_expect_success TTY 'git tag as alias ignores pager.tag with -a' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag -c alias.t=tag t -am message newtag &&
+ ! test -e paginated.out
+'
+
+test_expect_success TTY 'git tag as alias respects pager.tag with -l' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false -c alias.t=tag t -l &&
+ ! test -e paginated.out
+'
+
# A colored commit log will begin with an appropriate ANSI escape
# for the first color; the text "commit" comes later.
colorful() {
'
+test_expect_success 'git clean -i paints the header in HEADER color' '
+ >a.out &&
+ echo q |
+ git -c color.ui=always clean -i |
+ test_decode_color |
+ head -n 1 >header &&
+ # not i18ngrep
+ grep "^<BOLD>" header
+'
+
test_done
test_must_fail git -C multisuper_clone config --get submodule.sub1.active
'
+test_expect_success 'recursive clone respects -q' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
+ test_must_be_empty actual
+'
+
test_done
--- /dev/null
+#!/bin/sh
+
+test_description='git merge --signoff
+
+This test runs git merge --signoff and makes sure that it works.
+'
+
+. ./test-lib.sh
+
+# Setup test files
+test_setup() {
+ # Expected commit message after merge --signoff
+ cat >expected-signed <<EOF &&
+Merge branch 'master' into other-branch
+
+Signed-off-by: $(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/")
+EOF
+
+ # Expected commit message after merge without --signoff (or with --no-signoff)
+ cat >expected-unsigned <<EOF &&
+Merge branch 'master' into other-branch
+EOF
+
+ # Initial commit and feature branch to merge master into it.
+ git commit --allow-empty -m "Initial empty commit" &&
+ git checkout -b other-branch &&
+ test_commit other-branch file1 1
+}
+
+# Setup repository, files & feature branch
+# This step must be run if You want to test 2,3 or 4
+# Order of 2,3,4 is not important, but 1 must be run before
+# For example `-r 1,4` or `-r 1,4,2 -v` etc
+# But not `-r 2` or `-r 4,3,2,1`
+test_expect_success 'setup' '
+ test_setup
+'
+
+# Test with --signoff flag
+test_expect_success 'git merge --signoff adds a sign-off line' '
+ git checkout master &&
+ test_commit master-branch-2 file2 2 &&
+ git checkout other-branch &&
+ git merge master --signoff --no-edit &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-signed actual
+'
+
+# Test without --signoff flag
+test_expect_success 'git merge does not add a sign-off line' '
+ git checkout master &&
+ test_commit master-branch-3 file3 3 &&
+ git checkout other-branch &&
+ git merge master --no-edit &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-unsigned actual
+'
+
+# Test for --no-signoff flag
+test_expect_success 'git merge --no-signoff flag cancels --signoff flag' '
+ git checkout master &&
+ test_commit master-branch-4 file4 4 &&
+ git checkout other-branch &&
+ git merge master --no-edit --signoff --no-signoff &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-unsigned actual
+'
+
+test_done
echo c >>file &&
echo d >>file &&
test_tick &&
- git commit -a -m two
+ git commit -a -m two &&
+ ID1=$(git rev-parse HEAD^) &&
+ shortID1="^$(git rev-parse HEAD^ |cut -c 1-17)" &&
+ ID2=$(git rev-parse HEAD) &&
+ shortID2="$(git rev-parse HEAD |cut -c 1-18)"
'
-cat >expect <<'EOF'
-^baf5e0b (A U Thor 2005-04-07 15:13:13 -0700 1) a
-8825379d (A U Thor 2005-04-07 15:14:13 -0700 2) b
-8825379d (A U Thor 2005-04-07 15:14:13 -0700 3) c
-8825379d (A U Thor 2005-04-07 15:14:13 -0700 4) d
+cat >expect <<EOF
+$shortID1 (A U Thor 2005-04-07 15:13:13 -0700 1) a
+$shortID2 (A U Thor 2005-04-07 15:14:13 -0700 2) b
+$shortID2 (A U Thor 2005-04-07 15:14:13 -0700 3) c
+$shortID2 (A U Thor 2005-04-07 15:14:13 -0700 4) d
EOF
test_expect_success 'normal blame output' '
- git blame file >actual &&
+ git blame --abbrev=17 file >actual &&
test_cmp expect actual
'
-ID1=baf5e0b3869e0b2b2beb395a3720c7b51eac94fc
-COMMIT1='author A U Thor
+COMMIT1="author A U Thor
author-mail <author@example.com>
author-time 1112911993
author-tz -0700
committer-tz -0700
summary one
boundary
-filename file'
-ID2=8825379dfb8a1267b58e8e5bcf69eec838f685ec
-COMMIT2='author A U Thor
+filename file"
+COMMIT2="author A U Thor
author-mail <author@example.com>
author-time 1112912053
author-tz -0700
committer-time 1112912053
committer-tz -0700
summary two
-previous baf5e0b3869e0b2b2beb395a3720c7b51eac94fc file
-filename file'
+previous $ID1 file
+filename file"
cat >expect <<EOF
$ID1 1 1 1
function name(n) {
if (n == 0) return "RESET";
if (n == 1) return "BOLD";
+ if (n == 7) return "REVERSE";
if (n == 30) return "BLACK";
if (n == 31) return "RED";
if (n == 32) return "GREEN";
my $ok = join("|", qw(
TRACE
DEBUG
- USE_LOOKUP
TEST
.*_TEST
PROVE
return ret;
}
-int gpg_verify_tag(const unsigned char *sha1, const char *name_to_report,
+int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
unsigned flags)
{
enum object_type type;
unsigned long size;
int ret;
- type = sha1_object_info(sha1, NULL);
+ type = sha1_object_info(oid->hash, NULL);
if (type != OBJ_TAG)
return error("%s: cannot verify a non-tag object of type %s.",
name_to_report ?
name_to_report :
- find_unique_abbrev(sha1, DEFAULT_ABBREV),
+ find_unique_abbrev(oid->hash, DEFAULT_ABBREV),
typename(type));
- buf = read_sha1_file(sha1, &type, &size);
+ buf = read_sha1_file(oid->hash, &type, &size);
if (!buf)
return error("%s: unable to read file.",
name_to_report ?
name_to_report :
- find_unique_abbrev(sha1, DEFAULT_ABBREV));
+ find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
ret = run_gpg_verify(buf, size, flags);
extern int parse_tag(struct tag *item);
extern struct object *deref_tag(struct object *, const char *, int);
extern struct object *deref_tag_noverify(struct object *);
-extern int gpg_verify_tag(const unsigned char *sha1,
+extern int gpg_verify_tag(const struct object_id *oid,
const char *name_to_report, unsigned flags);
#endif /* TAG_H */
#
# To enable this hook, rename this file to "prepare-commit-msg".
-# This hook includes three examples. The first comments out the
-# "Conflicts:" part of a merge commit.
+# This hook includes three examples. The first one removes the
+# "# Please enter the commit message..." help message.
#
# The second includes the output of "git diff --name-status -r"
# into the message, just before the "git status" output. It is
# The third example adds a Signed-off-by line to the message, that can
# still be edited. This is rarely a good idea.
-case "$2,$3" in
- merge,)
- @PERL_PATH@ -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
+COMMIT_MSG_FILE=$1
+COMMIT_SOURCE=$2
+SHA1=$3
-# ,|template,)
-# @PERL_PATH@ -i.bak -pe '
-# print "\n" . `git diff --cached --name-status -r`
-# if /^#/ && $first++ == 0' "$1" ;;
+@PERL_PATH@ -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"
- *) ;;
-esac
+# case "$COMMIT_SOURCE,$SHA1" in
+# ,|template,)
+# @PERL_PATH@ -i.bak -pe '
+# print "\n" . `git diff --cached --name-status -r`
+# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;;
+# *) ;;
+# esac
-# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
-# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
+# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
+# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
+# if test -z "$COMMIT_SOURCE"
+# then
+# @PERL_PATH@ -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE"
+# fi
struct object_id oid;
private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
- if (private && !get_sha1(private, oid.hash)) {
+ if (private && !get_oid(private, &oid)) {
strbuf_addf(&buf, "^%s", private);
string_list_append(&revlist_args, strbuf_detach(&buf, NULL));
oidcpy(&ref->old_oid, &oid);
* diff_tree_oid(parent, commit) )
*/
for (i = 0; i < nparent; ++i)
- tptree[i] = fill_tree_descriptor(&tp[i], parents_oid[i]->hash);
- ttree = fill_tree_descriptor(&t, oid->hash);
+ tptree[i] = fill_tree_descriptor(&tp[i], parents_oid[i]);
+ ttree = fill_tree_descriptor(&t, oid);
/* Enable recursion indefinitely */
opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
return result;
}
-void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
+void *fill_tree_descriptor(struct tree_desc *desc, const struct object_id *oid)
{
unsigned long size = 0;
void *buf = NULL;
- if (sha1) {
- buf = read_object_with_reference(sha1, tree_type, &size, NULL);
+ if (oid) {
+ buf = read_object_with_reference(oid->hash, tree_type, &size,
+ NULL);
if (!buf)
- die("unable to read tree %s", sha1_to_hex(sha1));
+ die("unable to read tree %s", oid_to_hex(oid));
}
init_tree_desc(desc, buf, size);
return buf;
int tree_entry(struct tree_desc *, struct name_entry *);
int tree_entry_gently(struct tree_desc *, struct name_entry *);
-void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
+void *fill_tree_descriptor(struct tree_desc *desc, const struct object_id *oid);
struct traverse_info;
typedef int (*traverse_callback_t)(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *);
for (i = 0; i < index->cache_nr; i++) {
struct cache_entry *ce = index->cache[i];
if (ce->ce_flags & CE_UPDATE) {
- int r = strcmp(ce->name, ".gitmodules");
+ int r = strcmp(ce->name, GITMODULES_FILE);
if (r < 0)
continue;
else if (r == 0) {
if (should_update_submodules() && o->update && !o->dry_run)
reload_gitmodules_file(index, &state);
+ enable_delayed_checkout(&state);
for (i = 0; i < index->cache_nr; i++) {
struct cache_entry *ce = index->cache[i];
}
}
}
+ errs |= finish_delayed_checkout(&state);
stop_progress(&progress);
if (o->update)
git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))
t[i] = t[i - 2];
else {
- const unsigned char *sha1 = NULL;
+ const struct object_id *oid = NULL;
if (dirmask & 1)
- sha1 = names[i].oid->hash;
- buf[nr_buf++] = fill_tree_descriptor(t+i, sha1);
+ oid = names[i].oid;
+ buf[nr_buf++] = fill_tree_descriptor(t + i, oid);
}
}