@matheusbsbs / matheus-bagliano / commits / bebdd271ff
Fork 0

builtin-replace: teach "git replace" to actually replace

Teach the syntax: "git replace <object> <replacement>", so that "git replace" can now create replace refs. These replace refs will be used by read_sha1_file to substitute <object> with <replacement> for most of the commands. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Feb 2, 2009 at 06:12 UTC bebdd271ff660d603ad75fef346ad1ff19fca0cb
2 files changed +59 -1
builtin-replace.c
+49 -1
@@ -14,6 +14,7 @@
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
@@ -77,12 +78,46 @@ static int delete_replace_ref(const char *name, const char *ref,
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 {
82 - int list = 0, delete = 0;
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
@@ -91,15 +126,28 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
126 if (list && delete)
127 usage_with_options(git_replace_usage, options);
128
129 + if (force && (list || delete))
130 + usage_with_options(git_replace_usage, options);
131 +
132 + /* Delete refs */
133 if (delete) {
134 if (argc < 1)
135 usage_with_options(git_replace_usage, options);
136 return for_each_replace_name(argv, delete_replace_ref);
137 }
138
139 + /* Replace object */
140 + if (!list && argc) {
141 + if (argc != 2)
142 + usage_with_options(git_replace_usage, options);
143 + return replace_object(argv[0], argv[1], force);
144 + }
145 +
146 /* List refs, even if "list" is not set */
147 if (argc > 1)
148 usage_with_options(git_replace_usage, options);
149 + if (force)
150 + usage_with_options(git_replace_usage, options);
151
152 return list_replace_refs(argv[0]);
153 }
t/t6050-replace.sh
+10
@@ -114,9 +114,19 @@ test_expect_success '"git replace" listing and deleting' '
114 test_must_fail git replace -d &&
115 test_must_fail git replace -l -d $HASH2 &&
116 git replace -d $HASH2 &&
117 + git show $HASH2 | grep "A U Thor" &&
118 test -z "$(git replace -l)"
119 '
120
121 +test_expect_success '"git replace" replacing' '
122 + git replace $HASH2 $R &&
123 + git show $HASH2 | grep "O Thor" &&
124 + test_must_fail git replace $HASH2 $R &&
125 + git replace -f $HASH2 $R &&
126 + test_must_fail git replace -f &&
127 + test "$HASH2" = "$(git replace)"
128 +'
129 +
130 #
131 #
132 test_done