Skip to content

Commit f00ecbe

Browse files
committed
Merge branch 'cc/replace'
* cc/replace: t6050: check pushing something based on a replaced commit Documentation: add documentation for "git replace" Add git-replace to .gitignore builtin-replace: use "usage_msg_opt" to give better error messages parse-options: add new function "usage_msg_opt" builtin-replace: teach "git replace" to actually replace Add new "git replace" command environment: add global variable to disable replacement mktag: call "check_sha1_signature" with the replacement sha1 replace_object: add a test case object: call "check_sha1_signature" with the replacement sha1 sha1_file: add a "read_sha1_file_repl" function replace_object: add mechanism to replace objects found in "refs/replace/" refs: add a "for_each_replace_ref" function
2 parents 5e092b5 + 4e65b53 commit f00ecbe

23 files changed

+610
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ git-reflog
105105
git-relink
106106
git-remote
107107
git-repack
108+
git-replace
108109
git-repo-config
109110
git-request-pull
110111
git-rerere

Documentation/git-replace.txt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
git-replace(1)
2+
==============
3+
4+
NAME
5+
----
6+
git-replace - Create, list, delete refs to replace objects
7+
8+
SYNOPSIS
9+
--------
10+
[verse]
11+
'git replace' [-f] <object> <replacement>
12+
'git replace' -d <object>...
13+
'git replace' -l [<pattern>]
14+
15+
DESCRIPTION
16+
-----------
17+
Adds a 'replace' reference in `.git/refs/replace/`
18+
19+
The name of the 'replace' reference is the SHA1 of the object that is
20+
replaced. The content of the replace reference is the SHA1 of the
21+
replacement object.
22+
23+
Unless `-f` is given, the replace reference must not yet exist in
24+
`.git/refs/replace/` directory.
25+
26+
OPTIONS
27+
-------
28+
-f::
29+
If an existing replace ref for the same object exists, it will
30+
be overwritten (instead of failing).
31+
32+
-d::
33+
Delete existing replace refs for the given objects.
34+
35+
-l <pattern>::
36+
List replace refs for objects that match the given pattern (or
37+
all if no pattern is given).
38+
Typing "git replace" without arguments, also lists all replace
39+
refs.
40+
41+
BUGS
42+
----
43+
Comparing blobs or trees that have been replaced with those that
44+
replace them will not work properly. And using 'git reset --hard' to
45+
go back to a replaced commit will move the branch to the replacement
46+
commit instead of the replaced commit.
47+
48+
There may be other problems when using 'git rev-list' related to
49+
pending objects. And of course things may break if an object of one
50+
type is replaced by an object of another type (for example a blob
51+
replaced by a commit).
52+
53+
SEE ALSO
54+
--------
55+
linkgit:git-tag[1]
56+
linkgit:git-branch[1]
57+
58+
Author
59+
------
60+
Written by Christian Couder <[email protected]> and Junio C
61+
Hamano <[email protected]>, based on 'git tag' by Kristian Hogsberg
62+
63+
64+
Documentation
65+
--------------
66+
Documentation by Christian Couder <[email protected]> and the
67+
git-list <[email protected]>, based on 'git tag' documentation.
68+
69+
GIT
70+
---
71+
Part of the linkgit:git[1] suite

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ LIB_OBJS += read-cache.o
536536
LIB_OBJS += reflog-walk.o
537537
LIB_OBJS += refs.o
538538
LIB_OBJS += remote.o
539+
LIB_OBJS += replace_object.o
539540
LIB_OBJS += rerere.o
540541
LIB_OBJS += revision.o
541542
LIB_OBJS += run-command.o
@@ -625,6 +626,7 @@ BUILTIN_OBJS += builtin-read-tree.o
625626
BUILTIN_OBJS += builtin-receive-pack.o
626627
BUILTIN_OBJS += builtin-reflog.o
627628
BUILTIN_OBJS += builtin-remote.o
629+
BUILTIN_OBJS += builtin-replace.o
628630
BUILTIN_OBJS += builtin-rerere.o
629631
BUILTIN_OBJS += builtin-reset.o
630632
BUILTIN_OBJS += builtin-rev-list.o

builtin-fsck.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
589589
struct alternate_object_database *alt;
590590

591591
errors_found = 0;
592+
read_replace_refs = 0;
592593

593594
argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
594595
if (write_lost_and_found) {

builtin-pack-objects.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
20982098
int rp_ac_alloc = 64;
20992099
int rp_ac;
21002100

2101+
read_replace_refs = 0;
2102+
21012103
rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
21022104

21032105
rp_av[0] = "pack-objects";

builtin-prune.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
140140
char *s;
141141

142142
save_commit_buffer = 0;
143+
read_replace_refs = 0;
143144
init_revisions(&revs, prefix);
144145

145146
argc = parse_options(argc, argv, prefix, options, prune_usage, 0);

builtin-replace.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Builtin "git replace"
3+
*
4+
* Copyright (c) 2008 Christian Couder <[email protected]>
5+
*
6+
* Based on builtin-tag.c by Kristian Høgsberg <[email protected]>
7+
* and Carlos Rica <[email protected]> that was itself based on
8+
* git-tag.sh and mktag.c by Linus Torvalds.
9+
*/
10+
11+
#include "cache.h"
12+
#include "builtin.h"
13+
#include "refs.h"
14+
#include "parse-options.h"
15+
16+
static const char * const git_replace_usage[] = {
17+
"git replace [-f] <object> <replacement>",
18+
"git replace -d <object>...",
19+
"git replace -l [<pattern>]",
20+
NULL
21+
};
22+
23+
static int show_reference(const char *refname, const unsigned char *sha1,
24+
int flag, void *cb_data)
25+
{
26+
const char *pattern = cb_data;
27+
28+
if (!fnmatch(pattern, refname, 0))
29+
printf("%s\n", refname);
30+
31+
return 0;
32+
}
33+
34+
static int list_replace_refs(const char *pattern)
35+
{
36+
if (pattern == NULL)
37+
pattern = "*";
38+
39+
for_each_replace_ref(show_reference, (void *) pattern);
40+
41+
return 0;
42+
}
43+
44+
typedef int (*each_replace_name_fn)(const char *name, const char *ref,
45+
const unsigned char *sha1);
46+
47+
static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
48+
{
49+
const char **p;
50+
char ref[PATH_MAX];
51+
int had_error = 0;
52+
unsigned char sha1[20];
53+
54+
for (p = argv; *p; p++) {
55+
if (snprintf(ref, sizeof(ref), "refs/replace/%s", *p)
56+
>= sizeof(ref)) {
57+
error("replace ref name too long: %.*s...", 50, *p);
58+
had_error = 1;
59+
continue;
60+
}
61+
if (!resolve_ref(ref, sha1, 1, NULL)) {
62+
error("replace ref '%s' not found.", *p);
63+
had_error = 1;
64+
continue;
65+
}
66+
if (fn(*p, ref, sha1))
67+
had_error = 1;
68+
}
69+
return had_error;
70+
}
71+
72+
static int delete_replace_ref(const char *name, const char *ref,
73+
const unsigned char *sha1)
74+
{
75+
if (delete_ref(ref, sha1, 0))
76+
return 1;
77+
printf("Deleted replace ref '%s'\n", name);
78+
return 0;
79+
}
80+
81+
static int replace_object(const char *object_ref, const char *replace_ref,
82+
int force)
83+
{
84+
unsigned char object[20], prev[20], repl[20];
85+
char ref[PATH_MAX];
86+
struct ref_lock *lock;
87+
88+
if (get_sha1(object_ref, object))
89+
die("Failed to resolve '%s' as a valid ref.", object_ref);
90+
if (get_sha1(replace_ref, repl))
91+
die("Failed to resolve '%s' as a valid ref.", replace_ref);
92+
93+
if (snprintf(ref, sizeof(ref),
94+
"refs/replace/%s",
95+
sha1_to_hex(object)) > sizeof(ref) - 1)
96+
die("replace ref name too long: %.*s...", 50, ref);
97+
if (check_ref_format(ref))
98+
die("'%s' is not a valid ref name.", ref);
99+
100+
if (!resolve_ref(ref, prev, 1, NULL))
101+
hashclr(prev);
102+
else if (!force)
103+
die("replace ref '%s' already exists", ref);
104+
105+
lock = lock_any_ref_for_update(ref, prev, 0);
106+
if (!lock)
107+
die("%s: cannot lock the ref", ref);
108+
if (write_ref_sha1(lock, repl, NULL) < 0)
109+
die("%s: cannot update the ref", ref);
110+
111+
return 0;
112+
}
113+
114+
int cmd_replace(int argc, const char **argv, const char *prefix)
115+
{
116+
int list = 0, delete = 0, force = 0;
117+
struct option options[] = {
118+
OPT_BOOLEAN('l', NULL, &list, "list replace refs"),
119+
OPT_BOOLEAN('d', NULL, &delete, "delete replace refs"),
120+
OPT_BOOLEAN('f', NULL, &force, "replace the ref if it exists"),
121+
OPT_END()
122+
};
123+
124+
argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
125+
126+
if (list && delete)
127+
usage_msg_opt("-l and -d cannot be used together",
128+
git_replace_usage, options);
129+
130+
if (force && (list || delete))
131+
usage_msg_opt("-f cannot be used with -d or -l",
132+
git_replace_usage, options);
133+
134+
/* Delete refs */
135+
if (delete) {
136+
if (argc < 1)
137+
usage_msg_opt("-d needs at least one argument",
138+
git_replace_usage, options);
139+
return for_each_replace_name(argv, delete_replace_ref);
140+
}
141+
142+
/* Replace object */
143+
if (!list && argc) {
144+
if (argc != 2)
145+
usage_msg_opt("bad number of arguments",
146+
git_replace_usage, options);
147+
return replace_object(argv[0], argv[1], force);
148+
}
149+
150+
/* List refs, even if "list" is not set */
151+
if (argc > 1)
152+
usage_msg_opt("only one pattern can be given with -l",
153+
git_replace_usage, options);
154+
if (force)
155+
usage_msg_opt("-f needs some arguments",
156+
git_replace_usage, options);
157+
158+
return list_replace_refs(argv[0]);
159+
}

builtin-unpack-objects.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
495495
int i;
496496
unsigned char sha1[20];
497497

498+
read_replace_refs = 0;
499+
498500
git_config(git_default_config, NULL);
499501

500502
quiet = !isatty(2);

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,6 @@ extern int cmd_write_tree(int argc, const char **argv, const char *prefix);
111111
extern int cmd_verify_pack(int argc, const char **argv, const char *prefix);
112112
extern int cmd_show_ref(int argc, const char **argv, const char *prefix);
113113
extern int cmd_pack_refs(int argc, const char **argv, const char *prefix);
114+
extern int cmd_replace(int argc, const char **argv, const char *prefix);
114115

115116
#endif

cache.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ extern size_t packed_git_window_size;
520520
extern size_t packed_git_limit;
521521
extern size_t delta_base_cache_limit;
522522
extern int auto_crlf;
523+
extern int read_replace_refs;
523524
extern int fsync_object_files;
524525
extern int core_preload_index;
525526

@@ -656,7 +657,11 @@ char *strip_path_suffix(const char *path, const char *suffix);
656657

657658
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
658659
extern int sha1_object_info(const unsigned char *, unsigned long *);
659-
extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);
660+
extern void *read_sha1_file_repl(const unsigned char *sha1, enum object_type *type, unsigned long *size, const unsigned char **replacement);
661+
static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
662+
{
663+
return read_sha1_file_repl(sha1, type, size, NULL);
664+
}
660665
extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
661666
extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
662667
extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);

0 commit comments

Comments
 (0)