2

For example, I have a file t.json, the content is:

{
  "a": "abcdefg"
}

And file t.json is pushed to to master branch. Then I add some content to the file, so the file looks like this now:

{
  "a": "abcdefg",
  "b": "mkjuujj"
}

Now I can compare two commits by using PyGithub. Codes are like this:

last_head = repo.get_branch(WORKING_BRANCH)
fc = repo.get_file_contents("/t.json", ref=WORKING_BRANCH)
file = 't.json'
commit_message = "create a new branch with changes"
input_file = open(file, 'rb')
data = input_file.read()


result = repo.update_file("/t.json",
                          commit_message,
                          data,#need solve this problem, how to use a file pass in
                          fc.sha, branch=WORKING_BRANCH)

diff_url = repo.compare(last_head.commit.sha,
                        result['commit'].sha)

print diff_url.diff_url

this is what I got:

diff --git a/t.json b/t.json
index ef03bf5..b775e51 100644
--- a/t.json
+++ b/t.json
@@ -1,3 +1,4 @@
 {
-  "a": "abcdefg"
+  "a": "abcdefg",
+  "b": "mkjuujj"
 }

What I want is

 {
    "a": "abcdefg"
 +  "b": "mkjuujj"
 }

What should I do to compare the diff by passing entire file into repo.update_file(). Thank you very much. Really appreciate it.

ypeng
  • 151
  • 1
  • 8
  • 1
    The result you want is no correct diff in the given context. Notice the comma at the end of the "a"-line. – Dunedan Sep 20 '17 at 19:19
  • 1
    You can also refer the pygithub comparison class http://pygithub.readthedocs.io/en/latest/github_objects/Comparison.html#github.Comparison.Comparison. – Marina Liu Sep 21 '17 at 01:39
  • Thank you very much. You are right. I misunderstood the concept. It works already. – ypeng Sep 22 '17 at 18:25

0 Answers0