Skip to content

Commit

Permalink
Add per-repository eol normalization
Browse files Browse the repository at this point in the history
Change the semantics of the "crlf" attribute so that it enables
end-of-line normalization when it is set, regardless of "core.autocrlf".

Add a new setting for "crlf": "auto", which enables end-of-line
conversion but does not override the automatic text file detection.

Add a new attribute "eol" with possible values "crlf" and "lf".  When
set, this attribute enables normalization and forces git to use CRLF or
LF line endings in the working directory, respectively.

The line ending style to be used for normalized text files in the
working directory is set using "core.autocrlf".  When it is set to
"true", CRLFs are used in the working directory; when set to "input" or
"false", LFs are used.

Signed-off-by: Eyvind Bernhardsen <eyvind.bernhardsen@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Eyvind Bernhardsen authored and Junio C Hamano committed May 20, 2010
1 parent 56499eb commit fd6cce9
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 69 deletions.
4 changes: 2 additions & 2 deletions Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ core.autocrlf::
based on the file's contents. See linkgit:gitattributes[5].

core.safecrlf::
If true, makes git check if converting `CRLF` as controlled by
`core.autocrlf` is reversible. Git will verify if a command
If true, makes git check if converting `CRLF` is reversible when
end-of-line conversion is active. Git will verify if a command
modifies a file in the work tree either directly or indirectly.
For example, committing a file followed by checking out the
same file should yield the original file in the work tree. If
Expand Down
138 changes: 113 additions & 25 deletions Documentation/gitattributes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,50 +95,138 @@ repository upon 'git add' and 'git commit'.
`crlf`
^^^^^^

This attribute controls the line-ending convention.
This attribute enables and controls end-of-line normalization. When a
text file is normalized, its line endings are converted to LF in the
repository. To control what line ending style is used in the working
directory, use the `eol` attribute for a single file and the
`core.autocrlf` configuration variable for all text files.

Set::

Setting the `crlf` attribute on a path is meant to mark
the path as a "text" file. 'core.autocrlf' conversion
takes place without guessing the content type by
inspection.
Setting the `crlf` attribute on a path enables end-of-line
normalization and marks the path as a text file. End-of-line
conversion takes place without guessing the content type.

Unset::

Unsetting the `crlf` attribute on a path tells git not to
attempt any end-of-line conversion upon checkin or checkout.

Set to string value "auto"::

When `crlf` is set to "auto", the path is marked for automatic
end-of-line normalization. If git decides that the content is
text, its line endings are normalized to LF on checkin.

Unspecified::

Unspecified `crlf` attribute tells git to apply the
`core.autocrlf` conversion when the file content looks
like text.
If the `crlf` attribute is unspecified, git uses the `eol`
attribute and the `core.autocrlf` configuration variable to
determine if the file should be converted.

Set to string value "input"::
Any other value causes git to act as if `crlf` has been left
unspecified.

This is similar to setting the attribute to `true`, but
also forces git to act as if `core.autocrlf` is set to
`input` for the path.
`eol`
^^^^^

Any other value set to `crlf` attribute is ignored and git acts
as if the attribute is left unspecified.
This attribute sets a specific line-ending style to be used in the
working directory. It enables end-of-line normalization without any
content checks, similar to setting the `crlf` attribute.

Set to string value "crlf"::

The `core.autocrlf` conversion
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This setting forces git to normalize line endings on checkin
and convert them to CRLF when the file is checked out,
regardless of `crlf` and `core.autocrlf`.

Set to string value "lf"::

This setting forces git to normalize line endings to LF on
checkin and prevents conversion to CRLF when the file is
checked out, regardless of `crlf` and `core.autocrlf`.
`crlf=input` is a backwards compatibility alias for `eol=lf`.

End-of-line conversion
^^^^^^^^^^^^^^^^^^^^^^

While git normally leaves file contents alone, it can be configured to
normalize line endings to LF in the repository and, optionally, to
convert them to CRLF when files are checked out.

Here is an example that will make git normalize .txt, .vcproj and .sh
files, ensure that .vcproj files have CRLF and .sh files have LF in
the working directory, and prevent .jpg files from being normalized
regardless of their content.

------------------------
*.txt crlf
*.vcproj eol=crlf
*.sh eol=lf
*.jpg -crlf
------------------------

Other source code management systems normalize all text files in their
repositories, and there are two ways to enable similar automatic
normalization in git.

If you simply want to have CRLF line endings in your working directory
regardless of the repository you are working with, you can set the
config variable "core.autocrlf" without changing any attributes.

------------------------
[core]
autocrlf = true
------------------------

This does not force normalization of all text files, but does ensure
that text files that you introduce to the repository have their line
endings normalized to LF when they are added, and that files that are
already normalized in the repository stay normalized. You can also
set `autocrlf` to "input" to have automatic normalization of new text
files without conversion to CRLF in the working directory.

If you want to interoperate with a source code management system that
enforces end-of-line normalization, or you simply want all text files
in your repository to be normalized, you should instead set the `crlf`
attribute to "auto" for _all_ files.

------------------------
* crlf=auto
------------------------

This ensures that all files that git considers to be text will have
normalized (LF) line endings in the repository.

If the configuration variable `core.autocrlf` is false, no
conversion is done.
NOTE: When `crlf=auto` normalization is enabled in an existing
repository, any text files containing CRLFs should be normalized. If
they are not they will be normalized the next time someone tries to
change them, causing unfortunate misattribution. From a clean working
directory:

When `core.autocrlf` is true, it means that the platform wants
CRLF line endings for files in the working tree, and you want to
convert them back to the normal LF line endings when checking
in to the repository.
-------------------------------------------------
$ echo "* crlf=auto" >>.gitattributes
$ rm .git/index # Remove the index to force git to
$ git reset # re-scan the working directory
$ git status # Show files that will be normalized
$ git add -u
$ git add .gitattributes
$ git commit -m "Introduce end-of-line normalization"
-------------------------------------------------

When `core.autocrlf` is set to "input", line endings are
converted to LF upon checkin, but there is no conversion done
upon checkout.
If any files that should not be normalized show up in 'git status',
unset their `crlf` attribute before running 'git add -u'.

------------------------
manual.pdf -crlf
------------------------

Conversely, text files that git does not detect can have normalization
enabled manually.

------------------------
weirdchars.txt crlf
------------------------

If `core.safecrlf` is set to "true" or "warn", git verifies if
the conversion is reversible for the current setting of
Expand Down
9 changes: 8 additions & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ extern int core_compression_seen;
extern size_t packed_git_window_size;
extern size_t packed_git_limit;
extern size_t delta_base_cache_limit;
extern int auto_crlf;
extern int read_replace_refs;
extern int fsync_object_files;
extern int core_preload_index;
Expand All @@ -549,6 +548,14 @@ enum safe_crlf {

extern enum safe_crlf safe_crlf;

enum auto_crlf {
AUTO_CRLF_FALSE = 0,
AUTO_CRLF_TRUE = 1,
AUTO_CRLF_INPUT = -1,
};

extern enum auto_crlf auto_crlf;

enum branch_track {
BRANCH_TRACK_UNSPECIFIED = -1,
BRANCH_TRACK_NEVER = 0,
Expand Down
2 changes: 1 addition & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ static int git_default_core_config(const char *var, const char *value)

if (!strcmp(var, "core.autocrlf")) {
if (value && !strcasecmp(value, "input")) {
auto_crlf = -1;
auto_crlf = AUTO_CRLF_INPUT;
return 0;
}
auto_crlf = git_config_bool(var, value);
Expand Down
Loading

0 comments on commit fd6cce9

Please sign in to comment.