Skip to content

Commit 0958a24

Browse files
committed
Merge branch 'jc/sha1-name-more'
Teaches the object name parser things like a "git describe" output is always a commit object, "A" in "git log A" must be a committish, and "A" and "B" in "git log A...B" both must be committish, etc., to prolong the lifetime of abbreviated object names. * jc/sha1-name-more: (27 commits) t1512: match the "other" object names t1512: ignore whitespaces in wc -l output rev-parse --disambiguate=<prefix> rev-parse: A and B in "rev-parse A..B" refer to committish reset: the command takes committish commit-tree: the command wants a tree and commits apply: --build-fake-ancestor expects blobs sha1_name.c: add support for disambiguating other types revision.c: the "log" family, except for "show", takes committish revision.c: allow handle_revision_arg() to take other flags sha1_name.c: introduce get_sha1_committish() sha1_name.c: teach lookup context to get_sha1_with_context() sha1_name.c: many short names can only be committish sha1_name.c: get_sha1_1() takes lookup flags sha1_name.c: get_describe_name() by definition groks only commits sha1_name.c: teach get_short_sha1() a commit-only option sha1_name.c: allow get_short_sha1() to take other flags get_sha1(): fix error status regression sha1_name.c: restructure disambiguation of short names sha1_name.c: correct misnamed "canonical" and "res" ...
2 parents 9a0231b + 31ffd0c commit 0958a24

File tree

15 files changed

+714
-172
lines changed

15 files changed

+714
-172
lines changed

Documentation/git-rev-parse.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ OPTIONS
101101
The option core.warnAmbiguousRefs is used to select the strict
102102
abbreviation mode.
103103

104+
--disambiguate=<prefix>::
105+
Show every object whose name begins with the given prefix.
106+
The <prefix> must be at least 4 hexadecimal digits long to
107+
avoid listing each and every object in the repository by
108+
mistake.
109+
104110
--all::
105111
Show all refs found in `refs/`.
106112

builtin/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3589,7 +3589,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
35893589
name = patch->old_name ? patch->old_name : patch->new_name;
35903590
if (0 < patch->is_new)
35913591
continue;
3592-
else if (get_sha1(patch->old_sha1_prefix, sha1))
3592+
else if (get_sha1_blob(patch->old_sha1_prefix, sha1))
35933593
/* git diff has no index line for mode/type changes */
35943594
if (!patch->lines_added && !patch->lines_deleted) {
35953595
if (get_current_sha1(patch->old_name, sha1))

builtin/cat-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
9191
unsigned long size;
9292
struct object_context obj_context;
9393

94-
if (get_sha1_with_context(obj_name, sha1, &obj_context))
94+
if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
9595
die("Not a valid object name %s", obj_name);
9696

9797
buf = NULL;

builtin/commit-tree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
4848
if (argc < 2 || !strcmp(argv[1], "-h"))
4949
usage(commit_tree_usage);
5050

51-
if (get_sha1(argv[1], tree_sha1))
52-
die("Not a valid object name %s", argv[1]);
51+
if (get_sha1_tree(argv[1], tree_sha1))
52+
die("Not a valid tree object name %s", argv[1]);
5353

5454
for (i = 1; i < argc; i++) {
5555
const char *arg = argv[i];
5656
if (!strcmp(arg, "-p")) {
5757
unsigned char sha1[20];
5858
if (argc <= ++i)
5959
usage(commit_tree_usage);
60-
if (get_sha1(argv[i], sha1))
60+
if (get_sha1_commit(argv[i], sha1))
6161
die("Not a valid object name %s", argv[i]);
6262
assert_sha1_type(sha1, OBJ_COMMIT);
6363
new_parent(lookup_commit(sha1), &parents);
@@ -104,7 +104,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
104104
continue;
105105
}
106106

107-
if (get_sha1(arg, tree_sha1))
107+
if (get_sha1_tree(arg, tree_sha1))
108108
die("Not a valid object name %s", arg);
109109
if (got_tree)
110110
die("Cannot give more than one trees");

builtin/log.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
367367
rev.simplify_history = 0;
368368
memset(&opt, 0, sizeof(opt));
369369
opt.def = "HEAD";
370+
opt.revarg_opt = REVARG_COMMITTISH;
370371
cmd_log_init(argc, argv, prefix, &rev, &opt);
371372
if (!rev.diffopt.output_format)
372373
rev.diffopt.output_format = DIFF_FORMAT_RAW;
@@ -557,6 +558,7 @@ int cmd_log(int argc, const char **argv, const char *prefix)
557558
rev.always_show_header = 1;
558559
memset(&opt, 0, sizeof(opt));
559560
opt.def = "HEAD";
561+
opt.revarg_opt = REVARG_COMMITTISH;
560562
cmd_log_init(argc, argv, prefix, &rev, &opt);
561563
return cmd_log_walk(&rev);
562564
}
@@ -1132,6 +1134,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
11321134
rev.subject_prefix = fmt_patch_subject_prefix;
11331135
memset(&s_r_opt, 0, sizeof(s_r_opt));
11341136
s_r_opt.def = "HEAD";
1137+
s_r_opt.revarg_opt = REVARG_COMMITTISH;
11351138

11361139
if (default_attach) {
11371140
rev.mime_boundary = default_attach;

builtin/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ static void get_object_list(int ac, const char **av)
23732373
}
23742374
die("not a rev '%s'", line);
23752375
}
2376-
if (handle_revision_arg(line, &revs, flags, 1))
2376+
if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))
23772377
die("bad revision '%s'", line);
23782378
}
23792379

builtin/reset.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
276276
* Otherwise, argv[i] could be either <rev> or <paths> and
277277
* has to be unambiguous.
278278
*/
279-
else if (!get_sha1(argv[i], sha1)) {
279+
else if (!get_sha1_committish(argv[i], sha1)) {
280280
/*
281281
* Ok, argv[i] looks like a rev; it should not
282282
* be a filename.
@@ -289,9 +289,15 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
289289
}
290290
}
291291

292-
if (get_sha1(rev, sha1))
292+
if (get_sha1_committish(rev, sha1))
293293
die(_("Failed to resolve '%s' as a valid ref."), rev);
294294

295+
/*
296+
* NOTE: As "git reset $treeish -- $path" should be usable on
297+
* any tree-ish, this is not strictly correct. We are not
298+
* moving the HEAD to any commit; we are merely resetting the
299+
* entries in the index to that of a treeish.
300+
*/
295301
commit = lookup_commit_reference(sha1);
296302
if (!commit)
297303
die(_("Could not parse object '%s'."), rev);

builtin/rev-parse.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ static int anti_reference(const char *refname, const unsigned char *sha1, int fl
195195
return 0;
196196
}
197197

198+
static int show_abbrev(const unsigned char *sha1, void *cb_data)
199+
{
200+
show_rev(NORMAL, sha1, NULL);
201+
return 0;
202+
}
203+
198204
static void show_datestring(const char *flag, const char *datestr)
199205
{
200206
static char buffer[100];
@@ -238,7 +244,7 @@ static int try_difference(const char *arg)
238244
next = "HEAD";
239245
if (dotdot == arg)
240246
this = "HEAD";
241-
if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
247+
if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) {
242248
show_rev(NORMAL, end, next);
243249
show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
244250
if (symmetric) {
@@ -278,7 +284,7 @@ static int try_parent_shorthands(const char *arg)
278284
return 0;
279285

280286
*dotdot = 0;
281-
if (get_sha1(arg, sha1))
287+
if (get_sha1_committish(arg, sha1))
282288
return 0;
283289

284290
if (!parents_only)
@@ -589,6 +595,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
589595
for_each_ref(show_reference, NULL);
590596
continue;
591597
}
598+
if (!prefixcmp(arg, "--disambiguate=")) {
599+
for_each_abbrev(arg + 15, show_abbrev, NULL);
600+
continue;
601+
}
592602
if (!strcmp(arg, "--bisect")) {
593603
for_each_ref_in("refs/bisect/bad", show_reference, NULL);
594604
for_each_ref_in("refs/bisect/good", anti_reference, NULL);

cache.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -790,17 +790,25 @@ struct object_context {
790790
unsigned mode;
791791
};
792792

793+
#define GET_SHA1_QUIETLY 01
794+
#define GET_SHA1_COMMIT 02
795+
#define GET_SHA1_COMMITTISH 04
796+
#define GET_SHA1_TREE 010
797+
#define GET_SHA1_TREEISH 020
798+
#define GET_SHA1_BLOB 040
799+
#define GET_SHA1_ONLY_TO_DIE 04000
800+
793801
extern int get_sha1(const char *str, unsigned char *sha1);
794-
extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int only_to_die, const char *prefix);
795-
static inline int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
796-
{
797-
return get_sha1_with_mode_1(str, sha1, mode, 0, NULL);
798-
}
799-
extern int get_sha1_with_context_1(const char *name, unsigned char *sha1, struct object_context *orc, int only_to_die, const char *prefix);
800-
static inline int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc)
801-
{
802-
return get_sha1_with_context_1(str, sha1, orc, 0, NULL);
803-
}
802+
extern int get_sha1_commit(const char *str, unsigned char *sha1);
803+
extern int get_sha1_committish(const char *str, unsigned char *sha1);
804+
extern int get_sha1_tree(const char *str, unsigned char *sha1);
805+
extern int get_sha1_treeish(const char *str, unsigned char *sha1);
806+
extern int get_sha1_blob(const char *str, unsigned char *sha1);
807+
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
808+
extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc);
809+
810+
typedef int each_abbrev_fn(const unsigned char *sha1, void *);
811+
extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
804812

805813
/*
806814
* Try to read a SHA1 in hexadecimal format from the 40 characters

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct commit *lookup_commit_reference_by_name(const char *name)
6868
unsigned char sha1[20];
6969
struct commit *commit;
7070

71-
if (get_sha1(name, sha1))
71+
if (get_sha1_committish(name, sha1))
7272
return NULL;
7373
commit = lookup_commit_reference(sha1);
7474
if (!commit || parse_commit(commit))

0 commit comments

Comments
 (0)