8000 add context lines accessor by Murmele · Pull Request #6137 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

add context lines accessor #6137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Murmele
Copy link
@Murmele Murmele commented Dec 9, 2021

Add function to access context line settings

@boretrk
Copy link
Contributor
boretrk commented Jan 6, 2022

Duplicate of #5733

@boretrk
Copy link
Contributor
boretrk commented Jan 20, 2022

This could be a nice function to have, but it seems a bit specialized.
This is more a personal choice but I think it feels a bit more natural to have something like a const git_diff_options * git_patch_diff_opts(const git_patch *patch); function, much like git_patch_get_delta() to get information about how the patch was generated.
On the other hand, maybe that exposes too much?

@Murmele Murmele closed this Apr 28, 2022
@Murmele Murmele reopened this Apr 28, 2022
@Murmele Murmele closed this Apr 28, 2022
@Murmele
Copy link
Author
Murmele commented Jun 13, 2022

@ethomson what do you think makes more sense. Exposing complete diff_options or only returning the needed information?
Currently it is used in GitAhead and Gittyup to determine the conflicted parts of a patch. Is is in alternative instead of creating a new function using https://libgit2.org/docs/guides/101-samples/#index_conflicts to determine the conflicts?

Patch::Patch(git_patch *patch) : d(patch, git_patch_free) {
  if (!isValid() || !isConflicted())
    return;

  // Read conflict hunks.
  Repository repo(git_patch_owner(patch));
  if (!repo.isValid())
    return;

  QFile file(repo.workdir().filePath(name(Diff::OldFile)));
  if (!file.open(QFile::ReadOnly))
    re
94E9
turn;

  QList<QByteArray> lines;
  QByteArray line = file.readLine();
  while (!line.isEmpty()) {
    lines.append(line);
    line = file.readLine();
  }

  int lineCount = lines.size();
  int context = git_patch_context_lines(patch);
  for (int i = 0; i < lineCount; ++i) {
    if (!lines.at(i).startsWith("<<<<<<<"))
      continue;

    int mid = -1;
    for (int j = i + 1; j < lineCount; ++j) {
      if (!lines.at(j).startsWith("======="))
        continue;

      mid = j;
      break;
    }

    if (mid < 0)
      break;

    for (int j = mid + 1; j < lineCount; ++j) {
      if (!lines.at(j).startsWith(">>>>>>>"))
        continue;

      // Add conflict.
      int line = qMax(0, i - context);
      int count = qMin(lineCount, j + context + 1) - line;
      mConflicts.append({line, i, mid, j, lines.mid(line, count)});

      i = j + 1;
      break;
    }
  }
}

@Murmele Murmele reopened this Jun 13, 2022
@Murmele Murmele force-pushed the context_line_accessor´ branch from 2409c20 to c2c7329 Compare May 10, 2025 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0