Skip to content
/ git Public
forked from git/git

Commit

Permalink
use SWAP macro
Browse files Browse the repository at this point in the history
Apply the semantic patch swap.cocci to convert hand-rolled swaps to use
the macro SWAP.  The resulting code is shorter and easier to read, the
object code is effectively unchanged.

The patch for object.c had to be hand-edited in order to preserve the
comment before the change; Coccinelle tried to eat it for some reason.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
rscharfe authored and gitster committed Jan 30, 2017
1 parent db10199 commit 35d803b
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 39 deletions.
4 changes: 1 addition & 3 deletions builtin/diff-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
tree1 = opt->pending.objects[0].item;
tree2 = opt->pending.objects[1].item;
if (tree2->flags & UNINTERESTING) {
struct object *tmp = tree2;
tree2 = tree1;
tree1 = tmp;
SWAP(tree2, tree1);
}
diff_tree_sha1(tree1->oid.hash,
tree2->oid.hash,
Expand Down
9 changes: 3 additions & 6 deletions builtin/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ static void stuff_change(struct diff_options *opt,
return;

if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
unsigned tmp;
const unsigned char *tmp_u;
const char *tmp_c;
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
tmp_c = old_name; old_name = new_name; new_name = tmp_c;
SWAP(old_mode, new_mode);
SWAP(old_sha1, new_sha1);
SWAP(old_name, new_name);
}

if (opt->prefix &&
Expand Down
3 changes: 1 addition & 2 deletions diff-no-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ static int queue_diff(struct diff_options *o,

if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
unsigned tmp;
const char *tmp_c;
tmp = mode1; mode1 = mode2; mode2 = tmp;
tmp_c = name1; name1 = name2; name2 = tmp_c;
SWAP(name1, name2);
}

d1 = noindex_filespec(name1, mode1);
Expand Down
8 changes: 3 additions & 5 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -5118,13 +5118,11 @@ void diff_change(struct diff_options *options,

if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
unsigned tmp;
const unsigned char *tmp_c;
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
SWAP(old_mode, new_mode);
SWAP(old_sha1, new_sha1);
tmp = old_sha1_valid; old_sha1_valid = new_sha1_valid;
new_sha1_valid = tmp;
tmp = old_dirty_submodule; old_dirty_submodule = new_dirty_submodule;
new_dirty_submodule = tmp;
SWAP(old_dirty_submodule, new_dirty_submodule);
}

if (options->prefix &&
Expand Down
5 changes: 1 addition & 4 deletions graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,6 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf
static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
{
int i;
int *tmp_mapping;
short used_horizontal = 0;
int horizontal_edge = -1;
int horizontal_edge_target = -1;
Expand Down Expand Up @@ -1132,9 +1131,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf
/*
* Swap mapping and new_mapping
*/
tmp_mapping = graph->mapping;
graph->mapping = graph->new_mapping;
graph->new_mapping = tmp_mapping;
SWAP(graph->mapping, graph->new_mapping);

/*
* If graph->mapping indicates that all of the branch lines
Expand Down
3 changes: 1 addition & 2 deletions line-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
return -1;

if (*begin && *end && *end < *begin) {
long tmp;
tmp = *end; *end = *begin; *begin = tmp;
SWAP(*end, *begin);
}

return 0;
Expand Down
5 changes: 1 addition & 4 deletions merge-recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -1390,14 +1390,11 @@ static int process_renames(struct merge_options *o,
branch1 = o->branch1;
branch2 = o->branch2;
} else {
struct rename *tmp;
renames1 = b_renames;
renames2Dst = &a_by_dst;
branch1 = o->branch2;
branch2 = o->branch1;
tmp = ren2;
ren2 = ren1;
ren1 = tmp;
SWAP(ren2, ren1);
}

if (ren1->processed)
Expand Down
4 changes: 1 addition & 3 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ struct object *lookup_object(const unsigned char *sha1)
* that we do not need to walk the hash table the next
* time we look for it.
*/
struct object *tmp = obj_hash[i];
obj_hash[i] = obj_hash[first];
obj_hash[first] = tmp;
SWAP(obj_hash[i], obj_hash[first]);
}
return obj;
}
Expand Down
5 changes: 1 addition & 4 deletions pack-revindex.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
* be a no-op, as everybody lands in the same zero-th bucket.
*/
for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
struct revindex_entry *swap;
unsigned i;

memset(pos, 0, BUCKETS * sizeof(*pos));
Expand Down Expand Up @@ -97,9 +96,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
* Now "to" contains the most sorted list, so we swap "from" and
* "to" for the next iteration.
*/
swap = from;
from = to;
to = swap;
SWAP(from, to);
}

/*
Expand Down
4 changes: 1 addition & 3 deletions prio-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ static inline int compare(struct prio_queue *queue, int i, int j)

static inline void swap(struct prio_queue *queue, int i, int j)
{
struct prio_queue_entry tmp = queue->array[i];
queue->array[i] = queue->array[j];
queue->array[j] = tmp;
SWAP(queue->array[i], queue->array[j]);
}

void prio_queue_reverse(struct prio_queue *queue)
Expand Down
4 changes: 1 addition & 3 deletions strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
*/
static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
{
struct strbuf tmp = *a;
*a = *b;
*b = tmp;
SWAP(*a, *b);
}


Expand Down

0 comments on commit 35d803b

Please sign in to comment.