Skip to contentSkip to conversation

Make core.sharedRepository more generic

PATCH In review Heikki Orsila 3 participants 6 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make core.sharedRepository more generic

git init --shared=0xxx, where '0xxx' is an octal number, will create a repository with file modes set to '0xxx'. User's with a safe umask value (0077) can use this option to force file modes. For example, '0640' is a group-readable but not group-writable regardless of user's umask value.

"git config core.sharedRepository 0xxx" is also handled.

---
 Documentation/config.txt   |    7 +++++-
 Documentation/git-init.txt |    8 ++++++-
 builtin-init-db.c          |    4 +-
 cache.h                    |   16 ++++++++++++--
 path.c                     |   38 ++++++++++++++++++++----------------
 setup.c                    |   45 ++++++++++++++++++++++++++++++++++++++++---
 6 files changed, 90 insertions(+), 28 deletions(-)
Documentation/config.txt +6 -1
index fe43b12..7a24f6e 100644
@@ -261,7 +261,12 @@ core.sharedRepository::
 	group-writable). When 'all' (or 'world' or 'everybody'), the
 	repository will be readable by all users, additionally to being
 	group-shareable. When 'umask' (or 'false'), git will use permissions
-	reported by umask(2). See linkgit:git-init[1]. False by default.
+	reported by umask(2). When '0xxx', where '0xxx' is an octal number,
+	files in the repository will have this mode value. '0xxx' will override
+	user's umask value, and thus, users with a safe umask (0077) can use
+	this option. Examples: '0660' is equivalent to 'group'. '0640' is a
+	repository that is group-readable but not group-writable.
+	See linkgit:git-init[1]. False by default.
 
 core.warnAmbiguousRefs::
 	If true, git will warn you if the ref name you passed it is ambiguous
Documentation/git-init.txt +7 -1
index 62914da..b17ae84 100644
@@ -31,7 +31,7 @@ structure, some suggested "exclude patterns", and copies of non-executing
 "hook" files.  The suggested patterns and hook files are all modifiable and
 extensible.
 
---shared[={false|true|umask|group|all|world|everybody}]::
+--shared[={false|true|umask|group|all|world|everybody|0xxx}]::
 
 Specify that the git repository is to be shared amongst several users.  This
 allows users belonging to the same group to push into that
@@ -52,6 +52,12 @@ is given:
  - 'all' (or 'world' or 'everybody'): Same as 'group', but make the repository
    readable by all users.
 
+ - '0xxx': '0xxx' is an octal number and each file will have mode '0xxx'
+   Any option except 'umask' can be set using this option. '0xxx' will
+   override users umask(2) value, and thus, users with a safe umask (0077)
+   can use this option. '0640' will create a repository which is group-readable
+   but not writable. '0660' is equivalent to 'group'.
+
 By default, the configuration flag receive.denyNonFastForwards is enabled
 in shared repositories, so that you cannot force a non fast-forwarding push
 into it.
builtin-init-db.c +2 -2
index 2854868..8c63295 100644
@@ -400,9 +400,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 		char buf[10];
 		/* We do not spell "group" and such, so that
 		 * the configuration can be read by older version
-		 * of git.
+		 * of git. Note, we use octal numbers.
 		 */
-		sprintf(buf, "%d", shared_repository);
+		sprintf(buf, "0%o", shared_repository);
 		git_config_set("core.sharedrepository", buf);
 		git_config_set("receive.denyNonFastforwards", "true");
 	}
cache.h +13 -3
index 2a1e7ec..f9c8d2b 100644
@@ -474,10 +474,20 @@ static inline void hashclr(unsigned char *hash)
 
 int git_mkstemp(char *path, size_t n, const char *template);
 
+/*
+ * NOTE NOTE NOTE!!
+ *
+ * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must
+ * not be changed. Old repositories have core.sharedrepository written in
+ * numeric format, and therefore these values are preserved for compatibility
+ * reasons.
+ */
 enum sharedrepo {
-	PERM_UMASK = 0,
-	PERM_GROUP,
-	PERM_EVERYBODY
+	PERM_UMASK          = 0,
+	OLD_PERM_GROUP      = 1,
+	OLD_PERM_EVERYBODY  = 2,
+	PERM_GROUP          = 0660,
+	PERM_EVERYBODY      = 0664,
 };
 int git_config_perm(const char *var, const char *value);
 int adjust_shared_perm(const char *path);
path.c +21 -17
index f4ed979..79bfe82 100644
@@ -266,24 +266,28 @@ int adjust_shared_perm(const char *path)
 	if (lstat(path, &st) < 0)
 		return -1;
 	mode = st.st_mode;
-	if (mode & S_IRUSR)
-		mode |= (shared_repository == PERM_GROUP
-			 ? S_IRGRP
-			 : (shared_repository == PERM_EVERYBODY
-			    ? (S_IRGRP|S_IROTH)
-			    : 0));
-
-	if (mode & S_IWUSR)
-		mode |= S_IWGRP;
-
-	if (mode & S_IXUSR)
-		mode |= (shared_repository == PERM_GROUP
-			 ? S_IXGRP
-			 : (shared_repository == PERM_EVERYBODY
-			    ? (S_IXGRP|S_IXOTH)
-			    : 0));
-	if (S_ISDIR(mode))
+
+	if (shared_repository) {
+		mode = (mode & ~0777) | shared_repository;
+	} else {
+		/* Preserve old PERM_UMASK behaviour */
+		if (mode & S_IWUSR)
+			mode |= S_IWGRP;
+	}
+
+	if (S_ISDIR(mode)) {
 		mode |= FORCE_DIR_SET_GID;
+
+		/*
+		 * The x flag for directories is determined from rw flags of
+		 * user, group and others. Having a directory with +rw but
+		 * -x does not make sense for Git repositories.
+		 */
+		mode |= (shared_repository & 0600) ? S_IXUSR : 0;
+		mode |= (shared_repository & 0060) ? S_IXGRP : 0;
+		mode |= (shared_repository & 0006) ? S_IXOTH : 0;
+	}
+
 	if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
 		return -2;
 	return 0;
setup.c +41 -4

58 lines not on this page · raw message

Signature · 1 line
1.5.4.4
#1 lore raw

The use of named constants vs. literals seem inconsistent in your patch, compare

| + mode = (mode & ~0777) | shared_repository;

to

| +               mode |= (shared_repository & 0600) ? S_IXUSR : 0;
| +               mode |= (shared_repository & 0060) ? S_IXGRP : 0;
| +               mode |= (shared_repository & 0006) ? S_IXOTH : 0;

I first thought that you were using literals with "shared_repository" and named constants with mode but the first line I quoted shows that this is not the case.

Btw, aren't those last three lines better replaced by

/* Copy read bits to execute bits */
mode |= (shared_repository & 0444) >> 2;

I don't see where you deal with executable files.

Also, wouldn't it be more consistent to use a negative value to --shared, that is a umask-compatible one, rather than a positive value which needs to be tweaked for directories and executable files? You would only have to "&" 0666 or 0777 with "~perms" to get the right permissions.

--shared=0007 would be equivalent to PERM_GROUP, --shared=0027 to group-readable-but-not-writable, and --shared=0002 to PERM_EVERYBODY.

Sam

Signature · 1 line
Samuel Tardieu -- [email protected] -- http://www.rfc1149.net/
#2 lore raw
Quoted 11 lines · Samuel Tardieu

The use of named constants vs. literals seem inconsistent in your patch, compare

| + mode = (mode & ~0777) | shared_repository;

to

| +               mode |= (shared_repository & 0600) ? S_IXUSR : 0;
| +               mode |= (shared_repository & 0060) ? S_IXGRP : 0;
| +               mode |= (shared_repository & 0006) ? S_IXOTH : 0;
Quoted 8 lines

I first thought that you were using literals with "shared_repository" and named constants with mode but the first line I quoted shows that this is not the case.

Btw, aren't those last three lines better replaced by

/* Copy read bits to execute bits */
mode |= (shared_repository & 0444) >> 2;

Agreed on both previous points. Will submit a new patch.

Quoted 10 lines

I don't see where you deal with executable files.

Also, wouldn't it be more consistent to use a negative value to --shared, that is a umask-compatible one, rather than a positive value which needs to be tweaked for directories and executable files? You would only have to "&" 0666 or 0777 with "~perms" to get the right permissions.

--shared=0007 would be equivalent to PERM_GROUP, --shared=0027 to group-readable-but-not-writable, and --shared=0002 to PERM_EVERYBODY.

I see the point, but I like to think it as a chmod value rather than a umask value.

Signature · 3 lines
Heikki Orsila
[email protected]
http://www.iki.fi/shd
#3 lore raw
In reply to Samuel Tardieu
Quoted 1 line · Samuel Tardieu

I don't see where you deal with executable files.

This code sets permissions for files written to the .git/ directory. Are there script files in the .git/ directory that we want to set executable flag for?

Signature · 3 lines
Heikki Orsila
[email protected]
http://www.iki.fi/shd
#4 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Heikki" == Heikki Orsila <[email protected]> writes:

Heikki> Are there script files in the .git/ directory that we want to Heikki> set executable flag for?

Probably not.

Sam

Signature · 1 line
Samuel Tardieu -- [email protected] -- http://www.rfc1149.net/
#5 lore raw
In reply to Heikki Orsila
Quoted 7 lines · Heikki Orsila

git init --shared=0xxx, where '0xxx' is an octal number, will create a repository with file modes set to '0xxx'. User's with a safe umask value (0077) can use this option to force file modes. For example, '0640' is a group-readable but not group-writable regardless of user's umask value.

"git config core.sharedRepository 0xxx" is also handled.

Oops, forgot to sign this one.

Heikki