Commit 65b09b1d by Russell Belfer

Implement diff lists and formatters

This reworks the diff API to separate the steps of producing
a diff descriptions from formatting the diff.  This will allow
us to share diff output code with the various diff creation
scenarios and will allow us to implement rename detection as
an optional pass that can be run on a diff list.
parent cd33323b
......@@ -30,6 +30,7 @@
#include "git2/commit.h"
#include "git2/tag.h"
#include "git2/tree.h"
#include "git2/diff.h"
#include "git2/index.h"
#include "git2/config.h"
......
......@@ -21,81 +21,126 @@
*/
GIT_BEGIN_DECL
typedef struct {
int context_lines;
int interhunk_lines;
int ignore_whitespace;
int force_text;
git_strarray pathspec;
} git_diff_options;
typedef struct {
git_status_t status; /* value from tree.h */
unsigned int old_attr;
unsigned int new_attr;
git_oid old_oid;
git_oid new_oid;
git_blob *old_blob;
git_blob *new_blob;
const char *path;
const char *new_path; /* NULL unless status is RENAMED or COPIED */
int similarity; /* value from 0 to 100 */
int binary; /* diff as binary? */
} git_diff_delta;
typedef int (*git_diff_file_fn)(
void *cb_data,
const git_oid *old,
const char *old_path,
int old_mode,
const git_oid *new, /* hashed object if from work tree */
const char *new_path,
int new_mode);
git_diff_delta *delta,
float progress);
typedef struct {
int old_start;
int old_lines;
int new_start;
int new_lines;
} git_diff_range;
typedef int (*git_diff_hunk_fn)(
void *cb_data,
int old_start,
int old_lines,
int new_start,
int new_lines);
git_diff_delta *delta,
git_diff_range *range,
const char *header,
size_t header_len);
#define GIT_DIFF_LINE_CONTEXT 0
#define GIT_DIFF_LINE_ADDITION 1
#define GIT_DIFF_LINE_DELETION 2
#define GIT_DIFF_LINE_CONTEXT ' '
#define GIT_DIFF_LINE_ADDITION '+'
#define GIT_DIFF_LINE_DELETION '-'
#define GIT_DIFF_LINE_ADD_EOFNL '\n'
#define GIT_DIFF_LINE_DEL_EOFNL '\0'
typedef int (*git_diff_line_fn)(
void *cb_data,
int origin, /* GIT_DIFF_LINE value from above */
git_diff_delta *delta,
char line_origin, /* GIT_DIFF_LINE value from above */
const char *content,
size_t content_len);
typedef struct {
int context_lines;
int interhunk_lines;
int ignore_whitespace;
git_diff_file_fn file_cb;
git_diff_hunk_fn hunk_cb;
git_diff_line_fn line_cb;
void *cb_data;
} git_diff_opts;
typedef struct git_diff_list git_diff_list;
GIT_EXTERN(int) git_diff_blobs(
git_repository *repo,
git_blob *old,
git_blob *new,
git_diff_opts *options);
/*
* Generate diff lists
*/
GIT_EXTERN(int) git_diff_trees(
GIT_EXTERN(int) git_diff_tree_to_tree(
git_repository *repo,
const git_diff_options *opts,
git_tree *old,
git_tree *new,
git_diff_opts *options);
git_diff_list **diff);
GIT_EXTERN(int) git_diff_index(
GIT_EXTERN(int) git_diff_index_to_tree(
git_repository *repo,
const git_diff_options *opts,
git_tree *old,
git_diff_opts *options);
git_diff_list **diff);
/* pass NULL for the git_tree to diff workdir against index */
GIT_EXTERN(int) git_diff_workdir(
GIT_EXTERN(int) git_diff_workdir_to_tree(
git_repository *repo,
const git_diff_options *opts,
git_tree *old,
git_diff_opts *options);
git_diff_list **diff);
GIT_EXTERN(int) git_diff_workdir_file(
GIT_EXTERN(int) git_diff_workdir_to_index(
git_repository *repo,
git_blob *old,
const char *path,
git_diff_opts *options);
const git_diff_options *opts,
git_diff_list **diff);
/* pass git_objects to diff against or NULL for index.
* can handle: blob->blob, tree->index, tree->tree
* it will be an error if object types don't match
GIT_EXTERN(void) git_diff_list_free(git_diff_list *diff);
/*
* Process diff lists
*/
/* pass git_object to diff WT against or NULL for index
* can handle: index->wt, tree->wt, blob->wt with path
GIT_EXTERN(int) git_diff_foreach(
git_diff_list *diff,
void *cb_data,
git_diff_file_fn file_cb,
git_diff_hunk_fn hunk_cb,
git_diff_line_fn line_cb);
#ifndef _STDIO_H_
#include <stdio.h>
#endif
GIT_EXTERN(int) git_diff_print_compact(
FILE *fp, git_diff_list *diff);
GIT_EXTERN(int) git_diff_print_patch(
FILE *fp, git_diff_list *diff);
/*
* Misc
*/
GIT_EXTERN(int) git_diff_blobs(
git_repository *repo,
git_blob *old,
git_blob *new,
git_diff_options *options,
void *cb_data,
git_diff_hunk_fn hunk_cb,
git_diff_line_fn line_cb);
GIT_END_DECL
/** @} */
......
......@@ -316,9 +316,14 @@ GIT_EXTERN(int) git_tree_walk(git_tree *tree, git_treewalk_cb callback, int mode
/** @} */
typedef enum {
GIT_STATUS_UNMODIFIED = 0,
GIT_STATUS_ADDED = 1,
GIT_STATUS_DELETED = 2,
GIT_STATUS_MODIFIED = 3,
GIT_STATUS_RENAMED = 4,
GIT_STATUS_COPIED = 5,
GIT_STATUS_IGNORED = 6,
GIT_STATUS_UNTRACKED = 7
} git_status_t;
typedef struct {
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_diff_h__
#define INCLUDE_diff_h__
#include <stdio.h>
#include "vector.h"
#include "buffer.h"
struct git_diff_list {
git_repository *repo;
git_diff_options opts;
git_buf pfx;
git_vector files; /* vector of git_diff_file_delta */
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment