오픈소스 이야기

2022.10.03 15:36

diff & patch

조회 수 219 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

오픈소스에 대한 이해를 만화로 쉽게 할 수 있도록 작성한 컨텐츠를 원작자 님의 허락을 얻고 공유하고 있습니다.

공유를 허락해주신 원작자님에게 감사 드립니다.

원작자 : https://joone.net/

 

“diff와 patch는 무슨 툴인가?” 

잠깐 사용 방법을 소개하면,

hello_v1과 hello_v2 디렉토리에는 같은 hello.c라는 파일이 있지만, hello_v2에는 변경한 코드가 있습니다.

~/foss-comics-code/47.git/diff$ cat hello_v1/hello.c 

/* gcc hello.c -o hello */
#include <stdio.h>

int main() {
    printf("Hello World");
    return 0;
}

~/foss-comics-code/47.git/diff$ cat hello_v2/hello.c 


/* gcc hello.c -o hello */
#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}

두 hello.c파일을 비교할 때, diff라는 툴을 사용합니다.

~/foss-comics-code/47.git/diff$ diff -u hello_v1 hello_v2
diff -u hello_v1/hello.c hello_v2/hello.c
--- hello_v1/hello.c 2022-09-05 18:37:24.894542803 -0700
+++ hello_v2/hello.c 2022-09-05 18:38:58.740332080 -0700
@@ -4,7 +4,7 @@
   
 int main() {
-    printf("Hello World");
+    printf("Hello World\n");
     return 0;
 }

위와 같이 두 파일을 차이를 쉽게 볼 수 있습니다. 자, 이제 이를 파일로 만들어서 hello.c를 만든 사람에 전달해봅시다.

~/foss-comics-code/47.git/diff$ diff -u hello_v1 hello_v2 > diff.patch
~/foss-comics-code/47.git/diff$ cat diff.patch 
diff -u hello_v1/hello.c hello_v2/hello.c
--- hello_v1/hello.c 2022-09-05 17:25:53.992894733 -0700
+++ hello_v2/hello.c 2022-09-05 17:27:51.091147715 -0700
@@ -4,7 +4,7 @@

 int main() {
-    printf("Hello World");
+    printf("Hello World\n");
     return 0;
 }

diff.patch라는 파일이 만들어졌습니다. 이제 이를 hello_v1/hello.c에 적용해봅니다.

~/foss-comics-code/47.git/diff$ patch -d hello_v1/ -p1 < diff.patch 

이렇게 하면 hello_v1/hello.c와 hello_v2/hello.c이 같아야 합니다. 확인해보겠습니다.

~/foss-comics-code/47.git/diff$ diff -u hello_v1 hello_v2
~/foss-comics-code/47.git/diff$ 

diff 명령어 결과가 없는 것으로 보아 두 파일이 같다는 것을 알 수 있습니다.

 

참고로, diff 명령어는 1970년대 초기에 벨랩에서 Unix 운영체제를 개발할 당시에 만들어진 툴입니다.

 

직접 diff와 patch 명령어를 사용해보세요!

https://github.com/joone/foss-comics-code/tree/main/47.git/diff

참고: diff와 patch 프로그램을 사용하는 방법 (https://kldp.org/node/28938)

 

  1. 오픈소스 이야기 게시판 이용안내

    Date2018.04.13 ByKevin Views1648
    read more
  2. 리눅스는 인기가 있을까?

    Date2023.12.25 ByKevin Views697
    Read More
  3. 오픈소스 세계의 짠한 현실

    Date2023.12.19 ByKevin Views332
    Read More
  4. Python 프로그래밍 언어 2

    Date2023.04.25 ByKevin Views499
    Read More
  5. Python 프로그래밍 언어 1

    Date2023.04.17 ByKevin Views501
    Read More
  6. LLVM 프로젝트 (2/2)

    Date2023.01.20 ByKevin Views460
    Read More
  7. LLVM 프로젝트 (1/2)

    Date2023.01.20 ByKevin Views509
    Read More
  8. X-윈도우와 Wayland

    Date2022.11.03 ByKevin Views3197
    Read More
  9. GIT

    Date2022.10.03 ByKevin Views375
    Read More
  10. quilt 사용하기

    Date2022.10.03 ByKevin Views303
    Read More
  11. diff & patch

    Date2022.10.03 ByKevin Views219
    Read More
  12. gpl-violations.org

    Date2022.09.02 ByKevin Views386
    Read More
  13. KHTML

    Date2022.08.03 ByKevin Views383
    Read More
  14. 파이어폭스

    Date2022.06.30 ByKevin Views516
    Read More
  15. 넷스케이프 브라우저

    Date2022.05.16 ByKevin Views539
    Read More
  16. 모자익 브라우저의 탄생

    Date2021.10.12 ByKevin Views506
    Read More
  17. 월드와이드웹(WWW)의 시작 4

    Date2021.05.16 ByKevin Views431
    Read More
  18. 월드와이드웹(WWW)의 시작 3

    Date2021.04.12 ByKevin Views493
    Read More
  19. GIMP FAQ 한국어 번역

    Date2021.04.07 By세벌 Views457
    Read More
  20. 월드와이드웹(WWW)의 시작 2

    Date2021.02.01 ByKevin Views365
    Read More
  21. 월드와이드웹(WWW)의 시작 1

    Date2021.01.04 ByKevin Views494
    Read More
Board Pagination Prev 1 2 3 ... 4 Next
/ 4
CLOSE