Using git diff, how can I get added and modified lines numbers?

前端 未结 12 2029
清酒与你
清酒与你 2020-11-30 22:13

Assuming I have a text file

alex
bob
matrix
will be removed
git repo

and I have updated it to be

alex
new line here
another         


        
12条回答
  •  北海茫月
    2020-11-30 22:54

    Here's some Python copypasta to get the line numbers for modified / removed lines, in case you came across this question looking for that.

    It should be fairly easy to modify it into something that gets the modified and added line numbers as well.

    I've only tested on Windows, but it should be cross platform as well.

    import re
    import subprocess
    
    def main(file1: str, file2: str):
        diff = get_git_diff(file1, file2)
        print(edited_lines(diff))
    
    def edited_lines(git_diff: str):
        ans = []
        diff_lines = git_diff.split("\n")
        found_first = False
        # adjust for added lines
        adjust = 0
        # how many lines since the start
        count = 0
        for line in diff_lines:
            if found_first:
                count += 1
                if line.startswith('-'):
                    # minus one because count is 1 when we're looking at the start line
                    ans.append(start + count - adjust - 1)
                    continue
    
                if line.startswith('+'):
                    adjust += 1
                    continue
    
            # get the start line
            match = re.fullmatch(r'@@ \-(\d+),\d+ \+\d+,\d+ @@', line)
            if match:
                start = int(match.group(1))
                count = 0
                adjust = 0
                found_first = True
    
        return ans
    
    
    def get_git_diff(file1: str, file2: str):
        try:
            diff_process: subprocess.CompletedProcess = subprocess.run(['git', 'diff', '--no-index', '-u', file1, file2], shell=True, check=True, stdout=subprocess.PIPE)
            ans = diff_process.stdout
        # git may exit with 1 even though it worked
        except subprocess.CalledProcessError as e:
            if e.stdout and e.stderr is None:
                ans = e.stdout
            else:
                raise
    
        # remove carriage at the end of lines from Windows
        ans = ans.decode()
        ans.replace('\r', '')
        return ans
    
    
    if __name__ == "__main__":
        main("file1.txt", "file2.txt")
    

提交回复
热议问题