Skip to contentSkip to conversation

Support "core.excludesfile = ~/.gitignore"

PATCH Superseded Karl Chen 9 participants 45 messages Jun 15, 2016 Documentation

lore
Share & tools
lore

Karl Chen

author
#0 lore raw

PATCH 1/1 Support "core.excludesfile = ~/.gitignore"

I keep my rc files, including .gitconfig and my default gitignore list under version control and like to have the same contents everywhere. Unfortunately my home directory is at different locations on different systems.

I'd like to be able to put something like this in my ~/.gitconfig:

[core]
excludesfile = ~/.gitignore

or
excludesfile = $HOME/.gitignore

Another idea is to have a non-absolute path be interpreted relative to the location of .gitconfig, i.e. $HOME, instead of the current directory. $GIT_DIR/info/excludes is already for repository-specific excludes so no functionality would be lost.

Below is a sample patch that works for me. We could also use getpwuid(getuid()) instead of getenv("HOME") to be consistent with user_path() but this is simpler and arguably more likely what the user wants when it matters.

Quoted 1 line

From 6eb18f8ade791521bdad955e1da2b40399a426f0 Mon Sep 17 00:00:00 2001

From: Karl Chen <[email protected]>
Date: Thu, 21 Aug 2008 21:00:26 -0700
Subject: [PATCH] Support "core.excludesfile = ~/.gitignore"

The config variable core.excludesfile is parsed to substitute leading "~/" with getenv("HOME").

---
 config.c |   20 ++++++++++++++++++--
 1 files changed, 18 insertions(+), 2 deletions(-)
config.c +18 -2
index 53f04a0..41061d2 100644
@@ -334,6 +334,18 @@ int git_config_string(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
+static char const *git_config_subst_userdir(char const *value) {
+	if (value[0] == '~' && value[1] == '/') {
+		const char *home = getenv("HOME");
+		char *userdir_excludes_file = malloc(strlen(home) + strlen(value)-1 + 1);
+		strcpy(userdir_excludes_file, home);
+		strcat(userdir_excludes_file, value+1);
+		return userdir_excludes_file;
+	} else {
+		return xstrdup(value);
+	}
+}
+
 static int git_default_core_config(const char *var, const char *value)
 {
 	/* This needs a better name */
@@ -456,8 +468,12 @@ static int git_default_core_config(const char *var, const char *value)
 	if (!strcmp(var, "core.editor"))
 		return git_config_string(&editor_program, var, value);
 
-	if (!strcmp(var, "core.excludesfile"))
-		return git_config_string(&excludes_file, var, value);
+	if (!strcmp(var, "core.excludesfile")) {
+		if (!value)
+			return config_error_nonbool(var);
+		excludes_file = git_config_subst_userdir(value);
+		return 0;
+	}
 
 	if (!strcmp(var, "core.whitespace")) {
 		if (!value)
Signature · 1 line
1.5.6.2
#1 lore raw
Quoted 2 lines · Karl Chen

Another idea is to have a non-absolute path be interpreted relative to the location of .gitconfig.

If we were to support relative paths, I think it would be useful and consistent if a relative path found in ".git/config" is relative to the work tree root, in "config" in a bare repository relative to the bare repository, and in "$HOME/.gitconfig" relative to $HOME. I am not sure what a relative path in "/etc/gitconfig" should be relative to, though.

However, this has a technical difficulty. When configuration values are read, the code that knows what the value means does not in general know which configuration file is being read from.

Quoted 4 lines

Below is a sample patch that works for me. We could also use getpwuid(getuid()) instead of getenv("HOME") to be consistent with user_path() but this is simpler and arguably more likely what the user wants when it matters.

It is quite likely that somebody would want you to interpret "~name/" if you advertize that you support "~/", so you would need to call getpwuid() eventually if you go down this path. I wonder how this would affect Windows folks.

What are the paths valued configuration variables other than excludesfile that we would want to support? There was a topic to allow mail-aliases lookup for parameters given to the "--author" option today, and send-email takes aliasfile configuration. Because the latter is a script, we would need a "--path" option to "git config" (the idea is similar to existing "--bool" option) so that calling scripts can ask the same "magic" performed to configuration variables' values before being reported.

author
#2 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Junio C Hamano

Junio> Karl Chen <[email protected]> writes:
>> Another idea is to have a non-absolute path be interpreted >> relative to the location of .gitconfig.

Junio> If we were to support relative paths, I think it would Junio> be useful and consistent if a relative path found in Junio> ".git/config" is relative to the work tree root, in Junio> "config" in a bare repository relative to the bare Junio> repository, and in "$HOME/.gitconfig" relative to Junio> $HOME.

Makes sense to support it everywhere. For .git/config, isn't it more consistent for it to be relative to .git?

Junio> I am not sure what a relative path in "/etc/gitconfig" Junio> should be relative to, though.

Why not just relative to the location of that file? Normally /etc, but if some distro customizes the location of /etc/gitconfig (/etc/git/config), or on non-Linux/posix systems it's somewhere else, or git is installed in /usr/local or /opt or $HOME, then it's still relative to the location of system gitconfig.

Junio> However, this has a technical difficulty. When Junio> configuration values are read, the code that knows what Junio> the value means does not in general know which Junio> configuration file is being read from.

Sounds like a refactoring issue.

Junio> It is quite likely that somebody would want you to Junio> interpret "~name/" if you advertize that you support Junio> "~/", so you would need to call getpwuid() eventually Junio> if you go down this path. I wonder how this would Junio> affect Windows folks.

I would be happy either way. Though since git uses getenv("HOME") to find ~/.gitconfig, I can see arguments for looking for the ignore file there also, in case it's different.

Junio> we would need a "--path" option to "git config" (the Junio> idea is similar to existing "--bool" option) so that Junio> calling scripts can ask the same "magic" performed to Junio> configuration variables' values before being reported.

Sounds fine.

So, being new to git development, am I correctly assessing your response as "with refinement this can be included in git"?

Relative paths and ~ (and $HOME) are all mutually compatible so they could all be implemented. If $HOME were supported directly (either just "$HOME" or parsing all $ENVVARS) then it'd be easier to decide to use getpwuid for ~. Personally I'd use: 1) relative path, 2) $HOME (as "~" or "$HOME"), 3) getpwuid (as "~")

Karl

#3 lore raw
Quoted 11 lines · Karl Chen
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Junio C Hamano

Junio> If we were to support relative paths, I think it would Junio> be useful and consistent if a relative path found in Junio> ".git/config" is relative to the work tree root, in Junio> "config" in a bare repository relative to the bare Junio> repository, and in "$HOME/.gitconfig" relative to Junio> $HOME.

Makes sense to support it everywhere. For .git/config, isn't it more consistent for it to be relative to .git?

Consistency and usefulness are different things. Suppose you want as the upstream of your project maintain and distribute a mail-alias list in-tree (say, the file is at the root level, CONTRIBUTORS), and you suggest contributors to use it when using "commit --author".

Which one do you want to write in your README:

	[user]
        	nicknamelistfile = ../CONTRIBUTORS

or

	[user]
        	nicknamelistfile = CONTRIBUTORS

You have to say the former if it is relative to .git/config.

Quoted 2 lines

So, being new to git development, am I correctly assessing your response as "with refinement this can be included in git"?

I do not have fundamental objection to what you are trying to achieve (i.e. being able to say "relative to $HOME"). I personally think the approach you took in your patch (i.e. only support "~/" and use $HOME, without any other fancy stuff) is a sensible first cut for that issue.

I just pointed out possible design issues about the future direction after that first cut. When I make comments on design-level issues, I rarely read the patch itself very carefully, so it is a different issue if your particular implementation in the patch is the best implementation of that first cut approach.

#4 lore raw
Quoted 10 lines · Junio C Hamano

Consistency and usefulness are different things. Suppose you want as the upstream of your project maintain and distribute a mail-alias list in-tree (say, the file is at the root level, CONTRIBUTORS), and you suggest contributors to use it when using "commit --author".

Which one do you want to write in your README:

	[user]
        	nicknamelistfile = ../CONTRIBUTORS
Quoted 6 lines

or

	[user]
        	nicknamelistfile = CONTRIBUTORS

You have to say the former if it is relative to .git/config.

Couldn't the exact opposite argument be made for "suppose you want to put the mail-alias file in a repo-specific directory that was not tracked?" I.e., you are trading off "CONTRIBUTORS" against ".git/CONTRIBUTORS". So which one inconveniences the smallest number of people is really a question of what people want to do with such pointers (and since we don't support any yet, we don't really know...).

But more worrisome to me is that the working directory and git directory do not necessarily follow a "../" and ".git/" relationship. How would you resolve "../foo" with:

GIT_DIR=/path/to/other/place git ...

or

  git --work-tree=/path/to/other/place

?

If you want to be able to point to either, I suspect we are better off simply introducing some basic substitutions like $GIT_DIR and $GIT_WORK_TREE. Maybe even just allow environment variable expansion, and then promise to set those variables, which takes care of $HOME automagically.

-Peff

#5 lore raw
Quoted 17 lines · Jeff King
Quoted 15 lines · Junio C Hamano

Consistency and usefulness are different things. Suppose you want as the upstream of your project maintain and distribute a mail-alias list in-tree (say, the file is at the root level, CONTRIBUTORS), and you suggest contributors to use it when using "commit --author".

Which one do you want to write in your README:

	[user]
        	nicknamelistfile = ../CONTRIBUTORS

or

	[user]
        	nicknamelistfile = CONTRIBUTORS
Quoted 6 lines
Quoted 1 line

You have to say the former if it is relative to .git/config.

Couldn't the exact opposite argument be made for "suppose you want to put the mail-alias file in a repo-specific directory that was not tracked?" I.e., you are trading off "CONTRIBUTORS" against ".git/CONTRIBUTORS".

No, I couldn't ;-)

Why would you write what you wrote in README?

Anything you store in .git is not propagated, so the instruction would not likely to be "store it in .git/CONTRIBUTORS and point at it". There is no merit in forcing users to standardize on "in .git". The instruction would be to "store it anywhere you want, and point at it".

The example I gave is very different. It points at an in-tree thing, and anybody who has worktree checked out will have it _at the location I as the README writer expect it to be_. That is the difference that makes the exact opposite argument much weaker.

That's why I suggested "relative to work tree if in .git/config, or gitdir if in config of a bare repository", although honestly speaking I do not have very strong preference either way.

Quoted 5 lines

If you want to be able to point to either, I suspect we are better off simply introducing some basic substitutions like $GIT_DIR and $GIT_WORK_TREE. Maybe even just allow environment variable expansion, and then promise to set those variables, which takes care of $HOME automagically.

Because we haven't deprecated core.worktree (or $GIT_WORK_TREE) yet, your suggestion has an obvious chicken-and-egg problem, even though otherwise I think it makes perfect sense and very much like it.

Perhaps we should rid of the worktree that is separate and floats unrelated to where $GIT_DIR is.

#6 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 4 lines

Couldn't the exact opposite argument be made for "suppose you want to put the mail-alias file in a repo-specific directory that was not tracked?" I.e., you are trading off "CONTRIBUTORS" against ".git/CONTRIBUTORS".

Quoted 8 lines

No, I couldn't ;-)

Why would you write what you wrote in README?

Anything you store in .git is not propagated, so the instruction would not likely to be "store it in .git/CONTRIBUTORS and point at it". There is no merit in forcing users to standardize on "in .git". The instruction would be to "store it anywhere you want, and point at it".

Ah, right.

I still think there is a little bit of convenience when you are doing something totally personal (i.e., not putting it in a README, but rather just wanting to store the referenced file in .git for the sake of simplicity). But in that case, it is only slightly less convenient to just point to the full path. So your example trumps this, since you have no sane way of knowing the full path in README instructions.

Quoted 3 lines

Because we haven't deprecated core.worktree (or $GIT_WORK_TREE) yet, your suggestion has an obvious chicken-and-egg problem, even though otherwise I think it makes perfect sense and very much like it.

You might be able to get around that by lazily filling in the variable. IOW, expand it at the point-of-use rather than while reading the config. However, the point of use might easily have something to do with reading config, so that re-creates the cycle.

In general, I think we treat config as order-independent. We could make the use of such variables order-dependent (i.e., if you haven't set core.worktree, then we give you the value without having set it, and we recalculate later. Confusing results, but at least a simple rule to understand).

I think there actually are a few other order-dependent things in the config, like the order of multi-value keys like push and fetch refspecs.

Would you want this expansion only for specially marked variables, or for all variables? I like the concept of general templates for config values, but it will backwards compatibility, especially for alias.*.

Quoted 2 lines

Perhaps we should rid of the worktree that is separate and floats unrelated to where $GIT_DIR is.

I assumed people were actually using it, which is why it was implemented.

-Peff

#7 lore raw
Quoted 5 lines · Jeff King
Quoted 2 lines

Perhaps we should rid of the worktree that is separate and floats unrelated to where $GIT_DIR is.

I assumed people were actually using it, which is why it was implemented.

Judging from the occasional "I tried core.worktree but it does not work in this and that situations" I see here and on #git, my impression is that new people try it, saying "git is cool -- unlike cvs that sprinkles those ugly CVS directories all over the place, it only contaminates my work tree with a single directory '.git' and nothing else. Ah, wait --- what's this core.worktree thing? Can I get rid of that last one as well? That sounds even cooler".

IOW, I do not think it is really _needed_ per-se as a feature, but it was done because it was thought to be doable, which unfortunately turned out to involve hair-pulling complexity that the two attempts that led to the current code still haven't resolved.

I really wish we do not have to worry about that anymore.

#8 lore raw

limiting relationship of git dir and worktree (was Re: [PATCH] Support "core.excludesfile = ~/.gitignore")

Quoted 8 lines · Junio C Hamano

Judging from the occasional "I tried core.worktree but it does not work in this and that situations" I see here and on #git, my impression is that new people try it, saying "git is cool -- unlike cvs that sprinkles those ugly CVS directories all over the place, it only contaminates my work tree with a single directory '.git' and nothing else. Ah, wait --- what's this core.worktree thing? Can I get rid of that last one as well? That sounds even cooler".

Quoted 6 lines

IOW, I do not think it is really _needed_ per-se as a feature, but it was done because it was thought to be doable, which unfortunately turned out to involve hair-pulling complexity that the two attempts that led to the current code still haven't resolved.

I really wish we do not have to worry about that anymore.

Well, as a non-user of this feature, I certainly have no argument against taking it out. Maybe the subject line will pull some other people into the discussion.

-Peff

#9 lore raw

Dropping core.worktree and GIT_WORK_TREE support (was Re: limiting relationship of git dir and worktree)

Quoted 15 lines · Jeff King
Quoted 13 lines · Junio C Hamano

Judging from the occasional "I tried core.worktree but it does not work in this and that situations" I see here and on #git, my impression is that new people try it, saying "git is cool -- unlike cvs that sprinkles those ugly CVS directories all over the place, it only contaminates my work tree with a single directory '.git' and nothing else. Ah, wait --- what's this core.worktree thing? Can I get rid of that last one as well? That sounds even cooler".

IOW, I do not think it is really _needed_ per-se as a feature, but it was done because it was thought to be doable, which unfortunately turned out to involve hair-pulling complexity that the two attempts that led to the current code still haven't resolved.

Quoted 5 lines
Quoted 1 line

I really wish we do not have to worry about that anymore.

Well, as a non-user of this feature, I certainly have no argument against taking it out. Maybe the subject line will pull some other people into the discussion.

Heh, if we are to do the attention-getter, let's do so more strongly ;-)

#10 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support (was Re: limiting relationship of git dir and worktree)

Quoted 1 line · Junio C Hamano

Heh, if we are to do the attention-getter, let's do so more strongly ;-)

Does this include removing of --work-tree as well?

The git backend of Pootle (http://translate.sourceforge.net/wiki/) uses it.

Also, here is a question:

$ git --git-dir git/.git --work-tree git diff --stat|tail -n 1
 1443 files changed, 0 insertions(+), 299668 deletions(-)

So, it's like it thinks every file is removed.

But then:

$ cd git
$ git diff --stat|wc -l
0

is this a bug, or a user error?

Thanks.

#11 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support

Quoted 7 lines · Miklos Vajna
Quoted 1 line · Junio C Hamano

Heh, if we are to do the attention-getter, let's do so more strongly ;-)

Does this include removing of --work-tree as well?

The git backend of Pootle (http://translate.sourceforge.net/wiki/) uses it.

Interesting. Does it use it because it can (meaning, --work-tree is supposed to work), or because --work-tree is the cleanest way to do what it wants to do (if the feature worked properly, that is, which is not the case)?

Quoted 7 lines

Also, here is a question:

$ git --git-dir git/.git --work-tree git diff --stat|tail -n 1
 1443 files changed, 0 insertions(+), 299668 deletions(-)

So, it's like it thinks every file is removed.

Quoted 7 lines

But then:

$ cd git
$ git diff --stat|wc -l
0

is this a bug, or a user error?

I think it is among the many other things that falls into "the two attempts still haven't resolved" category.

#12 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support

Quoted 9 lines · Junio C Hamano
Quoted 4 lines

Does this include removing of --work-tree as well?

The git backend of Pootle (http://translate.sourceforge.net/wiki/) uses it.

Interesting. Does it use it because it can (meaning, --work-tree is supposed to work), or because --work-tree is the cleanest way to do what it wants to do (if the feature worked properly, that is, which is not the case)?

It's like:

The current working directory is like /usr/lib/python2.5/site-packages/Pootle. The git repository is under /some/other/path/outside/usr.

Then Pootle has two possibilities:

1) save the current directory, change to /some/other, execute git, and change the directory back

2) use git --work-tree / --git-dir

I guess the second form is more elegant. Of course if it is decided that this option will be removed then the old form can be still used, but I think that would be a step back.

Quoted 9 lines
Quoted 9 lines

Also, here is a question:

$ git --git-dir git/.git --work-tree git diff --stat|tail -n 1
 1443 files changed, 0 insertions(+), 299668 deletions(-)

So, it's like it thinks every file is removed.

But then:

Quoted 8 lines
Quoted 5 lines
$ cd git
$ git diff --stat|wc -l
0

is this a bug, or a user error?

I think it is among the many other things that falls into "the two attempts still haven't resolved" category.

I'm unfamiliar with this part of the codebase, so in case somebody other could look at it, that would be great, but I'm happy with write a testcase for it. (Or in case nobody cares, I can try to fix it, but that may take a bit more time.)

#13 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support

Quoted 11 lines · Miklos Vajna
Quoted 9 lines · Junio C Hamano
Quoted 4 lines

Does this include removing of --work-tree as well?

The git backend of Pootle (http://translate.sourceforge.net/wiki/) uses it.

Interesting. Does it use it because it can (meaning, --work-tree is supposed to work), or because --work-tree is the cleanest way to do what it wants to do (if the feature worked properly, that is, which is not the case)?

Quoted 7 lines

It's like:

The current working directory is like /usr/lib/python2.5/site-packages/Pootle. The git repository is under /some/other/path/outside/usr.

Then Pootle has two possibilities:

The real question was about if/why that git repository _has to be_ outside of /usr/lib/*/Pootle/. Is that because --work-tree, if worked properly, would have allowed it to be, or is that because for some external reason you are not allowed to have .git under /usr/lib/*/Pootle/ directory?

If the latter, that shows the real requirement to keep supporting it as a feature, and issues around it need to be fixed. Otherwise, i.e. if it does not require use of --work-tree but it uses it only because it could, that gives us less incentive to keep --work-tree as a feature.

I haven't read the breakage and fix around diff in this thread yet, but I will when I get home.

Thanks to both Nguyen and you for trying to salvage --work-tree support.

#14 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support

Quoted 4 lines · Junio C Hamano

The real question was about if/why that git repository _has to be_ outside of /usr/lib/*/Pootle/. Is that because --work-tree, if worked properly, would have allowed it to be, or is that because for some external reason you are not allowed to have .git under /usr/lib/*/Pootle/ directory?

I'm not 100% sure, but I think the reason is that Pootle runs as the user 'pootle' which has write access to some /var/pootle or /home/pootle dir, but has no write access to /usr/lib/*/Pootle/.

Quoted 4 lines

If the latter, that shows the real requirement to keep supporting it as a feature, and issues around it need to be fixed. Otherwise, i.e. if it does not require use of --work-tree but it uses it only because it could, that gives us less incentive to keep --work-tree as a feature.

I think this is the latter, though as I mentioned previously - it can be still worked around by a "cd /other/path; git <command>; cd -" (speaking in shell commands), so it is not a "must", it would be just ugly IMHO.

#15 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support

In reply to Miklos Vajna
Quoted 45 lines · Miklos Vajna
Quoted 9 lines · Junio C Hamano
Quoted 4 lines

Does this include removing of --work-tree as well?

The git backend of Pootle (http://translate.sourceforge.net/wiki/) uses it.

Interesting. Does it use it because it can (meaning, --work-tree is supposed to work), or because --work-tree is the cleanest way to do what it wants to do (if the feature worked properly, that is, which is not the case)?

It's like:

The current working directory is like /usr/lib/python2.5/site-packages/Pootle. The git repository is under /some/other/path/outside/usr.

Then Pootle has two possibilities:

1) save the current directory, change to /some/other, execute git, and change the directory back

2) use git --work-tree / --git-dir

I guess the second form is more elegant. Of course if it is decided that this option will be removed then the old form can be still used, but I think that would be a step back.

Quoted 15 lines
Quoted 14 lines

Also, here is a question:

$ git --git-dir git/.git --work-tree git diff --stat|tail -n 1
 1443 files changed, 0 insertions(+), 299668 deletions(-)

So, it's like it thinks every file is removed.

But then:

$ cd git
$ git diff --stat|wc -l
0

is this a bug, or a user error?

Quoted 8 lines
Quoted 2 lines

I think it is among the many other things that falls into "the two attempts still haven't resolved" category.

I'm unfamiliar with this part of the codebase, so in case somebody other
could look at it, that would be great, but I'm happy with write a testcase for it. (Or in case nobody cares, I can try to fix it, but that may take a bit more time.)

Because "git diff" did not call setup_work_tree(). The same happens for "git diff-index" that someone reported recently. IIRC "git diff-files" has the same problem.

Signature · 1 line
Duy
#16 lore raw

PATCH 1/1 git diff/diff-index/diff-files: call setup_work_tree()

This makes it possible to use git diff when we are outside the repo but --work-tree and --git-dir is used.

---

Quoted 3 lines · Nguyen Thai Ngoc Duy

Because "git diff" did not call setup_work_tree(). The same happens for "git diff-index" that someone reported recently. IIRC "git diff-files" has the same problem.

Thanks, that was the problem.

 builtin-diff-files.c |    1 +
 builtin-diff-index.c |    1 +
 builtin-diff.c       |    1 +
 3 files changed, 3 insertions(+), 0 deletions(-)
builtin-diff-files.c +1 -0
index 9bf10bb..4802e00 100644
@@ -19,6 +19,7 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
 	int result;
 	unsigned options = 0;
 
+	setup_work_tree();
 	init_revisions(&rev, prefix);
 	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 	rev.abbrev = 0;
builtin-diff-index.c +1 -0
index 17d851b..b8e0656 100644
@@ -16,6 +16,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 	int i;
 	int result;
 
+	setup_work_tree();
 	init_revisions(&rev, prefix);
 	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 	rev.abbrev = 0;
builtin-diff.c +1 -0
index 7ffea97..86f9255 100644
@@ -244,6 +244,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int nongit;
 	int result = 0;
 
+	setup_work_tree();
 	/*
 	 * We could get N tree-ish in the rev.pending_objects list.
 	 * Also there could be M blobs there, and P pathspecs.
Signature · 1 line
1.6.0.rc3.17.gc14c8.dirty
#17 lore raw

Re: [PATCH] git diff/diff-index/diff-files: call setup_work_tree()

Quoted 5 lines · Miklos Vajna

diff --git a/builtin-diff-index.c b/builtin-diff-index.c
index 17d851b..b8e0656 100644
--- a/builtin-diff-index.c
+++ b/builtin-diff-index.c
@@ -16,6 +16,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)

Quoted 7 lines
        int i;
        int result;
 +       setup_work_tree();
        init_revisions(&rev, prefix);
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
        rev.abbrev = 0;

I think this is only needed when cached == 0

Quoted 5 lines

diff --git a/builtin-diff.c b/builtin-diff.c
index 7ffea97..86f9255 100644
--- a/builtin-diff.c
+++ b/builtin-diff.c
@@ -244,6 +244,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)

Quoted 8 lines
        int nongit;
        int result = 0;

+ setup_work_tree();
/*
* We could get N tree-ish in the rev.pending_objects list.
* Also there could be M blobs there, and P pathspecs.

No. git-diff has too many modes, some does not need worktree. This forces worktree on all modes.

Signature · 1 line
Duy
#18 lore raw

Re: [PATCH] git diff/diff-index/diff-files: call setup_work_tree()

Quoted 24 lines · Nguyen Thai Ngoc Duy
Quoted 5 lines · Miklos Vajna

diff --git a/builtin-diff-index.c b/builtin-diff-index.c
index 17d851b..b8e0656 100644
--- a/builtin-diff-index.c
+++ b/builtin-diff-index.c
@@ -16,6 +16,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)

Quoted 7 lines
        int i;
        int result;
 +       setup_work_tree();
        init_revisions(&rev, prefix);
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
        rev.abbrev = 0;

I think this is only needed when cached == 0

Quoted 8 lines

diff --git a/builtin-diff.c b/builtin-diff.c
index 7ffea97..86f9255 100644
--- a/builtin-diff.c
+++ b/builtin-diff.c
@@ -244,6 +244,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)

        int nongit;
        int result = 0;
Quoted 8 lines
Quoted 5 lines

+ setup_work_tree();
/*
* We could get N tree-ish in the rev.pending_objects list.
* Also there could be M blobs there, and P pathspecs.

No. git-diff has too many modes, some does not need worktree. This forces worktree on all modes.

Ah, yes. I just wanted to say that I forgot do a 'make test' and actually this breaks at least t0020-crlf.sh. I'll post a fixed patch in a bit.

Sorry.

#19 lore raw

PATCH 1/1 git diff/diff-index/diff-files: call setup_work_tree()

This makes it possible to use git diff when we are outside the repo but --work-tree and --git-dir is used.

---
 builtin-diff-files.c |    1 +
 builtin-diff-index.c |    2 ++
 builtin-diff.c       |    1 +
 3 files changed, 4 insertions(+), 0 deletions(-)
builtin-diff-files.c +1 -0
index 9bf10bb..4802e00 100644
@@ -19,6 +19,7 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
 	int result;
 	unsigned options = 0;
 
+	setup_work_tree();
 	init_revisions(&rev, prefix);
 	git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 	rev.abbrev = 0;
builtin-diff-index.c +2 -0
index 17d851b..5510291 100644
@@ -29,6 +29,8 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 		else
 			usage(diff_cache_usage);
 	}
+	if (!cached)
+		setup_work_tree();
 	if (!rev.diffopt.output_format)
 		rev.diffopt.output_format = DIFF_FORMAT_RAW;
 
builtin-diff.c +1 -0
index 7ffea97..57da6ed 100644
@@ -279,6 +279,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	diff_no_index(&rev, argc, argv, nongit, prefix);
 
 	/* Otherwise, we are doing the usual "git" diff */
+	setup_work_tree();
 	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 
 	if (nongit)
Signature · 1 line
1.6.0.rc3.17.gc14c8.dirty
#20 lore raw

Re: [PATCH] git diff/diff-index/diff-files: call setup_work_tree()

Quoted 8 lines · Miklos Vajna

diff --git a/builtin-diff.c b/builtin-diff.c

index 7ffea97..57da6ed 100644

--- a/builtin-diff.c
+++ b/builtin-diff.c

builtin-diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 285

Quoted 7 lines
builtin-diff.c +0 -0
@@ -279,6 +279,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
        diff_no_index(&rev, argc, argv, nongit, prefix);
 
        /* Otherwise, we are doing the usual "git" diff */
 +       setup_work_tree();
        rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 
        if (nongit)

At least builtin_diff_blobs() and builtin_diff_tree() won't need worktree, so NACK again. Anyway I'm not familiar with diff*. Junio should know these better.

Signature · 1 line
Duy
#21 lore raw

Re: [PATCH] git diff/diff-index/diff-files: call setup_work_tree()

Quoted 15 lines · Nguyen Thai Ngoc Duy
Quoted 14 lines · Miklos Vajna

diff --git a/builtin-diff.c b/builtin-diff.c

index 7ffea97..57da6ed 100644

--- a/builtin-diff.c
+++ b/builtin-diff.c

(quoted diff) +0 -0
@@ -279,6 +279,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
        diff_no_index(&rev, argc, argv, nongit, prefix);
 
        /* Otherwise, we are doing the usual "git" diff */
 +       setup_work_tree();
        rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 
Quoted 5 lines
Quoted 1 line

if (nongit)

At least builtin_diff_blobs() and builtin_diff_tree() won't need worktree, so NACK again. Anyway I'm not familiar with diff*. Junio should know these better.

How about doing it this way then?

* diff-files is about comparing with work tree, so it obviously needs a work tree;

* diff-index also does;

* no-index is about random files outside git context, so it obviously doesn't need any work tree;

* comparing two (or more) trees doesn't;

* comparing two blobs doesn't;

* comparing a blob with a random file doesn't;

What could be problematic is "git diff --cached". Strictly speaking, it compares the index and a tree so it shouldn't need any work tree. The same obviously applies to "git diff-index --cached".

While it is theoretically possible to have an index in a bare repository and build your history using it without using any worktree, I do not think it is a use case worth worrying about. As long as setup_work_tree() does not complain and die in such a setup, "diff --cached" itself won't look at the work tree (whereever random place setup_work_tree() sets it) at all, so probably it is a non issue. I dunno.

I do not have a test environment that uses a separate worktree settings, so this is obviously untested.

Perhaps people who are interested in keeping core.worktree alive can add test scripts in t/ somewhere to help salvaging the feature?

---

 builtin-diff.c |    3 +++
 git.c          |    4 ++--
 2 files changed, 5 insertions(+), 2 deletions(-)
builtin-diff.c +3 -0
index 7ffea97..06c85da 100644
@@ -114,6 +114,8 @@ static int builtin_diff_index(struct rev_info *revs,
 			      int argc, const char **argv)
 {
 	int cached = 0;
+
+	setup_work_tree();
 	while (1 < argc) {
 		const char *arg = argv[1];
 		if (!strcmp(arg, "--cached"))
@@ -207,6 +209,7 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
 	int result;
 	unsigned int options = 0;
 
+	setup_work_tree();
 	while (1 < argc && argv[1][0] == '-') {
 		if (!strcmp(argv[1], "--base"))
 			revs->max_count = 1;
git.c +2 -2
index 37b1d76..a8e730d 100644
@@ -286,8 +286,8 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
 		{ "describe", cmd_describe, RUN_SETUP },
 		{ "diff", cmd_diff },
-		{ "diff-files", cmd_diff_files, RUN_SETUP },
-		{ "diff-index", cmd_diff_index, RUN_SETUP },
+		{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
+		{ "diff-index", cmd_diff_index, RUN_SETUP | NEED_WORK_TREE },
 		{ "diff-tree", cmd_diff_tree, RUN_SETUP },
 		{ "fast-export", cmd_fast_export, RUN_SETUP },
 		{ "fetch", cmd_fetch, RUN_SETUP },
#22 lore raw

PATCH 1/1 diff*: fix worktree setup

This fixes "git diff", "git diff-files" and "git diff-index" to work correctly under worktree setup. Because diff* family works in many modes and not all of them require worktree, Junio made a nice summary (with a little modification from me):

* diff-files is about comparing with work tree, so it obviously needs a work tree;

* diff-index also does, except "diff-index --cached" or "diff --cached TREE"

* no-index is about random files outside git context, so it obviously doesn't need any work tree;

* comparing two (or more) trees doesn't;

* comparing two blobs doesn't;

* comparing a blob with a random file doesn't;

---
 builtin-diff-index.c |    2 +
 builtin-diff.c       |    3 ++
 git.c                |    2 +-
 t/t1501-worktree.sh  |   59 ++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 63 insertions(+), 3 deletions(-)
builtin-diff-index.c +2 -0
index 17d851b..0483749 100644
@@ -39,6 +39,8 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 	if (rev.pending.nr != 1 ||
 	    rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
 		usage(diff_cache_usage);
+	if (!cached)
+		setup_work_tree();
 	if (read_cache() < 0) {
 		perror("read_cache");
 		return -1;
builtin-diff.c +3 -0
index 7ffea97..037c303 100644
@@ -122,6 +122,8 @@ static int builtin_diff_index(struct rev_info *revs,
 			usage(builtin_diff_usage);
 		argv++; argc--;
 	}
+	if (!cached)
+		setup_work_tree();
 	/*
 	 * Make sure there is one revision (i.e. pending object),
 	 * and there is no revision filtering parameters.
@@ -225,6 +227,7 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
 	    (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
 		revs->combine_merges = revs->dense_combined_merges = 1;
 
+	setup_work_tree();
 	if (read_cache() < 0) {
 		perror("read_cache");
 		return -1;
git.c +1 -1
index 37b1d76..fdb0f71 100644
@@ -286,7 +286,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
 		{ "describe", cmd_describe, RUN_SETUP },
 		{ "diff", cmd_diff },
-		{ "diff-files", cmd_diff_files, RUN_SETUP },
+		{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
 		{ "diff-index", cmd_diff_index, RUN_SETUP },
 		{ "diff-tree", cmd_diff_tree, RUN_SETUP },
 		{ "fast-export", cmd_fast_export, RUN_SETUP },
t/t1501-worktree.sh +57 -2

75 lines not on this page · raw message

Signature · 1 line
1.6.0.96.g2fad1.dirty
#23 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support (was Re: limiting relationship of git dir and worktree)

In reply to Junio C Hamano

Junio C Hamano venit, vidit, dixit 25.08.2008 02:30:

Quoted 8 lines
Quoted 3 lines · Jeff King

Well, as a non-user of this feature, I certainly have no argument against taking it out. Maybe the subject line will pull some other people into the discussion.

Heh, if we are to do the attention-getter, let's do so more strongly ;-)

Sorry for being late to the discussion.

I think there are many use cases or environments which differ substantially from those of the "typical" developer; this implies that they differ from those of the typical git contributor, which naturally leads to a certain bias in discussions like this one.

"Typical" developers track source code in the proper sense (somewhere in $HOME); on local file systems; mostly on machines where they have root access, or least can get extra accounts (for gitosis) or a port for "git daemon" etc; they collaborate with peers for whom basically the same assumptions apply.

Now think of a user say in academics, who tracks "source code" for scientific papers (somewhere in $HOME) but also needs to track, e.g., central web pages or other "sources" where he has partial write access but can't have ".git" in place (and shouldn't change ownership & permissions), but needs to be aware of changes and log own changes; on NFS; no extra accounts but in need of an authenticated protocol (papers in progress are private, public only when published); who collaborates with peers for whom the same assumptions apply, except most certainly for git usage...

Yes, that's me, but also many others, I would think and hope, at least increasingly so. That second scenario is one where I have to cope with how things are set up centrally, making the best possible use of git.

I would imagine that many corporate environments are basically similar, if individual employees want to use git without central support.

These remarks apply to the discussion about an authenticated protocol (some way for secure, private pull&push for users with access to $HOME and maybe cgi-bins), but also here:

I need to keep .git away from the work tree for several projects. Using --git-dir etc. leads to problems with some commands, especially git{k,-gui,-citool}. I found the most robust solution to be an alias (shell) which guesses the work tree (from core.worktree etc.) and cd's there before doing anything. This also solves the problems with diff.

I would strongly advocate for keeping the possibility of separating git-dir and work-tree, and possibly dropping the assumption that everything "foo.git" is a bare repo. There are config variables for this. The Tcl/Tk family I mentioned makes even stronger assumptions. I promise to have a look at these when I find time (oh yeah...).

Michael

#24 lore raw

Re: Dropping core.worktree and GIT_WORK_TREE support (was Re: limiting relationship of git dir and worktree)

[resend: urgh, I somehow missed the git-list when replying, so it is cc'd here]

Quoted 10 lines · Michael J Gruber

"Typical" developers track source code in the proper sense (somewhere in $HOME); on local file systems; mostly on machines where they have root access, or least can get extra accounts (for gitosis) or a port for "git daemon" etc; they collaborate with peers for whom basically the same assumptions apply.

Now think of a user say in academics, who tracks "source code" for scientific papers (somewhere in $HOME) but also needs to track, e.g., central web pages or other "sources" where he has partial write access but can't have ".git" in place (and shouldn't change ownership &

Actually, I do all of those things, and I don't use the work-tree config variable or command line options at all. :)

I think the general advice with things like web access is "don't just dump your git stuff into a production area; instead, build and/or install from your git work tree into your production area". Because things like merges _can_ leave your files in a broken state.

But I do recognize that there are some special circumstances where that isn't possible, and you are willing to accept the tradeoff. E.g., if the checkout is extremely large and you can't afford another copy, if you have clueless collaborators who can't understand a build procedure.

And even though I expect those cases to be the exception, it seems a shame for git not to support split git-dir/work-tree setups because we really are 99% there. This code has been the source of a number of problems.

I think what is really needed is somebody to look carefully at the git startup sequence and figure out a sane set of rules for the order of:

- looking at env variables
- looking at config
- figuring out GIT_DIR and GIT_WORK_TREE
- chdir'ing to top level of work tree if necessary

Because we obviously have some corner cases where very confusing things are happening.

This is on my long term todo list, but my git time is very short at least for the next few months. I think it would be great if somebody else wanted to take the lead on this, and I would be happy to give pointers about some of the corner cases we have already seen.

-Peff

author
#25 lore raw

PATCH v2 1/1 Support "core.excludesfile = ~/.gitignore"

In reply to Junio C Hamano

The config variable core.excludesfile is parsed to substitute ~ and ~user with getpw entries.

---
 config.c |   41 +++++++++++++++++++++++++++++++++++++++--
 1 files changed, 39 insertions(+), 2 deletions(-)

Based on the discussion it sounds like there are complications to supporting relative paths (due to worktree config), and "$HOME" (when generalized, due to bootstrapping issues with $GIT_*).

Since ~ and ~user are orthogonal to these, can I suggest going forward with this, without blocking on those two?

I have reworked the patch to use getpw to support ~user. $HOME can eventually be supported via $ENVVARs.

config.c +39 -2
index 53f04a0..6a83c64 100644
@@ -334,6 +334,42 @@ int git_config_string(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
+/*
+ * Expand ~ and ~user.  Returns a newly malloced string.  (If input does not
+ * start with "~", equivalent to xstrdup.)
+ */
+static char *expand_userdir(const char *value) {
+	if (value[0] == '~') {
+		struct passwd *pw;
+		char *expanded_dir;
+		const char *slash = strchr(value+1, '/');
+		const char *after_username = slash ? slash : value+strlen(value);
+		if (after_username == value+1) {
+			pw = getpwuid(getuid());
+			if (!pw) die("You don't exist!");
+		} else {
+			char save = *after_username;
+			*(char*)after_username = '\0';
+			pw = getpwnam(value+1);
+			if (!pw) die("No such user: '%s'", value+1);
+			*(char*)after_username = save;
+		}
+		expanded_dir = xmalloc(strlen(pw->pw_dir) + strlen(after_username) + 1);
+		strcpy(expanded_dir, pw->pw_dir);
+		strcat(expanded_dir, after_username);
+		return expanded_dir;
+	} else {
+		return xstrdup(value);
+	}
+}
+
+int git_config_userdir(const char **dest, const char *var, const char *value) {
+	if (!value)
+		return config_error_nonbool(var);
+	*dest = expand_userdir(value);
+	return 0;
+}
+
 static int git_default_core_config(const char *var, const char *value)
 {
 	/* This needs a better name */
@@ -456,8 +492,9 @@ static int git_default_core_config(const char *var, const char *value)
 	if (!strcmp(var, "core.editor"))
 		return git_config_string(&editor_program, var, value);
 
-	if (!strcmp(var, "core.excludesfile"))
-		return git_config_string(&excludes_file, var, value);
+	if (!strcmp(var, "core.excludesfile")) {
+		return git_config_userdir(&excludes_file, var, value);
+	}
 
 	if (!strcmp(var, "core.whitespace")) {
 		if (!value)
Signature · 1 line
1.5.6.2
#26 lore raw

config.c int git_config_string(const char **dest, const char *var, const char *value) · line 341

Quoted 5 lines · Karl Chen
config.c +5 -0
+/*
+ * Expand ~ and ~user.  Returns a newly malloced string.  (If input does not
+ * start with "~", equivalent to xstrdup.)
+ */
+static char *expand_userdir(const char *value) {

There is user_path() in path.c that does the same thing.

Watch your style: The opening brace of functions is on the next line.

-- Hannes

#27 lore raw
In reply to Karl Chen
Quoted 3 lines · Karl Chen

Based on the discussion it sounds like there are complications to supporting relative paths (due to worktree config), and "$HOME" (when generalized, due to bootstrapping issues with $GIT_*).

I think that is fine for now. One other simple possibility would be to expand _just_ $HOME, and then if we later decided to do all environment variables it would naturally encompass that. However, we might want to support "~" then anyway, so I think doing "~" first is fine.

However, there are two problems with the patch:

1. It should probably re-use path.c:user_path, as Johannes mentioned.

2. There is no documentation update.

Also, are there any other config variables which would benefit from this substitution (I can't think of any off-hand, but there are quite a few I don't use).

-Peff

author
#28 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jeff King

Jeff> 1. It should probably re-use path.c:user_path, as
Jeff> Johannes mentioned.

That function has the wrong interface for this task (requires extra strdup, imposes PATH_MAX, conflates all error conditions into returning NULL, also returns NULL if input doesn't have "~"). Do you still think it should re-use that function?

#29 lore raw
Quoted 9 lines · Karl Chen
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jeff King

Jeff> 1. It should probably re-use path.c:user_path, as
Jeff> Johannes mentioned.

That function has the wrong interface for this task (requires extra strdup, imposes PATH_MAX, conflates all error conditions into returning NULL, also returns NULL if input doesn't have "~"). Do you still think it should re-use that function?

On the other hand you have a pair of ugly "casting a const pointer down, temporarily modifying what is const" in your version.

In any case, I think their point is that these two functions are meant to do the same thing, and a unified interface would be desirable.

You do not necessarily have to use user_path() from path.c, but user_path(), if its interface is coarser, could become a thin wrapper around yours, don't you think? Even better, perhaps the current callers of user_path() may benefit from finer distinction among error conditions (I didn't look, though).

author
#30 lore raw

PATCH v3 1/1 Expand ~ and ~user in core.excludesfile, commit.template

These config variables are parsed to substitute ~ and ~user with getpw entries.

user_path() refactored into new function expand_user_path(), to allow dynamically allocating the return buffer.

---

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines · Junio C Hamano

On 2008-08-26 17:25 PDT, Jeff King writes:

Jeff> 1. It should probably re-use path.c:user_path, as Jeff> Johannes mentioned.
>>
>> That function has the wrong interface for this task >> (requires extra strdup, imposes PATH_MAX, conflates all >> error conditions into returning NULL, also returns NULL if >> input doesn't have "~"). Do you still think it should >> re-use that function?

Junio> On the other hand you have a pair of ugly "casting a Junio> const pointer down, temporarily modifying what is Junio> const" in your version.

user_path() does the same thing; it's just obscured by the lack of type qualifiers. I rationalized it because git has much bigger problems if threading support were added. But I agree it's ugly. The new patch avoids it.

Junio> In any case, I think their point is that these two Junio> functions are meant to do the same thing, and a unified Junio> interface would be desirable.

There is a lot of refactoring I would do were I maintaining the code. I figured a minimally invasive change would be more easily accepted. Anyway here is a patch to unify "~" expansion.

 builtin-commit.c |    2 +-
 cache.h          |    2 +
 config.c         |   13 +++++++-
 path.c           |   88 +++++++++++++++++++++++++++++++++--------------------
 4 files changed, 69 insertions(+), 36 deletions(-)
builtin-commit.c +1 -1
index 649c8be..e510207 100644
@@ -891,7 +891,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 static int git_commit_config(const char *k, const char *v, void *cb)
 {
 	if (!strcmp(k, "commit.template"))
-		return git_config_string(&template_file, k, v);
+		return git_config_userdir(&template_file, k, v);
 
 	return git_status_config(k, v, cb);
 }
cache.h +2 -0
index ab9f97e..096fd9d 100644
@@ -527,6 +527,7 @@ int git_config_perm(const char *var, const char *value);
 int adjust_shared_perm(const char *path);
 int safe_create_leading_directories(char *path);
 int safe_create_leading_directories_const(const char *path);
+extern char *expand_user_path(char *buf, const char *path, int sz);
 char *enter_repo(char *path, int strict);
 static inline int is_absolute_path(const char *path)
 {
@@ -748,6 +749,7 @@ extern unsigned long git_config_ulong(const char *, const char *);
 extern int git_config_bool_or_int(const char *, const char *, int *);
 extern int git_config_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
+extern int git_config_userdir(const char **, const char *, const char *);
 extern int git_config_set(const char *, const char *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
config.c +11 -2
index 53f04a0..3395283 100644
@@ -334,6 +334,14 @@ int git_config_string(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
+int git_config_userdir(const char **dest, const char *var, const char *value) {
+	if (!value)
+		return config_error_nonbool(var);
+	*dest = expand_user_path(NULL, value, 0);
+	if (!*dest || !**dest) die("Failed to expand user dir in: '%s'", value);
+	return 0;
+}
+
 static int git_default_core_config(const char *var, const char *value)
 {
 	/* This needs a better name */
@@ -456,8 +464,9 @@ static int git_default_core_config(const char *var, const char *value)
 	if (!strcmp(var, "core.editor"))
 		return git_config_string(&editor_program, var, value);
 
-	if (!strcmp(var, "core.excludesfile"))
-		return git_config_string(&excludes_file, var, value);
+	if (!strcmp(var, "core.excludesfile")) {
+		return git_config_userdir(&excludes_file, var, value);
+	}
 
 	if (!strcmp(var, "core.whitespace")) {
 		if (!value)
path.c +55 -33

105 lines not on this page · raw message

Signature · 1 line
1.5.6.3
#31 lore raw

Re: [PATCH v3] Expand ~ and ~user in core.excludesfile, commit.template

Quoted 5 lines · Karl Chen
 builtin-commit.c |    2 +-
 cache.h          |    2 +
 config.c         |   13 +++++++-
 path.c           |   88 +++++++++++++++++++++++++++++++++--------------------
 4 files changed, 69 insertions(+), 36 deletions(-)

Documentation?

builtin-commit.c static void print_summary(const char *prefix, const unsigned char *sha1) · line 894

Quoted 3 lines
builtin-commit.c +1 -1
 	if (!strcmp(k, "commit.template"))
-		return git_config_string(&template_file, k, v);
+		return git_config_userdir(&template_file, k, v);

I like this.

config.c int git_config_string(const char **dest, const char *var, const char *value) · line 343

Quoted 7 lines
config.c +7 -0
+int git_config_userdir(const char **dest, const char *var, const char *value) {
+	if (!value)
+		return config_error_nonbool(var);
+	*dest = expand_user_path(NULL, value, 0);
+	if (!*dest || !**dest) die("Failed to expand user dir in: '%s'", value);
+	return 0;
+}

I am not sure about !**dest here. This precludes somebody from using "". While it might not matter here, if there are other users of git_config_userdir(), they might want to allow a blank entry.

Also, style: there should be a newline after conditional but before executed code. IOW, replace

  if (cond) code;

with

if (cond)

          code;

path.c int validate_headref(const char *path) · line 141

Quoted 2 lines
path.c +2 -0
+static inline struct passwd *getpw_strspan(const char *begin_username,
+										   const char *end_username) 

1. There seem to be extra tabs in the second line, pushing the end_username argument way too far to the right.

2. I'm not sure "strspan" is a good name for this helper, since it calls to mind the strspn C function, which is not really related to this at all.

3. Usually helper functions that take a non-terminated string like this in git use the combination of (char *begin, int len) instead of two pointers. While you are currently the only user of the helper, I think it makes sense to follow that convention for future users.

path.c int validate_headref(const char *path) · line 145

Quoted 3 lines
path.c +3 -0
+	if (begin_username == end_username) {
+		return getpwuid(getuid());
+	} else {

Style: omit braces on one-liner conditionals:

  if (begin_username == end_username)
          return getwpuid(getuid());

Also, you do a lot of early returns in your code. I think this is good, because it makes it more readable. But that means you don't have to worry about "else"ing the other half of the conditional, because you have already returned. Which makes it even easier to read.

path.c int validate_headref(const char *path) · line 146

Quoted 1 line
path.c +1 -0
+		size_t username_len = end_username - begin_username;

See, here you end up converting back from two pointers to a pointer and a length. Which is why I think we tend to use the other representation.

path.c int validate_headref(const char *path) · line 147

Quoted 1 line
path.c +1 -0
+		char *username = alloca(username_len + 1);

I don't think we use alloca() anywhere else. I don't know if there are portability issues.

path.c int validate_headref(const char *path) · line 163

Quoted 10 lines
path.c +10 -0
+static inline char *concatstr(char *buf, const char *str1, const char *str2,
+							  size_t bufsz)
+{
+	size_t len1 = strlen(str1);
+	size_t len2 = strlen(str2);
+	size_t needbuflen = len1 + len2 + 1;
+	if (buf) {
+		if (needbuflen > bufsz) return NULL;
+	} else {
+		buf = xmalloc(needbuflen);

Style: more braces which can be omitted.

This function seems a little superfluous, since its semantics are so specific to this usage. I am all for splitting into little functions, but I think it would be quite confusing for somebody to try reusing this. Perhaps it at least needs a comment explaining the semantics of buf?

path.c int validate_headref(const char *path) · line 174

Quoted 5 lines
path.c +5 -0
+static inline const char *strchr_or_end(const char *str, char c)
+{
+	while (*str && *str != c) ++str;
+	return str;
+}

This really seems like premature optimization to me. The only advantage this has over

  p = strchr(s);
  if (!p)
    p = s + strlen(s);

is that we avoid traversing the string once. But balance that against an assembler-optimized strchr provided by your libc. And then wonder if it is even worth it, since this is not even remotely a critical path.

path.c int validate_headref(const char *path) · line 192

Quoted 11 lines
path.c +9 -0
+{
+	if (path == NULL) {
 		return NULL;
[...]
+	} else if (path[0] != '~') {
+		if (buf == NULL) {
+			return xstrdup(path);
+		} else {
+			if (strlen(path)+1 > sz) return NULL;
+			return strcpy(buf, path);
+		}

More early returns which can be removed from conditionals.

Also, some of this code seems duplicated with concatstr. Wouldn't it just be simpler to let concatstr take a NULL for one of the arguments, and then just use it again here? IOW, something like:

  if (!path)
    return NULL;
  if (path[0] != '~')
    return concatstr(path, NULL);

path.c char *enter_repo(char *path, int strict) · line 246

Quoted 2 lines
path.c +1 -1
-			if (!user_path(used_path, path, PATH_MAX))
+			if (!expand_user_path(used_path, path, PATH_MAX))

But these functions don't have the same semantics, do they? user_path used to return NULL if the path didn't start with ~, right?

-Peff

#32 lore raw

Re: [PATCH v3] Expand ~ and ~user in core.excludesfile, commit.template

Quoted 8 lines · Jeff King
Quoted 5 lines · Karl Chen
 builtin-commit.c |    2 +-
 cache.h          |    2 +
 config.c         |   13 +++++++-
 path.c           |   88 +++++++++++++++++++++++++++++++++--------------------
 4 files changed, 69 insertions(+), 36 deletions(-)
Quoted 7 lines

Documentation?

builtin-commit.c static void print_summary(const char *prefix, const unsigned char *sha1) · line 894

Quoted 3 lines
builtin-commit.c +1 -1
 	if (!strcmp(k, "commit.template"))
-		return git_config_string(&template_file, k, v);
+		return git_config_userdir(&template_file, k, v);

I like this.

Likewise, except for the name. It is more like "pathname"; "userdir" is stressing only one aspect of the magic we would do to a value that is a pathname compared to a value that is a string without any magic.

Quoted 11 lines

config.c int git_config_string(const char **dest, const char *var, const char *value) · line 343

Quoted 7 lines
config.c +7 -0
+int git_config_userdir(const char **dest, const char *var, const char *value) {
+	if (!value)
+		return config_error_nonbool(var);
+	*dest = expand_user_path(NULL, value, 0);
+	if (!*dest || !**dest) die("Failed to expand user dir in: '%s'", value);
+	return 0;
+}

I am not sure about !**dest here. This precludes somebody from using "". While it might not matter here, if there are other users of git_config_userdir(), they might want to allow a blank entry.

True again.

Quoted 5 lines

path.c int validate_headref(const char *path) · line 145

Quoted 3 lines
path.c +3 -0
+	if (begin_username == end_username) {
+		return getpwuid(getuid());
+	} else {

Style: omit braces on one-liner conditionals:

... except in cases like this where "else" side is a multi-statement block, in which case the above is fine. But as you pointed out, the early return from here makes the else block unnecessary so you do not need the braces around "if" side.

Quoted 4 lines

path.c int validate_headref(const char *path) · line 147

Quoted 1 line
path.c +1 -0
+		char *username = alloca(username_len + 1);

I don't think we use alloca() anywhere else. I don't know if there are portability issues.

Avoidance of alloca() and c99 dynamic array on stack is deliberate in the current codebase. Portable use of alloca() is quite hard to get right.

Quoted 7 lines

path.c int validate_headref(const char *path) · line 174

Quoted 5 lines
path.c +5 -0
+static inline const char *strchr_or_end(const char *str, char c)
+{
+	while (*str && *str != c) ++str;
+	return str;
+}

This really seems like premature optimization to me.

So is overuse of inline, it seems.

author
#33 lore raw

PATCH v4 1/1 Expand ~ and ~user in core.excludesfile, commit.template

These config variables are parsed to substitute ~ and ~user with getpw entries.

user_path() refactored into new function expand_user_path(), to allow dynamically allocating the return buffer.

---

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jeff King

Peff> Documentation?

Documentation added.

Peff> I am not sure about !**dest here. This precludes Peff> somebody from using "". While it might not matter here, Peff> if there are other users of git_config_userdir(), they Peff> might want to allow a blank entry.

Point taken.

Peff> 1. There seem to be extra tabs in the second line,
Peff> pushing the end_username argument way too far to the
Peff> right.

I had assumed that you guys use tab-width 4 instead of c-basic-offset 8. This is one of the reasons I never use tabs when I can help it.

Peff> 2. I'm not sure "strspan" is a good name for this
Peff> helper, since it calls to mind the strspn C function,
Peff> which is not really related to this at all.

Changed to getpw_str().

Peff> 3. Usually helper functions that take a non-terminated
Peff> string like this in git use the combination of (char
Peff> *begin, int len) instead of two pointers. While you
Peff> are currently the only user of the helper, I think it
Peff> makes sense to follow that convention for future
Peff> users.

Point taken, changed as suggested.

Peff> Also, you do a lot of early returns in your code. I Peff> think this is good, because it makes it more Peff> readable. But that means you don't have to worry about Peff> "else"ing the other half of the conditional, because you Peff> have already returned. Which makes it even easier to Peff> read.

Personally I think early returns without "else"ing is appropriate for error conditions but not when it's a "do this" or "do that" switch. (I wonder if indenting 8 spaces has a long-term effect on things like this?) Anyway, I accept the color you picked for this bikeshed.

Peff> This function seems a little superfluous, since its Peff> semantics are so specific to this usage. I am all for Peff> splitting into little functions, but I think it would be Peff> quite confusing for somebody to try reusing Peff> this. Perhaps it at least needs a comment explaining the Peff> semantics of buf?

Comment added.

Peff> Also, some of this code seems duplicated with Peff> concatstr. Wouldn't it just be simpler to let concatstr Peff> take a NULL for one of the arguments, and then just use Peff> it again here? IOW, something like:

Peff> if (!path)
Peff> return NULL;
Peff> if (path[0] != '~')
Peff> return concatstr(path, NULL);

Refactored as suggested.

Quoted 2 lines
Quoted 2 lines

- if (!user_path(used_path, path, PATH_MAX))
+ if (!expand_user_path(used_path, path, PATH_MAX))

Peff> But these functions don't have the same semantics, do Peff> they? user_path used to return NULL if the path didn't Peff> start with ~, right?

Yes, but user_path was only called when the input starts with ~.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · Junio C Hamano

+ return git_config_userdir(&template_file, k, v);

Junio> It is more like "pathname"; "userdir" is stressing only Junio> one aspect of the magic we would do to a value that is Junio> a pathname compared to a value that is a string without Junio> any magic.

Renamed as suggested.

Junio> Avoidance of alloca() and c99 dynamic array on stack is Junio> deliberate in the current codebase. Portable use of Junio> alloca() is quite hard to get right.

Alloca replaced with xmalloc+free.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Johannes Sixt

Hannes> Use strchrnul() instead of a home-grown Hannes> strchr_or_end().

Didn't know about strchrnul, thanks.

Hannes> You really should use the strbuf API here. Look for Hannes> strbuf_detach() in the existing code.

Unfortunately expand_user_path() needs to support both a fixed buffer and mallocing return. I don't think the strbuf API can do that easily?

 Documentation/config.txt |    4 ++-
 builtin-commit.c         |    2 +-
 cache.h                  |    2 +
 config.c                 |   11 +++++-
 path.c                   |   86 +++++++++++++++++++++++++++++-----------------
 5 files changed, 70 insertions(+), 35 deletions(-)
Documentation/config.txt +3 -1
index af57d94..05e846d 100644
@@ -346,7 +346,8 @@ Common unit suffixes of 'k', 'm', or 'g' are supported.
 core.excludesfile::
 	In addition to '.gitignore' (per-directory) and
 	'.git/info/exclude', git looks into this file for patterns
-	of files which are not meant to be tracked.  See
+	of files which are not meant to be tracked.  "~" and "~user"
+	are expanded to the user's home directory.  See
 	linkgit:gitignore[5].
 
 core.editor::
@@ -554,6 +555,7 @@ color.status.<slot>::
 
 commit.template::
 	Specify a file to use as the template for new commit messages.
+	"~" and "~user" are expanded to the user's home directory.
 
 color.ui::
 	When set to `always`, always use colors in all git commands which
builtin-commit.c +1 -1
index 649c8be..905ebde 100644
@@ -891,7 +891,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 static int git_commit_config(const char *k, const char *v, void *cb)
 {
 	if (!strcmp(k, "commit.template"))
-		return git_config_string(&template_file, k, v);
+		return git_config_pathname(&template_file, k, v);
 
 	return git_status_config(k, v, cb);
 }
cache.h +2 -0
index ab9f97e..3e04794 100644
@@ -527,6 +527,7 @@ int git_config_perm(const char *var, const char *value);
 int adjust_shared_perm(const char *path);
 int safe_create_leading_directories(char *path);
 int safe_create_leading_directories_const(const char *path);
+extern char *expand_user_path(char *buf, const char *path, int sz);
 char *enter_repo(char *path, int strict);
 static inline int is_absolute_path(const char *path)
 {
@@ -748,6 +749,7 @@ extern unsigned long git_config_ulong(const char *, const char *);
 extern int git_config_bool_or_int(const char *, const char *, int *);
 extern int git_config_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
+extern int git_config_pathname(const char **, const char *, const char *);
 extern int git_config_set(const char *, const char *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
config.c +10 -1
index 53f04a0..55353d9 100644
@@ -334,6 +334,15 @@ int git_config_string(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
+int git_config_pathname(const char **dest, const char *var, const char *value) {
+	if (!value)
+		return config_error_nonbool(var);
+	*dest = expand_user_path(NULL, value, 0);
+	if (!*dest)
+		die("Failed to expand user dir in: '%s'", value);
+	return 0;
+}
+
 static int git_default_core_config(const char *var, const char *value)
 {
 	/* This needs a better name */
@@ -457,7 +466,7 @@ static int git_default_core_config(const char *var, const char *value)
 		return git_config_string(&editor_program, var, value);
 
 	if (!strcmp(var, "core.excludesfile"))
-		return git_config_string(&excludes_file, var, value);
+		return git_config_pathname(&excludes_file, var, value);
 
 	if (!strcmp(var, "core.whitespace")) {
 		if (!value)
path.c +54 -32

107 lines not on this page · raw message

Signature · 1 line
1.5.6.3
#34 lore raw

Re: [PATCH v4] Expand ~ and ~user in core.excludesfile, commit.template

Documentation/config.txt Common unit suffixes of 'k', 'm', or 'g' are supported. · line 351

Quoted 7 lines · Karl Chen

core.excludesfile::

Documentation/config.txt +2 -1
 	In addition to '.gitignore' (per-directory) and
 	'.git/info/exclude', git looks into this file for patterns
-	of files which are not meant to be tracked.  See
+	of files which are not meant to be tracked.  "~" and "~user"
+	are expanded to the user's home directory.  See
 	linkgit:gitignore[5].

How about:

A leading "~" or "~user" is expanded to the home directory of the current user or "user", as in the shell.

Specifically:

1. We obviously handle only leading cases, so /path/to/~file~with~tildes is ok.

2. It was a little unclear to me whether both "~" and "~user" expande to the same thing. I.e., can one use this for arbitrary "user" (and the answer of course is yes).

path.c int validate_headref(const char *path) · line 195

Quoted 11 lines
path.c +11 -0
+char *expand_user_path(char *buf, const char *path, int bufsz)
+{
+	if (path == NULL)
+		return NULL;
+
+	if (path[0] != '~')
+		return concatstr(buf, path, NULL, bufsz);
+
+	const char *username = path + 1;
+	size_t username_len = strchrnul(username, '/') - username;
+	struct passwd *pw = getpw_str(username, username_len);

Declaration after statement (we try to remain portable to non-C99 systems).

Other than that, I think the patch is fine (though I am not opposed to the improvements Junio has mentioned).

-Peff

#35 lore raw

Re: [PATCH v4] Expand ~ and ~user in core.excludesfile, commit.template

In reply to Karl Chen
Quoted 7 lines · Karl Chen

These config variables are parsed to substitute ~ and ~user with getpw entries.

user_path() refactored into new function expand_user_path(), to allow dynamically allocating the return buffer.

Thanks.

Quoted 2 lines

... Anyway, I accept the color you picked for this bikeshed.

I do not think Documentation/CodingStyle is bikesheding but just behaving like Romans do while in Rome, so that the end result will blend in better.

Quoted 6 lines

Hannes> You really should use the strbuf API here. Look for Hannes> strbuf_detach() in the existing code.

Unfortunately expand_user_path() needs to support both a fixed buffer and mallocing return. I don't think the strbuf API can do that easily?

I do not see any strong reason why the single caller of user_path() has to keep using the static allocation. Would it help to reduce the complexity of your expand_user_path() implementation, if we fixed the caller along the lines of this patch (untested, but just to illustrate the point)?

 path.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)
path.c +9 -6
index 76e8872..c5b253c 100644
@@ -221,19 +221,22 @@ char *enter_repo(char *path, int strict)
 		if (PATH_MAX <= len)
 			return NULL;
 		if (path[0] == '~') {
-			if (!user_path(used_path, path, PATH_MAX))
+			char *newpath = expand_user_path(path);
+			if (!newpath || (PATH_MAX <= strlen(newpath))) {
+				if (path != newpath)
+					free(newpath);
 				return NULL;
+			}
 			strcpy(validated_path, path);
-			path = used_path;
-		}
-		else if (PATH_MAX - 10 < len)
-			return NULL;
-		else {
+			path = newpath;
+		} else {
 			path = strcpy(used_path, path);
 			strcpy(validated_path, path);
 		}
 		len = strlen(path);
 		for (i = 0; suffix[i]; i++) {
+			if (PATH_MAX <= strlen(suffix[i] + len))
+				continue;
 			strcpy(path + len, suffix[i]);
 			if (!access(path, F_OK)) {
 				strcat(validated_path, suffix[i]);
author
#36 lore raw

Re: [PATCH v4] Expand ~ and ~user in core.excludesfile, commit.template

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Junio C Hamano

Junio> I do not see any strong reason why the single caller of Junio> user_path() has to keep using the static allocation. Junio> Would it help to reduce the complexity of your Junio> expand_user_path() implementation, if we fixed the Junio> caller along the lines of this patch (untested, but Junio> just to illustrate the point)?

Yes, expand_user_path() would be much simpler, it would basically be me original implementation except for returning NULL on error.

#37 lore raw

Re: [PATCH v4] Expand ~ and ~user in core.excludesfile, commit.template

Quoted 11 lines · Karl Chen
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Junio C Hamano

Junio> I do not see any strong reason why the single caller of Junio> user_path() has to keep using the static allocation. Junio> Would it help to reduce the complexity of your Junio> expand_user_path() implementation, if we fixed the Junio> caller along the lines of this patch (untested, but Junio> just to illustrate the point)?

Yes, expand_user_path() would be much simpler, it would basically be me original implementation except for returning NULL on error.

Yeah, modulo those styles issues your v3 and v4 addressed, and use of strbuf.

It might feel that we went full circles, wasting your time. But it's not. We found out that the final series would look like this:

[1/3] Introduce expand_user_path();

[2/3] Using #1, introduce git_config_pathname() and use it to parse your

       two variables;

[3/3] Update the sole caller of user_path() to use expand_user_path().

Patch #1 and #2 can be squashed into one if you want. Also you do not have to do #3 yourself if you do not feel like it (but now we know how the code would look like, why not?).

Thanks to these three initial rounds, we know whoever implements #1 knows what kind of interface the (to-be-rewritten) user of user_path() would want, so #3 will become much cleaner. We made progress.

Thanks.

author
#38 lore raw

Re: [PATCH v4] Expand ~ and ~user in core.excludesfile, commit.template

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Junio C Hamano

Junio> [3/3] Update the sole caller of user_path() to use Junio> expand_user_path().

Actually I just looked closer at enter_repo() and it's not quite as simple as your proposed patch, because enter_repo() wants to concatenate suffixes like ".git". So either the malloced string would have to be copied to the static buffer again, or return a strbuf, or take an argument for allocating extra chars.

Wow, I'd forgotten how much work it is to do string manipulation in C.

#39 lore raw

Re: [PATCH v4] Expand ~ and ~user in core.excludesfile, commit.template

Quoted 8 lines · Karl Chen
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Junio C Hamano

Junio> [3/3] Update the sole caller of user_path() to use Junio> expand_user_path().

Actually I just looked closer at enter_repo() and it's not quite as simple as your proposed patch, because enter_repo() wants to concatenate suffixes like ".git".

I thought the "just an illustration" patch at least took care of that part.

The thing is that enter_repo() is not performance critical, it is where server side programs validate the repository path received from the other end of the network, and we can be stricter than necessary about path lengths. I do not particularly think it is necessary to convert it and use strbuf to allow arbitrarily long paths --- rejecting requests to an absurdly deep directory is not an end of the world there.

#40 lore raw

Re: [PATCH v3] Expand ~ and ~user in core.excludesfile, commit.template

In reply to Karl Chen

path.c int validate_headref(const char *path) · line 194

Quoted 1 line · Karl Chen
path.c +1 -0
+		const char *after_username = strchr_or_end(path+1, '/');

Use strchrnul() instead of a home-grown strchr_or_end().

path.c int validate_headref(const char *path) · line 197

Quoted 3 lines
path.c +3 -0
+		struct passwd *pw = getpw_strspan(path+1, after_username);
+		if (!pw) return NULL;
+		return concatstr(buf, pw->pw_dir, after_username, sz);

You really should use the strbuf API here. Look for strbuf_detach() in the existing code.

-- Hannes

author
#41 lore raw
In reply to Jeff King
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jeff King

Jeff> 2. There is no documentation update.

Relative paths and $ENVVARS would need explanation; not sure what needs to be said about ~user since the new behavior is what people expect to just work. Would it go in git-config.txt if something were added?

#42 lore raw
Quoted 8 lines · Karl Chen
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jeff King

Jeff> 2. There is no documentation update.

Relative paths and $ENVVARS would need explanation; not sure what needs to be said about ~user since the new behavior is what people expect to just work. Would it go in git-config.txt if something were added?

Traditionally, we never has interpret ~/ or ~user/. Users expected that they need to spell it like:

[core]
excludesfile = "/home/joe/.my-ignore-pattern"

Especially since this is called "variables", it is natural, without explanation, to expect this not to work, just like this use of a variable won't work:

        $ EDITOR='~/bin/editor' ;# notice the single quote
        $ export EDITOR;
        $ git commit -a
        fatal: exec ~/bin/editor failed.

Your patch improves the situation and now allow:

[core]
excludesfile = "~/.my-ignore-pattern"

If you do not document that it is now possible for this particular configuration variable, the users will never know.

#43 lore raw
In reply to Karl Chen

config.c int git_config_string(const char **dest, const char *var, const char *value) · line 338

Quoted 2 lines · Karl Chen <quarl <at> cs.berkeley.edu> writes:
config.c +2 -0
+static char const *git_config_subst_userdir(char const *value) {
+	if (value[0] == '~' && value[1] == '/') {

Might you want to check that strlen(value) is at least 2?

- Eric

#44 lore raw
Quoted 6 lines · Eric Raible

config.c int git_config_string(const char **dest, const char *var, const char *value) · line 338

Quoted 2 lines · Karl Chen <quarl <at> cs.berkeley.edu> writes:
config.c +2 -0
+static char const *git_config_subst_userdir(char const *value) {
+     if (value[0] == '~' && value[1] == '/') {

Might you want to check that strlen(value) is at least 2?

No.

    swtich (strlen(value)) {
    case 0:
        /* value[0] == '\0' => value[0] != '~'
           value[1] will never be dereferenced, because of lazy && */
    case 1:
       /* value[0] != '\0'
          if (value[0] == '~')
              value[1] == '\0' => value[1] != '/' */
    default:
       /* ... */
    }

So no invalid memory dereferences.

Regards
Bert

Quoted 8 lines

- Eric

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html