summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2023-05-17 10:11:41 -0700
committerJunio C Hamano <gitster@pobox.com>2023-05-17 10:11:41 -0700
commit67a3b2b39f638872531324e03217fa58f7b9ad1e (patch)
tree5c4e9c7b9e2c0efc9c9f9e7e70fb11b104f95890
parent3307f7dde2ae8f5281d0782f7291a073c9b1cdc2 (diff)
parent44451a2e5eec5360378be23e2cdbd9ecee49e14e (diff)
downloadgit-67a3b2b39f638872531324e03217fa58f7b9ad1e.tar.gz
Merge branch 'jc/attr-source-tree'
"git --attr-source=<tree> cmd $args" is a new way to have any command to read attributes not from the working tree but from the given tree object. * jc/attr-source-tree: attr: teach "--attr-source=<tree>" global option to "git"
-rw-r--r--Documentation/git.txt8
-rw-r--r--archive.c2
-rw-r--r--attr.c37
-rw-r--r--attr.h13
-rw-r--r--builtin/check-attr.c17
-rw-r--r--builtin/pack-objects.c2
-rw-r--r--convert.c2
-rw-r--r--environment.h1
-rw-r--r--git.c16
-rw-r--r--ll-merge.c4
-rw-r--r--pathspec.c2
-rw-r--r--t/lib-diff-alternative.sh31
-rwxr-xr-xt/t0003-attributes.sh11
-rwxr-xr-xt/t4018-diff-funcname.sh19
-rw-r--r--userdiff.c2
-rw-r--r--ws.c2
16 files changed, 140 insertions, 29 deletions
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 54b043899f..f0cafa2290 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -212,6 +212,11 @@ If you just want to run git as if it was started in `<path>` then use
nohelpers (exclude helper commands), alias and config
(retrieve command list from config variable completion.commands)
+--attr-source=<tree-ish>::
+ Read gitattributes from <tree-ish> instead of the worktree. See
+ linkgit:gitattributes[5]. This is equivalent to setting the
+ `GIT_ATTR_SOURCE` environment variable.
+
GIT COMMANDS
------------
@@ -686,6 +691,9 @@ for further details.
tells Git not to verify the SSL certificate when fetching or
pushing over HTTPS.
+`GIT_ATTR_SOURCE`::
+ Sets the treeish that gitattributes will be read from.
+
`GIT_ASKPASS`::
If this environment variable is set, then Git commands which need to
acquire passwords or passphrases (e.g. for HTTP or IMAP authentication)
diff --git a/archive.c b/archive.c
index 641baad627..2ea9cbef92 100644
--- a/archive.c
+++ b/archive.c
@@ -130,7 +130,7 @@ static const struct attr_check *get_archive_attrs(struct index_state *istate,
static struct attr_check *check;
if (!check)
check = attr_check_initl("export-ignore", "export-subst", NULL);
- git_check_attr(istate, NULL, path, check);
+ git_check_attr(istate, path, check);
return check;
}
diff --git a/attr.c b/attr.c
index ddf2b0cbc2..d45d34058d 100644
--- a/attr.c
+++ b/attr.c
@@ -21,6 +21,7 @@
#include "setup.h"
#include "thread-utils.h"
#include "tree-walk.h"
+#include "object-name.h"
const char git_attr__true[] = "(builtin)true";
const char git_attr__false[] = "\0(builtin)false";
@@ -1170,11 +1171,42 @@ static void collect_some_attrs(struct index_state *istate,
fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem);
}
+static const char *default_attr_source_tree_object_name;
+
+void set_git_attr_source(const char *tree_object_name)
+{
+ default_attr_source_tree_object_name = xstrdup(tree_object_name);
+}
+
+static void compute_default_attr_source(struct object_id *attr_source)
+{
+ if (!default_attr_source_tree_object_name)
+ default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
+
+ if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
+ return;
+
+ if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source))
+ die(_("bad --attr-source or GIT_ATTR_SOURCE"));
+}
+
+static struct object_id *default_attr_source(void)
+{
+ static struct object_id attr_source;
+
+ if (is_null_oid(&attr_source))
+ compute_default_attr_source(&attr_source);
+ if (is_null_oid(&attr_source))
+ return NULL;
+ return &attr_source;
+}
+
void git_check_attr(struct index_state *istate,
- const struct object_id *tree_oid, const char *path,
+ const char *path,
struct attr_check *check)
{
int i;
+ const struct object_id *tree_oid = default_attr_source();
collect_some_attrs(istate, tree_oid, path, check);
@@ -1187,10 +1219,11 @@ void git_check_attr(struct index_state *istate,
}
}
-void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid,
+void git_all_attrs(struct index_state *istate,
const char *path, struct attr_check *check)
{
int i;
+ const struct object_id *tree_oid = default_attr_source();
attr_check_reset(check);
collect_some_attrs(istate, tree_oid, path, check);
diff --git a/attr.h b/attr.h
index 9884ea2bc6..676bd17ce2 100644
--- a/attr.h
+++ b/attr.h
@@ -45,7 +45,7 @@
* const char *path;
*
* setup_check();
- * git_check_attr(&the_index, tree_oid, path, check);
+ * git_check_attr(&the_index, path, check);
* ------------
*
* - Act on `.value` member of the result, left in `check->items[]`:
@@ -120,7 +120,6 @@
#define ATTR_MAX_FILE_SIZE (100 * 1024 * 1024)
struct index_state;
-struct object_id;
/**
* An attribute is an opaque object that is identified by its name. Pass the
@@ -136,6 +135,12 @@ struct all_attrs_item;
struct attr_stack;
/*
+ * The textual object name for the tree-ish used by git_check_attr()
+ * to read attributes from (instead of from the working tree).
+ */
+void set_git_attr_source(const char *);
+
+/*
* Given a string, return the gitattribute object that
* corresponds to it.
*/
@@ -203,14 +208,14 @@ void attr_check_free(struct attr_check *check);
const char *git_attr_name(const struct git_attr *);
void git_check_attr(struct index_state *istate,
- const struct object_id *tree_oid, const char *path,
+ const char *path,
struct attr_check *check);
/*
* Retrieve all attributes that apply to the specified path.
* check holds the attributes and their values.
*/
-void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid,
+void git_all_attrs(struct index_state *istate,
const char *path, struct attr_check *check);
enum git_attr_direction {
diff --git a/builtin/check-attr.c b/builtin/check-attr.c
index b2b678847f..e27b86d150 100644
--- a/builtin/check-attr.c
+++ b/builtin/check-attr.c
@@ -64,7 +64,7 @@ static void output_attr(struct attr_check *check, const char *file)
}
static void check_attr(const char *prefix, struct attr_check *check,
- const struct object_id *tree_oid, int collect_all,
+ int collect_all,
const char *file)
{
@@ -72,9 +72,9 @@ static void check_attr(const char *prefix, struct attr_check *check,
prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
if (collect_all) {
- git_all_attrs(&the_index, tree_oid, full_path, check);
+ git_all_attrs(&the_index, full_path, check);
} else {
- git_check_attr(&the_index, tree_oid, full_path, check);
+ git_check_attr(&the_index, full_path, check);
}
output_attr(check, file);
@@ -82,7 +82,7 @@ static void check_attr(const char *prefix, struct attr_check *check,
}
static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
- const struct object_id *tree_oid, int collect_all)
+ int collect_all)
{
struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;
@@ -96,7 +96,7 @@ static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
die("line is badly quoted");
strbuf_swap(&buf, &unquoted);
}
- check_attr(prefix, check, tree_oid, collect_all, buf.buf);
+ check_attr(prefix, check, collect_all, buf.buf);
maybe_flush_or_die(stdout, "attribute to stdout");
}
strbuf_release(&buf);
@@ -112,7 +112,6 @@ static NORETURN void error_with_usage(const char *msg)
int cmd_check_attr(int argc, const char **argv, const char *prefix)
{
struct attr_check *check;
- struct object_id *tree_oid = NULL;
struct object_id initialized_oid;
int cnt, i, doubledash, filei;
@@ -188,14 +187,14 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
if (source) {
if (repo_get_oid_tree(the_repository, source, &initialized_oid))
die("%s: not a valid tree-ish source", source);
- tree_oid = &initialized_oid;
+ set_git_attr_source(source);
}
if (stdin_paths)
- check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
+ check_attr_stdin_paths(prefix, check, all_attrs);
else {
for (i = filei; i < argc; i++)
- check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
+ check_attr(prefix, check, all_attrs, argv[i]);
maybe_flush_or_die(stdout, "attribute to stdout");
}
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index a5b466839b..9cfc8801f9 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1331,7 +1331,7 @@ static int no_try_delta(const char *path)
if (!check)
check = attr_check_initl("delta", NULL);
- git_check_attr(the_repository->index, NULL, path, check);
+ git_check_attr(the_repository->index, path, check);
if (ATTR_FALSE(check->items[0].value))
return 1;
return 0;
diff --git a/convert.c b/convert.c
index 7cf7bd0c88..9ee79fe469 100644
--- a/convert.c
+++ b/convert.c
@@ -1315,7 +1315,7 @@ void convert_attrs(struct index_state *istate,
git_config(read_convert_config, NULL);
}
- git_check_attr(istate, NULL, path, check);
+ git_check_attr(istate, path, check);
ccheck = check->items;
ca->crlf_action = git_path_check_crlf(ccheck + 4);
if (ca->crlf_action == CRLF_UNDEFINED)
diff --git a/environment.h b/environment.h
index a63f0c6a24..30cb7e0fa3 100644
--- a/environment.h
+++ b/environment.h
@@ -55,6 +55,7 @@ const char *getenv_safe(struct strvec *argv, const char *name);
#define GIT_QUARANTINE_ENVIRONMENT "GIT_QUARANTINE_PATH"
#define GIT_OPTIONAL_LOCKS_ENVIRONMENT "GIT_OPTIONAL_LOCKS"
#define GIT_TEXT_DOMAIN_DIR_ENVIRONMENT "GIT_TEXTDOMAINDIR"
+#define GIT_ATTR_SOURCE_ENVIRONMENT "GIT_ATTR_SOURCE"
/*
* Environment variable used in handshaking the wire protocol.
diff --git a/git.c b/git.c
index 45899be826..2f42da20f4 100644
--- a/git.c
+++ b/git.c
@@ -9,6 +9,7 @@
#include "alias.h"
#include "replace-object.h"
#include "setup.h"
+#include "attr.h"
#include "shallow.h"
#include "trace.h"
#include "trace2.h"
@@ -314,6 +315,21 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
} else {
exit(list_cmds(cmd));
}
+ } else if (!strcmp(cmd, "--attr-source")) {
+ if (*argc < 2) {
+ fprintf(stderr, _("no attribute source given for --attr-source\n" ));
+ usage(git_usage_string);
+ }
+ setenv(GIT_ATTR_SOURCE_ENVIRONMENT, (*argv)[1], 1);
+ if (envchanged)
+ *envchanged = 1;
+ (*argv)++;
+ (*argc)--;
+ } else if (skip_prefix(cmd, "--attr-source=", &cmd)) {
+ set_git_attr_source(cmd);
+ setenv(GIT_ATTR_SOURCE_ENVIRONMENT, cmd, 1);
+ if (envchanged)
+ *envchanged = 1;
} else {
fprintf(stderr, _("unknown option: %s\n"), cmd);
usage(git_usage_string);
diff --git a/ll-merge.c b/ll-merge.c
index 1993530688..07ec16e8e5 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -394,7 +394,7 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
normalize_file(theirs, path, istate);
}
- git_check_attr(istate, NULL, path, check);
+ git_check_attr(istate, path, check);
ll_driver_name = check->items[0].value;
if (check->items[1].value) {
marker_size = atoi(check->items[1].value);
@@ -422,7 +422,7 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
if (!check)
check = attr_check_initl("conflict-marker-size", NULL);
- git_check_attr(istate, NULL, path, check);
+ git_check_attr(istate, path, check);
if (check->items[0].value) {
marker_size = atoi(check->items[0].value);
if (marker_size <= 0)
diff --git a/pathspec.c b/pathspec.c
index 1e57b6c667..6966b265d3 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -736,7 +736,7 @@ int match_pathspec_attrs(struct index_state *istate,
if (name[namelen])
name = to_free = xmemdupz(name, namelen);
- git_check_attr(istate, NULL, name, item->attr_check);
+ git_check_attr(istate, name, item->attr_check);
free(to_free);
diff --git a/t/lib-diff-alternative.sh b/t/lib-diff-alternative.sh
index a8f5d3274a..c4dc2d46dc 100644
--- a/t/lib-diff-alternative.sh
+++ b/t/lib-diff-alternative.sh
@@ -112,15 +112,36 @@ EOF
STRATEGY=$1
- test_expect_success "$STRATEGY diff from attributes" '
+ test_expect_success "setup attributes files for tests with $STRATEGY" '
+ git checkout -b master &&
echo "file* diff=driver" >.gitattributes &&
- git config diff.driver.algorithm "$STRATEGY" &&
- test_must_fail git diff --no-index file1 file2 > output &&
- cat expect &&
- cat output &&
+ git add file1 file2 .gitattributes &&
+ git commit -m "adding files" &&
+ git checkout -b branchA &&
+ echo "file* diff=driverA" >.gitattributes &&
+ git add .gitattributes &&
+ git commit -m "adding driverA as diff driver" &&
+ git checkout master &&
+ git clone --bare --no-local . bare.git
+ '
+
+ test_expect_success "$STRATEGY diff from attributes" '
+ test_must_fail git -c diff.driver.algorithm=$STRATEGY diff --no-index file1 file2 > output &&
test_cmp expect output
'
+ test_expect_success "diff from attributes with bare repo with source" '
+ git -C bare.git --attr-source=branchA -c diff.driver.algorithm=myers \
+ -c diff.driverA.algorithm=$STRATEGY \
+ diff HEAD:file1 HEAD:file2 >output &&
+ test_cmp expect output
+ '
+
+ test_expect_success "diff from attributes with bare repo with invalid source" '
+ test_must_fail git -C bare.git --attr-source=invalid-branch diff \
+ HEAD:file1 HEAD:file2
+ '
+
test_expect_success "$STRATEGY diff from attributes has valid diffstat" '
echo "file* diff=driver" >.gitattributes &&
git config diff.driver.algorithm "$STRATEGY" &&
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index 89b306cb11..26e082f05b 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -30,8 +30,17 @@ attr_check_quote () {
attr_check_source () {
path="$1" expect="$2" source="$3" git_opts="$4" &&
- git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
echo "$path: test: $expect" >expect &&
+
+ git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err &&
+
+ git $git_opts --attr-source="$source" check-attr test -- "$path" >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+
+ GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
test_cmp expect actual &&
test_must_be_empty err
}
diff --git a/t/t4018-diff-funcname.sh b/t/t4018-diff-funcname.sh
index 42a2b9a13b..c8d555771d 100755
--- a/t/t4018-diff-funcname.sh
+++ b/t/t4018-diff-funcname.sh
@@ -63,6 +63,25 @@ do
test_i18ngrep ! fatal msg &&
test_i18ngrep ! error msg
'
+
+ test_expect_success "builtin $p pattern compiles on bare repo with --attr-source" '
+ test_when_finished "rm -rf bare.git" &&
+ git checkout -B master &&
+ git add . &&
+ echo "*.java diff=notexist" >.gitattributes &&
+ git add .gitattributes &&
+ git commit -am "changing gitattributes" &&
+ git checkout -B branchA &&
+ echo "*.java diff=$p" >.gitattributes &&
+ git add .gitattributes &&
+ git commit -am "changing gitattributes" &&
+ git clone --bare --no-local . bare.git &&
+ git -C bare.git symbolic-ref HEAD refs/heads/master &&
+ test_expect_code 1 git -C bare.git --attr-source=branchA \
+ diff --exit-code HEAD:A.java HEAD:B.java 2>msg &&
+ test_i18ngrep ! fatal msg &&
+ test_i18ngrep ! error msg
+ '
done
test_expect_success 'last regexp must not be negated' '
diff --git a/userdiff.c b/userdiff.c
index eaec6ebb5e..664c7c1402 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -444,7 +444,7 @@ struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
check = attr_check_initl("diff", NULL);
if (!path)
return NULL;
- git_check_attr(istate, NULL, path, check);
+ git_check_attr(istate, path, check);
if (ATTR_TRUE(check->items[0].value))
return &driver_true;
diff --git a/ws.c b/ws.c
index d356d4ec9e..9456e2fdbe 100644
--- a/ws.c
+++ b/ws.c
@@ -83,7 +83,7 @@ unsigned whitespace_rule(struct index_state *istate, const char *pathname)
if (!attr_whitespace_rule)
attr_whitespace_rule = attr_check_initl("whitespace", NULL);
- git_check_attr(istate, NULL, pathname, attr_whitespace_rule);
+ git_check_attr(istate, pathname, attr_whitespace_rule);
value = attr_whitespace_rule->items[0].value;
if (ATTR_TRUE(value)) {
/* true (whitespace) */