Skip to content

Commit

Permalink
Merge branch 'cc/replace'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Junio C Hamano committed Aug 22, 2009
2 parents 5e092b5 + 4e65b53 commit f00ecbe
Show file tree
Hide file tree
Showing 23 changed files with 610 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ git-reflog
git-relink
git-remote
git-repack
git-replace
git-repo-config
git-request-pull
git-rerere
Expand Down
71 changes: 71 additions & 0 deletions Documentation/git-replace.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
git-replace(1)
==============

NAME
----
git-replace - Create, list, delete refs to replace objects

SYNOPSIS
--------
[verse]
'git replace' [-f] <object> <replacement>
'git replace' -d <object>...
'git replace' -l [<pattern>]

DESCRIPTION
-----------
Adds a 'replace' reference in `.git/refs/replace/`

The name of the 'replace' reference is the SHA1 of the object that is
replaced. The content of the replace reference is the SHA1 of the
replacement object.

Unless `-f` is given, the replace reference must not yet exist in
`.git/refs/replace/` directory.

OPTIONS
-------
-f::
If an existing replace ref for the same object exists, it will
be overwritten (instead of failing).

-d::
Delete existing replace refs for the given objects.

-l <pattern>::
List replace refs for objects that match the given pattern (or
all if no pattern is given).
Typing "git replace" without arguments, also lists all replace
refs.

BUGS
----
Comparing blobs or trees that have been replaced with those that
replace them will not work properly. And using 'git reset --hard' to
go back to a replaced commit will move the branch to the replacement
commit instead of the replaced commit.

There may be other problems when using 'git rev-list' related to
pending objects. And of course things may break if an object of one
type is replaced by an object of another type (for example a blob
replaced by a commit).

SEE ALSO
--------
linkgit:git-tag[1]
linkgit:git-branch[1]

Author
------
Written by Christian Couder <chriscool@tuxfamily.org> and Junio C
Hamano <gitster@pobox.com>, based on 'git tag' by Kristian Hogsberg
<krh@redhat.com> and Carlos Rica <jasampler@gmail.com>.

Documentation
--------------
Documentation by Christian Couder <chriscool@tuxfamily.org> and the
git-list <git@vger.kernel.org>, based on 'git tag' documentation.

GIT
---
Part of the linkgit:git[1] suite
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ LIB_OBJS += read-cache.o
LIB_OBJS += reflog-walk.o
LIB_OBJS += refs.o
LIB_OBJS += remote.o
LIB_OBJS += replace_object.o
LIB_OBJS += rerere.o
LIB_OBJS += revision.o
LIB_OBJS += run-command.o
Expand Down Expand Up @@ -625,6 +626,7 @@ BUILTIN_OBJS += builtin-read-tree.o
BUILTIN_OBJS += builtin-receive-pack.o
BUILTIN_OBJS += builtin-reflog.o
BUILTIN_OBJS += builtin-remote.o
BUILTIN_OBJS += builtin-replace.o
BUILTIN_OBJS += builtin-rerere.o
BUILTIN_OBJS += builtin-reset.o
BUILTIN_OBJS += builtin-rev-list.o
Expand Down
1 change: 1 addition & 0 deletions builtin-fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
struct alternate_object_database *alt;

errors_found = 0;
read_replace_refs = 0;

argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
if (write_lost_and_found) {
Expand Down
2 changes: 2 additions & 0 deletions builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
int rp_ac_alloc = 64;
int rp_ac;

read_replace_refs = 0;

rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));

rp_av[0] = "pack-objects";
Expand Down
1 change: 1 addition & 0 deletions builtin-prune.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
char *s;

save_commit_buffer = 0;
read_replace_refs = 0;
init_revisions(&revs, prefix);

argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
Expand Down
159 changes: 159 additions & 0 deletions builtin-replace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Builtin "git replace"
*
* Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
*
* Based on builtin-tag.c by Kristian Høgsberg <krh@redhat.com>
* and Carlos Rica <jasampler@gmail.com> that was itself based on
* git-tag.sh and mktag.c by Linus Torvalds.
*/

#include "cache.h"
#include "builtin.h"
#include "refs.h"
#include "parse-options.h"

static const char * const git_replace_usage[] = {
"git replace [-f] <object> <replacement>",
"git replace -d <object>...",
"git replace -l [<pattern>]",
NULL
};

static int show_reference(const char *refname, const unsigned char *sha1,
int flag, void *cb_data)
{
const char *pattern = cb_data;

if (!fnmatch(pattern, refname, 0))
printf("%s\n", refname);

return 0;
}

static int list_replace_refs(const char *pattern)
{
if (pattern == NULL)
pattern = "*";

for_each_replace_ref(show_reference, (void *) pattern);

return 0;
}

typedef int (*each_replace_name_fn)(const char *name, const char *ref,
const unsigned char *sha1);

static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
{
const char **p;
char ref[PATH_MAX];
int had_error = 0;
unsigned char sha1[20];

for (p = argv; *p; p++) {
if (snprintf(ref, sizeof(ref), "refs/replace/%s", *p)
>= sizeof(ref)) {
error("replace ref name too long: %.*s...", 50, *p);
had_error = 1;
continue;
}
if (!resolve_ref(ref, sha1, 1, NULL)) {
error("replace ref '%s' not found.", *p);
had_error = 1;
continue;
}
if (fn(*p, ref, sha1))
had_error = 1;
}
return had_error;
}

static int delete_replace_ref(const char *name, const char *ref,
const unsigned char *sha1)
{
if (delete_ref(ref, sha1, 0))
return 1;
printf("Deleted replace ref '%s'\n", name);
return 0;
}

static int replace_object(const char *object_ref, const char *replace_ref,
int force)
{
unsigned char object[20], prev[20], repl[20];
char ref[PATH_MAX];
struct ref_lock *lock;

if (get_sha1(object_ref, object))
die("Failed to resolve '%s' as a valid ref.", object_ref);
if (get_sha1(replace_ref, repl))
die("Failed to resolve '%s' as a valid ref.", replace_ref);

if (snprintf(ref, sizeof(ref),
"refs/replace/%s",
sha1_to_hex(object)) > sizeof(ref) - 1)
die("replace ref name too long: %.*s...", 50, ref);
if (check_ref_format(ref))
die("'%s' is not a valid ref name.", ref);

if (!resolve_ref(ref, prev, 1, NULL))
hashclr(prev);
else if (!force)
die("replace ref '%s' already exists", ref);

lock = lock_any_ref_for_update(ref, prev, 0);
if (!lock)
die("%s: cannot lock the ref", ref);
if (write_ref_sha1(lock, repl, NULL) < 0)
die("%s: cannot update the ref", ref);

return 0;
}

int cmd_replace(int argc, const char **argv, const char *prefix)
{
int list = 0, delete = 0, force = 0;
struct option options[] = {
OPT_BOOLEAN('l', NULL, &list, "list replace refs"),
OPT_BOOLEAN('d', NULL, &delete, "delete replace refs"),
OPT_BOOLEAN('f', NULL, &force, "replace the ref if it exists"),
OPT_END()
};

argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);

if (list && delete)
usage_msg_opt("-l and -d cannot be used together",
git_replace_usage, options);

if (force && (list || delete))
usage_msg_opt("-f cannot be used with -d or -l",
git_replace_usage, options);

/* Delete refs */
if (delete) {
if (argc < 1)
usage_msg_opt("-d needs at least one argument",
git_replace_usage, options);
return for_each_replace_name(argv, delete_replace_ref);
}

/* Replace object */
if (!list && argc) {
if (argc != 2)
usage_msg_opt("bad number of arguments",
git_replace_usage, options);
return replace_object(argv[0], argv[1], force);
}

/* List refs, even if "list" is not set */
if (argc > 1)
usage_msg_opt("only one pattern can be given with -l",
git_replace_usage, options);
if (force)
usage_msg_opt("-f needs some arguments",
git_replace_usage, options);

return list_replace_refs(argv[0]);
}
2 changes: 2 additions & 0 deletions builtin-unpack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
int i;
unsigned char sha1[20];

read_replace_refs = 0;

git_config(git_default_config, NULL);

quiet = !isatty(2);
Expand Down
1 change: 1 addition & 0 deletions builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@ extern int cmd_write_tree(int argc, const char **argv, const char *prefix);
extern int cmd_verify_pack(int argc, const char **argv, const char *prefix);
extern int cmd_show_ref(int argc, const char **argv, const char *prefix);
extern int cmd_pack_refs(int argc, const char **argv, const char *prefix);
extern int cmd_replace(int argc, const char **argv, const char *prefix);

#endif
7 changes: 6 additions & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ extern size_t packed_git_window_size;
extern size_t packed_git_limit;
extern size_t delta_base_cache_limit;
extern int auto_crlf;
extern int read_replace_refs;
extern int fsync_object_files;
extern int core_preload_index;

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

/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
extern int sha1_object_info(const unsigned char *, unsigned long *);
extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);
extern void *read_sha1_file_repl(const unsigned char *sha1, enum object_type *type, unsigned long *size, const unsigned char **replacement);
static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
{
return read_sha1_file_repl(sha1, type, size, NULL);
}
extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
Expand Down
2 changes: 2 additions & 0 deletions commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ struct commit_graft *read_graft_line(char *buf, int len);
int register_commit_graft(struct commit_graft *, int);
struct commit_graft *lookup_commit_graft(const unsigned char *sha1);

const unsigned char *lookup_replace_object(const unsigned char *sha1);

extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2, int cleanup);
extern struct commit_list *get_merge_bases_many(struct commit *one, int n, struct commit **twos, int cleanup);
extern struct commit_list *get_octopus_merge_bases(struct commit_list *in);
Expand Down
1 change: 1 addition & 0 deletions environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int pager_use_color = 1;
const char *editor_program;
const char *excludes_file;
int auto_crlf = 0; /* 1: both ways, -1: only when adding git objects */
int read_replace_refs = 1;
enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
Expand Down
1 change: 1 addition & 0 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "receive-pack", cmd_receive_pack },
{ "reflog", cmd_reflog, RUN_SETUP },
{ "remote", cmd_remote, RUN_SETUP },
{ "replace", cmd_replace, RUN_SETUP },
{ "repo-config", cmd_config },
{ "rerere", cmd_rerere, RUN_SETUP },
{ "reset", cmd_reset, RUN_SETUP },
Expand Down
7 changes: 4 additions & 3 deletions mktag.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
/*
* We refuse to tag something we can't verify. Just because.
*/
static int verify_object(unsigned char *sha1, const char *expected_type)
static int verify_object(const unsigned char *sha1, const char *expected_type)
{
int ret = -1;
enum object_type type;
unsigned long size;
void *buffer = read_sha1_file(sha1, &type, &size);
const unsigned char *repl;
void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);

if (buffer) {
if (type == type_from_string(expected_type))
ret = check_sha1_signature(sha1, buffer, size, expected_type);
ret = check_sha1_signature(repl, buffer, size, expected_type);
free(buffer);
}
return ret;
Expand Down
9 changes: 5 additions & 4 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,18 @@ struct object *parse_object(const unsigned char *sha1)
unsigned long size;
enum object_type type;
int eaten;
void *buffer = read_sha1_file(sha1, &type, &size);
const unsigned char *repl;
void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);

if (buffer) {
struct object *obj;
if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0) {
if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
free(buffer);
error("sha1 mismatch %s\n", sha1_to_hex(sha1));
error("sha1 mismatch %s\n", sha1_to_hex(repl));
return NULL;
}

obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
obj = parse_object_buffer(repl, type, size, buffer, &eaten);
if (!eaten)
free(buffer);
return obj;
Expand Down
Loading

0 comments on commit f00ecbe

Please sign in to comment.